/*Use Object Detection to detect IE6*/
    var  m = document.uniqueID /*IE*/
    && document.compatMode  /*>=IE6*/
    && !window.XMLHttpRequest /*<=IE6*/
    && document.execCommand;
    
    try{
        if(!!m){
            m("BackgroundImageCache", false, true) /* = IE6 only */
        }
        
    }catch(oh){};
    
    var IE = document.all?true:false
    //load xml file
	// code for IE
	if (window.ActiveXObject)
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async=false;
		xmlDoc.load("/c/config/Glossary.xml");
		//getmessage();
	}
	// code for Mozilla, Firefox, Opera, etc.
	else if (document.implementation && document.implementation.createDocument)
	{
		xmlDoc=document.implementation.createDocument("","",null);
		xmlDoc.load("/c/config/Glossary.xml");
		//xmlDoc.onload=getmessage;
	}
	else
	{
		alert('Your browser cannot handle this script');
	}

	
	var the_popupname;	// used for "click away hide" functionality
	
	
	onload = function(){
	glospopup();
	}
	function glospopup(){
		//alert(xmlDoc.getElementsByTagName("name")[0].childNodes[0].nodeValue)
		
		var links = document.getElementsByTagName('a');
		var popuplinks = new Array();
		var e = 0;
			
		for (var i=0; i<links.length; i++){
		if(links[i].name=="popup"){
			//alert(links[i].innerHTML);
			popuplinks[e] = links[i];
			e++;						
			}
		}
		//popuplinks.length NUMBER OF glossary LINKS ON THE PAGE (2)
		for (var j=0; j<popuplinks.length; j++){ 
				
			//xmlDoc.getElementsByTagName("name").length
			//NUMBER OF "<name>" TAGS IN THE XML (3)
			for (var jj=0; jj<xmlDoc.getElementsByTagName("name").length; jj++){	
							
				//CHECK BROWSER TO SEE IF ITS IE (.text) OR FIREFOX (.textContent) good 'ol browser bs
				if(xmlDoc.getElementsByTagName("glossary")[0].text == undefined){
					
					//IF THE "<link>" TAG'S CONTENT EQUALS THE GLOSSARY ANCHOR TAG'S LINK TEXT
					//might want to add a field to the xml so this isnt dependant on the name tag
					
					if(xmlDoc.getElementsByTagName("link")[jj].textContent.trim() == popuplinks[j].innerHTML){
						
						//THIS IS FOR FireFox
											
						var divIdTitle = popuplinks[j].id + popuplinks[j].name + "title";
						var divIdText = popuplinks[j].id + popuplinks[j].name + "text";
						var divIdName = popuplinks[j].id + popuplinks[j].name;
						var divIdNameTop = popuplinks[j].id + popuplinks[j].name + "top";
						var divIdNameMid = popuplinks[j].id + popuplinks[j].name + "mid";
						var divIdClose = popuplinks[j].id + popuplinks[j].name + "close";
												
						var newdiv = document.createElement('div');
						var newdivTop = document.createElement('div');
						var newdivMid = document.createElement('div');
						var newdivBtm = document.createElement('div');
						var newdivTitle = document.createElement('div');
						var newdivText = document.createElement('div');
						var newdivClose = document.createElement('div');					
												
						newdiv.setAttribute('id', divIdName);
						newdivTop.setAttribute('id',divIdNameTop);
						newdivMid.setAttribute('id',divIdNameMid);
						newdivTitle.setAttribute('id', divIdTitle);
						newdivText.setAttribute('id', divIdText);
						newdivClose.setAttribute('id', divIdClose);
						
						newdiv.setAttribute('class',"glossary_popup");
						newdivTop.setAttribute('class',"glossary_popup_top");
						newdivMid.setAttribute('class',"glossary_popup_mid");
						newdivBtm.setAttribute('class',"glossary_popup_btm");
						newdivTitle.setAttribute('class',"glossary_popup_title");
						newdivText.setAttribute('class',"glossary_popup_text");
						newdivClose.setAttribute('class',"glossary_popup_close");
						newdivClose.setAttribute('onclick',"closePopup('"+divIdName+"')");			
						
						newdivTitle.innerHTML = xmlDoc.getElementsByTagName("name")[jj].textContent
						newdivText.innerHTML = xmlDoc.getElementsByTagName("description")[jj].textContent
													
						document.getElementById("main").appendChild(newdiv);
						
						document.getElementById(divIdName).appendChild(newdivTop);
						document.getElementById(divIdName).appendChild(newdivMid);
						document.getElementById(divIdName).appendChild(newdivBtm);
						document.getElementById(divIdNameTop).appendChild(newdivClose);						
						document.getElementById(divIdNameTop).appendChild(newdivTitle);
						document.getElementById(divIdNameMid).appendChild(newdivText);
					}
				}else{
					if(xmlDoc.getElementsByTagName("glossary")[0].childNodes[jj].childNodes[2].text == popuplinks[j].innerHTML){
						//THIS IS FOR IE
												
						var divIdTitle = popuplinks[j].id + popuplinks[j].name + "title";
						var divIdText = popuplinks[j].id + popuplinks[j].name + "text";
						var divIdName = popuplinks[j].id + popuplinks[j].name;
						var divIdNameTop = popuplinks[j].id + popuplinks[j].name + "top";
						var divIdNameMid = popuplinks[j].id + popuplinks[j].name + "mid";
						var divIdClose = popuplinks[j].id + popuplinks[j].name + "close";
												
						var newdiv = document.createElement('div');
						var newdivTop = document.createElement('div');
						var newdivMid = document.createElement('div');
						var newdivBtm = document.createElement('div');
						var newdivTitle = document.createElement('div');
						var newdivText = document.createElement('div');
						var newdivClose = document.createElement('div');					
												
						newdiv.setAttribute('id', divIdName);
						newdivTop.setAttribute('id',divIdNameTop);
						newdivMid.setAttribute('id',divIdNameMid);
						newdivTitle.setAttribute('id', divIdTitle);
						newdivText.setAttribute('id', divIdText);
						newdivClose.setAttribute('id', divIdClose);
						
						newdiv.setAttribute('className',"glossary_popup");
						newdivTop.setAttribute('className',"glossary_popup_top");
						newdivMid.setAttribute('className',"glossary_popup_mid");
						newdivBtm.setAttribute('className',"glossary_popup_btm");
						newdivTitle.setAttribute('className',"glossary_popup_title");
						newdivText.setAttribute('className',"glossary_popup_text");
						newdivClose.setAttribute('className',"glossary_popup_close");	
						newdivClose.onclick = new Function( "return closePopup('"+divIdName+"');" );		
																								
						newdivTitle.innerHTML = xmlDoc.getElementsByTagName("glossary")[0].childNodes[jj].childNodes[0].text
						newdivText.innerHTML = xmlDoc.getElementsByTagName("glossary")[0].childNodes[jj].childNodes[1].text
												
						document.getElementById("main").appendChild(newdiv);
						document.getElementById(divIdName).appendChild(newdivClose);
						document.getElementById(divIdName).appendChild(newdivTop);						
						document.getElementById(divIdName).appendChild(newdivMid);
						document.getElementById(divIdName).appendChild(newdivBtm);
												
						document.getElementById(divIdNameTop).appendChild(newdivTitle);
						document.getElementById(divIdNameMid).appendChild(newdivText);
						
					}
				}// IE / FF IF 
			}// for jj
		}//for j
	}
	function closePopup(popupname)
	{
		document.getElementById(popupname).style.display="none";
		removeMouseDownEvent();
	}
	function showglos(popupname, e)
	{
		if(the_popupname)
			closePopup(the_popupname);		// If there is already a glossary item open, close it before opening the new one. 
		
		the_popupname = popupname;
		
		var xPos;
		var yPos;
		var popupheight;
		if(IE)
		{
			xPos = window.event.x;
			yPos = window.event.y;
			yPos = yPos + document.documentElement.scrollTop	
		}
		else
		{
			xPos = e.pageX;
			yPos = e.pageY;
		}	
		
		
		//alert(xPos - 200);
		document.getElementById(popupname).style.display="block";
		popupheight = document.getElementById(popupname).offsetHeight;
		
		yPos = (yPos - (popupheight-10));
		if(yPos < 0)
		{
			yPos=0;
		}
		document.getElementById(popupname).style.top = yPos+"px";
		document.getElementById(popupname).style.left = (xPos-60)+"px";
		
		addMouseDownEvent(popupname);
		
	}
	
	String.prototype.trim=function()
	{
		return this.replace(/^\s*|\s*$/g,'');
	}
	
	
	
	
	// For "click-away hide" functionality 
	
	function addMouseDownEvent(pname)
	{		
		var popupname = pname;
		
		if( document.captureEvents && Event.CLICK )
			document.captureEvents(Event.CLICK);
			
        document.onclick = clickHandler;
	}
	
	function removeMouseDownEvent()
	{
		if(document.captureEvents && Event.CLICK)
			document.releaseEvents(Event.CLICK);
	}
	
	function clickHandler(e)
	{
		var newEvent = e;
		
		if(newEvent)
			element = e.target;
		else
			element = window.event.srcElement;
			
		var arrayOfStrings = the_popupname.split('popup');
		var linkId = arrayOfStrings[0];
		var linkIdimg = linkId + 'img'
		
		if(element.id != linkId && element.id != linkIdimg)
		{
			var parentId = element.parentNode.id;
			var superParentId = element.parentNode.parentNode.id;
			
			if(!(parentId == the_popupname || superParentId == the_popupname))
				closePopup(the_popupname);
		}
	}
			

	
	
	
