var SDETT_browserIsIE = (navigator.appName=="Microsoft Internet Explorer");
var SDETT_browserIsOpera = (navigator.userAgent.search(/Opera/)!=-1);
var SDETT_browserIsSafari = (navigator.userAgent.search(/Safari/)!=-1);
var SDETT_browserIsNetscape = (!SDETT_browserIsSafari && navigator.appName=="Netscape");

function SDETT_addEvent(obj, eventType, func) {
	if(obj.addEventListener) {
		obj.addEventListener(eventType, func, false);
		return true;
	} else if(obj.attachEvent) {
		var retVal=obj.attachEvent("on"+eventType, func);
		return retVal;
	} else {
		return false;
	}
}

function SDETT_startAJAXRequest(url) {
	var element=document.getElementById("SDETTRequest");
	var h=document.getElementsByTagName("head")[0];
	if(element) h.removeChild(element);
	document.charset="UTF-8";
	element=document.createElement("script");
	element.id="SDETTRequest";
	element.type="text/javascript";
	element.src=url;
	h.appendChild(element);
}

function SDETT_processMouseOver(e) {
	if(!e) e=window.event;
	var t=e.target?e.target:e.srcElement;
	if(!t || t.nodeName!="A" || !t.href.length) return;
	var m=t.href.match(/http:\/\/(www\.)?speedydragon\.(gamestar\.de|de|at|ch|net)\/(item|spell)\?id=([0-9]+)/);
	if(m) {
		var type=m[3];
		var id=m[4];
		if(!t.onmousemove) {
			t.onmousemove=SDETT_tooltipMouseMove;
			t.onmouseout=SDETT_tooltipMouseOut;
		}
		SDETT_showTooltip(type, id, null, e, null, null);
		return;
	}
	m=t.href.match(/http:\/\/(www\.)?speedydragon\.(gamestar\.de|de|at|ch|net)\/item\?name=(.+)$/);
	if(m) {
		var name=SDETT_filterItemName(m[3]);
		var id=SDETT_getTemporaryItemID(name);
		if(!t.onmousemove) {
			t.onmousemove=SDETT_tooltipMouseMove;
			t.onmouseout=SDETT_tooltipMouseOut;
		}
		SDETT_showTooltip("nameditem", id, name, e, null, null);
		return;
	}
}

function SDETT_filterItemName(name) {
	if(name==null) return null;
	name=name.replace(/%E4/g, "%C3%A4");
	name=name.replace(/%F6/g, "%C3%B6");
	name=name.replace(/%FC/g, "%C3%BC");
	name=name.replace(/%C4/g, "%C3%84");
	name=name.replace(/%D6/g, "%C3%96");
	name=name.replace(/%DC/g, "%C3%9C");
	name=decodeURIComponent(name.replace(/\+/g, " "));
	return name;
}

function SDETT_getMousePosX(event) {
	if(SDETT_browserIsIE) {
		return window.event.clientX+SDETT_getScrollX();
	} else {
		return event.pageX;
	}
}

function SDETT_getMousePosY(event) {
	if(SDETT_browserIsIE) {
		return window.event.clientY+SDETT_getScrollY();
	} else {
		return event.pageY;
	}
}

function SDETT_getWindowWidth() {
  	if(window.innerWidth) {
	    return window.innerWidth;
  	} else if(document.documentElement && document.documentElement.clientWidth) {
    	return document.documentElement.clientWidth;
	} else if(document.body && document.body.clientWidth) {
		return document.body.clientWidth;
	} else {
    	return 0;
	}
}

function SDETT_getWindowHeight() {
  	if(window.innerHeight) {
   		return window.innerHeight;
  	} else if(document.documentElement && document.documentElement.clientHeight) {
    	return document.documentElement.clientHeight;
	} else if(document.body && document.body.clientHeight) {
		return document.body.clientHeight;
  	} else {
    	return 0;
  	}
}

function SDETT_getScrollX() {
	if(SDETT_browserIsNetscape || SDETT_browserIsOpera) {
		return window.pageXOffset;
	} else if(document.documentElement && document.documentElement.scrollLeft) {
		return document.documentElement.scrollLeft;
	} else if(document.body && document.body.scrollLeft) {
		return document.body.scrollLeft;
	} else {
		return 0;
	}
}

function SDETT_getScrollY() {
	if(SDETT_browserIsNetscape || SDETT_browserIsOpera) {
		return window.pageYOffset;
	} else if(document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	} else if(document.body && document.body.scrollTop) {
		return document.body.scrollTop;
	} else {
		return 0;
	}
}

var SDETT_ItemCache={};
var SDETT_SpellCache={};
var SDETT_ItemNameCache=new Array();
var SDETT_LastRequest_Type=null;
var SDETT_LastRequest_ID=null;
var SDETT_LastRequest_X=null;
var SDETT_LastRequest_Y=null;
var SDETT_BaseURL="http://www.speedydragon.de/";

function SDETT_getTemporaryItemID(itemname) {
	var offset=60000;
	var i;
	itemname=itemname.toLowerCase();
	for(i=0;i<SDETT_ItemNameCache.length;i++) {
		if(SDETT_ItemNameCache[i]==itemname) return i+offset;
	}
	SDETT_ItemNameCache.push(itemname);
	return SDETT_ItemNameCache.length+offset-1;
}

function SDETT_showTooltip(type, id, name, e, xpos, ypos) {
	var cache=(type=="item" || type=="nameditem")?SDETT_ItemCache:SDETT_SpellCache;
	if(!cache[id]) {
		if(e) {
			SDETT_LastRequest_Type=type;
			SDETT_LastRequest_ID=id;
			SDETT_LastRequest_X=SDETT_getMousePosX(e);
			SDETT_LastRequest_Y=SDETT_getMousePosY(e);
			SDETT_startAJAXRequest(SDETT_BaseURL+"exttooltip?type="+type+((name==null)?"&id="+id:"&name="+name));
		}
	} else {
		if(!cache[id] || cache[id].length!=2) return;
		var icon=cache[id][0];
		var tooltip=cache[id][1];
		var ttdiv=document.getElementById("SDETT_Tooltip");
		if(!ttdiv) {
			var b=document.getElementsByTagName("body")[0];
			if(!b) return;
			ttdiv=document.createElement("div");
			ttdiv.id="SDETT_Tooltip";
			ttdiv.style.display="none";
			ttdiv.style.visibility="hidden";
			if(!(typeof SDETT_TooltipStyle_DisplayIcons=="boolean" && SDETT_TooltipStyle_DisplayIcons==false)) {
				var ttdiv_icon_div=document.createElement("div");
				ttdiv_icon_div.id="SDETTTooltipIcon";
				var ttdiv_icon_div_border=document.createElement("div");
				ttdiv_icon_div_border.id="SDETTTooltipIconBorder";
				if(SDETT_browserIsIE && navigator.userAgent.indexOf("MSIE 6.0")!=-1) {
					ttdiv_icon_div_border.style.backgroundImage="url('"+SDETT_BaseURL+"images/iconborder_medium.gif')";
				} else {			
					ttdiv_icon_div_border.style.backgroundImage="url('"+SDETT_BaseURL+"images/iconborder_medium.png')";
				}
				ttdiv_icon_div.appendChild(ttdiv_icon_div_border);
				ttdiv.appendChild(ttdiv_icon_div);
			}
			var ttdiv_data_div=document.createElement("div");
			ttdiv_data_div.id="SDETTTooltipData";
			ttdiv.appendChild(ttdiv_data_div);			
			b.appendChild(ttdiv);
		}
		ttdiv.style.display="";
		ttdiv.style.visibility="hidden";
		SDETT_moveTooltip(0, 0);
		if(document.getElementById("SDETTTooltipIcon")!=null) document.getElementById("SDETTTooltipIcon").style.backgroundImage="url('"+SDETT_BaseURL+"images/wowicons/medium/"+icon+".jpg')";
		document.getElementById("SDETTTooltipData").innerHTML=tooltip;
		var container=document.getElementById("SDETTTooltipContainer");
		ttdiv.style.width="";
		ttdiv.style.width=(container.clientWidth+50)+"px";
		ttdiv.cwidth=ttdiv.clientWidth;
		if((container.clientWidth+50)<ttdiv.cwidth) {
			ttdiv.cwidth=(container.clientWidth+50);
		}
		ttdiv.cheight=ttdiv.clientHeight;
		if(!(xpos && ypos) && e) {
			xpos=SDETT_getMousePosX(e);
			ypos=SDETT_getMousePosY(e);
		}
		SDETT_moveTooltip(xpos, ypos);
		ttdiv.style.visibility="visible";
	}
}

function SDETT_moveTooltip(xpos, ypos) {
	var ttdiv=document.getElementById("SDETT_Tooltip");
	if(ttdiv) {
		var windowheight=SDETT_getWindowHeight();
		var windowwidth=SDETT_getWindowWidth();
		var height=ttdiv.cheight;
		var width=ttdiv.cwidth;
		var xoffset=-40;
		var yoffset=20;
		var xscroll=SDETT_getScrollX();
		var yscroll=SDETT_getScrollY();
		var xzero=0;
		var yzero=0;
		if(xpos+width>windowwidth+xscroll-5) {
			xzero=-width+xzero;
		}
		if(xpos+xzero<xscroll) {
			xzero=0;
		}
		if(ypos+height>windowheight+yscroll-20) {
			yzero=-height-25+yzero;	
		}
		if(ypos+yzero<yscroll) {
			yzero=0;
		}
		ttdiv.style.left=(xpos+xoffset+xzero)+"px";
		ttdiv.style.top=(ypos+yoffset+yzero)+"px";
	}
}

function SDETT_hideTooltip() {
	var ttdiv=document.getElementById("SDETT_Tooltip");
	if(ttdiv) {
		ttdiv.style.visibility="hidden";
	}
}

function SDETT_tooltipMouseMove(e) {
	SDETT_moveTooltip(SDETT_getMousePosX(e), SDETT_getMousePosY(e));
}

function SDETT_tooltipMouseOut() {
	SDETT_LastRequest_Type=null;
	SDETT_LastRequest_ID=null;
	SDETT_hideTooltip();
}

function SDETT_AddTooltip(id, name, type, icon, data) {
	var element=document.getElementById("SDETTRequest");
	var h=document.getElementsByTagName("head")[0];
	if(element) h.removeChild(element);
	if(type=="item" || type=="nameditem") {
		SDETT_ItemCache[id]=[icon, data];
		if(type=="nameditem") {
			id=SDETT_getTemporaryItemID(name);
			SDETT_ItemCache[id]=[icon, data];
		}
	}
	if(type=="spell") SDETT_SpellCache[id]=[icon, data];
	if(SDETT_LastRequest_Type==type && SDETT_LastRequest_ID==id) SDETT_showTooltip(type, id, null, null, SDETT_LastRequest_X, SDETT_LastRequest_Y);
}

function SDETT_setup() {
	SDETT_addEvent(document, "mouseover", SDETT_processMouseOver);
	var h=document.getElementsByTagName("head")[0];
	if(h) {
		var l=document.createElement("link");
		var cssparams="";
		if(typeof SDETT_TooltipStyle_FontSize=="number") cssparams=cssparams+"&fs="+SDETT_TooltipStyle_FontSize;
		if(typeof SDETT_TooltipStyle_TitleSize=="number") cssparams=cssparams+"&ts="+SDETT_TooltipStyle_TitleSize;
		if(typeof SDETT_TooltipStyle_LineDistance=="number") cssparams=cssparams+"&ld="+SDETT_TooltipStyle_LineDistance;
		if(typeof SDETT_TooltipStyle_TitleWeight=="string") cssparams=cssparams+"&tw="+SDETT_TooltipStyle_TitleWeight;
		if(typeof SDETT_TooltipStyle_FontFamily=="string") cssparams=cssparams+"&ff="+encodeURIComponent(SDETT_TooltipStyle_FontFamily);
		if(cssparams.length>0) cssparams="?"+cssparams.substring(1);
		l.type="text/css";
		l.href=SDETT_BaseURL+"css/exttooltip.css"+cssparams;
		l.rel="stylesheet";
		h.appendChild(l);
		if(SDETT_browserIsIE) {
			l=document.createElement("link");
			l.type="text/css";
			l.href=SDETT_BaseURL+"css/exttooltip_ie.css";
			l.rel="stylesheet";
			h.appendChild(l);
			if(navigator.userAgent.indexOf("MSIE 6.0")!=-1) {
				l=document.createElement("link");
				l.type="text/css";
				l.href=SDETT_BaseURL+"css/exttooltip_ie6.css";
				l.rel="stylesheet";
				h.appendChild(l);
			}
		}
	}
}

SDETT_setup();
