var timeHash=undefined, currentHash=undefined;
var imIndexMax, imIndex, imIndexDir = 0;

$(document).ready(init);

function init() {
	var sb=CONSTANT.get("ROOT")+"/#", href=window.location.href;
	if(href.length>sb.length && href.substr(0,sb.length)!=sb) {
		var href_n=href.search(/\?/)+1, hash="";
		if(href_n>0) { hash=href.substring(href_n); }
		window.location.href = sb+hash;
		return false;
	}
	
	if(imList.length<1) {
		$("#messagebox").html("<h1>Inga inlägg</h1>");
		return false;
	}
	imIndexMax = imList.length-1;
	imIndex = imIndexMax;
	
	$("#messagebox").html("<h1>Laddar...</h1>");
	
	updateHash();
}

function setHash(str) {
	if(currentHash!=str) {
		window.location.href=CONSTANT.get("ROOT")+"/#"+(currentHash=str);
	}
}

function updateHash() {
	if(timeHash!=undefined) { clearTimeout(timeHash); }
	var href=window.location.href, href_n=href.search(/\#/)+1, hash="", res=false;
	if(href_n>0 && href.length>href_n) { hash=href.substring(href_n); }
	if(currentHash==undefined) { currentHash=hash; res=true; }
	else if(hash!=currentHash) { currentHash=hash; res=true; }
	if(res) {
		var index = -1;
		for(var n=0; n<imList.length && index==-1; n++) {
			if(imList[n][0].toString()==hash) { index = n; }
		}
		if(index>=0) {
			if(imIndex>index) { imIndexDir = 1; }
			else { imIndexDir = -1; }
			imIndex = index;
			imGoTo();
		}
		else {
			window.location.href = CONSTANT.get("ROOT")+"/#"+imList[imIndex][0];
		}
	}
	timeHash = setTimeout("updateHash()",1000);
	return res;
}

function imGoTo() {
	var insert = document.createElement('img');
	insert.src = 'upload/'+imList[imIndex][0]+'.jpg';
	insert.alt = 'Foto #'+imIndex;
	insert.style.position = 'absolute';
	insert.style.left = '0px';
	insert.style.top = '0px';
	insert.style.width = document.getElementById('photos').offsetWidth+'px';
	insert.style.height = document.getElementById('photos').offsetHeight+'px';
	if(document.getElementById('photos').childNodes.length==0) {
		document.getElementById('photos').appendChild(insert);
		imGoToPrint();
	}
	else {
		var old = document.getElementById('photos').childNodes[0];
		insert.style.left = (document.getElementById('photos').offsetWidth*imIndexDir)+'px';
		document.getElementById('photos').appendChild(insert);
		var f = setInterval(
			function() {
				var dl = Math.ceil(insert.offsetLeft/3);
				if(Math.abs(dl)>0) {
					insert.style.left = (insert.offsetLeft-dl)+'px';
				}
				else {
					window.clearInterval(f);
					insert.style.left = '0px';
					document.getElementById('photos').removeChild(old);
					imGoToPrint();
				}
			}, 30
		);
	}
}

function imGoToPrint() {
	document.getElementById('pagenumber').innerHTML = (imIndexMax-imIndex+1)+'/'+(imIndexMax+1);
	document.getElementById('messagebox').innerHTML = '<h1>'+imList[imIndex][1]+'</h1><p>'+imList[imIndex][2]+'</p>';
	document.getElementById('datebox').innerHTML = imList[imIndex][3];
	
	imInMotion = false;
	var al = imIndex+1, ar = imIndex-1;
	if(al>imIndexMax) { al = 0; }
	if(ar<0) { ar = imIndexMax; }
	
	document.getElementById('arrowleft').style.zIndex = (imIndex==imIndexMax ? '-1' : '1');
	document.getElementById('arrowleft').src = (imIndex==imIndexMax ? 'graphics/arrowhidden.png' : 'graphics/arrowleft.png');
	document.getElementById('arrowleft').parentNode.href = '#'+imList[al][0];
	
	document.getElementById('arrowright').style.zIndex = (imIndex==0 ? '-1' : '1');
	document.getElementById('arrowright').src = (imIndex==0 ? 'graphics/arrowhidden.png' : 'graphics/arrowright.png');
	document.getElementById('arrowright').parentNode.href = '#'+imList[ar][0];
	
	imLoadSpan(imIndex, 0);
}

function imLoadSpan(_index, _num) {
	if(_num<=10 && imIndex==_index) {
		var im = new Image();
		var f = new function() {
			imLoadSpan(_index, _num+1);
		};
		var l = _index;
		if(Math.abs(_num/2-Math.round(_num/2))>0) { l-= Math.floor(_num/2); }
		else { l+= Math.floor(_num/2); }
		if(l>=0 && l<=imList.length-1) {
			im.src = 'upload/'+imList[l][0]+'.jpg';
			if(((''+navigator.userAgent).toUpperCase()).indexOf('MSIE')>0 && im.complete==true) {
				imLoadSpan(_index, _num+1);
			}
			else {
				im.onload = f;
				im.onrerror = f;
			}
		}
	}
}

function keyboard(e) {
	var kn = -1;
	if(window.event) kn = e.keyCode;
	else if(e.which) kn = e.which;
	if(kn==37) window.location.href = document.getElementById('arrowleft').parentNode.href;
	else if(kn==39) window.location.href = document.getElementById('arrowright').parentNode.href;
}
window.onkeyup = keyboard;
