/**
 * @author	Sven Kutzner
 * @copyright	2007-2008 deixu.net
 * @license	GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
 */

function WoWItemTooltip() {
	this.windows = new Array();
	this.ajaxRequest;
	this.currentItem;
	this.window;
	this.requestDone = true;
	this.mouseX = 0;
	this.mouseY = 0;
	
   this.parseText = function (str, contentType) {
		if (typeof ActiveXObject != "undefined") {
			var d = new ActiveXObject("MSXML.DomDocument");
			d.loadXML(str);
			return d;
		} else if (typeof XMLHttpRequest != "undefined") {
			var req = new XMLHttpRequest;
			req.open("GET", "data:" + (contentType || "application/xml") +
						";charset=utf-8," + encodeURIComponent(str), false);
			if (req.overrideMimeType) {
				req.overrideMimeType(contentType);
			}
			req.send(null);
			return req.responseXML;
		}
	}
	
	this.clearItems = function() {
		this.windows = new Array();
	}
	
	this.register = function(sourceId, itemId, save, onclick, mousepos, withIcon) {
		if(save == undefined) save = true;
		if(onclick == undefined) onclick = false;
		if(withIcon == undefined) withIcon = true;

		var element = document.getElementById(sourceId);
		var contentURI = "index.php?page=WoWItemTooltip&item="+itemId;

		if(!save) {
			contentURI = contentURI + "&save=0";
		}
		if(!withIcon) {
			contentURI = contentURI + "&icon=0";
		}

		this.windows[sourceId] = new Array(); this.windows[sourceId] = { "element":element, "id": sourceId, "item": itemId, "contentURI":contentURI,"content":"","icon":withIcon,"mouse":mousepos}
		if(onclick) {
			element.onclick = function(e) { return wowitemtooltip.onmouseover(e); }
		} else {
			element.onmouseover = function(e) { return wowitemtooltip.onmouseover(e); }
		}
		element.onmouseout = function(e) { return wowitemtooltip.onmouseout(e); }
	}
	
	
	this.showWindow = function() {
		if(!this.window) {
			this.window = document.createElement("div");
			this.window.style.display = "none"
			this.window.className = "wowPopup";
			document.body.appendChild(this.window);
			}
		
		// get parent height
		var parentHeight = window.parentNode ? window.parentNode.offsetHeight : 0;
					
		// get inner window height
		var windowInnerHeight = window.innerHeight ? window.innerHeight : (document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight);
					
		// get inner window width
		var windowInnerWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);

		var scrollRight = (window.pageXOffset ? window.pageXOffset : (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft)) + windowInnerWidth;
		var scrollBottom = (window.pageYOffset ? window.pageYOffset : (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) + windowInnerHeight;
		var offset = this.getPositionArray(this.currentItem.element);

		this.window.innerHTML = this.currentItem.content;
		this.window.style.visibility = "hidden";
		this.window.style.width = "auto";
		this.window.style.height = "auto";
		this.window.style.display = "block";
		var windowWidth = this.window.offsetWidth;
		var windowHeight = this.window.offsetHeight;
		if(windowWidth < 310) {
			this.window.style.width = "310px";
			windowWidth = 350;
		} else if(windowWidth > 460) {
			this.window.style.width = "460px";
			windowWidth = 500;
		}
		if(windowHeight < 50) {
			this.window.style.height = "50px";
			windowHeight = 50;
		}
		if(!this.currentItem.mouse) {
			this.window.style.top = (offset['bottom'] + 2) + "px";
			this.window.style.left = (offset['left'] + 30) + "px";
			var windowOffset = this.getPositionArray(this.window);
			if(windowOffset['bottom'] > scrollBottom) {
				this.window.style.top = (scrollBottom - this.window.offsetHeight - 15) + "px";
				this.window.style.left = (offset['right']) + "px";
			}

		} else {
			this.window.style.top = (this.mouseY + 2) + "px";
			this.window.style.left = (this.mouseX + 30) + "px";
			var windowOffset = this.getPositionArray(this.window);
			if(windowOffset['bottom'] > scrollBottom) {
				this.window.style.top = (scrollBottom - this.window.offsetHeight - 15) + "px";
				this.window.style.left = (this.mouseX + 30) + "px";
			}
		}
		windowOffset = this.getPositionArray(this.window);
		windowInnerWidth = window.innerWidth ? window.innerWidth : (document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth);
		scrollRight = (window.pageXOffset ? window.pageXOffset : (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft)) + windowInnerWidth;
		if(windowOffset['right'] > scrollRight) {
			this.window.style.left = (this.mouseX + 15) + "px";
		}
		this.window.style.visibility = "visible";
	}
	
	this.hideWindow = function() {
		if(this.window) {
			this.window.style.visibility = "hidden";
			this.window.style.display = "none";
		}
	}

	this.alertError = function(objError,strMessage) {
		var strError = strMessage + "\n\n";
		for (var i in objError)strError += i + ': ' + objError[i] + '\n';
		alert(strError);
	}
	
	this.handleResponse = function() {
		if (this.ajaxRequest.xmlHttpRequest.readyState == 4 && this.ajaxRequest.xmlHttpRequest.status == 200 && this.ajaxRequest.xmlHttpRequest.responseXML) {
				var text = this.ajaxRequest.xmlHttpRequest.responseText;
				var response = this.ajaxRequest.xmlHttpRequest.responseXML;
				if(!response || response.childNodes.length==0){ 
					response = this.parseText(text, "text/xml");
				}
				if(response) {
					var content = response.getElementsByTagName('content')[0];
					this.currentItem.content = content.childNodes[0].nodeValue;
					this.showWindow();
					this.requestDone = true;
				} else {
					this.requestDone = false;
				}
		}
	}
	
	this.onmouseover = function(event) {
		event = event || window.event;
		var target = event.target || event.srcElement;
		this.mouseX = event ? event.pageX : window.event.x;
		this.mouseY = event ? event.pageY : window.event.y;

		if (document.all && !document.captureEvents) {
			this.mouseX    += document.documentElement.scrollLeft;
			this.mouseY    += document.documentElement.scrollTop;
		}

		if(this.requestDone) {
			this.currentItem = this.windows[target.id];
			if(this.currentItem.content != "") {
				this.showWindow();
			} else {
				if(!this.ajaxRequest) this.ajaxRequest = new AjaxRequest();
				this.requestDone = false;
				this.ajaxRequest.openGet(this.currentItem.contentURI, function() { try { wowitemtooltip.handleResponse(); } catch(e) { } });
			}
		}
	}
	
	this.onmouseout = function(event) {
		this.hideWindow();
	}

	/**
	 * Returns the offset of an element.
	 */
	this.getOffset = function(element) {
		var offsetLeft = element.offsetLeft;
		var offsetTop = element.offsetTop;
		
		while ((element = element.offsetParent) != null) {
			offsetLeft += element.offsetLeft;
			offsetTop += element.offsetTop;
		}
		
		return {'left' : offsetLeft, 'top' : offsetTop};
	}
	
	/**
	 * Returns the position of an element.
	 */
	this.getPositionArray = function(element) {
		var offset = this.getOffset(element);
		offset['right'] = offset['left'] + element.offsetWidth;
		offset['bottom'] = offset['top'] + element.offsetHeight;
		
		return offset;
	}

}

var wowitemtooltip = new WoWItemTooltip();
