

// JavaScript Document

var photoHeight = 508;
var photoGap = 200;
var firstHeight = 85;
var photoBlock = photoHeight+photoGap;
var scrollOld = -1;
var scrollNew = 0;
var timeout = 0;
var closestOldFolder = -1;
var closestNewFolder = 0;
var closestOldPhoto = -1;
var closestNewPhoto = 0;
var folderOld = -1;
var folderNew = 0;
var imgArr = new Array();
var foldArr = new Array();
var firstArr = new Array();
var twitter_status = "open";
var twitter_animating = 0;
var originalTop = 0;
var deeplink = false;
var header_status = "closed";
var about_status = "closed";
var firstPopIn = 300;

var header_spacer = 260;

//$("#trace").html();

function navigate(where,time)
{
	deeplink = false;
	
	if(where == null)
	{	
		$.scrollTo(0,time, {easing:'easeInOutQuart'});
	}
	else
	{
		var offset = $(where).offset();
		
		ypos = offset.top-102; //-(window.innerHeight/2);
		
		//ypos = ypos+(photoHeight/2);
		//if(about_status == "open")
		//	ypos-=($("#about").height()-3);
		
		$.scrollTo(ypos,time, {easing:'easeInOutQuart'});
		
	}
}


function saveHeightsDelay(jumpto)
{
	// photo positions
	photoPos = new Array();
	
	
	if(jumpto!=false)
	{
		navigate(jumpto,0);
	}
	
	var i = 0;
	jQuery.each($('.photo'), function() 
	{
		photoPos[i]=$(this).offset(); 
		i++;		
	}); 
	
	setNavClick();
	
	// deep link
	//if(location.href.indexOf("#") != -1 && $(this).find('input').val().split("|")[2]==location.href.substr(location.href.indexOf("#")).substr(1) && deeplink==false) 
	//{
	//	deeplink = true;
	//	
	//	navigate(jumpto,0);
	//}	
	
	
	//checkScrollEvent();
	//init();
	
}

$(document).ready(function() 
{
	
	var i = 0;
	var j = 0;
				  
	// first time
	if(location.href.indexOf("#") != -1)
	{  	}
	else
	{
		if(location.href.indexOf("start") == -1)
		setTitle(0,$("#photoNo_0").val().split("|")[3]);
		//onScrollCheck();
	}
				  
	//keyboard
	$("#keyboard").find('img').hover(
	   // over
	   function () {  $(this).attr("src", 'img/keys.png'); },
	   // out
	   function () { $(this).attr("src", 'img/keyboard.png'); }
				  
		);
					   
				  
				  
	//position about
	shift = ($("#about").height()*-1);
	$("#about").css('margin-top',shift); //$("#white_header_box").height();
	originalPos = $("#about").css('marginTop');
				  	

	//			  
	jQuery.each($('.photo'), function() 
		{
				
			//console.log($(this));
			var myNum = i;
			imgArr[i] = $(this);
			i++;
							  
			//hover function
			//$(this).hover(
			// over
			//function () { },
			// out
			//function () { });

			// click function
			//$(this).click(
			//function()
			//{ navigate(imgArr[myNum+1],800) } );
			
			// first of each folder
			folderNew = $(this).find('input').val().split("|")[1];

			if(folderNew!=folderOld)
			{
				foldArr[j] = $(this);
				firstArr[j] = myNum;
				j++;
			}	
				
			//link to project
			
			if(link[folderNew]!="" && link[folderNew]!="-")
			{
				$(this).css('cursor','pointer');
				$(this).click(function()
				{ 
					window.open(link[$(this).find('input').val().split("|")[1]]);
					return false; 
				} );
			}
			else
			{
				$(this).unbind('click');
				$(this).css('cursor','default');
			}	
			///////////	
			
				
			folderOld = folderNew;	
				
			if(location.href.indexOf("#") != -1 && $(this).find('input').val().split("|")[2]==location.href.substr(location.href.indexOf("#")).substr(1) && deeplink==false)				
			{
				if(location.href.indexOf("start") == -1)
				{
					deeplink = true;
					jumpto = $(this);
				
					folder_id = $(this).find('input').val().split("|")[1];
					setNavItem(folder_id);
					setTitle(folder_id,$(this).find('input').val().split("|")[3]);
					setLocationHash($(this).find('input').val().split("|")[2],$(this).find('input').val().split("|")[4]);
				}
				
			}	
		}); // this closes each folio-ctn
	

	//
	if(location.href.indexOf("#") != -1) //deeplink
	{
		if(location.href.indexOf("start") == -1)
			setTimeout(saveHeightsDelay,100,jumpto);
		else
			setTimeout(saveHeightsDelay,100,false);
	}
	else
	{
		setTimeout(saveHeightsDelay,100,false);		  
	}
					
	$("img").lazyload({ threshold : (window.innerHeight),failurelimit : 10 });

	//$("img").lazyload({effect : "fadeIn"});

	setTimeout(onScrollCheck,100);
				  keyboard();
				  
}); //this closes doc.ready




deltaset = false;
startMode = false;


$(window).scroll(function() { onScrollCheck(); } );



function onScrollCheck()
{
	
				 
	posTop = $(window).scrollTop();
				 
	

	// fix line
	if(posTop>$("#intro").height()-4)
	$("#intro_line").css('display','block');	
				 
	// fix line
	if(posTop<=$("#intro").height()-4)
	$("#intro_line").css('display','none');				 

	if(posTop<$("#intro").height())
	{
		$("#intro").css('top',posTop*-1);
	}
	else
	{
		if(!startMode)
		$("#intro").css('top',$("#intro").height()*-1);
	}
	
	
	// fixed header
	if(posTop<(header_spacer-1))
	{
		positionHeader();
	}
	else
	{ 
	    if(!startMode)
		fixedHeader();
	}
				 
	
	// happens once
	/*if($(window).scrollTop()>((window.innerHeight*.4)-firstPopIn) && !startMode)
	{
		startMode = true;
		//$("#header").css('position','fixed');
		removeIntro();
	}*/
	

	if((header_status == "open" || about_status == "open") && deltaset == false)
	{
		delta = $(window).scrollTop();
		deltaset = true;
	}
	
	//$("#trace").html(closestNewFolder+" "+closestOldFolder);
	
				 
	if(header_status == "open" || about_status == "open")
	{
		newDelta = $(window).scrollTop() - delta;
				 
		if(newDelta<0)
		{
			newDelta = newDelta*-1;
		}
	
		if(newDelta>70)
		{
			
			if(header_status == "open" && about_status == "closed")
			{
				readMore(false);
			}	
			else if(header_status == "closed" && about_status == "open")
			{
				openAbout(false);
			}
			else if(header_status == "open" && about_status == "open")
			{
				readMore(false);
				openAbout(false);			
			}
			
			newDelta = 0;			
		}	
				 
				 
		//$("#trace").html(newDelta+" "+delta+" "+$(window).scrollTop());				 
				 
	}
	
	if(startMode && about_status == "closed" && header_status == "closed")
	{
	
	
	page_center = $(window).scrollTop();
	page_center+=50; 
	
	dis = 10000000000;
	
	j = 0;
	jQuery.each($('.photo'), function() 
	{
		photo_center = $(this).offset().top;

		if((page_center-photo_center)<0)
			distance_pos = ((page_center-photo_center)*-1);
		else
			distance_pos = (page_center-photo_center);
			
		if(distance_pos<dis)
		{
			dis = distance_pos;
			closestNewFolder = j;
			closestNewPhoto = j;
			closestNewFolder = $("#photoNo_"+closestNewPhoto).val().split("|")[1];
		}
		j++;
	});
		
		
	
	if(closestNewFolder!=closestOldFolder) // once
	{
		
		
		setLocationHash($("#photoNo_"+closestNewPhoto).val().split("|")[2],$("#photoNo_"+closestNewPhoto).val().split("|")[4]);
		folder_id = $("#photoNo_"+closestNewPhoto).val().split("|")[1];
		setNavItem(folder_id);
		setTitle(folder_id,$("#photoNo_"+closestNewPhoto).val().split("|")[3]);
		
	}
	
	closestOldFolder = closestNewFolder;
	closestOldPhoto = closestNewPhoto;
		
	} //startmode
	
	
	//} // 
	
				 
}


function positionHeader()
{
	startMode = false;
	value =  ($(window).scrollTop()*-1)+(header_spacer-1); // ($(window).scrollTop()*-1)+innerH-(firstPopIn/2);
	$("#header").css('top',value);
	$("#more_button").css('display','none');
	$("#whiteSlider").css('display','none');
	resetTitle();
	$("#nav_item_0").children().removeClass('nav_item_on').addClass('nav_item_off');
}

function resetTitle()
{
	window.location.hash = "#start";
	document.title = "The Work of Thorsten Hayer"; 
}

function fixedHeader()
{
	setNavItem(0);
	setLocationHash($("#photoNo_0").val().split("|")[2],$("#photoNo_0").val().split("|")[4]);
	$("#header").css('top',0);
	$("#whiteSlider").css('display','block');
	$("#more_button").css('display','block');
	startMode = true;
}

function setTitle(no,name)
{
	$("#project_title").html(title[no]);
	$("#project_name").html(name);
}


function setNavItem(no)
{
	//$("#trace").html(no);
	var i=0;
	jQuery.each($('.nav_item'), function() 
	{
		$(this).children().removeClass('nav_item_on').addClass('nav_item_off');
		
		if(i==no)
		$(this).children().removeClass('nav_item_off').addClass('nav_item_on');
				
		i++;
	});
}

function setNavClick()
{
	
	var k=0;
	jQuery.each($('.nav_item'), function() 
	{
		var myNumK = k;	
				
		$(this).click(function()
		{			 
					  
				if(!startMode)
				{
					  fixedHeader();
					  about_status = "open";
					  openAbout(true);
			    }	  
				
				if(header_status == "open")	
					readMore(true);		
				if(about_status == "open")	
					openAbout(true);		
			
				navigate(foldArr[myNumK],0);
					  
			//}

		});
		
		k++;		
	});
	
}

function init()
{
	checkerID = setInterval(checkScrollEvent,150);
}

function savePhotoPosition()
{
	photoPos = new Array();
	
	for (i=0;i<=photoNumber;i++)
	{
		calcs=photoBlock*(i+1);
		calcs+=firstHeight;
		photoPos[i]=calcs;
	}
	
	init();
}


function setLocationHash(link,title)
{
	window.location.hash = "#"+link;
	document.title = "The Work of Thorsten Hayer | "+title;
	//$("#photo_id").html("Photo #"+no);
}


original_height = 0;

function readMore(clicked)
{
	
	//fb_code = '<iframe src="http://www.facebook.com/plugins/like.php?href='+window.location+'&amp;layout=standard&amp;show_faces=false&amp;width=300&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=light&amp;height=30" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:300px; height:30px;" allowTransparency="true"></iframe>';

	fb_code ='<a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>';
	
	if(header_status == "closed")
	{
				
		$("#award").css('display','');
		header_status = "open";
		setTimeout(lessMore,200,'Close');
		$("#more").css('marginTop','-2000px');
		$("#credit").html(credit[closestNewFolder]+"<br /><br />");
		$("#award").html(award[closestNewFolder]+"<br /><br />");
		$("#desc").html(desc[closestNewFolder]+"<br /><br /><br />"+fb_code);
		
		if(link[closestNewFolder]!="")
		{
		
			$("#visit").css('display','');
			$("#visit").css('width','50px');
			$("#visit").css('background','#000');
			$("#visit").css('cursor','pointer');
			$("#visit").html('Visit Site');
			$("#visit").unbind('click').click(function()
						  {  
						  window.open(link[closestNewFolder]);
						  return false; 
						  });
		
		}
		if(link[closestNewFolder]=="")
		{
			$("#visit").css('display','');
			$("#visit").unbind('click');
			$("#visit").css('width','90px');
			$("#visit").css('cursor','default');
			$("#visit").css('background','#959595');
			$("#visit").html('Site discontinued');
		}
		
		if(link[closestNewFolder]=="-")
		{
			$("#visit").css('display','none');
		}		
		
		if(award[closestNewFolder]=="")
		{
			$("#award").css('display','none');
		}	
		
		
		original_height = $("#more").height();
		original_height_neg = ((original_height*-1)-20);

		$("#more").css('marginTop',original_height_neg);
		$("#mask").css('marginTop',0);
		$("#mask").css('height',original_height+40);
		
		// shift page
		
		//if(about_status == "open")
		//	$("#photo_container").animate( { paddingTop: (original_height+302+4)+$("#intro").height() }, 400,'easeInOutQuart', function() {  } );
		//else
		//	$("#photo_container").animate( { paddingTop: original_height+302+4 }, 400,'easeInOutQuart', function() {  } );

		$("#whiteSlider").animate( { marginTop: (original_height+110)-600 }, 400,'easeInOutQuart', function() {  } );
		$("#more_button").animate( { marginTop: original_height+64 }, 400,'easeInOutQuart', function() {  } );
		$("#more").animate( { marginTop: 7 }, 400,'easeInOutQuart', function() {  } );

	}
	else // close 
	{
		deltaset = false;
		
		
		if(!clicked)
		{
			//if(about_status == "open")
			//	$("#photo_container").animate( { paddingTop: 302+$("#intro").height() }, 300,'easeInOutQuart', function() { } );
			//else
			//	$("#photo_container").animate( { paddingTop: 302 }, 300,'easeInOutQuart', function() {  } );
				
			$("#whiteSlider").animate( { marginTop: -518 }, 300,'easeInOutQuart', function() { header_status = "closed";   } );
			$("#more_button").animate( { marginTop: 31 }, 300,'easeInOutQuart', function() {  } );
			$("#more").animate( { marginTop: original_height_neg }, 300,'easeInOutQuart', function() { $("#mask").css('marginTop',-800); } );
			setTimeout(lessMore,150,'More');
		}
		else
		{
			header_status = "closed";
			
			//if(about_status == "open")
		    //	$("#photo_container").css('paddingTop',302+$("#intro").height());
			//else
			// 	$("#photo_container").css('paddingTop',302);
				
			$("#whiteSlider").css('marginTop',-518); 
			$("#more_button").css('marginTop',31);   
			$("#more").css('marginTop',original_height_neg);  
			$("#mask").css('marginTop',-800); 
			lessMore('More');
		}
			
		
		
	}		

}

window.onresize = keyboard;

function keyboard()
{
	$("#keyboard").css('display','block');
	$("#keyboard").css('top',window.innerHeight-40);
	$("#keyboard").css('left',window.innerWidth-90);
}


function lessMore(read)
{
	$("#more_button").html(read);
}

function openAbout(clicked)
{

	if(!startMode)
	{
		if(about_status == "closed")
		{
			about_status = "open";
			$("#about").animate( { marginTop: 0 }, 400,'easeInOutQuart', function() {  } );
			$("#photo_container").animate( { marginTop: $("#about").height() }, 400,'easeInOutQuart', function() {  } );
			$("#header").animate( { marginTop: $("#about").height() }, 400,'easeInOutQuart', function() {  } );
			//$("#photo_container").animate( { marginTop: 357 }, 400,'easeInOutQuart', function() {  } );
		}
		else 
		{
			
			deltaset = false;
			about_status = "closed";
			$("#about").animate( { marginTop: originalPos }, 300,'easeInOutQuart', function() {  } );
			$("#photo_container").animate( { marginTop: 0 }, 300,'easeInOutQuart', function() {  } );
			$("#header").animate( { marginTop: 0 }, 300,'easeInOutQuart', function() {  } );
		}
	}
	
	if(startMode)
	{
		if(about_status == "closed")
		{
			about_status = "open";
			
			$("#intro").css('top',($("#intro").height()*-1)-$("#about").height());
			$("#about").css('marginTop',0);

			$("#intro").animate( { top: 0 }, 400,'easeInOutQuart', function() {  } );
			
			//if(header_status == "open")			
			//	$("#photo_container").animate( { paddingTop: (302+$("#intro").height())+$("#more").height() }, 400,'easeInOutQuart', function() {  } );
			//else
				$("#photo_container").animate( { marginTop: $("#intro").height() }, 400,'easeInOutQuart', function() {  } );
			
			$("#header").animate( { marginTop: $("#intro").height() }, 400,'easeInOutQuart', function() {  } );
		}
		else 
		{
			
			if(!clicked)
			{
			
				deltaset = false;
				
				$("#intro").animate( { top: ($("#intro").height()*-1) }, 300,'easeInOutQuart', function() { $("#about").css('marginTop',originalPos); } );
				
				//if(header_status == "open")			
				//	$("#photo_container").animate( { paddingTop: 302+$("#more").height() }, 300,'easeInOutQuart', function() { } );
				//else
					$("#photo_container").animate( { marginTop: 0 }, 300,'easeInOutQuart', function() {  } );
				
				$("#header").animate( { marginTop: 0 }, 300,'easeInOutQuart', function() { about_status = "closed"; } );
				
			}
			else
			{
				deltaset = false;
				about_status = "closed";
				$("#intro").css('top',($("#intro").height()*-1)); 
				$("#about").css('marginTop',originalPos); 
				
				//if(header_status == "open")			
				//	$("#photo_container").css('paddingTop',302+$("#more").height());
				//else
					$("#photo_container").css('marginTop',0);
				
				$("#header").css('marginTop',0); 
				
			}
		}
	}	
}

key_enabled = true;

$(document.documentElement).keyup(function(event) 
{
		if(key_enabled)
		{
								  
				// handle cursor keys

				if (event.keyCode == 39)  // next photo // key right
				{		
					if(header_status=="open")  readMore(true);	  
					key_enabled = false; setTimeout(enableKey,310);
								  
					if(!startMode)
						goto = closestNewPhoto;
					else
						goto = closestNewPhoto+1;
										  
					navigate(imgArr[goto],300);
				} 
				else if (event.keyCode == 37)  // prev photo // key left
				{
						if(header_status=="open")  readMore(true);	 
						key_enabled = false; setTimeout(enableKey,310);

						goto = closestNewPhoto-1;
										  
						navigate(imgArr[goto],300);
				} 			
										  
				  if (event.keyCode == 38)  // prev project // key up
				  {		
					  if(header_status=="open")  readMore(true);	 			  
					  key_enabled = false; setTimeout(enableKey,310);			  
								
					  goto = closestNewFolder;
					  goto--;
					  navigate(foldArr[goto],300);
										  
				  } 
				  else if (event.keyCode == 40)  // next project // key down
				  {	
					if(header_status=="open")  readMore(true);	 			  
					key_enabled = false; setTimeout(enableKey,310);									  
								  
					if(startMode)
					{
						goto = closestNewFolder;
						goto++;
						navigate(foldArr[goto],300);
					 }
					else
					  {
						goto = closestNewPhoto;
						navigate(imgArr[goto],300);
					  }
					 
				  } 	
								  
		} // if
});

function enableKey()
{
	key_enabled = true;
}
/*
function writeFrame(no)
{
	$("#facebook").html("<iframe id='fb_frame' onLoad='IFrameDone()' src='http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fphotography.thorstenhayer.com%2F%23"+no+"&amp;layout=button_count&amp;show_faces=false&amp;width=120&amp;action=like&amp;font=trebuchet+ms&amp;colorscheme=dark&amp;height=30' scrolling='no' frameborder='0' style='border:none; overflow:hidden; width:120px; height:30px;margin-top:10px;margin-left:5px;' allowTransparency='true'></iframe>");
	$("#facebook").css("visibility","hidden");
	//WaitForIFrame();
}

function WaitForIFrame() 
{
	if ($("#fb_frame").document.readyState != "complete") {
		setTimeout("WaitForIFrame();", 500);
	} else {
		IFrameDone();
	}
}

function IFrameDone() 
{
	$("#facebook").css("visibility","visible");
}*/

