var ajax;
var disp;
var disp_img;
var disp_info;
var disp_name;
var disp_author, disp_a;
var curr, last, first;

function init() {
	initAjax();
	disp=document.getElementById("disp");
	disp_a=document.getElementById("disp_a");
	disp_name=document.getElementById("disp_name");
	disp_author=document.getElementById("disp_author");
	disp_img=document.getElementById("disp_img");
	disp_info=document.getElementById("disp_info");

	if (!document.importNode) {
	document.importNode = function(node, allChildren) {
		switch (node.nodeType) {
		case 1:
			var newNode = document.createElement(node.nodeName);
			/*if (node.attributes && node.attributes.length > 0)
				for (var i = 0; il = node.attributes.length; i < il)
					newNode.setAttribute(node.attributes[i].nodeName, node.getAttribute(node.attributes[i++].nodeName));*/
			//alert("Attr done");
			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;
		}
	};
	}

	if(!disp_name.firstChild)
		disp_name.appendChild(document.createTextNode(' '));

	if(!disp_author.firstChild)
		disp_author.appendChild(document.createTextNode(' '));

}

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;
	while(disp_info.hasChildNodes()) disp_info.removeChild(disp_info.firstChild);
	elemXML=xml.getElementsByTagName("desc")[0];
	if(document.importNode) children=document.importNode(elemXML, true).childNodes;
	else children=elemXML.childNodes;
	for(i=0; i<children.length; i++) {
		disp_info.appendChild(children[i]);
	}
	disp_img.src= 'images/upload/' + xml.getElementsByTagName("src")[0].firstChild.nodeValue;
	disp_a.href= 'images/upload/' + xml.getElementsByTagName("src")[0].firstChild.nodeValue;
	
	disp_name.firstChild.nodeValue=xml.getElementsByTagName("name")[0].firstChild.nodeValue;
	disp_author.firstChild.nodeValue=xml.getElementsByTagName("author")[0].firstChild.nodeValue;
	if(xml.getElementsByTagName("last").length) {
		document.getElementById("disp_next").src="images/next_i.png";
		last=true;
	}
	if(xml.getElementsByTagName("first").length) {
		document.getElementById("disp_prev").src="images/prev_i.png";
		first=true;
	}
	disp.style.display="block";
}

function previewImage(el) {
	curr=el;
	last=false;
	first=false
	document.getElementById("disp_next").src="images/next.png";
	document.getElementById("disp_prev").src="images/prev.png";
	ajax.onreadystatechange = response;
	ajax.open('GET', sciezka + 'ask.php?name=' + el, true);
	ajax.send(null);
}
function prev() {
	if(!first) previewImage(curr-1);
}

function next() {
	if(!last) previewImage(curr+1);
}

function closeDisp() {
	disp.style.display="none";
}
