﻿// carousel plugin

jQuery.fn.carousel = function (previous, next, stateID, options) {
    var sliderList = jQuery(this).children()[0];
    if (sliderList) {
        setCarouselItemsToSameHeight(jQuery(sliderList).children());

        var increment = jQuery(sliderList).children().outerWidth("true"),
		elmnts = jQuery(sliderList).children(),
		numElmts = elmnts.length,
		sizeFirstElmnt = increment,
		shownInViewport = Math.round(jQuery(this).width() / sizeFirstElmnt),
		firstElementOnViewPort = 1,
		isAnimating = false;
        for (i = 0; i < shownInViewport; i++) {
            jQuery(sliderList).css('width', (numElmts + shownInViewport) * increment + increment + "px");
            jQuery(sliderList).append(jQuery(elmnts[i]).clone());
        }

        // set correct slide in viewport according to query string and update first element option for slide tracking
        if ((stateID != 1) && (stateID <= numElmts)) {
            firstElementOnViewPort = stateID;
            jQuery(sliderList).css('left', "-" + (stateID - 1) * sizeFirstElmnt + "px");
        }

        jQuery(previous).click(function (event) {
            showPrevious();
            Tracking.publish('scenariosHero_previous_click');
        });

        jQuery(next).click(function (event) {
            showNext();
            Tracking.publish('scenariosHero_next_click');
        });


        var isRotate = true;

        //TFS BUG 12: Logic to stop rotation of Hero carousel in IT-IT
        //Start of Logic
        var pageLocationTemp = location.href;
        var windowsIndexTemp = pageLocationTemp.indexOf("/windows/enterprise");
        var localeBaseUrlTemp = pageLocationTemp.substring(0, windowsIndexTemp);
        
        if (localeBaseUrlTemp.indexOf("it-it") != -1)
        {
            isRotate = false;

        }
        //End of Logic
        if (isRotate) 
        {
           
           var rotate = null;
            if (options && options.autoRotate > 0) {
            rotate = setInterval(showNext, options.autoRotate);
            jQuery(this).mouseover(function () {
                clearInterval(rotate);
            });
            jQuery(this).mouseout(function () {
                rotate = setInterval(showNext, options.autoRotate);
            });
            }
        }
    }

    function setCarouselItemsToSameHeight(group) {
        var tallest = 0;
        group.each(function () {
            var thisHeight = $(this).height();
            var ctaHeight = $(this).children('.ctaLinks').height();
            if (ctaHeight == null) ctaHeight = $(this).children('.enterpriseCTAWhite').height();
            thisHeight += ctaHeight;

            if (thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.height(tallest);
    }

    function showPrevious() {
        if (!isAnimating) {
            if (firstElementOnViewPort == 1) {
                jQuery(sliderList).css('left', "-" + numElmts * sizeFirstElmnt + "px");
                firstElementOnViewPort = numElmts;
            }
            else {
                firstElementOnViewPort--;
            }
            // if on scenarios page, call the content switch animation
            if ($('#scenariosHeroCarousel').length > 0) {
                if (firstElementOnViewPort < 1) {
                    setScenarioOnClick(1)
                }
                else setScenarioOnClick(firstElementOnViewPort);
            }
            jQuery(sliderList).animate({
                left: "+=" + increment,
                y: 0,
                queue: true
            }, "swing", function () { isAnimating = false; });
            isAnimating = true;
        }
    }

    function showNext() {
        if (!isAnimating) {
            if (firstElementOnViewPort > numElmts) {
                firstElementOnViewPort = 2;
                jQuery(sliderList).css('left', "0px");
            }
            else {
                firstElementOnViewPort++;
            }
            // if on scenarios page, call the content switch animation
            if ($('#scenariosHeroCarousel').length > 0) {
                if (firstElementOnViewPort > numElmts) {
                    setScenarioOnClick(1);
                }
                else setScenarioOnClick(firstElementOnViewPort);
            }
            jQuery(sliderList).animate({
                left: "-=" + increment,
                y: 0,
                queue: true
            }, "swing", function () { isAnimating = false; });
            isAnimating = true;
        }
    }
};
