String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function getHTTPObject() {
	if (typeof XMLHttpRequest != 'undefined') {
		return new XMLHttpRequest();
	}
	try {
		return new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try {
			return new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e) {}
	}
	return false;
}

function get_hbrowser() {
	if (window.innerHeight)
	{
		return window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientHeight != 0)
	{
		return document.documentElement.clientHeight;
	}
	else if (document.body)
	{
		return document.body.clientHeight;
	}	
	return false;
}

var data = new Array();
var images = new Array();
var imageProgress = new Array();
var curimage = 0;
var total = 0;
var defaultHeight = 600;
var topPad = 60;
var page = 0;
var listLoaded = false;
var pageLinks = new Array('Tilbage', 'Indeks');
var imageTimeout = 0;
var aboutLoaded = true;

function addPhoto(response, id, updateNow, callBack) {
	if(response==null)
		return;
	var xmldoc = response.documentElement;
	var kids = xmldoc.childNodes;
	var title = '';
	var description = '';
	for(var i = 0; i < kids.length; i++) {
		var tagname = kids[i].nodeName.toLowerCase();
		if(tagname=='title') {
			if(kids[i].firstChild)
				var title = kids[i].firstChild.data;
		} else if(tagname=='description') {
			if(kids[i].firstChild)
				var description = kids[i].firstChild.data;
		} else if(tagname=='url') {
			var url = kids[i].firstChild.data;
		} else if(tagname=='width') {
			var x = kids[i].firstChild.data;
		} else if(tagname=='height') {
			var y = kids[i].firstChild.data;
		}
	}
	data[id] = {'title' : title, 'description' : description};
	preloadPhoto(url, id, x, y);
	imageProgress[id] = false;
	if(updateNow)
		setPhoto(id, callBack);
	if(id<=total)
		getPhoto(id+1, false, null);
}

function preloadPhoto(img, id, x, y) {
	if(document.images) {
		images[id] = new Image(x, y);
		images[id].src = img;
	}
}

function setPhoto(id, callBack) {
	id = parseInt(id);
	if(id!=curimage)
		return;
	if(imageProgress[id]) {
		imageTimeout = 0;
		imageTimeout = setTimeout('setPhoto('+id+')', 500);
	}
	var img = document.getElementById('photo');
	if(!data[id]) {
		img.src = "spacer.png";
		imageLoading();
	} else {
		img.src = images[id].src;
		setTitle(data[id]['title'], false);
		setDescription(data[id]['description']);
		setCurrent();
	}
	if(callBack!=null)
		callBack();
	return true;
}

function getPhoto(id, updateNow, callBack) {
	if(imageProgress[id])
		return;
	imageProgress[id] = true;
	if(data[id])
		return;
	var url = 'getPhoto.php?photo=' + id;
	var request = getHTTPObject();
	request.open('GET', url, true);
	request.onreadystatechange = function() {
		if(request.readyState == 4 && request.status == 200) {
			addPhoto(request.responseXML, id, updateNow, callBack);
		}// else if(request.status != 200) {
		//	alert('omg!');
		//}
	}
	request.send(null);
}

function imageLoading() {
	setTitle('Loading...', true);
	setDescription('');
}

function getQueryVariable(variable) {
	var query = window.location.search.split('?')[1];
	if(!query)
		return false;
	var vars = query.split("&");
	for (var i=0;i<vars.length;i++) {
		var pair = vars[i].split("=");
		if (pair[0] == variable) {
			return pair[1];
		}
	}
	return false;
}

function setTitle(title, withLoading) {
	var el = document.getElementById('phototitle');
	while(el.firstChild)
		el.removeChild(el.firstChild);
	el.appendChild(document.createTextNode(title));
	if(withLoading) {
		return;
		var img = document.createElement('img');
		img.setAttribute('src', 'loading.gif');
		img.setAttribute('alt', '[loading image]');
		el.appendChild(document.createTextNode(' '));
		el.appendChild(img);
	}
}

function setDescription(description) {
	var el = document.getElementById('photodescription');
	while(el.firstChild)
		el.removeChild(el.firstChild);
	el.appendChild(document.createTextNode(description));
}

function init() {
	var photoid = getQueryVariable('photo');
	switchPage(0);
	if(photoid==false)
		var photoid = 0;
	else
		var photoid = parseInt(photoid) - 1;
	curimage = photoid;
	imageLoading();
	getPhoto(curimage, true);
	setNav();
	var last = document.getElementById('total').firstChild.data;
	total = parseInt(last);
	if(curimage!=0)
		getPhoto(curimage - 1, false);
	if(curimage!=(total-1))
		getPhoto(curimage+1, false);
}

function setCurrent() {
	var current = document.getElementById('current');
	current.firstChild.data = (curimage + 1);
}

function setNav() {
	var left = document.getElementById('photonavleft');
	var right = document.getElementById('photonavright');
	if(curimage==0)
		left.setAttribute('class', 'unavailable');
	else
		left.removeAttribute('class');
	if(curimage==(total-1))
		right.setAttribute('class', 'unavailable');
	else
		right.removeAttribute('class');
	if(images[curimage]) {
		var h = images[curimage].height;
		left.height = h;
		right.height = h;
		left.paddingTop = (h/2) + 'px';
		right.paddingTop = (h/2) + 'px';
	}
}

function goLeft() {
	if(curimage==0)
		return;
	curimage--;
	setPhoto(curimage);
	setNav();
	if(curimage!=0)
		getPhoto(curimage - 1, false);
}

function goRight() {
	if(curimage==(total-1))
		return;
	curimage++;
	setPhoto(curimage);
	setNav();
	if(curimage!=(total-1))
		getPhoto(curimage + 1, false);
}

function selectPhoto(id) {
	setTitle('Indeks', true);
	curimage = id;
	if(!data[curimage])
		getPhoto(curimage, true, null);
	setPhoto(curimage, function() {switchPage(0);});
	if(curimage!=0)
		getPhoto(curimage - 1, false);
	if(curimage!=(total-1))
		getPhoto(curimage + 1, false);
}

function putAboutText(response) {
	var xmldoc = response.documentElement;
	var info = xmldoc.childNodes;
	var el = document.getElementById('aboutcontainer');
	for(var i = 0; i < info.length; i++) {
		if(!info[i].nodeName || info[i].nodeName!='description')
			continue;
		var description = info[i].firstChild.data;
		break;
	}
	el.appendChild(document.createTextNode(description));
}

function addPhotoList(response) {
	var xmldoc = response.documentElement;
	var photos = xmldoc.childNodes;
	var parent = document.getElementById('listcontainer');
	for(var i = 0; i < photos.length; i++) {
		if(!photos[i].nodeName || photos[i].nodeName!='photo')
			continue;
		var photodata = photos[i].childNodes;
		for(var j = 0; j < photodata.length; j++) {
			if(!photodata[j].nodeName)
				continue;
			var tagname = photodata[j].nodeName.toLowerCase();
			if(tagname=='id')
				var pid = photodata[j].firstChild.data;
			if(tagname=='title')
				var ptitle = photodata[j].firstChild.data;
			if(tagname=='url')
				var purl = photodata[j].firstChild.data;
		}
		var a = document.createElement('a');
		a.setAttribute('href', 'javascript:selectPhoto('+pid+');');
		a.setAttribute('title', ptitle);
		var img = document.createElement('img');
		img.src = purl;
		a.appendChild(img);
		parent.appendChild(a);
	}
	listLoaded = true;
	listLoading(false);
}

function listLoading(isLoading) {
	if(isLoading) {
		setTitle('Loading...', true);
	} else {
		setTitle("Indeks", false);
	}
}

function getPhotoList() {
	var url = 'getPhotoList.php';
	var request = getHTTPObject();
	request.open('GET', url, true);
	request.onreadystatechange = function() {
		if(request.readyState == 4 && request.status == 200) {
			addPhotoList(request.responseXML);
		}// else if(request.status != 200) {
			//	alert('omg!');
			//}
	}
	request.send(null);
}



function getAboutPage() {
	var url = 'getInfo.php';
	var request = getHTTPObject();
	request.open('GET', url, true);
	request.onreadystatechange = function() {
		if(request.readyState == 4 && request.status == 200) {
			putAboutText(request.responseXML);
		}
	}
	request.send(null);
}

function switchPage(toPage) {
	if(toPage==1) {
		document.getElementById('photocontainer').style.display = 'none';
		document.getElementById('listcontainer').style.display = 'block';
		document.getElementById('nav').style.display = 'none';
		document.getElementById('title-hold').style.display = 'none';
		document.getElementById('description-hold').style.display = 'none';
		document.getElementById('aboutcontainer').style.display = 'none';
		page = 1;
		highlightLink(1);
		listLoading(true);
		if(!listLoaded)
			getPhotoList();
		else
			listLoading(false);
	} else if(toPage==0) {
		document.getElementById('photocontainer').style.display = 'block';
		document.getElementById('listcontainer').style.display = 'none';
		document.getElementById('nav').style.display = 'block';
		document.getElementById('title-hold').style.display = 'block';
		document.getElementById('description-hold').style.display = 'block';
		document.getElementById('aboutcontainer').style.display = 'none';
		highlightLink(0);
		page = 0;
	} else {
		document.getElementById('photocontainer').style.display = 'none';
		document.getElementById('listcontainer').style.display = 'none';
		document.getElementById('nav').style.display = 'none';
		document.getElementById('title-hold').style.display = 'none';
		document.getElementById('description-hold').style.display = 'none';
		document.getElementById('aboutcontainer').style.display = 'block';
		highlightLink(2);
		page = 2;
		if(!aboutLoaded)
			getAboutPage();
	}
}

function highlightLink(page) {
	var lp = document.getElementById("link-photos");
	var li = document.getElementById("link-index");
	var la = document.getElementById("link-about");
	lp.removeAttribute("class");
	li.removeAttribute("class");
	la.removeAttribute("class");
	switch(page) {
		case 0:
			lp.setAttribute("class", "selected");
			break;
		case 1:
			li.setAttribute("class", "selected");
			break;
		case 2:
			la.setAttribute("class", "selected");
			break;
	}
}
