$(document).ready(function(){
	imageTimer = setInterval("showNextImage();", 5000);
	//Hide all images, Set z-indexs to 0 and fade first image in
	$("ul#image li").hide();
	$("ul#image li").css("z-index","0");
	$("ul#image li:eq(0)").fadeTo(500,1).css("z-index","2");
	//Hide all stories, Set z-indexs to 0 and fade first image in
	$("ul#headerNewsContent li").hide();
	$("ul#headerNewsContent li").css("z-index","0");
	$("ul#headerNewsContent li:eq(0)").fadeTo(500,1).css("z-index","2");
	
	//Loop through preview elements and add click events
	$("ul#thumb").children().each(function(){
		$(this).find("a").click(function(event){
			event.preventDefault();
			tempSelector = $(this).find("a.selector");
			//only applied if image is not already selected
			if(!tempSelector.hasClass("selected")){
				clearTimeout( imageTimer );
				//Find selected child
				var picNum = $(this).parent().index();
				showHidePreview(picNum);
				showHideContent(picNum);
				showHideContentStory(picNum);
				imageTimer = setInterval("showNextImage();", 7000);
			}
		});
	});
});
function showHidePreview(picNum){
	//strips all selectors and adds correct selector
	$("ul#thumb a.selector").removeClass("selected");
	$("ul#thumb a.selector:eq(" + picNum + ")").addClass("selected");
}
function showHideContent(picNum){
	$("ul#image li:eq(" + picNum + ")").hide();
	$("ul#image").children().each(function(){
		if($(this).css("z-index") == "2"){
			$(this).css("z-index","1");
		}else{
			$(this).css("z-index","0");
		}
	});
	$("ul#image li:eq(" + picNum + ")").css("z-index","2");
	$("ul#image li:eq(" + picNum + ")").fadeTo(500,1);
}
function showHideContentStory(picNum){
	$("ul#headerNewsContent li:eq(" + picNum + ")").hide();
	$("ul#headerNewsContent").children().each(function(){
		if($(this).css("z-index") == "2"){
			$(this).css("z-index","1");
		}else{
			$(this).css("z-index","0");
		}
	});
	$("ul#headerNewsContent li:eq(" + picNum + ")").css("z-index","2");
	$("ul#headerNewsContent li:eq(" + picNum + ")").fadeTo(500,1);
}
function showNextImage(){
	var cTotal = $("ul#image").children().size();
	$("ul#image").children().each(function(){
		if($(this).css("z-index") == "2"){
			var cCurrent = $(this).index();
			if(cCurrent < cTotal - 1){
				cToShow = cCurrent + 1;
			}else{
				cToShow = 0;
			}
		}
	});
	showHidePreview(cToShow);
	showHideContent(cToShow);
	showHideContentStory(cToShow);
}
