//HomepageNewsScroller

$j(document).ready(function (e) {

    var i;
    for (i in document.images) {
        if (document.images[i].src) {
            var imgSrc = document.images[i].src;
            if (imgSrc.substr(imgSrc.length - 4) === '.png' || imgSrc.substr(imgSrc.length - 4) === '.PNG') {
                document.images[i].style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + imgSrc + "')";
            }
        }
    }


    //variables
    var autoAnimate = true;
    var autoAnimateInterval = 6000;
    var scrollSpeed = 500;
    var featureMaxAtOnce = 1;

    //var featurePrefixId = "#home_feature_scroller .main_content ";
    var featureThumbsPrefixId = "#offers_scroller_content 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(featureThumbsPrefixId).width();
    var liWidth;
    var defaultLiString = " <li><img alt=\"{1}\" height=\"123\" width=\"240\" src=\"{0}\" /><h3>{1}</h3><span class=\"advanced\" >{3}</span><p class=\"intro\" >{4}</p><p><a href=\"{2}\" class=\"yellow_btn\">Find out more &#9654;</a></p></li>";
    var scrollerStartPoint = 0;
    var allowNextPrev;
    var currentFeature;
    var firstFeatureSet = true;

    var liCount;
    var arr = window["feature_all_items"];
    var catChangedFirst = true;
    var indexToDisplayForAutoRotateWithNoNextAndPrev = 0;

    setUpNewsCat();

    AutoAnimate_Timeout();

    firstFeatureSet = false;

    function AutoAnimate_Timeout() {


        //if (autoAnimate) {
        setTimeout(AutoAnimate_Timeout, autoAnimateInterval);
        //}

        if (!firstFeatureSet && !catChangedFirst) {
            if (autoAnimate) {
                if (allowNextPrev) {
                    appendNext();
                }
                else {
                    indexToDisplayForAutoRotateWithNoNextAndPrev++;
                    if (indexToDisplayForAutoRotateWithNoNextAndPrev >= liCount) {
                        indexToDisplayForAutoRotateWithNoNextAndPrev = 0;
                    }
                }
            }
        }
        catChangedFirst = false;
    }


    function setUpNewsCat() {
        firstIndexOnScreen = 0;
        indexToDisplayForAutoRotateWithNoNextAndPrev = 0;
        liCount = arr.length;
        $j(featureThumbsPrefixId + "li").remove();


        $j.each(arr, function (index, v) {


            if (index < featureMaxAtOnce) {
                if (liWidth == null) {

                    liWidth = "240"
                }

                $j(featureThumbsPrefixId).append(formatliString(index)).children().last().css('left', ((liWidth * index) + scrollerStartPoint) + 'px');
                $j(featureThumbsPrefixId + "li:last-child a").bind('click', function (e) {
                    thumbClick(this, e);
                });
            }
        });

        if (liCount >= featureMaxAtOnce) {
            allowNextPrev = true;
        }
        else {
            allowNextPrev = false;
        }

    }

    function formatliString(index) {

        var liString = defaultLiString;
        if (arr != undefined && arr[index] != undefined) {
            liString = liString.replace(new RegExp("\\{0\\}", "gm"), arr[index][0]);
            liString = liString.replace(new RegExp("\\{1\\}", "gm"), arr[index][1]);
            liString = liString.replace(new RegExp("\\{2\\}", "gm"), arr[index][2]);
            liString = liString.replace(new RegExp("\\{3\\}", "gm"), arr[index][3]);
            liString = liString.replace(new RegExp("\\{4\\}", "gm"), arr[index][4]);
        }
        return liString;
    }

    function thumbClick(t, e) {
        e.preventDefault();
        autoAnimate = false;

        window.location = $j(featureThumbsPrefixId + "li:last-child a").attr("href");

    }


    $j('#offers_next').click(function (e) {
        e.preventDefault();
        autoAnimate = false;
        if (allowNextPrev) {
            animating = true;
            appendNext();
            animating = false;
        }
    });


    $j('#offers_previous').click(function (e) {
        e.preventDefault();
        autoAnimate = false;
        if (allowNextPrev) {
            appendPrevious();
        }
    });

    function appendNext() {
        if (autoAnimate || animating) {
            //get the next index
            var nextIndex = ((featureMaxAtOnce % liCount) + firstIndexOnScreen) % liCount;

            $j(featureThumbsPrefixId).append(formatliString(nextIndex)).children().last().css('left', ((liWidth * featureMaxAtOnce) + scrollerStartPoint) + 'px');
            $j(featureThumbsPrefixId + 'li').stop(true, true).animate({ left: '-=' + liWidth }, scrollSpeed, "linear");

            $j(featureThumbsPrefixId + "li:last-child a").bind('click', function (e) {
                thumbClick(this, e);
            });

            firstIndexOnScreen++;
            if (firstIndexOnScreen >= liCount) {
                firstIndexOnScreen = 0;
            }

            setTimeout(function () { removeLi(featureThumbsPrefixId + "li:first") }, scrollSpeed);
        }
    }

    function removeLi(itemtoRemove) {

        $j(itemtoRemove).remove();
    }

    function appendPrevious() {
        firstIndexOnScreen--;
        if (firstIndexOnScreen < 0) {
            firstIndexOnScreen = (liCount - 1);
        }

        var prevIndex = firstIndexOnScreen;
        $j(featureThumbsPrefixId).prepend(formatliString(prevIndex)).children().first().css('left', -(liWidth - scrollerStartPoint) + 'px');
        $j(featureThumbsPrefixId + 'li').stop(true, true).animate({ left: '+=' + liWidth }, scrollSpeed, "linear");

        $j(featureThumbsPrefixId + "li:first-child a").bind('click', function (e) {
            thumbClick(this, e);
        });

        setTimeout(function () { removeLi(featureThumbsPrefixId + "li:last") }, scrollSpeed);
    }



});

