//Header Control.
var ITEMTIMEOUTLENGTH; //Global timeout value.
var cycleItemTimer; //Global timer.
var firstItem;
var lastItem;

$(document).ready(function() {         
    if ($('div.header-container'))
    {
        //If only one item (or zero items) exist, then no need for animation. 	
        if ($('div.header-items').children('div.header-item').length == 1) return;      
        
        //Show the first item.
        $('div.header-items > div.header-item:eq(0)').attr({
	        style: "display:block;"
        });
        $('div.header-items > div.header-item:gt(0)').attr({
	        style: "display:none;"
        });        

        //Store first and last items in collection.
        firstItem = $('div.header-items > div.header-item:first');
        lastItem = $('div.header-items > div.header-item:last');
        
        //Set our global timeout.          
        ITEMTIMEOUTLENGTH = $('input#transitionInterval').val();              

        $('div.header-items > div.header-item').hover(
	        function() {			            
	            clearTimeout(cycleItemTimer);	            
	            $(this).children('h2').addClass('hover');
	        },
	        function() {	            
	            cycleItemTimer = setTimeout("cycleItems()", ITEMTIMEOUTLENGTH);	            
	            $(this).children('h2').removeClass('hover');
	        }
        );
	    
        //Cycle through ticker items.
        cycleItemTimer = setTimeout("cycleItems()", ITEMTIMEOUTLENGTH);
    }
});

//Function to cycle through items.
function cycleItems()
{   
    clearTimeout(cycleItemTimer);						

    var activeItem = $('div.header-items > div.header-item:visible');        
    var nextItem = (activeItem.attr('class') == lastItem.attr('class')) ? firstItem : (activeItem.next());
    
    activeItem.fadeOut("slow");
    nextItem.find("h2").attr({ style : "left : 715px;" });
    nextItem.fadeIn("slow");
    nextItem.find("h2").animate( { left : "0px" }, 500, function(){
        cycleItemTimer = setTimeout("cycleItems()", ITEMTIMEOUTLENGTH);
    });
}