var ajax, previd, nextid;

function init() {
	initAjax();
	if (!document.importNode) {
	document.importNode = function(node, allChildren) {
		switch (node.nodeType) {
		case 1:
			var newNode = document.createElement(node.nodeName);
			if (allChildren && node.childNodes && node.childNodes.length > 0) {
				il=node.childNodes.length;
				for (var i = 0; i < il; i++)
					newNode.appendChild(document.importNode(node.childNodes[i], allChildren));
				}
			return newNode;
			break;
		default:
			return document.createTextNode(node.nodeValue);
			break;
		}
	};
	}

	nextid=(document.getElementById("next_image")!=null)?document.getElementById("next_image").alt:-1;
	previd=(document.getElementById("prev_image")!=null)?document.getElementById("prev_image").alt:-1;
}

function initAjax() {
	try {
		if (window.XMLHttpRequest) {
			ajax = new XMLHttpRequest();
			ajax.overrideMimeType('text/xml');
		}
		else if (window.ActiveXObject)
			ajax = new ActiveXObject('Microsoft.XMLHTTP');
		else throw 'AJAX Error';
	}
	catch (e) {
		return false;
	}
	if (!ajax) {
		alert('AJAX Error');
		return false;
	}
	return true;
}

 
function response() {
	if (ajax.readyState != 4 || ajax.status != 200)
		return;

	var xml = ajax.responseXML;
	document.getElementById("curr_image").parentNode.href=document.getElementById("curr_image").src='images/upload/'+xml.getElementsByTagName("current")[0].getAttribute("src");
	document.title = 'Podgląd zdjęcia nr ' + xml.getElementsByTagName("current")[0].getAttribute("id");
	document.getElementById("author").firstChild.nodeValue=xml.getElementsByTagName("author")[0].firstChild.nodeValue;

	desc=document.getElementById("description");
	while(desc.hasChildNodes()) desc.removeChild(desc.firstChild);

	children=document.importNode(xml.getElementsByTagName("desc")[0], true).childNodes;
	for(i=0; i<children.length; i++) {
		desc.appendChild(children[i]);
	}

	document.getElementById("current").src=xml.getElementsByTagName("current")[0].getAttribute("src");

	if(xml.getElementsByTagName("next").length>0) {
		document.getElementById("next_image").src='images/upload/'+xml.getElementsByTagName("next")[0].getAttribute("thumb");
		nextid=xml.getElementsByTagName("next")[0].getAttribute("id");
		document.getElementById("next_image").alt=nextid;
		document.getElementById("next").style.display="block";
	}
	else {
		document.getElementById("next").style.display="none";
		nextid=-1;
	}

	if(xml.getElementsByTagName("prev").length>0) {
		document.getElementById("prev_image").src='images/upload/'+xml.getElementsByTagName("prev")[0].getAttribute("thumb");
		previd=xml.getElementsByTagName("prev")[0].getAttribute("id");
		document.getElementById("prev_image").alt=previd;
		document.getElementById("prev").style.display="block";
	}
	else {
		document.getElementById("prev").style.display="none";
		previd=-1;
	}
}

function previewImage(el) {
	if(el==-1) return 0;
	ajax.onreadystatechange = response;
	ajax.open('GET', path+ '/ask.php?id=' + el, true);
	ajax.send(null);
}

function prev() {
	previewImage(previd);
}

function next() {
	previewImage(nextid);
}
