﻿//Jquery banner rotator
//By Matt Sears
//6/4/09
//
//Jquery must be used for this script to work!!

//GOLBAL VARIABLES
////
    //EXTREMLY Important variable, this allows jquery to work with other libraries such as protoype
    var $j = jQuery.noConflict();

    //Variable used to intialize the BannerRotation() Method
    var BannerSetter = 1;
    
    //set the rotation speed here...
    var BannerRotationTimer = 7000;
    
    //set the fade speed here...
    var FadeSpeed = 2000;
    
      
    var isRotatorOn = true;     
    //var CarouselTimer;  uncomment to turn autocycling back on
////


//Initializes the links and banner rotator
$j(function() {


    // setTimeout(rotateCarousel,5000); uncomment to turn autocycling back on
        
            $j("#CarouselPreviousButton").click(moveBanner);        
            $j("#CarouselNextButton").click(moveBanner); 

    $j('#RotatingBanner li:first').css('display', 'block');
    $j('#BannerSelectorBox a').addClass('selectedLink');

    BannerRotation(BannerSetter);


    $j('#BannerSelectorBox a').click(function() {

        switch (this.id) {
            case ('BannerLink1'):

                setLinkClass_onClick(this);
                SetFirstBanner();
                BannerSetter = 1;
                break;

            case ('BannerLink2'):

                setLinkClass_onClick(this);
                SetSecondBanner();
                BannerSetter = 2;
                break;

            case ('BannerLink3'):

                setLinkClass_onClick(this);
                SetThirdBanner();
                BannerSetter = 3;
                break;
                
            case ('BannerLink4'):

                setLinkClass_onClick(this);
                SetFourthBanner();
                BannerSetter = 4;
                break;

            case ('BannerLink5'):

                setLinkClass_onClick(this);
                SetFifthBanner();
                BannerSetter = 5;
                break;
        }
        return false;
    });

});


//Banner Rotator Method
function BannerRotation() {

    if (BannerSetter == 6) {
        BannerSetter = 1;
    }
    
    switch (BannerSetter) {
        case 1:
            
            setLinkClass_onClick($j('#BannerLink1'));
            SetFirstBanner();          
            BannerSetter++;
            break;

        case 2:

            setLinkClass_onClick($j('#BannerLink2'));
            SetSecondBanner();           
            BannerSetter++;
            break;

        case 3:

            setLinkClass_onClick($j('#BannerLink3'));
            SetThirdBanner();
            BannerSetter++;
            break;
        case 4:

            setLinkClass_onClick($j('#BannerLink4'));
            SetFourthBanner();
            BannerSetter++;
            break;

        case 5:

            setLinkClass_onClick($j('#BannerLink5'));
            SetFifthBanner();
            BannerSetter++;
            break;
    }
    setTimeout(BannerRotation, BannerRotationTimer);

}

//Sets teh background of the banner links Depending on which banner is active
function setLinkClass_onClick(thisLink) {
    $j('#BannerSelectorBox a').removeClass('selectedLink').addClass('notSelectedLink');
    $j(thisLink).removeClass('notSelectedLink').addClass('selectedLink');
}


//These 4 Methods set the active banner
function SetFirstBanner() {
    $j('#RotatingBanner li:first').fadeIn(FadeSpeed);
    $j('#RotatingBanner li:nth-child(2)').fadeOut(FadeSpeed);
    $j('#RotatingBanner li:nth-child(3)').fadeOut(FadeSpeed);
    $j('#RotatingBanner li:nth-child(4)').fadeOut(FadeSpeed);
    $j('#RotatingBanner li:nth-child(5)').fadeOut(FadeSpeed);
 }
 function SetSecondBanner() {
     $j('#RotatingBanner li:nth-child(2)').fadeIn(FadeSpeed);
     $j('#RotatingBanner li:nth-child(1)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(3)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(4)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(5)').fadeOut(FadeSpeed);
 }
 function SetThirdBanner() {
     $j('#RotatingBanner li:nth-child(3)').fadeIn(FadeSpeed);
     $j('#RotatingBanner li:nth-child(1)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(2)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(4)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(5)').fadeOut(FadeSpeed);
 }
 function SetFourthBanner() {
     $j('#RotatingBanner li:nth-child(4)').fadeIn(FadeSpeed);
     $j('#RotatingBanner li:nth-child(1)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(2)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(3)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(5)').fadeOut(FadeSpeed);
 }
 function SetFifthBanner() {
     $j('#RotatingBanner li:nth-child(5)').fadeIn(FadeSpeed);
     $j('#RotatingBanner li:nth-child(1)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(2)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(3)').fadeOut(FadeSpeed);
     $j('#RotatingBanner li:nth-child(4)').fadeOut(FadeSpeed);
 }
  function rotateCarousel()
    {   
        // Rotator is turned on then set the classes(rotate the banner)
        if(isRotatorOn == true)
        {     
            setClasses('');                            
        }
        //CarouselTimer = setTimeout(rotateCarousel,5000); uncomment to turn autocycling back on
    }
    
    function moveBanner(evt)
    {
        var buttonClicked = ' ';
        
        //Retrieve the information of the button that raised the Event
        if(evt)//if Every other Browser
        {
            buttonClicked = evt.target;            
        }
        else//if IE
        {            
            buttonClicked = window.event.srcElement;            
        }
       
       //pass button info to setClasses
        setClasses(buttonClicked);       
             
    }

   
    function setClasses(theSource)
    {  
        if(theSource.id == "CarouselNextButton")
        {         
             //turnRotatorOff(); uncomment to turn autocycling back on            
             SlideLeft();                                     
             //RestartSlide();uncomment to turn autocycling back on
        }
        else
        {
           if(theSource.id == "CarouselPreviousButton")
           {    
                //turnRotatorOff();uncomment to turn autocycling back on
                SlideRight();                                                                         
                //RestartSlide();uncomment to turn autocycling back on
           }
           else
           {
               SlideLeft();  
           }
           
        }
        
    }
    //Called when Next button is clicked and rotateCarousel is called
    function SlideLeft()
    {   
        
        //Animate the Slides
        $j(".Current").stop().animate({
            "left" : "-482px"
            },300);
        $j(".Next").stop().animate({
            "left" : "0px",
            "z-index" : "9"
            },300);
        
        //Reset all the slides to the right side
        $j('.CarouselElements').css({'left':'700px'});
        
        //adjust the classes accordingly
        if($j('#LastSlide').hasClass('Next') )
        {   
            //If Last Slide is the "Next" Slide
            $j(".Current").prev().removeClass('Previous')
            $j("#LastSlide").addClass('Current');
            $j("#LastSlide").removeClass('Next');                   
            $j("#LastSlide").prev().addClass('Previous'); 
            $j(".Previous").removeClass('Current');           
            $j("#FirstSlide").addClass('Next'); 
            
        }
        else       
        {   
            //If Last Slide is the "Current" Slide
            if($j('#LastSlide').hasClass('Current'))
            {
                
                $j("#LastSlide").prev().removeClass('Previous');
                $j("#LastSlide").addClass('Previous');
                $j("#LastSlide").removeClass('Current');
                $j("#FirstSlide").removeClass('Next');           
                $j("#FirstSlide").addClass('Current');
                $j("#FirstSlide").next().addClass('Next');
            }
            else    
            {
                //If Last Slide is the "previous" Slide
                if($j('#LastSlide').hasClass('Previous'))                    
                {
                    $j("#LastSlide").removeClass('Previous');
                    $j("#FirstSlide").removeClass('Current');            
                    $j("#FirstSlide").addClass('Previous');           
                    $j("#FirstSlide").next().removeClass('Next');
                    $j("#FirstSlide").next().addClass('Current');
                    $j('.Current').next().addClass('Next');                   
                }
                else
                {
                    //DEFAULT
                    $j(".Current").prev().removeClass('Previous');
                    $j(".Current").addClass('Previous');
                    $j(".Previous").removeClass('Current');
                    
                    //set Next class to current
                    $j(".Next").addClass('Current');
                    $j(".Current").removeClass('Next'); 
                    
                    // set class before current to previous        
                    $j('.Current').next().addClass('Next');
                    
                }//END IF                 
            }//END IF   
        }//END IF   
        
    }
    
    //Called When Previous Button is clicked
    function SlideRight()
    {
        
        $j(".Current").stop().animate({
            "left" : "482px"
            },500);
        $j(".Previous").stop().animate({
            "left" : "0px",
            "z-index" : "9"
            },500);
            
        //Reset all the slides to the left side
        $j('.CarouselElements').css({'left':'-482px'});
        
        //Adjust the classes accordingly
        if($j('#FirstSlide').hasClass('Previous'))
        {   
            //If Last Slide is the "Next" Slide
            $j("#LastSlide").addClass('Previous');
            $j("#FirstSlide").removeClass('Previous');
            $j(".Current").next().removeClass('Next');                   
            $j("#FirstSlide").addClass('Current'); 
            $j("#FirstSlide").next().removeClass('Current');
            $j("#FirstSlide").next().addClass('Next'); 
        }
        else       
        {   
            //If Last Slide is the "Current" Slide
            if($j('#FirstSlide').hasClass('Current'))
            {
                $j("#LastSlide").prev().addClass('Previous');
                $j("#LastSlide").removeClass('Previous');
                $j("#LastSlide").addClass('Current');                                
                $j("#FirstSlide").removeClass('Current');
                $j("#FirstSlide").addClass('Next');
                $j("#FirstSlide").next().removeClass('Next');          
                
                
            }
            else    
            {
                //If Last Slide is the "previous" Slide
                if($j('#FirstSlide').hasClass('Next'))
                {
                    $j("#FirstSlide").removeClass('Next');
                    $j("#LastSlide").addClass('Next');
                    $j("#LastSlide").removeClass('Current');
                    $j("#LastSlide").prev().addClass('Current');                     
                    $j("#LastSlide").prev().removeClass('Previous');                                                                        
                    $j(".Current").prev().addClass('Previous');
                }
                else
                {
                    //set current to class next
                    $j(".Current").next().removeClass('Next');
                    $j(".Current").addClass('Next');
                    $j(".Next").removeClass('Current');
                     
                     //set class previous to current
                    $j(".Previous").addClass('Current');
                    $j(".Current").removeClass('Previous');   
                       
                    // set class before current to previous        
                    $j('.Current').prev().addClass('Previous');
                }//END IF                 
            }//END IF   
        }//END IF   
    }  
        
    function RestartSlide()
    {                           
            setTimeout(turnRotatorOn, 10000);           
            rotateCarousel();
    }

    function turnRotatorOn()
    {
        isRotatorOn = true;
        
    }
    function turnRotatorOff()
    {         
        clearTimeout(CarouselTimer);  
        isRotatorOn = false;
    }
    
    