// AJAX Hello World Demo http://www.hackorama.com/ajax

// Make a POST to the server 
// and pass on any data from browser
// via the XMLHTTPRequest

var reqid = 0;
function getImage(i_gal_id, i_id)
{
	var ts = Math.random();
	reqid = ts;

	var req = newXMLHttpRequest();
	//register the callback handler function
	var callbackHandler = getReadyStateHandler(req, updateMsgOnBrowser);
	req.onreadystatechange = callbackHandler;
	req.open("POST", "/php/jsgallery.php", true);
	req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	//get the value from the text input element and send it to server
	var msg = "ts="+ts+"&gal="+i_gal_id;
	if(i_id) msg += "&id="+i_id;
	req.send(msg);
}

// This is the callback functions that gets called
// for the response from the server with the XML data

function getValue(s_tag, o_obj)
{
	var element = o_obj.getElementsByTagName(s_tag);
	//alert(element[0].firstChild.nodeValue);
	if(!element[0] || !element[0].firstChild) return '';

	var s_retval = element[0].firstChild.nodeValue;
	
	return (s_retval) ? s_retval : '';
}

var lastPing = 0;
function updateMsgOnBrowser(resultXML) 
{
	processing = false;
	var o_galimg = resultXML.getElementsByTagName("galimg")[0];
	if(!o_galimg);
	//var o_current = o_galimg.getElementsByTagName("current")[0];
	//var o_previous = o_galimg.getElementsByTagName("previous")[0];
	//var o_next = o_galimg.getElementsByTagName("next")[0];


	var timestamp = o_galimg.getAttribute("timestamp");
	if (timestamp != reqid)  return;
	
	lastPing = timestamp;

	var o_link = new GalleryImage();
	o_link.i_gal_id 	= o_galimg.getAttribute("gal");
	o_link.i_size 		= o_galimg.getAttribute("size");
	o_link.i_pos 			= o_galimg.getAttribute("pos");
	o_link.i_id 			= o_galimg.getAttribute("id");
	o_link.s_name 		= getValue('name', o_galimg);
	o_link.s_image 		= getValue('file', o_galimg);
	o_link.i_previous = getValue('previous', o_galimg);
	o_link.i_next 		= getValue('next', o_galimg);
	// test
	o_link.s_image_next = o_link.s_image;
	o_link.s_image_previous = o_link.s_image;

	// maybe not
	if(gallery_id != o_link.i_gal_id) 
	{
		o_image.clear();
		slide(0);
	}
	else
	{
		myLightbox.changeImage(o_link);
	}
}


//the following two functions are helper infrastructure to 
//craete a XMLHTTPRequest and register a listner callback function

function newXMLHttpRequest() 
{
	var xmlreq = false;
	if (window.XMLHttpRequest) 
	{
		xmlreq = new XMLHttpRequest();
	} 
	else if (window.ActiveXObject) 
	{
		// Try ActiveX
		try 
		{ 
			xmlreq = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e1) 
		{ 
			// first method failed 
			try 
			{
				xmlreq = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (e2) 
			{
				 // both methods failed 
			} 
		}
	}
	return xmlreq;
} 

function getReadyStateHandler(req, responseXmlHandler) 
{
	return function () 
	{
		if (req.readyState == 4) 
		{
			if (req.status == 200) 
			{
				responseXmlHandler(req.responseXML);
			} 
			else 
			{
				alert(req.status);
				//var hellomsg = document.getElementById("hellomsg");
				//hellomsg.innerHTML = "ERROR: "+ req.status;
			}
		}
	}
}

GalleryImage.prototype.getGallery = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getSize = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getPos = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getId = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getName = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getImage = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getPrevious = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.getNext = function () {if(this.i_gal_id) return this.i_gal_id; };
GalleryImage.prototype.clear = function () {
	this.i_gal_id = '';
	this.i_size = '';
	this.i_pos = '';
	this.i_id = '';
	this.s_name = '';
	this.s_image = '';
	this.i_previous = '';
	this.i_next = '';
};

function GalleryImage(){}
