/**
*
* This is a Magic: the Gathering autocard script.
*
* Information: http://apps.addictedtoedh.com/autocard
*
* Copyright (C) 2013 Steven Lazidis
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*
*
* Wizards of the Coast, Magic: the Gathering, and their logos are
* trademarks of Wizards of the Coast LLC in the United States and
* other countries. © 2010 Wizards. All Rights Reserved.
* Addictedtoedh.com is not affiliated with Wizards of the Coast.
*
*/
var aUrls = new Array();
aUrls[0] = ["magiccards.info", "name"];
aUrls[1] = ["magiccards.info", "q"];
aUrls[2] = ["store.tcgplayer.com", "productname"];
aUrls[3] = ["store.tcgplayer.com", "name"];
aUrls[4] = ["gatherer.wizards.com", "multiverseid"];
aUrls[5] = ["gatherer.wizards.com", "name"];
aUrls[6] = ["magiccards.info", "card"];
aUrls[7] = ["m-tg.co/c/", ""];
var body;
var apiKey = "ADD2EDH";
var timer;
var link = "";
var posx; var posy;
var mouseX, mouseY;
// Work out name of card
function getname(element, link) {
if (element.hasAttribute("anum")) {
if (aUrls[element.getAttribute("anum")][1] == "") {
return (link.split(aUrls[element.getAttribute("anum")][0])[1]).split("?")[0];
} else {
var a2 = "";
var a1 = link.split("?")[1];
a1 = a1.split("&");
for (var k = 0; k < a1.length; k++) {
a2 = a1[k].split("=");
//console.log(a2[0].toLowerCase() + "," + aUrls[element.getAttribute("anum")][1].toLowerCase());
if (a2[0].toLowerCase() == aUrls[element.getAttribute("anum")][1].toLowerCase()) { return a2[1]; }
}
}
}
return false;
}
// "textify" card name string
function cleanname(name) {
if (name != false) {
return name.replace(/’/g, "'").replace(/\?/g, "").replace(/\%20/g, " ").replace(/\s+/g, " ");
} else {
return false;
}
}
// is number?
function isNumber(o) {
if (o != false) {
return !isNaN(o - 0) && o !== null && o.replace(/^\s\s*/, '') !== "" && o !== false;
} else {
return false;
}
}
// hide autocard
function hideCard() {
aC = document.getElementById("autocard-container");
aC.style.visibility = "Hidden";
}
// show autocard
function showCard(element) {
var imgSrc = "";
var name = false;
var mvid = false;
if (link != element.href) {
link = element.href;
name = cleanname(getname(element, link));
if (isNumber(name)) { mvid = name; name = false; }
console.log(name);
}
aC = document.getElementById("autocard-container");
//Check for mouse position, edges of page
if (body != null && body.offsetWidth != null && body.offsetWidth < mouseX + 239) {
aC.style.left = body.offsetWidth - 216 + "px";
} else {
aC.style.left = mouseX + 5 + "px";
}
if (body != null && body.offsetHeight != null && (body.offsetHeight + window.pageYOffset) < (mouseY + 329)) {
aC.style.top = body.offsetHeight + window.pageYOffset - 306 + "px";
} else {
aC.style.top = mouseY + 0 + "px";
}
aC.onmouseover = function () {
stopTimer();
}
aC.onmouseout = function () {
startTimer();
}
if (name != false) {
aC.innerHTML = "";
} else {
if (mvid != false) {
aC.innerHTML = "";
}
}
aC.style.visibility = "Visible";
}
// Where is the mouse?
function getMousePos(e) {
if (!e)
var e = window.event || window.Event;
if ('undefined' != typeof e.pageX) {
mouseX = e.pageX;
mouseY = e.pageY;
}
else {
mouseX = e.clientX + document.body.scrollLeft;
mouseY = e.clientY + document.body.scrollTop;
}
}
// Time since mouse was over link/autocard
function startTimer() {
timer = setTimeout("hideCard()", 700);
}
// Mouse is back over the autocard/link
function stopTimer() {
clearTimeout(timer);
}
// This is for monitoring the mouse position
if (window.Event && document.captureEvents)
document.captureEvents(Event.MOUSEMOVE);
// mouse handler
document.onmousemove = getMousePos;
// This is the container for the autocard
aCont = document.createElement("div");
aCont.id = "autocard-container";
aCont.setAttribute("class", "card");
aCont.setAttribute("style", "visibility:hidden; position:absolute; top:10px; left:5px; z-index:100; padding:0px;");
// Inserting placeholder into the body
body = document.getElementsByTagName('body')[0];
body.appendChild(aCont);
// Get all the elements in the document
var allAnchors = document.getElementsByTagName("a");
// iterate through them
for (var i = 0; i < allAnchors.length; i++) {
var elem = allAnchors[i];
//iterate through the url list
for (var j = 0; j < aUrls.length; j++) {
// check if link matches url from list
if ((aUrls[j][1] == "" && elem.href.toLowerCase().indexOf(aUrls[j][0].toLowerCase()) != -1) || (elem.href.toLowerCase().indexOf(aUrls[j][0].toLowerCase()) != -1 && (elem.href.toLowerCase().indexOf("&" + aUrls[j][1].toLowerCase()) != -1 || elem.href.toLowerCase().indexOf("?" + aUrls[j][1].toLowerCase()) != -1))) {
//mark which item on list (don't have to repeat later)
elem.setAttribute("anum", j);
//open popup
elem.onmouseover = function (evt) {
var sourceElement;
if (document.all) {
sourceElement = evt.srcElement; // for IE
} else {
sourceElement = evt.target;
}
stopTimer();
showCard(sourceElement);
}
//close popup
elem.onmouseout = function () {
startTimer();
}
}
}
}