User:Scormus/global.js

/*

Tools: reload cache

// The source code on this page 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 // // Any and all other copyright restriction remain in place.

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

var worldDX = 0; var worldDY = 0; var worldScaleX = 1.; var worldScaleY = 1.; var params = {};

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 getMapNotationData(notationCode) { if (notationCode == "debug") { 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; } return getMapNotationDataFromPage(notationCode); }

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"] = "10,10"; return notationData; }

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

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

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

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

function urlDecode(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 setParams { 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 = urlDecode(hash[0]); var value = urlDecode(hash[1]); params[name] = value; } }

function getLocationFromSearch { var co_location = decodeURI(location.search).substr(1); return co_location; }

function getYFromLocationString(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 getXFromLocationString(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 writeAllNotations { setParams; var notationCode = "Default"; var anchorTitle = "";

setScale;

var i = 1; while (params['l' + i] && i < 1000) { anchorTitle = params['l' + 1]; if (params['nc' + i]) { notationCode = params['nc' + i]; }   if (params['lk' + i]) { anchorTitle = params['lk' + i]; }   writeNotation(params['l' + i], notationCode, anchorTitle); i++; } }

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

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

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

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

worldDX = xLocationAtZero; worldDY = yLocationAtZero; if (xLocation1 != worldDX) { worldScaleX = xLocationWorld1 / (xLocation1 - worldDX); } else { worldScaleX = xLocationWorld2 / (xLocation2 - worldDX); } if (yLocation1 != worldDY)  { worldScaleY = yLocationWorld1 / (yLocation1 - worldDY); } else { worldScaleY = yLocationWorld2 / (yLocation2 - worldDY); }	}

function getScreenXFromLocationString(locationString) { var worldX = getXFromLocationString(locationString); return (worldX / worldScaleX) + worldDX; }

function getScreenYFromLocationString(locationString) { var worldY = getYFromLocationString(locationString); return (worldY / worldScaleY) + worldDY; }

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

var notationImageHeight =getXFromLocationString(notationImageSize); var notationImageWidth = getYFromLocationString(notationImageSize);

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

var newY = getScreenYFromLocationString(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 = ""; 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); }

//