/* The following function creates an XMLHttpRequest object... */

function createRequestObject(){
	var request_o; //declare the variable to hold the object.
	var browser = navigator.appName; //find the browser name
	if(browser == "Microsoft Internet Explorer"){
		/* Create the object using MSIE's method */
		request_o = new ActiveXObject("Microsoft.XMLHTTP");
	}else{
		/* Create the object using other browser's method */
		request_o = new XMLHttpRequest();
	}
	return request_o; //return the object
}

/* You can get more specific with version information by using 
	parseInt(navigator.appVersion)
	Which will extract an integer value containing the version 
	of the browser being used.
*/



/* The variable http will hold our new XMLHttpRequest object. */
var http = createRequestObject(); 

/* Function called to get the product categories list */
function run_imager(photo_dir){
	/* Create the request. The first argument to the open function is the method (POST/GET),
		and the second argument is the url... 
	*/
	http.open('get', '/product_imager/imager.php?photo_dir=' + photo_dir);
	/* Define a function to call once a response has been received. This will be our handleImager function that we define below. */
	http.onreadystatechange = handleImager; 
	/* Send the data. We use something other than null when we are sending using the POST method. */
	http.send(null);
}

/* Function called to handle the text returned from the imager.php file.. */
function handleImager(){
	/* Make sure that the transaction has finished. The XMLHttpRequest object 
		has a property called readyState with several states:
		0: Uninitialized
		1: Loading
		2: Loaded
		3: Interactive
		4: Finished */
	if(http.readyState == 4){ //Finished loading the response
		/* We have got the response from the server-side script,
			let's see just what it was. using the responseText property of 
			the XMLHttpRequest object. */

		var response = http.responseXML;
		var div_contents = response.getElementsByTagName('div_content')[0].childNodes[0].nodeValue;
		var div_width = response.getElementsByTagName('div_width')[0].childNodes[0].nodeValue;
		var div_height = response.getElementsByTagName('div_height')[0].childNodes[0].nodeValue;
		var preload_images = response.getElementsByTagName('images_to_preload');
		
		// now we need to pull out all the images to be preloaded then loop over them
		for (i=0;i<preload_images.length;i++) {
			var myImage = new Image;
			myImage.src = preload_images[i].childNodes[0].nodeValue;
		}
		
		// response is always in the form of x;y;text where x is the width of the div, y is the height
//		var response = http.responseText;		
//		var div_width = response.substr(0,response.indexOf(";"));
//		response = response.substr(response.indexOf(";")+1);
//		var div_height = response.substr(0,response.indexOf(";"));
//		div_contents = response.substr(response.indexOf(";")+1);
		
		
		// document.getElementById('productImages').style.height = div_height+"px";		// avoid setting so that it can grow to accomodate all thumbnails
		document.getElementById('productImages').style.width = div_width+"px";
		document.getElementById('productImages').innerHTML = div_contents;
	}
}




/*

The following will send info using a POST method... 
http://www.phpbuilder.com/columns/kassemi20050606.php3
http://www.phpbuilder.com/columns/kassemi20050613.php3


	http.abort;
	http.open('post',  'back_end.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send('arg1=val1&arg2=val2&arg3=val3');
	
	
	*/