//***** CONTAINER ***** 
// JS object to store data coming from server
function serverData(title, body, image){
	this.title = title;
	this.body = body;
	this.image = image;
}

function convertTrees() { return true;}
function expandToItem(v1,v2) {return true;}
//Create the server data object at the page level
var localServerData = new serverData();

//***** START AJAX SECTION *****	
/*Core AJAX function - Retrieve a connection for messaging between the client and server*/
function getHTTPObject() {
	var xmlhttp;
	//conditional compilation for ie
	/*@cc_on
	@if (@_jscript_version >= 5)
		try {
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (E) {
				xmlhttp = false;
			}
		}
	@else
		xmlhttp = false;
	@end @*/

	//logic for browsers other than ie; 
  	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
    	try {
      		xmlhttp = new XMLHttpRequest();
    	} catch (e) {
      		xmlhttp = false;
    	}
  	}
  	return xmlhttp;
}//end getHTTPObject function

/*Call specific file on the server and retrieve the specified data from the XML file*/
function processServerCommunication(file){
 	
	//create the XMLHTTPRequest object for this event
	var localHTTPObject = getHTTPObject(); 
	
	//scripting version of programatical recursion
	localHTTPObject.onreadystatechange = function(){
		
		//spg		
		//alert(file);
		
		//indicates that file on server is completed processing
		if(localHTTPObject.readyState == 4){
				
			try {
			
				//local variables to store incoming XML
				var lclTitle;
				var lclBody;
				var lclImage;
				var displayInfoLayer = true;
				
				//verify that all data is coming over as expected
				//if it's not parsed as expected, set to blank and tell it not to display
				// getting the first child data does not easily allow the DOM to be copied over if it has HTML bits in it.
				try {
					lclTitle = localHTTPObject.responseXML.getElementsByTagName('title')[0].firstChild;
					}
				catch (e) { lclTitle = ""; displayInfoLayer = false;}
				
				try { 
				lclBody = localHTTPObject.responseXML.getElementsByTagName('body')[0].firstChild; //firstChild.data; 
				} 
				catch (e) { lclBody = ""; displayInfoLayer = false;}
				
				try { lclImage = localHTTPObject.responseXML.getElementsByTagName('image')[0].firstChild.data;
				} 
				catch (e) { lclImage = "null"; }
							
				//should the infoLayer be displayed???
				if ( displayInfoLayer ) {
				
					//store the data in a local object 
					var serverDataObject = new serverData(lclTitle, lclBody, lclImage);
				
					//store the data in the page level object
					localServerData = serverDataObject;
				 	
					//place the data in the layer's attributes
					fillInfoLayer();
					
					//display the popup
					showInfoLayer();
				}
								  
			} catch (e) {
				//TODO what to do if exception occurs?
				alert('JavaScript Error: ' + e);
		    }
    	}
	}
	
	//make the call using a GET & close connection
	localHTTPObject.open ('GET', file, true);
	localHTTPObject.send (null);
}
//***** END AJAX SECTION *****

//***** START MOUSE POSITIONING SECTION *****

// Store the value of the mouse coordinates at the page level
var mouseX, mouseY;

// Gets the current mouse position
function getMousePos(e)
{
	if (!e) var e = window.event||window.Event;

	if('undefined'!=typeof e.pageX)
	{	
		//Mozilla
		mouseX = e.pageX;
		mouseY = e.pageY;
	}
	else
	{
		mouseX = e.clientX + document.body.scrollLeft;
		mouseY = e.clientY + document.body.scrollTop;
	}
}

// Tell Mozilla browsers to start listening:
if(window.Event && document.captureEvents)document.captureEvents(Event.MOUSEMOVE);

// Then assign the valuses to the mouse handler
document.onmousemove = getMousePos;

//***** END MOUSE POSITIONING SECTION *****

//***** START DHTML Display SECTION *****

//DHTML layer functions
function showInfoLayer(){
 
 	with( document.getElementById('infoLayer').style ) {
		position = "absolute";
		left = (mouseX + 70) + 'px';
		top = (mouseY - 30) + 'px';
		visibility = "visible";
	}
	 
	with( document.getElementById('ear').style ) {
		position = "absolute";
		left = (mouseX + 29) + 'px';
		top = (mouseY + 10) + 'px';
		visibility = "visible";
	}
}

function hideInfoLayer(){
	document.getElementById('infoLayer').style.visibility = "hidden";	
	document.getElementById('ear').style.visibility = "hidden";	
}

function fillInfoLayer(){
	//insert content from server side
//	document.getElementById('ajaxTitle').innerHTML = localServerData.title.innerHTML;
	if (document.getElementById('ajaxTitle').firstChild == null) {
		document.getElementById('ajaxTitle').appendChild(localServerData.title)
	} else {
		document.getElementById('ajaxTitle').replaceChild(localServerData.title, document.getElementById('ajaxTitle').firstChild );
	}
	//is there a thumbnail image?
	
	
	if (localServerData.image != 'null'){
		document.getElementById('ajaxBody').innerHTML = '<center><img src="/ml/assets/ajax/' + localServerData.image + '" vspace="3"></center>' + localServerData.body;
	} else { 
		if (document.getElementById('ajaxBody').firstChild == null) {
			document.getElementById('ajaxBody').appendChild(localServerData.body)
		} else {
			document.getElementById('ajaxBody').replaceChild(localServerData.body, document.getElementById('ajaxBody').firstChild );
		}
	}
		//document.getElementById('ajaxBody').innerHTML = localServerData.body;	
	
}

//***** END DHTML Display SECTION *****

//***** IE UTILITY FUNCTION *****
// PNG Transparency fix for Windows IE 5.5 & higher.
function correctPNG() {
	for(var i=0; i<document.images.length; i++){
  		var img = document.images[i]
  		var imgName = img.src.toUpperCase()
  		if (imgName.substring(imgName.length-3, imgName.length) == "PNG"){
			var imgID = (img.id) ? "id='" + img.id + "' " : ""
		 	var imgClass = (img.className) ? "class='" + img.className + "' " : ""
		 	var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
		 	var imgStyle = "display:inline-block;" + img.style.cssText 
		 	if (img.align == "left") imgStyle = "float:left;" + imgStyle
		 	if (img.align == "right") imgStyle = "float:right;" + imgStyle
		 	if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle		
		 	var strNewHTML = "<span " + imgID + imgClass + imgTitle
		 	+ " style=\"" + "width:100%; height:100%;" + imgStyle + ";"
	     	+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
		 	+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
		 	img.outerHTML = strNewHTML
		 	i = i-1
		}
	}
}

//***** PROCESSOR *****
function getData(inid){
	//get the data from the server\
	processServerCommunication('/face/query.jsp?id=' + inid);
}

