User:Toranosuke/core.js

// from http://www.wowwiki.com/User:Pcj/core.js /* Defines universal variables and functions for hover tooltip generation.

// loot comparison off by default var ttCompare = false; // debug mode off by default var ttDebug = true;

var ttHTMLStart = ''; var crlf = new RegExp("\r\n", "g");

function $A(a) { var r = []; for (var i = 0, len = a.length; i < len; ++i) r.push(a[i]); return r; }

Function.prototype.bind = function { var __method = this, args = $A(arguments), object = args.shift; return function { return __method.apply(object, args.concat($A(arguments))) }; }

// Empty variables to hold the mouse position and the window size var mousePos = null; var winSize = null;

// Set events to catch mouse position and window size document.onmousemove = mouseMove; window.onresize = windowResize;

// The mouseMove and mouseCoords function track the mouse position for us function mouseMove(ev) { ev = ev || window.event; mousePos = mouseCoords(ev); }

function mouseCoords(ev) { if(ev.pageX || ev.pageY) return {x:ev.pageX, y:ev.pageY}; if (document.documentElement && document.documentElement.scrollTop) { // IE6 +4.01 and user has scrolled dbSleft = document.documentElement.scrollLeft; dbStop = document.documentElement.scrollTop; dbCleft = document.documentElement.clientLeft; dbCtop = document.documentElement.clientTop; } else { // IE5 or DTD 3.2 dbSleft = document.body.scrollLeft; dbStop = document.body.scrollTop; dbCleft = document.body.clientLeft; dbCtop = document.body.clientTop; } return {x:ev.clientX + dbSleft - dbCleft, y:ev.clientY + dbStop - dbCtop}; }

function ttError(type) { extraText = ""; switch(type) { case "NPC": extraText = " A correctly formatted {" + "{npcbox}" + "} must be added to the target NPC page."; return ttHTMLStart + "Error This " + type + " either has no tooltip or was not intended to have one." + extraText + " "; break; case "item": extraText = " A correctly formatted {" + "{itembox}" + "} must be added to the target Item page."; return ttHTMLStart + "Error This " + type + " either has no tooltip or was not intended to have one." + extraText + " "; break; case "quest": extraText = " A correctly formatted {" + "{questbox}" + "} must be added to the target Quest page."; return ttHTMLStart + "Error This " + type + " either has no tooltip or was not intended to have one." + extraText + " "; break; default: return type; } }

// The windowResize function keeps track of the window size for us function windowResize { if (document.documentElement && document.documentElement.clientWidth) { // IE6 +4.01 dbCwidth = document.documentElement.clientWidth; dbCheight = document.documentElement.clientHeight; } else { // IE5 or DTD 3.2 dbCwidth = document.body.clientWidth; dbCheight = document.body.clientHeight; } winSize = {x:(dbCwidth) ? dbCwidth:window.innerWidth, y:(dbCheight) ? dbCheight:window.innerHeight} }

//Determine what XmlHttpRequest object we will use. function getXmlHttpRequestObject { if (window.XMLHttpRequest) { return new XMLHttpRequest; //Not Internet Explorer } else if(window.ActiveXObject) { return new ActiveXObject("Microsoft.XMLHTTP"); //Internet Explorer } else { //fail silently } }

var getRequest = getXmlHttpRequestObject;

function hideTip { var tip = document.getElementById('tfb'); tip.innerHTML = ""; tip.style.display = "none"; if (ttCompare) { var tip2 = document.getElementById('tfb2'); tip2.innerHTML = ""; tip2.style.display = "none"; } }

function getInfo(pagename, type) { //Checks to see if XmlHttpRequest object is ready. if (getRequest.readyState == 4 || getRequest.readyState == 0) { getRequest.open("GET", 'http://www.lotro-wiki.com/index.php?action=render&title=' + pagename, true); switch (type) { case 1: getRequest.onreadystatechange = parseText; break; case 2: getRequest.onreadystatechange = parseSetText; break; case 3: getRequest.onreadystatechange = parseCompText; break; case 4: getRequest.onreadystatechange = parseQuestText; break; case 5: getRequest.onreadystatechange = parseNPCText; break; default: getRequest.onreadystatechange = parseText; } //Makes the request. getRequest.send(null); } }

function displayTip { var tip = document.getElementById('tfb'); tip.style.position = "absolute"; tip.style.visibility = "hidden"; tip.style.display = "block"; tip.style.zIndex = "999"; moveTip; tip.style.visibility = "visible"; }

// This function moves the tool-tips when our mouse moves function moveTip { if (document.documentElement && document.documentElement.scrollTop) { // IE6 +4.01 and user has scrolled dbSleft = document.documentElement.scrollLeft; dbStop = document.documentElement.scrollTop; } else { // IE5 or DTD 3.2 dbSleft = document.body.scrollLeft; dbStop = document.body.scrollTop; } var tip = document.getElementById('tfb'); var newTop = mousePos.y - (tip.clientHeight + 40); var newLeft = mousePos.x - ( tip.clientWidth / 2 ); if( newTop < dbStop ) { newTop = mousePos.y + 1; if ( newTop + tip.clientHeight > winSize.y ) newTop = dbStop; } if( newLeft < dbSleft ) newLeft = dbSleft; if( ( mousePos.x + ( tip.clientWidth / 2 ) ) >= winSize.x - 300 ) newLeft = mousePos.x - ( 1.75 * tip.clientWidth ); tip.style.top = newTop + "px"; tip.style.left = newLeft + "px"; }

windowResize; ttfdiv = document.createElement("div"); ttfdiv.setAttribute("id", "tfb"); contentstart = document.getElementById("content"); contentstart.insertBefore(ttfdiv, contentstart.childNodes[0]); document.body.onmouseover = hideTip; /*