/* The main purpose of this javascript is to send the coordinates of the mouseclick to the server so it knows where to merge the scaled image of the art.  Additionally, when the 
server is done, it refreshes the image so (for Firefox anyway) the process is seamless.  Other browsers definitely have trouble due to image caching and other issues, unfortunately. */

var searchnode;
var resultsnode;
var request;
var x;
var y;

function ajaxCall() {
	// From "Head Rush Ajax" by Brett McLaughlin
	try {
		request = new XMLHttpRequest();
	} catch (trymicrosoft) {
		try {
			request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (othermicrosoft) {
			try {
				request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (failed) {
				request = null;
			}
		}
	}
	
	if (request == null) {
		alert("Error creating request object!");
	}
}

function process(event) {
	//Actual AJAX call
	//Get coordinates of mouseclick and adjust due to location of image; send coordinates to server to merge at that location
	x= event.pageX - this.offsetLeft;
	y= event.pageY - this.offsetTop;
	var temp= new Array();
	var string= location.href;
	temp= string.split('?');
	var aid= temp[1];
	var z= "idpreview_server.php?"+aid+"&x="+x+"&y="+y;	
	
	//Send Data to Server via a GET request
	request.open("GET",z,true);
	request.onreadystatechange = callback;
	request.send(null);
}

function initialize() {
	searchnode = document.getElementById("room");
	resultsnode = document.getElementById("ajaxresult");
	searchnode.onclick= process;
	request = null;
	ajaxCall();
}

function callback(){
	
	//Check if server is still processing the call...
	if(request.readyState<4){
		return;
	}
	
	//Check if call is complete....
	if(request.readyState==4){
		document.getElementById('room').src = document.getElementById('room').src + '#';
	}
}