$(function (){

	var imgPath ="http://annefwalterscompany.com/assets/images/homepage/";
	var ImgList = new Array( "garden.jpg", "stdav_est_5.jpg" ,"obw_0.jpg","dcc.jpg","coatesville.jpg", "ro_0.jpg", "cf_2.jpg" ,"church.jpg","200nhighst.jpg", "kennet_02.jpg");
	var hrefs = new Array( "residential/West-Chester-Residence", "residential/St.-Davids-Estate" ,"residential/Bryn-Mawr-Residence" ,"commercial/Downingtown-Country-Club", "residential/Coatesville-Estate", "portfolio/residential/Malvern-Residence", "residential/Chadds-Ford-Residence" , "institutional/Westminster-Presbyterian-Church", "commercial/200-N.-High-Street", "institutional/Kennett-Township-Municipal-Building");
	var introLen = ImgList.length;
	var loadedImgs = 0;
	var output = $('<div/>', {
		  "id": "output"
		}).appendTo('body');

	
	
	var intro =  $('#intro');
	var imgIndex = 0;
	var p_nav;
	
	//hide end navigation
	intro.find('a').css("display", "none");
	
	function preloadImages(ImgList){
		
		var loadingImg = '<div id="loading" ><img src=\"assets/images/loader.gif\" /><span>Loading...</span></div>';
		
		p_nav = intro.find('a').detach();
		
		intro.html(loadingImg);
		var numImgs = ImgList.length;
		var img = new Array();
		
		for(i=0; i < numImgs ;i++){

			img[i] = new Image();
			$(img[i]).load(function () {
				loadedImgs ++;
				//alert($(this).attr('src'));
				//$('body').append($(this))
				if(loadedImgs == numImgs){
				//alert('all done');
					//output.append('<p>All images loaded '+loadedImgs+"</p>");
					runIntro(true);
				}
				
			}).error(function () {
				//alert('image could not be loaded');
			}).attr('src',imgPath+ImgList[i]);
		}
		return img;
	}
	
	var ImgArr = preloadImages(ImgList);
	
	//alert(ImgArr[0].src);
	
	function runIntro(start){
	
		if(start===undefined){start=false};		
		
		var overlay = $("<a/>", {
		  "class": "overlay",
		  "style": "background-image:url("+imgPath+ImgList[imgIndex]+");",
		  "href" : "portfolio/"+hrefs[imgIndex]
		});
		
		if(start){
			overlay.css({opacity:0});
			intro.html(overlay);
			intro.children().first().animate({opacity:1},{duration:1000});
			//$("#output").append('<p>start is true </p>');
		} else /*if (imgIndex < introLen)*/ {
			intro.prepend(overlay);
			intro.children().last().animate({opacity: 0},{duration:2000})
				.animate({opacity: 0},{duration:1,  complete:function(){
					//$("#output").append('<p>removing' +$(this).attr('href')+ '</p>');
					$(this).remove();
					//$(this).dequeue();
				}
			});
			//output.html('imgIndex <= introLen');
		} /*else if (imgIndex){
			//$('body').append('<p>call endintro</p>');
			endIntro();
		}*/
		
		if(imgIndex < introLen){
			
			//output.html('animating now');
			
			/*  Set starting img, opacity and position   */
			//$("#output").append('<p>setting up '+ImgList[imgIndex]+"</p>");
			intro.children().first().delay(2000).animate({opacity: 1}, {duration:0, complete:function(){
						
						imgIndex++; 
						//$("#output").append('<p>imgIndex++ '+imgIndex+"</p>");
						
						if(imgIndex == introLen){ 
							//$("#output").append("<p>reached end "+imgIndex+" "+introLen+"</p>");
							imgIndex = 0;
						}else{
							//$("#output").append("<p>else</p>");
						}
						runIntro();
					}
				});
			}
	}
	

   function endIntro(){
		//$('body').append('<p>endintro</p>');
		intro.animate({opacity: 0}, 1000, function(){buildNav();})
	}	

	function buildNav(){
	
		intro.html(p_nav);
		intro.find('a')
			.fadeIn(0)
			.bind({
				mouseover: function() {  
				//alert('mouseover');
					$(this).siblings().stop(true, true).animate({"opacity": .6}, "fast");  
					$(this).stop(true, true).animate({opacity: 1}, "fast");  
				},
				mouseout: function() {  
					$(this).siblings().andSelf().delay(800).animate({"opacity": 1}, "fast");  
				}
			});
			
		intro.delay(100).animate({opacity: 1}, 1000);  
	}
});


function StopWatch(){

	var startTime = null; 
	var stopTime = null; 
	var running = false; 
	  
	function getTime(){
		var day = new Date();
		return day.getTime();
	}
	this.start = function(){ 

	if (running == true){
		return;}
			
		else if (startTime != null) 
			stopTime = null; 

		running = true;    
		startTime = getTime();
	}
	
	this.duration = function(){ 

	if(startTime == null || stopTime == null)
	   return 'Undefined';
	else  
	   return (stopTime - startTime) / 1000;

	}
}

var _StopWatch = new StopWatch();
_StopWatch.start();
