//HomepageNewsScroller

$j(document).ready(function (e) {

    //variables
    var autoAnimate = true;
    var autoAnimateInterval = 6000;
    var scrollSpeed = 500;
    var maxAtOnce = 4;

    var featurePrefixId = "#home_feature_scroller .main_content ";
    var thumbsPrefixId = "#home_feature_scroller .scroller_nav_container .scroller_nav ul ";
    var animating = false;
    var firstIndexOnScreen = 0; //keeps track if the index of first on the screen. Used to figure out which one comes next!

    var ulWidth = $j(thumbsPrefixId).width();
    var liWidth;
    var defaultLiString = " <li class=\"item{0}\"><span class=\"featureThumbnail\"><a alt=\"{1}\" id=\"thumb_{0}\" title=\"(Home Feature Link) Click here to go to {1}\" href=\"{3}\"><img alt=\"{1}\" src=\"{2}\" /><span class=\"tagLine\">{1}</span> </a></span></li>"
    var scrollerStartPoint = 0;
    var allowNextPrev = false;
    var currentFeature;
    var firstFeatureSet = false;
    var liCount;
    var arr = window["newsCat_all_items"];
    var catChangedFirst = false;
    var indexToDisplayForAutoRotateWithNoNextAndPrev = 0;

    setUpNewsCat();

    AutoAnimate_Timeout();


    function AutoAnimate_Timeout() {
        if (autoAnimate) {
            setTimeout(AutoAnimate_Timeout, autoAnimateInterval);
        }

        if (!firstFeatureSet && !catChangedFirst) {
            if (autoAnimate) {

                if (allowNextPrev) {
                    appendNext();

                }


                indexToDisplayForAutoRotateWithNoNextAndPrev++;
                if (indexToDisplayForAutoRotateWithNoNextAndPrev >= liCount) {
                    indexToDisplayForAutoRotateWithNoNextAndPrev = 0;
                }
                setFeature(arr[indexToDisplayForAutoRotateWithNoNextAndPrev]);

            }
        }
        catChangedFirst = false;
    }


    function setUpNewsCat() {
        firstIndexOnScreen = 0;
        indexToDisplayForAutoRotateWithNoNextAndPrev = 0;
        liCount = arr.length;
        $j(thumbsPrefixId + "li").remove();

        $j.each(arr, function (index, v) {
            /* Set feature news article*/
            if (index == 0) {
                setFeature(v);
            }

            /* Build up thumbs */
            //append the li's back in to the ul, but repeat them if there's less than maxAtOnce

            if (index < maxAtOnce) {
                if (liWidth == null) {
                    liWidth = $j(thumbsPrefixId + 'li').outerWidth();
                }


                $j(thumbsPrefixId).append(formatliString(index)).children().last().css('left', ((liWidth * index) + scrollerStartPoint) + 'px');
                $j(thumbsPrefixId + "li:last-child a").bind('click', function (e) {
                    thumbClick(this, e);
                });
            }
        });

 

    }

    function formatliString(index) {

        var liString = defaultLiString;

        if (arr != undefined && arr[index] != undefined) {

            liString = liString.replace(new RegExp("\\{1\\}", "gm"), arr[index][8]);
            liString = liString.replace(new RegExp("\\{2\\}", "gm"), arr[index][7]);
            liString = liString.replace(new RegExp("\\{0\\}", "gm"), index);
            liString = liString.replace(new RegExp("\\{3\\}", "gm"), arr[index][5]);
        }
        return liString;
    }

    function thumbClick(t, e) {
        e.preventDefault();
        autoAnimate = false;
        currentFeature = t.id.replace("thumb_", "");
        setFeature(arr[currentFeature]);
    }


    function setFeature(thisArr) {
        if (firstFeatureSet) {
            setFeatureContent(thisArr);
        }
        else {
            $j(featurePrefixId).fadeOut(400, function () {
                setFeatureContent(thisArr);
            });
        }
    }

    function setFeatureContent(thisArr) {

        if (thisArr != undefined) {

            $j(featurePrefixId + "h2").empty();
            $j(featurePrefixId + "h2").append(thisArr[0]);

            $j(featurePrefixId + "h3").empty();
            $j(featurePrefixId + "h3").append(thisArr[1]);

            $j(featurePrefixId + "h4").empty();
            $j(featurePrefixId + "h4").append(thisArr[2]);

            $j(featurePrefixId + "p#feature_article_summary").empty();
            $j(featurePrefixId + "p#feature_article_summary").append(thisArr[3]);

            $j(featurePrefixId + "a#feature_article_link").empty();
            $j(featurePrefixId + "a#feature_article_link").append(thisArr[4]);


            $j(featurePrefixId + "a#feature_article_link").attr('href', thisArr[5]);
            $j(featurePrefixId + "a#feature_article_link").attr('title', '(Main Home Feature Link) Click here to go to ' + thisArr[4]);



            $j(featurePrefixId + "#imgFeature").attr('src', thisArr[6]);
            $j(featurePrefixId + "#imgFeature").attr('alt', thisArr[4]);

        }

        $j(featurePrefixId).fadeIn(400);
    }


    $j('#next').click(function (e) {
        e.preventDefault();
        autoAnimate = false;
       
            animating = true;
            appendNext();
            animating = false;
        
    });


    $j('#previous').click(function (e) {
        e.preventDefault();
        autoAnimate = false;
        
            appendPrevious();
        
    });

    function appendNext() {
        if (autoAnimate || animating) {
            //get the next index
            var nextIndex = ((maxAtOnce % liCount) + firstIndexOnScreen) % liCount;


            $j(thumbsPrefixId).append(formatliString(nextIndex)).children().last().css('left', ((liWidth * maxAtOnce) + scrollerStartPoint) + 'px');
            $j(thumbsPrefixId + 'li').stop(true, true).animate({ left: '-=' + liWidth }, scrollSpeed, "linear");

            $j(thumbsPrefixId + "li:last-child a").bind('click', function (e) {
                thumbClick(this, e);
            });



            setTimeout(function () { removeLi(thumbsPrefixId + "li:first") }, scrollSpeed);
        }
    }

    function removeLi(itemtoRemove) {
        $j(itemtoRemove).remove();
    }

    function appendPrevious() {
        firstIndexOnScreen--;
        if (firstIndexOnScreen < 0) {
            firstIndexOnScreen = (liCount - 1);
        }

        var prevIndex = firstIndexOnScreen;
        $j(thumbsPrefixId).prepend(formatliString(prevIndex)).children().first().css('left', -(liWidth - scrollerStartPoint) + 'px');
        $j(thumbsPrefixId + 'li').stop(true, true).animate({ left: '+=' + liWidth }, scrollSpeed, "linear");

        $j(thumbsPrefixId + "li:first-child a").bind('click', function (e) {
            thumbClick(this, e);
        });

        setTimeout(function () { removeLi(thumbsPrefixId + "li:last") }, scrollSpeed);
    }



});
