var photoid = -1; // Current Photo Popped Up
document.onkeyup = KeyCheck;   

$(function(){
	var q = $("#search")[0].value;
	$("#dates>div").html('<ul><li><a href="/" class="y">Loading...</a></li></ul>');
	$.ajax({ 
		method: "get",url:"/ajax/dates.php?q=" + q,
		success: function(html){ //so, if data is retrieved, store it in html
			$("#dates>div").html(html);
		},
		cache: false
	});
		   
	$("#search").keyup(function() {
		var q = this.value;
		$.ajax({ 
		method: "get",url:"/ajax/dates.php?q=" + q,
		success: function(html){ //so, if data is retrieved, store it in html
			$("#dates>div").html(html);
		},
		cache: false		
	});
		
	});
	
});






// OLD JAVASCRIPT

function KeyCheck(e){
	if (!e) e = window.event;
	e = e.keyCode;
	if(document.getElementById("popup").style.display=="block"){
		if(document.getElementById("photo-link-0")){
			if(e==39){
				popup_go('+1');
			}
			if(e==37){
				popup_go('-1');
			}			
		}
		if(e==27){
			popup_close();
		}		
	}else{
		if(e==39 && document.getElementById("photo-link-0")){
			popup_open('photo-link-0');
		}
	}
}

function LoadCheck(){
	if(location.hash.length>12 && location.hash.substr(0,12)=="#photo-link-"){
		popup_open(location.hash.substr(1));
	}
}

function navdates(year){
	els = document.getElementById('dates').getElementsByTagName('li');
	for (var i=0, len=els.length; i<len; i++) {
		var id = document.getElementById('dates').getElementsByTagName('li')[i].id;
		if(id.substr(0,6)=="dates-"){
			if(id=="dates-" + year){
				document.getElementById(id).className = "dates show";
			}else{
				document.getElementById(id).className = "dates hide";
			}
		}
		if(id.substr(0,6)=="years-"){
			if(id=="years-" + year){
				document.getElementById(id).className = "sel";
			}else{
				document.getElementById(id).className = "";
			}
		}
	}
	return false;
}

function popup_close(){
	document.getElementById("popup").style.display = "none";
	document.getElementById("popup").innerHTML = "";
	if($("#facebookcom").length>0) $("#facebookcom").show();
	location.href = "#";
	return false;
}

function popup_open(id){
	photoid = id.substr(11);
	//document.getElementById("popup").innerHTML = '<a href="/home/2002-07-19/friday-19th-july-2002" onclick="return popup_go(\'+1\');"><img id="popupphoto" src="' + document.getElementById("photo-link-" + photoid).href + '" alt="Large Photo" /></a>';
	document.getElementById("popup").innerHTML = '<div><a href="/" onclick="return popup_go(\'-1\');" class="back">&nbsp;</a><a href="/" onclick="return popup_close();" class="close">&nbsp;</a><a href="/" onclick="return popup_go(\'+1\');" class="next">&nbsp;</a></div>';
	document.getElementById("popup").style.display = "block";
	if($("#facebookcom").length>0) $("#facebookcom").hide();
	popup_go(0);
	return false;
}

function popup_go(change){
	photoid = parseFloat(photoid) + parseFloat(change);
	if(document.getElementById("photo-link-" + photoid)){
		// Set Image Size
		var imgurl;
		var newsize;
		var newsizeh;
		var newsizew;
		newsizew = Math.round((document.documentElement.clientWidth * 0.8)/10) * 10;
		newsizeh = Math.round((newsizew/14)*9);
		if(newsizeh>(document.documentElement.clientHeight-20)){
			newsizeh = Math.round((document.documentElement.clientHeight-20)/10) * 10;
			newsizew = Math.round((newsizeh/9)*14);
		}
		newsize = "." + newsizew + "x" + newsizeh + ".";
		imgurl = document.getElementById("photo-link-" + photoid).href;
		imgurl = imgurl.replace(".1024x1024.", newsize);
		
		document.getElementById('popup').getElementsByTagName('div')[0].style.backgroundImage = "url(" + imgurl + ")";
		location.href = "#photo-link-" + photoid;
	}else{
		popup_close();
	}
	preloadphoto()
	return false;
}

function preloadphoto(){
	var tmp = parseFloat(photoid) + 1;
	tmpimg = new Image(); 
	if(document.getElementById("photo-link-" + tmp)){
		// Set Image Size
		var imgurl;
		var newsize;
		var newsizeh;
		var newsizew;
		newsizew = Math.round((document.documentElement.clientWidth * 0.8)/10) * 10;
		newsizeh = Math.round((newsizew/14)*9);
		if(newsizeh>(document.documentElement.clientHeight-20)){
			newsizeh = Math.round((document.documentElement.clientHeight-20)/10) * 10;
			newsizew = Math.round((newsizeh/9)*14);
		}
		newsize = "." + newsizew + "x" + newsizeh + ".";
		imgurl = document.getElementById("photo-link-" + tmp).href;
		imgurl = imgurl.replace(".1024x1024.", newsize);
		
		tmpimg.src = imgurl;
	}
	return false;
}

function video_play(id){
	
	document.getElementById("popup").style.display = "block";
	if($("#facebookcom").length>0) $("#facebookcom").hide();
	// Calc Size
	var vidw;
	var vidh;
	vidw = document.documentElement.clientWidth - 100;
	vidh = Math.round((vidw/16)*9)+25;
	if(vidh>(document.documentElement.clientHeight-150)){
		vidh = document.documentElement.clientHeight - 150;
		vidw = Math.round((vidh/9)*16)+25;
	}
	//vidw = (Math.round(Math.min(window.innerHeight,window.innerWidth)/10)*10) - 100;
	//vidh = Math.round((vidw/16)*9)+25;
	document.getElementById("popup").innerHTML = '<object id="popupvideo" type="application/x-shockwave-flash" data="' + id.href + '?rel=0&border=0&showinfo=0&autoplay=1" height="' + vidh + '" width="' + vidw + '" style="margin-top:' + (document.documentElement.clientHeight-vidh-50)/2 + 'px;"><param name="movie" value="' + id.href + '?rel=0&border=0&showinfo=0&autoplay=1" /><param name="wmode" value="transparent" /></object><a href="/" onclick="return popup_close();" class="closevid">Close Video</a>';
	return false;
}

function popup_save(){
	if(document.getElementById("photo-link-" + photoid)){
		window.location.href = "/img/download.php?url=" + encodeURIComponent(document.getElementById("photo-link-" + photoid).href.substr(38));
	}
	return false;
}

function startss(){
	
	var id;
	id = document.getElementById("content").getElementsByTagName("div");
	var x;
	for (x in id){
		if(id[x].className=="slideshow") slideshow(id[x].id,85,1,1);
	}	
}

var ssx;
var ssy;
ssx = 0;
ssy = 0;
var sx;
var sy;
sx = 1;
sy = 1;

function unislide(){
	
	// Check Boundaries
	if(ssy>=0) sy = -1;
	if(ssx>=0) sx = -1;
	if(ssy<=(80 - 150)) sy = 1;
	if(ssx<=(80 - 200)) sx = 1;
	
	// Calculate Move
	ssy = parseFloat(ssy) + parseFloat(sy);
	ssx = parseFloat(ssx) + parseFloat(sx);
	
	// Move All
	var id;
	id = document.getElementById("content").getElementsByTagName("a");
	var cc;
	for (cc in id){
		if(id[cc] && id[cc].className=="slideshow"){
			id[cc].style.backgroundPosition = ssx + "px " + ssy + "px";
		}
	}	
	
	setTimeout("unislide()",80); 	
}


function slideshow(id,time,x,y){

	var top;
	var left;

	top = parseFloat(document.getElementById(id).getElementsByTagName("a")[0].style.top);
	left = parseFloat(document.getElementById(id).getElementsByTagName("a")[0].style.left);
	if(!top) top = 0;
	if(!left) left = 0;

	if(top>=0) y = 0 - parseFloat(y);
	if(left>=0) x = 0 - parseFloat(x);
	
	if(top<=(80 - 150)) y = 0 - parseFloat(y);
	if(left<=(80 - 150)) x = 0 - parseFloat(x);
	
	top = parseFloat(top) + parseFloat(y);
	left = parseFloat(left) + parseFloat(x);
	
	document.getElementById(id).getElementsByTagName("a")[0].style.top = top + "px";
	document.getElementById(id).getElementsByTagName("a")[0].style.left = left + "px";
	

	var e;
	e = "slideshow('" + id + "'," + time + "," + x + "," + y + ")";
	
	setTimeout(e,time); 
	
}
