/**
 * DOM Ready Functions
 */
$(function(){
	/* Setup the top nav hovers */
	setupHovers();
	zIndexHandler();
});

var $button = 2;
var timer = 0;

/**
 * Setup the top navigation hovers and such
 */
function setupHovers(){
	/* Add a hover event to all ul topNav li's */
	$('ul#topNav li').hover(function(){		/* Hover in */
		/* Replace the Item, in it's id with Selected byt using the attr function */
		$(this).attr('id',$(this).attr('id').replace("Item","Selected"));		
	},function(){							/* Hover out */
		/* If it doesn't have a class of selected */
		if(!$(this).hasClass('selected')){
			/* Then re-switch the id back to show Item instead of Selected */
			$(this).attr('id',$(this).attr('id').replace("Selected","Item"));
		}
		/* End of Hover in/out, now add a click event function */
	}).click(function(){
		/* Once we click, iterate through this item's Siblings */
		$(this).siblings().each(function(index){
			/* Remove the selected Class and replace all 'Selected' text in id's with 'Item' so they are all unselected*/
			$(this).removeClass('selected').attr('id',$(this).attr('id').replace("Selected","Item"));
		});
		/* Now add the class of selected to this item,
		 * replace 'Item' with 'Selected' in the id 
		 * so we don't lose the selected state if we mouseOut */
		$(this).addClass('selected').attr('id',$(this).attr('id').replace("Item","Selected"));
	});
	
	/* End of setupHovers */
	
}

/**
 * AJAX Banner animations
 * 
 */
//Add new function to pickup the homepage click events
$(function(){
	setupFeatureBanners();
});

function setupFeatureBanners(){
	$('ul.tabs li a').unbind('click').click(function(){
		
		clearTimeout(timer);
		
		$('ul.tabs li a').removeClass('selected').unbind('click').click(function(e){
			e.preventDefault();
			return false;
		});
		
		$(this).addClass('selected');
		
		$.get("/ajax-banner/",{
			section:$(this).attr('id')
		},function(data){
			if(data != ""){	
				$('.feature-banner').append(data);
				$('.banner-content2').hide();
				$('.banner-content1').fadeOut('1000',function(){
					$('.banner-content1').detach();
					$('.banner-content2').attr('class','banner-content1').fadeIn('1000',function(){
						setupFeatureBanners();
					});
				});
			}
		});
		
		return false;
	});
}

$(function(){
	
	/* Make a timer and run it */
	timer = setTimeout("clickFeatureButton()",10000);
});

function clickFeatureButton()
{
	$('a[data-button='+$button+']', 'ul.tabs').click();
	
	if($button == 5){
		$button = 1;
	}else{
		$button = $button + 1;
	}
	
	timer = setTimeout("clickFeatureButton()",10000);
}

/**
 * Sorts out the z-index bug in ie
 */
function zIndexHandler(){
	var zIndexNumber = 1000;
	$('div').each(function() {
		$(this).css('zIndex', zIndexNumber);
		zIndexNumber -= 1;
	});
}
