var AGLSlider = {

    delay: 12000, 	//you can change this. Go ahead. Do it.
    timers: null,
    currentIndex: 0,
    sliderItemCnt: 1,
    sliderItemWdth: 1,
    ul: null,
    start: null,
    stop: null,
    mouseOverride: null,
    moveTo: null,

    dispose: function() {
        jQuery('#paging li').unbind('click');
        //jQuery(".panelContainer li").unbind('click');
        jQuery("#sliderBanner .next").unbind('click');
        jQuery("#sliderBanner .prev").unbind('click');
        jQuery(".slideContainer").unbind('mouseenter');
        jQuery(".slideContainer").unbind('mouseleave');
        jQuery('#paging', '#sliderWrap').empty();
        if (this.stop)
            this.stop();
    },

    init: function() {
        this.dispose();
        this.currentIndex = 0;
        this.sliderItemCnt = 1;
        this.sliderItemWdth = 1;
    },

    run: function(startIndex, options) {
        var $that = this;
        var canStart = false;

        if (!options)
            options = { startSlider: true };

        // set public property to be accessed from the outside
        $that.start = startTimer;
        $that.stop = stopTimer;
        $that.moveTo = goToIndex;
        // clear everything out just in case it's called twice
        this.init();

        // initialise slider variables used plugin wide
        $that.timers = null;
        $that.timers = new Array();

        $that.sliderItemCnt = jQuery('#slider li').length;

        if ($that.sliderItemCnt > 0) {
            // we have items... set everything up!
            canStart = true;

            $that.sliderItemWdth = jQuery('#slider li').width();
            $that.ul = jQuery('#sliderWrap #slider');

            // create the paging links
            var paging = jQuery('#paging', '#sliderWrap');
            if (paging.length > 0)
                paging.empty();
            else {
                paging = jQuery('<ul id="paging"></ul>');
            }
            jQuery('#sliderWrap').prepend(paging);

            for (var p = 0; p < $that.sliderItemCnt; p++) {
                paging.append('<li>' + p + '</li>')
            }
            jQuery('#paging li:first-child').addClass('active');
            // click events
            jQuery('#paging li').bind('click', pageClick);
            paging.show();
            jQuery("#sliderBanner .next").bind('click', pageNext);
            jQuery("#sliderBanner .prev").bind('click', pagePrev);

            $that.ul.css('width', ($that.sliderItemWdth + 1) * $that.sliderItemCnt);

            if (startIndex)
                goToIndex(startIndex, false);
            else
                $that.ul.css('left', '0');

            // setup the mouseenter and leave event
            jQuery(".slideContainer").mouseenter(function() {
                stopTimer();
            }).mouseleave(function() {
                // because of the nutrition popup, we need to check if it's been overriden
                if (!$that.mouseOverride && (options && options.startSlider))
                    startTimer();
            });
        }

        function pageNext() {
            stopTimer();
            slide();
            if (options && options.startSlider);
                startTimer();
        }

        function pagePrev() {
            stopTimer();
            var newIndex = ($that.currentIndex - 1);

            if (newIndex < 0)
                newIndex = $that.sliderItemCnt - 1;

            goToIndex(newIndex, false);
            if (options && options.startSlider);
                startTimer();
        }

        function pageClick(e) {
            // clear out the interval so we don't clash
            stopTimer();
            // get the new clicked index and goto
            var $this = jQuery(this);
            var newIndex = $this.index();
            goToIndex(newIndex, false);
            // we need to restart the interval now
            if (options && options.startSlider)
                startTimer();
        }

        function slide() {
            // get the new slide index and check if at end
            var newIndex = ($that.currentIndex + 1);

            if (newIndex == $that.sliderItemCnt)
                newIndex = 0;
            // slide
            goToIndex(newIndex, false);
        }

        function processTracking(li) {
            if (li.length > 0) {
                var trackCategory = li.attr('trackCategory');
                var trackLabel = li.attr('trackLabel');

                if ((trackCategory && trackCategory.length > 0) && (trackLabel && trackLabel.length > 0)) {
                    GATracking.TrackEvent(trackCategory, 'Scroll_Promo_View', trackLabel);
                }
            }
        }

        function goToIndex(index, suppressSlide) {
            // set the new paging link and initiate the animation slide
            jQuery('#paging li').removeClass();
            jQuery('#paging li:nth-child(' + (index + 1) + ')').addClass('active');

            var nextLi = $that.ul.find("li:nth-child(" + (index + 1) + ")");

            if (suppressSlide) {
                $that.ul.css({ left: '-' + $that.sliderItemWdth * index + 'px' });
            }
            else {

                //easeInOutExpo
                $that.ul.animate({ 'left': '-' + $that.sliderItemWdth * index }, 1000, "easeInOutExpo");

            }

            $that.currentIndex = index;
            processTracking(nextLi);
        }

        function stopTimer() {
            if ($that.timers) {
                for (var i = 0; i < $that.timers.length; i++) {
                    clearInterval($that.timers[i]);
                }
            }
            $that.timers = null;
            $that.timers = new Array();
        }

        function startTimer() {
            stopTimer();
            $that.timers = new Array();
            var timeoutInt = setInterval(slide, $that.delay);
            $that.timers.push(timeoutInt);
        }

        if (canStart && options.startSlider)
            startTimer();
			
		//Stop and start timer when store locator is focused
		$(".store-searchbox input:text").focus(function(){
			stopTimer();
		});
		
		$(".store-searchbox input:text").blur(function(){
			startTimer();
		});
    }
}
