var popup_ob

function popup_class(){
	var _me=this
	this.xml_loader_arr=new Array()
	this.xml_loaded_arr=new Array()
	this.xml_doc_arr=new Array()

	this.definition_xml_ob=0
	this.active_panel_str=""
	this.active_panel_id=""
	this.click_item=0
	
	this.title_str=""
	this.content_str=""
	
	this.definition_node=0
	
	this.coords_arr=new Array()
	
	this.setup_interface=function(){

		_me.load_xml("xml/popup_definition.xml","definition","")
	}
	
	this.load_xml=function(thispath,thisname,params){
		
		_me.xml_loaded_arr[thisname]=0
		xmlpth=pthstr+thispath
		//window.open(xmlpth+"?"+params)
		/*@cc_on @*/
		/*@if (@_jscript_version >= 5)
			// JScript gives us Conditional compilation, we can cope with old IE versions.
			// and security blocked creation of the objects.
	
			try {
				_me.xml_loader_arr[thisname] = new ActiveXObject("Msxml2.XMLHTTP");
			} catch (e) {
				try {
					_me.xml_loader_arr[thisname]= new ActiveXObject("Microsoft.XMLHTTP");
				} catch (E) {
					_me.xml_loader_arr[thisname] = false;
				}
			}
	
		/*@end @*/
		
		if (!_me.xml_loader_arr[thisname] && typeof XMLHttpRequest!='undefined') {
			_me.xml_loader_arr[thisname] = new XMLHttpRequest();
		}
		
		if(params==""){
			_me.xml_loader_arr[thisname].open("GET", xmlpth,true);
		}else{
			_me.xml_loader_arr[thisname].open("POST", xmlpth,true);
			_me.xml_loader_arr[thisname].setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		}
	
		

		switch(thisname){
			case "definition":
				_me.xml_loader_arr[thisname].onreadystatechange=_me.parse_definition_xml
				break;
			case "data":
				_me.xml_loader_arr[thisname].onreadystatechange=_me.parse_data_xml
				break;
		}
		
		
		if(params==""){
			_me.xml_loader_arr[thisname].send(null)
		}else{
			_me.xml_loader_arr[thisname].send(params)
		}
	
	
	}

		
	this.parse_definition_xml=function(){
		var obname="definition"
		
		if(_me.xml_loader_arr[obname].readyState==4){
			
			if(browser.isIE){
				_me.xml_doc_arr[obname]=new ActiveXObject('Microsoft.XMLDOM')
				_me.xml_doc_arr[obname].loadXML(_me.xml_loader_arr[obname].responseText)
			}else{
				_me.xml_doc_arr[obname]=new DOMParser().parseFromString(_me.xml_loader_arr[obname].responseText, 'text/xml')
			}
			test_nodes=_me.xml_doc_arr[obname].selectNodes("//popup")
			
		
			
			if(test_nodes.length>0){
			
				_me.xml_loaded_arr[obname]=1
				_me.definition_xml_ob=_me.xml_doc_arr[obname]
			}
	
	
		}
	}
	
	this.parse_data_xml=function(){
		var obname="data"
		
		if(_me.xml_loader_arr[obname].readyState==4){
			
			if(browser.isIE){
				_me.xml_doc_arr[obname]=new ActiveXObject('Microsoft.XMLDOM')
				_me.xml_doc_arr[obname].loadXML(_me.xml_loader_arr[obname].responseText)
			}else{
				_me.xml_doc_arr[obname]=new DOMParser().parseFromString(_me.xml_loader_arr[obname].responseText, 'text/xml')
			}
			test_nodes=_me.xml_doc_arr[obname].selectNodes("//popup_data")
			
		
			
			if(test_nodes.length>0){
				var title_nodes=_me.xml_doc_arr[obname].selectNodes("//popup_title")	
				if(title_nodes.length>0){
					_me.title_str=title_nodes[0].firstChild.nodeValue
				}
				var content_nodes=_me.xml_doc_arr[obname].selectNodes("//popup_content")
				if(content_nodes.length>0){			
					_me.content_str=content_nodes[0].firstChild.nodeValue
				}
				_me.draw()
			}
	
	
		}
	}

	this.draw=function(){
		if(_me.active_panel_id!=""){
			var ob=document.getElementById(_me.active_panel_id)
			switch(_me.active_panel_id){
				case "info_panel_float":
					var title_holder=ob.getElementsByTagName("h1")[0]
					var content_holder=ob.getElementsByTagName("p")[0]
					
					title_holder.innerHTML=_me.title_str
					content_holder.innerHTML=_me.content_str
					
				break;
				case "feedback_panel_float":
					feedback_form_ob.reset_form()
				break;
			}
			
			//GET LOCATION OF THE ELEMENT THAT'S BEEN CLICKED ON 
			var click_x=findPosX(_me.click_item)
			var click_y=findPosY(_me.click_item)
			
			//GET DIMENSIONS OF ELEMENT THAT'S BEEN CLICKED ON
			var click_w=_me.click_item.offsetWidth
			var click_h=_me.click_item.offsetHeight
			
			//GET DIMENSIONS OF FLOATING PANEL
			var panel_w=ob.offsetWidth
			var panel_h=ob.offsetHeight
		
			//GET PAGE PROPERTIES, DEPENDENT ON BROWSER
			//PAGE WIDTH AND HEIGHT
			if(browser.isIE){	
				var page_clientWidth=(window.document.documentElement.clientWidth==0)? window.document.body.clientWidth : window.document.documentElement.clientWidth
				var page_clientHeight=(window.document.documentElement.clientHeight==0)? window.document.body.clientHeight : window.document.documentElement.clientHeight
			}else{
				var page_clientWidth= window.document.body.clientWidth
				var page_clientHeight= window.document.body.clientHeight
			}
			//PAGE SCROLL LEFT AND SCROLLTOP
			if(browser.isIE){
				var page_scrollTop=(window.document.documentElement.scrollTop==0)? window.document.body.scrollTop : window.document.documentElement.scrollTop
				var page_scrollLeft=(window.document.documentElement.scrollLeft==0)? window.document.body.scrollLeft : window.document.documentElement.scrollLeft
			}else{
				var page_scrollTop= window.document.body.scrollTop
				var page_scrollLeft= window.document.body.scrollLeft
			}
			
			//GET BOTTOM AND RIGHT EDGES OF PAGE	
			var page_right=page_clientWidth+page_scrollLeft
			var page_bottom=page_clientHeight+page_scrollTop
				
			ob.style.position="absolute"
			ob.style.zIndex="15"
		
			switch(_me.definition_node.getAttribute("halign")){
				case "left_left":
					//LEFT OF PANEL ALIGNS TO LEFT OF CLICKED ITEM
					x=click_x
				break;
				case "left_right":
					//LEFT OF PANEL ALIGNS TO RIGHT OF CLICKED ITEM
					x=click_x+click_w
				break;
				case "right_left":
					//RIGHT OF PANEL ALIGNS TO LEFT OF CLICKED ITEM
					x=click_x-panel_w
				break;
				case "right_right":
					//RIGHT OF PANEL ALIGNS TO RIGHT OF CLICKED ITEM
					x=(click_x+click_w)-panel_w
				break;
			}
			
			switch(_me.definition_node.getAttribute("valign")){
				case "top_top":
					//TOP OF PANEL ALIGNS TO TOP OF CLICKED ITEM
					y=click_y
				break;
				case "top_bottom":
					//TOP OF PANEL ALIGNS TO BOTTOM OF CLICKED ITEM
					y=click_y+click_h
				break;
				case "bottom_top":
					//BOTTOM OF PANEL ALIGNS TO TOP OF CLICKED ITEM
					y=click_y-panel_h
				break;
				case "bottom_bottom":
					//BOTTOM OF PANEL ALIGNS TO BOTTOM OF CLICKED ITEM
					y=(click_y+click_h)-panel_h
				break;
			}
			
			//ADJUST A LITTLE BIT MORE
			x=x-2
			y=y+1
			
			//IF CLICKED ITEM IS ROUND THEN IT MIGHT BE GOOD TO ADJUST THE POSITIONING BY A COUPLE OF PIXELS, TO MOVE IT CLOSER TO THE EDGE OF THE CIRCLE
			//y=y-3
			//x=x+3
			
			ob.style.left=x+"px"
			ob.style.top=y+"px"
			ob.style.visibility="visible"
			
			//POTENTIAL RIGHT OVERLAP IS CALCULATED BY ADDING THE ADJUSTED X TO THE WIDTH OF THE POPUP AND SUBTRACTING THE PAGE_RIGHT FROM THE TOTAL
			right_overlap=(x+ob.offsetWidth)-page_right
			
			//POTENTIAL LEFT OVERLAP IS CALCULATED BY SUBTRACTING THE PAGE_SCROLLLEFT FROM THE ADJUSTED X 
			left_overlap=x-page_scrollLeft
			
			if(right_overlap>0){
				//IF THERE IS A RIGHT OVERLAP, LEFT OVERLAP CAN BE IGNORED
				left_overlap=0
				
				//THEN ADJUST THE X COORDINATE
				right_overlap+=10
				x=x-right_overlap
				ob.style.top=x+"px"
			}else{
				//IF THERE ISN'T A RIGHT OVERLAP IT CAN BE IGNORED	
				right_overlap=0
			
				if(left_overlap<0){
					//IF LEFT_OVERLAP IS NEGATIVE THE PANEL WILL NEED TO BE REPOSITIONED
					left_overlap=left_overlap-10
					x=x-left_overlap
					
					
				}
			}
			
			
			//POTENTIAL BOTTOM OVERLAP IS CALCULATED BY ADDING THE ADJUSTED Y TO THE HEIGHT OF THE POPUP AND SUBTRACTING THE PAGE_BOTTOM FROM THE TOTAL
			bottom_overlap=(y+ob.offsetHeight)-page_bottom
			
			//POTENTIAL TOP OVERLAP IS CALCULATED BY SUBTRACTING THE PAGE_SCROLLTOP FROM THE ADJUSTED Y 
			top_overlap=y-page_scrollTop

			if(bottom_overlap>0){
				//IF THERE IS A BOTTOM OVERLAP, TOP OVERLAP CAN BE IGNORED
				top_overlap=0
				
				//THEN ADJUST THE Y COORDINATE
				bottom_overlap+=10
				y=y-bottom_overlap
				ob.style.top=y+"px"
			}else{
				//IF THERE ISN'T A BOTTOM OVERLAP IT CAN BE IGNORED	
				bottom_overlap=0
				
				if(top_overlap<0){
					//IF TOP_OVERLAP IS NEGATIVE THE PANEL WILL NEED TO BE REPOSITIONED
					top_overlap=top_overlap-10
					y=y-top_overlap
					
					
				}
			}
			
			//THE 250 CHECK IS USED TO MAKE SURE THE PANEL DOESN'T OVERLAP THE TOP CRITERIA DROPDOWNS
			if(y<250){
			//	y=250	
			}
			ob.style.left=x+"px"
			ob.style.top=y+"px"
			
			_me.coords_arr=new Array()
			
			_me.coords_arr.push(x)
			_me.coords_arr.push(y)
			_me.coords_arr.push(x+ob.offsetWidth)
			_me.coords_arr.push(y+ob.offsetHeight)

		}
		
		
	}
	
	this.hide=function(){
		is_help_mousedown=0
		if(document.removeEventListener){
			document.removeEventListener('mouseup',check_close_help,false)
			window.removeEventListener('resize',check_close_help,false)
		}else{
			if(document.detachEvent) {
				document.detachEvent('onmouseup',check_close_help,false);
				window.detachEvent('onresize',check_close_help,false);
			}
		}
	
		if(_me.active_panel_id!=""){
			var ob=document.getElementById(_me.active_panel_id)
			ob.style.visibility="hidden"
			_me.active_panel_str=""
			_me.active_panel_id=""
			_me.title_str=""
			_me.content_str=""
			_me.definition_node=0
		}
	}
	
	this.show=function(event,panel_type,info_id){
		
		is_help_mousedown=0
		
		if(_me.active_panel_str==panel_type+"__"+info_id){
		
			_me.hide()
		
		}else{
			_me.hide()
			_me.active_panel_str=panel_type+"__"+info_id
			var def_nodes=_me.definition_xml_ob.selectNodes("//popup[@nickname='"+panel_type+"']")
			
			if(def_nodes.length>0){
				_me.definition_node=def_nodes[0]
				
				switch(_me.definition_node.getAttribute("data_source")){
					case "xml":
						//LOAD THE XML AND WRITE IT INTO THE GENERIC INFO PANEL
						_me.active_panel_id="info_panel_float"
					break;
					case "eval":
						//RETURN DATA BY EVALUATING FUNCTION AND WRITE IT INTO THE GENERIC INFO PANEL
						_me.active_panel_id="info_panel_float"

					break;
					case "panel":
						//DISPLAY NAMED PANEL, CLEARING FORM IF NECESSARY
						_me.active_panel_id=_me.definition_node.getAttribute("data")
					break;
				}
			
			
				var ob=document.getElementById(_me.active_panel_id)
				ob.style.zIndex=20
		
				//GET THE CLICKED ON ITEM
				if(typeof ob!="undefined" && !!ob){
					//GET ELEMENT THAT HAS BEEN CLICKED ON
					if (browser.isIE){	
						_me.click_item=(event.srcElement);    
					}
					if (browser.isNS){
						_me.click_item=(event.target);
					}
				}
			
			
				
				switch(_me.definition_node.getAttribute("data_source")){
					case "xml":
						//LOAD THE XML AND WRITE IT INTO THE GENERIC INFO PANEL
						_me.load_xml(_me.definition_node.getAttribute("data"),"data","data_ID="+info_id)
					break;
					case "eval":
						//THE EVAL SHOULD SET THE TITLE AND CONTENT OF THE POPUP OBJECT
						
						eval(_me.definition_node.getAttribute("data")+"('"+info_id+"')")
						_me.draw()
					break;
					case "panel":
						//DISPLAY NAMED PANEL, CLEARING FORM IF NECESSARY
						_me.draw()
					break;
				}
				
				setTimeout(_me.delay_close_help_setup,20)
			//	if(_me.definition_node.getAttribute("auto_close")=="1"){
					//if(override_is_help_mousedown==0){
//						if(document.addEventListener){
//							document.addEventListener('mouseup',check_close_help,false);
//							window.addEventListener('resize',check_close_help,false);
//						}else{
//							if(document.attachEvent) {
//								document.attachEvent('onmouseup',check_close_help,false);
//								window.attachEvent('onresize',check_close_help,false);
//							}
//						}
//					}else{
//						if(document.addEventListener){
//							document.addEventListener('mouseup',setup_check_close_help,false);
//						}else{
//							if(document.attachEvent) {
//								document.attachEvent('onmouseup',setup_check_close_help,false);
//							}
//						}
//					}
				//}
			}
			
		}
		override_is_help_mousedown=0
	}
	
	
	this.check_coords=function(x,y){
		if(x>_me.coords_arr[0] && y>_me.coords_arr[1] && x<_me.coords_arr[2] && y<_me.coords_arr[3]){
			return 1
			
		}else{
			return 0		
		}
	}
	
	this.delay_close_help_setup=function(){
		if(override_is_help_mousedown==0){
			if(document.addEventListener){
				document.addEventListener('mouseup',check_close_help,false);
				window.addEventListener('resize',check_close_help,false);
			}else{
				if(document.attachEvent) {
					document.attachEvent('onmouseup',check_close_help,false);
					window.attachEvent('onresize',check_close_help,false);
				}
			}
		}else{
			if(document.addEventListener){
				document.addEventListener('mouseup',setup_check_close_help,false);
			}else{
				if(document.attachEvent) {
					document.attachEvent('onmouseup',setup_check_close_help,false);
				}
			}
		}
	}
}

//HELP_MOUSEDOWN IS USED WHEN DETERMINING HOW A POPUP SHOULD BE REMOVED
var is_help_mousedown=0
var override_is_help_mousedown=0
function help_mousedown(){
	is_help_mousedown=1
}

function setup_check_close_help(){
	if(document.addEventListener){
		document.addEventListener('mouseup',check_close_help,false);
		window.addEventListener('resize',check_close_help,false);
	}else{
		if(document.attachEvent) {
			document.attachEvent('onmouseup',check_close_help,false);
			window.attachEvent('onresize',check_close_help,false);
		}
	}
}
function check_close_help(event){
	
	if(is_help_mousedown==0){
		
		var can_hide=1
		if(popup_ob.definition_node.getAttribute("auto_close")=="0"){
			if (browser.isIE){	
				var ev_item=(event.srcElement);    
			}
			if (browser.isNS){
				var ev_item=(event.target);
			}
			
			
			if(typeof ev_item!="undefined" && !!ev_item){
				
				coordX=event.pageX||event.clientX+document.body.scrollLeft,
				coordY=event.pageY||event.clientY+document.body.scrollTop;
				
				if(browser.isIE){	
					var page_clientWidth=(window.document.documentElement.clientWidth==0)? window.document.body.clientWidth : window.document.documentElement.clientWidth
					var page_clientHeight=(window.document.documentElement.clientHeight==0)? window.document.body.clientHeight : window.document.documentElement.clientHeight
				}else{
					var page_clientWidth= window.document.body.clientWidth
					var page_clientHeight= window.document.body.clientHeight
				}
				//PAGE SCROLL LEFT AND SCROLLTOP
				if(browser.isIE){
					var page_scrollTop=(window.document.documentElement.scrollTop==0)? window.document.body.scrollTop : window.document.documentElement.scrollTop
					var page_scrollLeft=(window.document.documentElement.scrollLeft==0)? window.document.body.scrollLeft : window.document.documentElement.scrollLeft
				}else{
					var page_scrollTop= window.document.body.scrollTop
					var page_scrollLeft= window.document.body.scrollLeft
				}
			
				coordX=event.pageX||event.clientX+page_scrollLeft,
				coordY=event.pageY||event.clientY+page_scrollTop;
				
				if(popup_ob.check_coords(coordX,coordY)==1){
					can_hide=0
				}
				
//				var ob=document.getElementById(popup_ob.active_panel_id)
//				if(ev_item==ob){
//					can_hide=0
//				}else{
//					for(var i=0;i<ob.childNodes.length;i++){
//						if(ob.childNodes[i]==ev_item){
//							can_hide=0
//						}
//					}
//				}
			}
		}
		if(can_hide==1){
			popup_ob.hide()	
		}
	}
}



//show_popup WILL CALL THE POPUP OBJECT AFTER CHECKING THAT IT EXISTS
function show_popup(event,panel_type,info_id){
	
	if(typeof popup_ob!="undefined" && !!popup_ob){
		popup_ob.show(event,panel_type,info_id)
	}
}
//FUNCTIONS THE WILL BE EVALUATED
function get_domain_info(dID){
	
	if(xmlob_loaded_arr["domain_groups"]==1){
		var desc_pos=-1
		
		desc_nodes=xmlob_doc_arr["domain_groups"].selectNodes("//LDomain[@DOMAIN_ID="+dID+"]/LDomain_descriptions")	
		desc_pos=0
		
		if(desc_nodes.length>0 && desc_pos>-1){
			popup_ob.title_str=desc_nodes[desc_pos].getAttribute("DOMAIN_description")
			popup_ob.content_str=desc_nodes[desc_pos].getAttribute("DOMAIN_long_description")
		}else{
			
		}
	}
}

function get_comparison_info(nn){

	if(xmlob_loaded_arr["comparisons"]==1){
		var desc_pos=-1
		desc_nodes=xmlob_doc_arr["comparisons"].selectNodes("//item[@nickname='"+nn+"']")	
		desc_pos=0
		if(desc_nodes.length>0 && desc_pos>-1){
			popup_ob.title_str=desc_nodes[desc_pos].getAttribute("display")
			popup_ob.content_str=desc_nodes[desc_pos].firstChild.nodeValue
		}else{
			
		}
	}
}
