﻿$(document).ready(function() {
    aniAbbottMamaHomepageRotator.init(this);
    aniAbbottMamaHomepageRotator.start();    
});

// This object handles the rotation of the content panels at the top of the Abbott Mama homepage
aniAbbottMamaHomepageRotator = {
    slideChangeDelay : 6000,
    currentIndex : 0,
    slides : [],
    wrappingElement : '#homepage_header_content',
    subTabs : '#homepage_header_content>#tab_bar>#sub_tabs',
    isAnimated : true,
    rotatorTimeoutId : null,
    
    init : function(s) {
        $('.homepage_rotating_item', '#homepage_header_content>#rotating_items').each(function() {
            var current = this;
            var item = {
                slideID : current.id,
                backgroundUrl : $('span.background_url', current).html()
            }
            aniAbbottMamaHomepageRotator.slides.push(item);      
        });
        
        this.goTo(0);
    },
    
    clearInterval : function() {
        window.clearTimeout(this.rotatorTimeoutId);
        this.rotatorTimeoutID = null;
    },
    
    goTo : function(index) {
        this.clearInterval();
    
        var rotator = aniAbbottMamaHomepageRotator;            
              
        var buttonSelector = "li:nth-child(" + (index + 1) + ")";      
              
        $('li.selected', rotator.subTabs).removeClass('selected');        
        $(buttonSelector, rotator.subTabs).addClass('selected');
                
        var bg = rotator.slides[index].backgroundUrl;       
        var newWrappingCss = {
            "background-image" : "url('" + bg + "')"
        };        
        
        $('.homepage_rotating_item').css('display', 'none');
        $("#" + rotator.slides[index].slideID).css('display', 'block');
              
       $(rotator.wrappingElement).css(newWrappingCss);
       
       rotator.currentIndex = index;       
       
       this.rotatorTimeoutId = setTimeout("aniAbbottMamaHomepageRotator.animate()", this.slideChangeDelay); 
             
    },
    
    goNext : function()
    {
        var rotator = aniAbbottMamaHomepageRotator;   
        
        if(rotator.currentIndex == rotator.slides.length - 1)
        {            
            rotator.goTo(0);
        }
        else
        {   
            rotator.currentIndex = rotator.currentIndex + 1;
            rotator.goTo(rotator.currentIndex);
        }
    },
    
    animate : function() {
        this.goNext();       
    },
    
    start : function()
    {
        this.rotatorTimeoutId = setTimeout("aniAbbottMamaHomepageRotator.goNext()", this.slideChangeDelay);  
    }  
    
    
}
