$(document).ready(function() {
	//initial play
	window.backgroundTimerId = setTimeout('setBackground(1, window.action)', 0);	//FIRST START
	
	toggleShow = "hide";
	//console.log("initial window.action: " + window.action);
	
	//loadImage(0, max);
	$('#secondaryNav').fadeIn(2000);
	$('#clientControl').fadeIn(2000);
	
 	//setBackground via clicking on the numbers
 	$('.controlBox ul li').each(function(intIndex) {
			$(this).children().bind("click", function() { 
 					if(intIndex > 0) {	//not story button
 						$('.slideshowBox ul li > a').removeClass('active');
 						$('.slideshowBox ul'). find('li:eq(1) a').addClass('active');
						
						if(toggleShow == "show") { //project story is visible
 							$('.mainContent').fadeOut('fast');
 							//console.log("turnoff");
 							toggleShow = "hide";
 							$('.controlBox ul li:eq(0)').children().text("view project story");
 						}
 						
						clearTimeout(window.backgroundTimerId);
 						window.action = "stop";
 						//console.log("pressed number");
 						setBackground(intIndex, "stop"); return false; 
 						
 					} else {
 						if((toggleShow == "hide") && (window.ready == "true")) {
				
							clearTimeout(window.backgroundTimerId);
							//setBackground(window.stepNum, "stop");
							
							//hide images
							$('#mainContentBox').fadeOut('fast');
							// show project story
							$('.mainContent').fadeIn('slow');
							window.action = "stop";
							
							$('.slideshowBox ul li > a').removeClass('active');
							$('.slideshowBox ul'). find('li:eq(1) a').addClass('active');
							toggleShow = "show";
							
							$('.controlBox ul li:eq(0)').children().text("hide project story");
							
						} else if ((toggleShow == "show") && (window.ready == "true")) {
							// hide project story
							$('.mainContent').fadeOut('fast');
							//show images
							$('#mainContentBox').fadeIn('slow');
							
							//clearTimeout(window.backgroundTimerId);
							window.action = "play";
							
							tempNum = window.stepNum;
							++tempNum; //play the next slide
							
							$childrenNumber = $('.controlBox ul').children().size();
							//console.log("children " + $childrenNumber);
	
							if($childrenNumber > 2) {
								window.backgroundTimerId = setTimeout('setBackground(tempNum, window.action)',1000);	//START PLAYING AGAIN
							}
							
							$('.slideshowBox ul li > a').removeClass('active');
							$('.slideshowBox ul'). find('li:eq(0) a').addClass('active');	
							toggleShow = "hide";
							
							$('.controlBox ul li:eq(0)').children().text("view project story");
						}	
 					}
			});
 	});
 	
 	//stop and start slide show function
 	//stop
 	$('.slideshowBox ul li:eq(1) a').click(function() {
 		clearTimeout(window.backgroundTimerId);
 		window.action = "stop";
 		$('.slideshowBox ul li > a').removeClass('active');
 		$('.slideshowBox ul'). find('li:eq(1) a').addClass('active');
 	});
 	
 	//start
 	$('.slideshowBox ul li:eq(0) a').click(function() {
 		clearTimeout(window.backgroundTimerId);
 		//loadImage();
 		window.action = "play";
 		window.backgroundTimerId = setTimeout('changeBackground(window.action)',100);	//START PLAYING AGAIN
 		$('.slideshowBox ul li > a').removeClass('active');
 		$('.slideshowBox ul'). find('li:eq(0) a').addClass('active');
 	});
});