User:Scormus/global.js

/* Any JavaScript here will be loaded for all users on every page load.

// The source code below is covered by the CC-BY-SA license and this code may be freely used on any Wikia site for any purpose under this license. // Beyond Wikia, this code must retain the attribution below and this must be easily visible on any page that uses the source code directly or indirectly: // // This code was created by Scormus for championsonline.wikia.com, 2010. // // Any and all other copyright restriction remain in place.

$(function { if ($(".mapDiv").length) writeAllMapNotations; });

var mapWorldDX = 0; var mapWorldDY = 0; var mapWorldScaleX = 1.; var mapWorldScaleY = 1.; var mapParams = {};

function writeAllMapNotations { setMapParams; var notationCode = "Default"; var anchorTitle = ""; var anchorHref = "";

setMapScale;

var i = 1; while (mapParams['l' + i] && i < 1000) { anchorTitle = mapParams['l' + i]; anchorHref = ""; if (mapParams['nc' + i]) { notationCode = mapParams['nc' + i]; }   if (mapParams['lk' + i]) { anchorTitle = mapParams['lk' + i]; }   if (mapParams['hr' + i]) { anchorHref = mapParams['hr' + i]; }   writeMapNotation(mapParams['l' + i], notationCode, anchorTitle, anchorHref); i++; } }

function setMapScale { var worldGridPoints = getMapWorldGridPoints; var imageGridPoints = getMapImageGridPoints;

var worldGridPoint1 = worldGridPoints[0]; var imageGridPoint1 = imageGridPoints[0]; var xLocation1 = getXFromMapLocationString(imageGridPoint1); var yLocation1 = getYFromMapLocationString(imageGridPoint1); var xLocationWorld1 = getXFromMapLocationString(worldGridPoint1); var yLocationWorld1 = getYFromMapLocationString(worldGridPoint1);

var worldGridPoint2 = worldGridPoints[1]; var imageGridPoint2 = imageGridPoints[1]; var xLocation2 = getXFromMapLocationString(imageGridPoint2); var yLocation2 = getYFromMapLocationString(imageGridPoint2); var xLocationWorld2 = getXFromMapLocationString(worldGridPoint2); var yLocationWorld2 = getYFromMapLocationString(worldGridPoint2);

var xLocationAtZero = ((xLocationWorld1 * xLocation2) - (xLocationWorld2 * xLocation1)) / (xLocationWorld1 - xLocationWorld2); var yLocationAtZero = ((yLocationWorld1 * yLocation2) - (yLocationWorld2 * yLocation1)) / (yLocationWorld1 - yLocationWorld2);

mapWorldDX = xLocationAtZero; mapWorldDY = yLocationAtZero; if (xLocation1 != mapWorldDX) { mapWorldScaleX = xLocationWorld1 / (xLocation1 - mapWorldDX); } else { mapWorldScaleX = xLocationWorld2 / (xLocation2 - mapWorldDX); } if (yLocation1 != mapWorldDY)  { mapWorldScaleY = yLocationWorld1 / (yLocation1 - mapWorldDY); } else { mapWorldScaleY = yLocationWorld2 / (yLocation2 - mapWorldDY); }	}

function writeMapNotation(locationString, notationCode, anchorTitle, anchorHref) { var mapNotationURL = getMapNotationURL(notationCode); var notationImageSize = getMapNotationSize(notationCode);

var notationImageHeight =getXFromMapLocationString(notationImageSize); var notationImageWidth = getYFromMapLocationString(notationImageSize);

var newX = getScreenXFromMapLocationString(locationString); newX = newX - notationImageWidth / 2 ; newX = Math.round(newX);

var newY = getScreenYFromMapLocationString(locationString); newY = newY - notationImageHeight / 2 ; newY = Math.round(newY);

var div1 = document.createElement("div"); div1.style.position = "absolute"; div1.style.left = newX + "px"; div1.style.top = newY + "px";

var ahrf = document.createElement("a"); ahrf.href = ""; if (anchorHref) { ahrf.href = anchorHref; } ahrf.class="image" ahrf.title= anchorTitle;

div1.appendChild(ahrf);

var img2 = document.createElement("img"); img2.src = mapNotationURL; ahrf.appendChild(img2);

var mainDiv=document.getElementById("mapDiv");

mainDiv.appendChild(div1); }

function getMapImageDataFromPage { var refWorldGridPoint1=document.getElementById("worldGridPoint1"); if (!refWorldGridPoint1) { return getDefaultMapImageData; }

var refWorldGridPoint2=document.getElementById("worldGridPoint2"); if (!refWorldGridPoint2) { return getDefaultMapImageData; }

var refImageGridPoint1=document.getElementById("imageGridPoint1"); if (!refImageGridPoint1) { return getDefaultMapImageData; }

var refImageGridPoint2=document.getElementById("imageGridPoint2"); if (!refImageGridPoint2) { return getDefaultMapImageData; }

var mapData = new Object; mapData["worldGridPoints"]= [refWorldGridPoint1.firstChild.nodeValue, refWorldGridPoint2.firstChild.nodeValue]; mapData["imageGridPoints"] = [refImageGridPoint1.firstChild.nodeValue, refImageGridPoint2.firstChild.nodeValue]; return mapData; }

function getDefaultMapImageData { var mapData = new Object; mapData["worldGridPoints"]= ["1169 66 1920", "10061 198 -2712"]; mapData["imageGridPoints"] = ["374,100", "1263,564"]; return mapData; }

function getMapNotationDataFromPage(notationCode) { var refLabel=document.getElementById("refLabel" + notationCode);

if (!refLabel) { return getDefaultNotationData; }

var refLabelLink =refLabel.childNodes[0];

if (!refLabelLink ) { return getDefaultNotationData; }

var refImage = refLabelLink.childNodes[0];

if (!refImage) { return getDefaultNotationData; }

var notationData = new Object; notationData["imageURL"] = refImage.src; notationData["notationSize"] = refImage.height + "," + refImage.width; return notationData; }

function getDefaultNotationData { var notationData = new Object; notationData["imageURL"] = "http://images2.wikia.nocookie.net/__cb20100413235557/championsonline/images/7/7e/Map_location_highlight_green_circle_30x30.png"; notationData["notationSize"] = "30,30"; return notationData; }

function getMapWorldGridPoints { var mapData = getMapImageDataFromPage; return mapData["worldGridPoints"]; }

function getMapImageGridPoints { var mapData = getMapImageDataFromPage; return mapData["imageGridPoints"]; }

function getMapNotationURL(notationCode) { var notationData = getMapNotationDataFromPage(notationCode); return notationData["imageURL"]; }

function getMapNotationSize(notationCode) { var notationData = getMapNotationDataFromPage(notationCode); return notationData["notationSize"]; }

function mapUrlDecode(s) { if (s) { return decodeURIComponent( s.replace( /\+/g, '%20' ).replace( /\%21/g, '!' ).replace( /\%27/g, "'" ).replace( /\%28/g, '(' ).replace( /\%29/g, ')' ).replace( /\%2A/g, '*' ).replace( /\%7E/g, '~' ) ); } return s; }

function setMapParams { var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&'); for(var i = 0; i < hashes.length; i++) { var hash = hashes[i].split('='); var name = mapUrlDecode(hash[0]); var value = mapUrlDecode(hash[1]); mapParams[name] = value; } }

function getYFromMapLocationString(str) { var lastCommaIndex = str.lastIndexOf(","); if (lastCommaIndex > 0) { return str.substr(lastCommaIndex + 1); }

var lastSpaceIndex = str.lastIndexOf(" "); if (lastSpaceIndex > 0) { return str.substr(lastSpaceIndex + 1); } return str; }

function getXFromMapLocationString(str) { var firstCommaIndex = str.indexOf(","); if (firstCommaIndex > 0) { return str.substr(0, firstCommaIndex); }

var firstSpaceIndex = str.indexOf(" "); if (firstSpaceIndex > 0) { return str.substr(0, firstSpaceIndex); } return str; }

function getScreenXFromMapLocationString(locationString) { var worldX = getXFromMapLocationString(locationString); return (worldX / mapWorldScaleX) + mapWorldDX; }

function getScreenYFromMapLocationString(locationString) { var worldY = getYFromMapLocationString(locationString); return (worldY / mapWorldScaleY) + mapWorldDY; }

//