




var popUps = new Array();

/** call popUp on href */
function getPopUp(href) {
	if(!popUps[href]) {
		var popUp = new HelpPopUp(href);
		popUp.init();
		popUps[href] = popUp;
	}	
	return popUps[href];
}

/** HelpPopup Class */
function HelpPopUp(href) {	
	var ie;
	
	var x, y;
	var xOffset, yOffset;
			
	var popUpContainer;
	var helpMessageContainer;
	var	clickAreaContainer;
	var	selectItemCover;
	var helpMessage;
	var ajaxError;
	var isDrag;
	
	var href;
	this.href = href;
	
	
	/** initialize popup */
	this.init = function() {	
		// identiy ie
		this.idIe();
		
		// create & add popUpContainer
		this.popUpContainer = document.createElement('div');
		this.popUpContainer.className = "loadingContextHelp";
		document.body.appendChild(this.popUpContainer);	
		
		// create & add selectItemCover for ie6 and below	
		if(this.ie < 7) {
			this.selectItemCover = document.createElement("iframe");
			this.selectItemCover.className = "ie6Bug";
			this.popUpContainer.appendChild(this.selectItemCover);
		}
			
		// create & add clickAreaContainer
		this.clickAreaContainer = document.createElement('div');
		this.clickAreaContainer.className = "clickArea";		
		this.popUpContainer.appendChild(this.clickAreaContainer);
							
		// create & add closeButton
		var closeButton = document.createElement('a');
		closeButton.className = "close";
		closeButton.setAttribute("href", "javascript:getPopUp('" + encodeURIComponent(this.href) + "').close();");
		closeButton.setAttribute("title", "Hilfe Schließen");
			
		var imgUrl = "/cms/export/system/modules/com.synchronity.gfaw.qualiservice.web/resources/img/icon_close.gif";
		
		var closeIcon = document.createElement('img');
		closeIcon.setAttribute("alt", "Hilfe Schliessen");
		closeIcon.setAttribute("src", imgUrl);
		closeButton.appendChild(closeIcon);
		this.popUpContainer.appendChild(closeButton);
											
		// create & add helpContentContainer
		this.helpContentContainer = document.createElement('div');
		this.helpContentContainer.className = "portal_help";
		this.popUpContainer.appendChild(this.helpContentContainer);
					
		// add eventHandling
		var _this = this;
		this.popUpContainer.onmousemove = function(event) { 
			if(!event)
				event = window.event;
			_this.move(event);
		}
		this.popUpContainer.onmousedown = function(event) { 
			if(!event)
				event = window.event;
			_this.startMoving(event);	
		 }
		this.popUpContainer.onmouseup = function() {
			_this.stopMoving();
		}
		this.popUpContainer.onmouseout = function() {
			_this.stopMoving();
		}
		this.popUpContainer.ondrag = function() {
			return false;
		}
		this.popUpContainer.onselectstart=function() {
			return false;
		}		
		
		// init request && set helpMessageContent
		this.initRequest();
	}
	
	
	/** open popup */
	this.open = function(element) {
		if(this.ajaxError)
			window.open(this.href, 'help', 'width=400, height=300, top=100, left=100, status=no, toolbar=no, location=no, resizable=yes, scrollbars=auto');
		else {
			this.x = this.pos(element).x -260;
			this.y = this.pos(element).y + 20;
			this.popUpContainer.style.left = this.x + "px"
			this.popUpContainer.style.top = this.y + "px"						
			this.popUpContainer.style.display = "block";
		}
	}

	
	/** close popup */
	this.close = function() {
		this.popUpContainer.style.display = "none";	
	}


	/** closes the popup and removes it from document */
	this.remove = function() {
		this.ajaxError = true;
		window.open(this.href, 'help', 'width=400, height=300, top=100, left=100, status=no, toolbar=no, location=no, resizable=yes, scrollbars=auto');
		try {
			this.close();
			this.popUpContainer.parentNode.removeChild(this.popUpContainer);
		} catch(e) {}
	}
	
	
	/** initializes asynchronous request */
	this.initRequest = function() {
		var httpRequest;
		if(window.XMLHttpRequest) { // !ie6&5 && ie7
			httpRequest = new XMLHttpRequest();
			if(httpRequest.overrideMimeType) 
				httpRequest.overrideMimeType("text/xml");
		} else if(window.ActiveXObject) { // ie6&5
			try {
				httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
			} catch(e) {
				try {
					httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
				} catch(e) {
					this.remove();
					return;
				}
			}
		}

		if(!httpRequest)
			this.remove();
		
		try {
			httpRequest.open('GET', href, true);
			var _this = this;
			httpRequest.onreadystatechange = function() { _this.handleRequest(httpRequest); }
			httpRequest.send(null);
		} catch(e) {
			this.remove();
		}		
	}
	
	
	/** handle request */
	this.handleRequest = function(httpRequest) {
		if(httpRequest.readyState == 4) /*	0 (uninitialized) 1 (loading) 2 (loaded) 3 (interactive) 4 (complete)*/
			if(httpRequest.status == 200) {
                var xmlDoc = null;
               				
				if(document.implementation && document.implementation.createDocument) // !ie
					xmlDoc = httpRequest.responseXML;
				else if(window.ActiveXObject) { // ie
					xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
				
 					xmlDoc.async = false;
 					xmlDoc.validateOnParse = false;
 					
 					var text = httpRequest.responseText;
					if(this.ie > 6)
						text = text.replace(/<!DOCTYPE.*">/, "");
 					
 					xmlDoc.loadXML(text);
				}
							
				var helpBody = xmlDoc.getElementsByTagName("body")[0];
								
				if(!helpBody) {
					this.remove();
					return;
				}
				
				if(helpBody.getAttribute("id") == "portal_help") {			
					// append helpStyle
					var hasStyle = false;
					var styles = document.getElementsByTagName("style");
					for(var i = 0 ; i < styles.length; i++) {
						if(styles[i].getAttribute("id") == "helpStyle") {
							hasStyle = true;
							break;
						}
					}
					if(!hasStyle) {
						var newStyle = document.createElement("style");
						newStyle.setAttribute("id", "helpStyle");
						newStyle.setAttribute("type", "text/css");
						var styleText = xmlDoc.getElementsByTagName("style")[0].firstChild.data;
						if(newStyle.styleSheet) // ie
							newStyle.styleSheet.cssText = styleText;
						else // !ie
							newStyle.appendChild(document.createTextNode(styleText));
							document.getElementsByTagName("head")[0].appendChild(newStyle);
					}
						
					// append message
					this.popUpContainer.className = "contextHelp";
						
					for(var i = 0; i < helpBody.childNodes.length; i++) 
						this.cloneAndAppend(helpBody.childNodes[i], this.helpContentContainer);
				} else
					this.remove();
			} else
				this.remove();
	}
	
	
	/** clone xml to html node */
	this.cloneAndAppend = function(nodeToClone, nodeToAppendTo) {		
		//debug("clone: " + nodeToClone.nodeName + "(" + nodeToClone.nodeType + ")");
		var clone = null;		
		if(this.ie) { // ie			
			// nodes
			if(nodeToClone.nodeType == 1) {
				clone = document.createElement(nodeToClone.nodeName);
				
				// childs
				for(var i = 0; i < nodeToClone.childNodes.length; i++)
					this.cloneAndAppend(nodeToClone.childNodes[i], clone);					
				
				// attributes
				for(var i = 0; i < nodeToClone.attributes.length; i++)
					clone.setAttribute(nodeToClone.attributes[i].nodeName, nodeToClone.attributes[i].nodeValue);
			}
			
			// text
			if(nodeToClone.nodeType == 3)
				clone  = document.createTextNode(nodeToClone.nodeValue);
		} else { // !ie
			if(document.importNode) // safari
				clone = document.importNode(nodeToClone, true);
			else
				clone = nodeToClone.cloneNode(true);
		}
		
		if(clone != null)
			try {
				nodeToAppendTo.appendChild(clone);
			} catch (e) {
				//debug(e);
			}
	}
	
	
	/** initialize start moving of popup */
	this.startMoving = function(event) {
		this.isDrag = true;	
		this.xOffset = event.clientX;
		this.yOffset = event.clientY;
		this.popUpContainer.style.cursor = "move";
		this.clickAreaContainer.style.height = this.popUpContainer.offsetHeight + "px";
		if(this.ie < 7)
			this.selectItemCover.style.height = this.popUpContainer.offsetHeight + "px";
			
		document.body.onselectstart = function() {
			return false; 
		}
	}	
	
	
	/** move popup */
	this.move = function(event) {
		if(this.isDrag) {		
			var x = event.clientX - this.xOffset + this.x;
			var y = event.clientY - this.yOffset + this.y;			
						
			this.popUpContainer.style.left = x + "px";
			this.popUpContainer.style.top = y + "px";
			
			return false;
		}
	}	
	
		
	/** stop moving of popup */
	this.stopMoving = function(event) {
		this.isDrag = false;
		this.xOffset = 0;
		this.yOffset = 0;
		this.x = parseInt(this.popUpContainer.style.left);
		this.y = parseInt(this.popUpContainer.style.top);
		this.popUpContainer.style.cursor = "auto";
		
		document.body.onselectstart = null;
		
		return false;
	}
	
	
	/** compute position of html-element */
	this.pos = function(element) {
		var x = 0;
		var y = 0;
		do {
			x += element.offsetLeft;
			y += element.offsetTop;		
		} while(element = element.offsetParent);
				
		var position = new Object();
			position.x = x;
			position.y = y;
		return position;
	}
	
	
	/** identify ie, versionNumber if ie, else blank */
	this.idIe = function() {
		var appVersion = navigator.appVersion.toLowerCase();
		if(appVersion.indexOf('msie') != -1) {
			var pos  = appVersion.indexOf('msie');
			if(pos != -1)
				this.ie = parseInt(parseFloat(appVersion.substring(pos + 5, appVersion.indexOf(';', pos))))
		}
	}
}
