if (typeof $WowheadPower == "undefined") {
	
	var $WowheadPower = new function() {
	
		function _createElement(name, attr) {
			var result = document.createElement(name);
			if (attr) {
				deepCopy(result, attr);
			}
			return result;
		}
		
		function _appendChild(object, childObject) {
			return object.appendChild(childObject);
		}
		
		function _attachEvent(object, eventName, eventFunc) {
			if (window.attachEvent) {
				object.attachEvent("on"+eventName, eventFunc);
			} else {
				object.addEventListener(eventName, eventFunc, false);
			}
		}
		
		function deepCopy(d, s) {
			for (var p in s) {
				if (typeof s[p] == "object") {
					if (!d[p]) {
						d[p] = {};
					}
					deepCopy(d[p], s[p]);
				} else {
					d[p]=s[p];
				}
			}
		}
		
		function $E(e) {
			if (!e) {
				e=event;
			}
			if (!e._button) {
				e._button = e.which?e.which:e.button;
				e._target = e.target?e.target:e.srcElement;
			}
			return e;
		}
		
		function getWindowSize() {
			var _w = 0, _h = 0;
			if (typeof(window.innerWidth) == "number") {
				_w = window.innerWidth;
				_h = window.innerHeight;
			} else {
				if (document.documentElement&&(document.documentElement.clientWidth||document.documentElement.clientHeight)) {
					_w = document.documentElement.clientWidth;
					_h = document.documentElement.clientHeight;
				} else {
					if (document.body&&(document.body.clientWidth||document.body.clientHeight)) {
						_w=document.body.clientWidth;
						_h=document.body.clientHeight;
					}
				}
			}
		
			return {w:_w, h:_h};
		}
	
		function getScroll() {
			var x=0, y=0;
			if (typeof(window.pageYOffset) == "number") {
				x = window.pageXOffset;
				y = window.pageYOffset;
			} else {
				if (document.body&&(document.body.scrollLeft||document.body.scrollTop)) {
					x = document.body.scrollLeft;
					y = document.body.scrollTop;
				} else {
					if (document.documentElement&&(document.documentElement.scrollLeft||document.documentElement.scrollTop)) {
						x = document.documentElement.scrollLeft;
						y = document.documentElement.scrollTop;
					}
				}
			}
			return {x:x, y:y};
		}
	
		function getCursorPos(e) {
			var x, y;
			if (window.innerHeight) {
				x = e.pageX;
				y = e.pageY;
			} else {
				var _14 = getScroll();
				x = e.clientX+_14.x;
				y = e.clientY+_14.y;
			}
			return {x:x, y:y};
		}
		
		var opt = {applyto:7},
			_displayType, _displayID,
			xPos, yPos,
			documentHead = document.getElementsByTagName("head")[0],
			_itemArray = [], _questArray = [], _spellArray = [],
			t_div,
			t_table,
			t_column,
			t_p,
			_showIcon = 1,
			ie = !!(window.attachEvent&&!window.opera),
			ie7 = navigator.userAgent.indexOf("MSIE 7.0") != -1,
			ie6 = navigator.userAgent.indexOf("MSIE 6.0") != -1 && !ie7,
			msgData = {loading:"Loading...", noresponse:"No response from server :("},
			statusNull = 0, statusLoading = 1, statusNotFound = 2, statusNoData = 3, statusDataFound = 4,
			_item = 3, _quest = 5, _spell = 6,
			hSpacing = 15,
			vSpacing = 15,
			typeData = {3:[_itemArray, "item", "Item"], 5:[_questArray, "quest", "Quest"], 6:[_spellArray, "spell", "Spell"]};
		
		function init() {
			_appendChild(documentHead, _createElement("link", {type:"text/css", href:"http://www.wowhead.com/widgets/power/power.css?3", rel:"stylesheet"}));
			if (ie) {
				_appendChild(documentHead, _createElement("link", {type:"text/css", href:"http://www.wowhead.com/widgets/power/power_ie.css?3", rel:"stylesheet"}));
				if (ie6) {
					_appendChild(documentHead, _createElement("link", {type:"text/css", href:"http://www.wowhead.com/widgets/power/power_ie6.css?3", rel:"stylesheet"}));
				}
			}
		
			_attachEvent(document, "mouseover", onMouseOver);
		}
	
		function updateCursorPos(e) {
			var pos = getCursorPos(e);
			xPos = pos.x;
			yPos = pos.y;
		}
		
		function scanElement(t, e) {
			if (t.nodeName != "A") {
				return -2323;
			}
			if (!t.href.length) {
				return;
			}
			var i1, i2, m;
			if (opt.applyto&1) {
				i1 = 2;
				i2 = 3;
				m = t.href.match(/http:\/\/(www\.)?wowhead\.com\/\?(item|quest|spell)=([0-9]+)/);
			}
			if (m == null && (opt.applyto&2) && t.rel) {
				i1 = 1;
				i2 = 2;
				m = t.rel.match(/(item|quest|spell).?([0-9]+)/);
			}
			if (m == null && (opt.applyto&4)) {
				i1 = 2;
				i2 = 3;
				m = t.href.match(/http:\/\/(www\.|wow\.)?buffed\.de\/\?(i)=([0-9]+)/);
			}
			if (m) {
				if (!t.onmousemove) {
					t.onmousemove = onMouseMove;
					t.onmouseout = onMouseOut;
				}
				updateCursorPos(e);
				if (m[i1] == "item" || m[i1] == "i") {
					display(_item, m[i2]);
				} else {
					if (m[i1] == "quest") {
						display(_quest, m[i2]);
					} else {
						display(_spell, m[i2]);
					}
				}
			}
		}
		
		function onMouseOver(e) {
			e = $E(e);
			var t = e._target;
			var i = 0;
			while (t != null && i<3 && scanElement(t, e) == -2323) {
				t = t.parentNode;
				++i;
			}
		}
		
		function onMouseMove(e) {
			e = $E(e);
			updateCursorPos(e);
			moveTooltip();
		}
		
		function onMouseOut() {
			_displayType = null;
			hideTooltip();
		}
		
		function initTooltip() {
			if (!t_div) {
				var d = _createElement("div"),
					t = _createElement("table"),
					tb = _createElement("tbody"),
					tr1 = _createElement("tr"),
					tr2 = _createElement("tr"),
					td = _createElement("td"),
					th1 = _createElement("th"),
					th2 = _createElement("th"),
					th3 = _createElement("th");
				
				d.className = "wowhead-tooltip";
				th1.style.backgroundPosition = "top right";
				th2.style.backgroundPosition = "bottom left";
				th3.style.backgroundPosition = "bottom right";
				_appendChild(tr1, td);
				_appendChild(tr1, th1);
				_appendChild(tb, tr1);
				_appendChild(tr2, th2);
				_appendChild(tr2, th3);
				_appendChild(tb, tr2);
				_appendChild(t, tb);
				t_p = _createElement("p");
				t_p.style.display = "none";
				_appendChild(t_p, _createElement("div"));
				_appendChild(d, t_p);
				_appendChild(d, t);
				_appendChild(document.body, d);
				t_div = d;
				t_table = t;
				t_column = td;
				if (opt.showlogo) {
					var img = _createElement("div");
					img.className = "wowhead-tooltip-powered";
					_appendChild(d, img);
				}
				hideTooltip();
			}
		}
		
		function showTooltip(data, icon) {
			if (!t_div) {
				initTooltip();
			}
			if (!data) {
				data = typeData[_displayType][2]+" not found :(";
				icon = "Temp";
			}
			if (_showIcon && icon) {
				t_p.style.backgroundImage = "url(http://static.wowhead.com/images/icons/medium/"+icon.toLowerCase()+".jpg)";
				t_p.style.display = "";
			} else {
				t_p.style.backgroundImage = "none";
				t_p.style.display = "none";
			}
			t_div.style.display = "";
			t_div.style.width = "320px";
			t_column.innerHTML = data;
			fixTooltip();
			moveTooltip();
			t_div.style.visibility = "visible";
		}
		
		function hideTooltip() {
			if(!t_div) {
				return;
			}
			t_div.style.display="none";
			t_div.style.visibility="hidden";
		}
		
		function fixTooltip() {
			var c = t_column.childNodes;
			if (c.length >= 2 && c[0].nodeName == "TABLE" && c[1].nodeName == "TABLE") {
				var m;
				if (c[1].offsetWidth > 300) {
					m=Math.max(300, c[0].offsetWidth)+20;
				} else {
					m=Math.max(c[0].offsetWidth, c[1].offsetWidth)+20;
				}
				if (m > 20) {
					t_div.style.width = m+"px";
					c[0].style.width = c[1].style.width = "100%";
				}
			} else {
				t_div.style.width = t_table.offsetWidth+"px";
			}
		}
		
		function moveTooltip() {
			if (!t_div) {
				return;
			}
			if (xPos == null) {
				return;
			}
			var wSize = getWindowSize(),
				scrollPos = getScroll(),
				windowWidth = wSize.w,
				windowHeight = wSize.h,
				scrollPosX = scrollPos.x,
				scrollPosY = scrollPos.y,
				tableOffsetWidth = t_table.offsetWidth,
				tableOffsetHeight = t_table.offsetHeight,
				left = xPos+hSpacing,
				top = yPos-tableOffsetHeight-vSpacing;
		
			if (left+hSpacing+tableOffsetWidth+4 >= scrollPosX+windowWidth) {
				var tmpLeft = xPos-tableOffsetWidth-hSpacing;
				if (tmpLeft >= 0) {
					left = tmpLeft;
				} else {
					left = scrollPosX+windowWidth-tableOffsetWidth-hSpacing-4;
				}
			}
			
			if (top < scrollPosY) {
				top = yPos+vSpacing;
				if (top+tableOffsetHeight > scrollPosY+windowHeight) {
					top = scrollPosY+windowHeight-tableOffsetHeight;
					if (_showIcon) {
						if (xPos >= left-48 && xPos <= left && yPos >= top-4 && yPos <= top+48) {
							top -= 48-(yPos-top);
						}
					}
				}
			}
		
			t_div.style.left = left+"px";
			t_div.style.top = top+"px";
		}
		
		function initElement(type, id) {
			var arr = typeData[type][0];
		
			if (arr[id] == null) {
				arr[id] = {};
			}
		
			if (arr[id].status == null) {
				arr[id].status = statusNull;
			}
		}
		
		function display(_type, id) {
			_displayType = _type;
			_displayID = id;
			initElement(_type, id);
			
			var arr = typeData[_type][0];
			
			if (arr[id].status == statusDataFound || arr[id].status == statusNoData) {
				showTooltip(arr[id].tooltip, arr[id].icon);
			} else {
				if (arr[id].status == statusLoading) {
					showTooltip(msgData.loading);
				} else {
					request(_type, id);
				}
			}
		}
		
		function request(_type, id, _5e) {
			var arr = typeData[_type][0];
		
			if (arr[id].status != statusNull && arr[id].status != statusNotFound) {
				return;
			}
			
			arr[id].status = statusLoading;
		
			if (!_5e) {
				arr[id].timer = setTimeout(function() {showLoading.apply(this, [_type, id]);}, 333);
			}
		
			ajaxIshRequest("http://www.wowhead.com/?"+typeData[_type][1]+"="+id+"&power");
		}
		
		function ajaxIshRequest(url) {
			_appendChild(documentHead, _createElement("script", {type:"text/javascript", src:url}));
		}
		
		function showLoading(type, id) {
			if (_displayType == type && _displayID == id) {
				showTooltip(msgData.loading);
				var arr = typeData[type][0];
				arr[id].timer = setTimeout(function() {notFound.apply(this, [type, id]);}, 3850);
			}
		}
		
		function notFound(type, id) {
			var arr = typeData[type][0];
			arr[id].status = statusNotFound;
		
			if (_displayType == type && _displayID == id) {
				showTooltip(msgData.noresponse);
			}
		}
		
		this.register = function(type, id, source) {
			var arr = typeData[type][0];
			clearTimeout(arr[id].timer);
			deepCopy(arr[id], source);
			if (arr[id].tooltip) {
				arr[id].status = statusDataFound;
			} else {
				arr[id].status = statusNoData;
			}
			if (_displayType == type && id == _displayID) {
				showTooltip(arr[id].tooltip, arr[id].icon);
			}
		};
		
		this.registerItem = function(id, object) {
			this.register(_item, id, object);
		};
		
		this.registerQuest = function(id, object) {
			this.register(_quest, id, object);
		};
		
		this.registerSpell = function(id, object) {
			this.register(_spell, id, object);
		};
		
		this.set = function(foo) {
			deepCopy(opt, foo);
		};
		
		this.showTooltip = function(e, data, icon) {
			updateCursorPos(e);
			showTooltip(data, icon);
		};
		
		this.hideTooltip = function() {
			hideTooltip();
		};
		
		this.moveTooltip = function(e) {
			onMouseMove(e);
		};
		
		init();
	};
}
