var host = 'http://www.mtgaddict.net';
var mtga_popup_cache = new Array();
var mtga_popup_timeouts = new Array();
var mtga_ajax = null;
var mtga_init = false;


var _link = document.createElement("script");
_link.type = "text/javascript";
_link.src = host+"/js/overlib.js";
//_link.innerHTML = "<!-- overLIB (c) Erik Bosrup -->";
var head = document.getElementsByTagName('head')[0];
head.appendChild(_link);


/**
 * Function to emulate document.getElementById
 *
 * @param	string	Object ID
 * @return	mixed	null if not found, object if found
 */
function fetch_object(idname) {
	if (document.getElementById) {
		return document.getElementById(idname);
	} else if (document.all) {
		return document.all[idname];
	} else if (document.layers) {
		return document.layers[idname];
	} else {
		return null;
	}
}


/**
 * Function to show or hide a html node
 *
 * @param	string	Object ID
 * @return	boolean	false if object not found
 */
function toggle_display(idname) {
	var obj = fetch_object(idname);


	if (obj) {
		if (obj.style.display == "none") {
			obj.style.display = "";
		} else {
			obj.style.display = "none";
		}
	}

	return false;
}


function mtga_cardpopup(name) {
	if ( !mtga_init) {
		var _link = document.createElement("link");
		_link.rel = "stylesheet";
		_link.type = "text/css";
		_link.href = host+"/style/mtgalib.css";
		var head = document.getElementsByTagName('head')[0];
		head.appendChild(_link);

		mtga_init = true;
	}

	if (mtga_popup_cache[name]) {
		var item = mtga_popup_cache[name];
		var obj = fetch_object('overDiv');
		if (obj) {
//			obj.innerHTML = item;
nd();
overlib(item, VAUTO, HAUTO, FULLHTML, STICKY);
		}
		return true;
	} else {
//	alert('no cache');
/*		if (mtga_ajax==null) {
			mtga_ajax = createRequestObject();
		}

		mtga_ajax.open('get', host+'/ws/card?input='+name+'&html&limit=1', false);
		mtga_ajax.send(null);
		item = mtga_ajax.responseText;
		mtga_popup_cache[name] = item;
*/		crossdomain_request(name);
		return false;
	}

/*	obj = fetch_object('overDiv');
	if (obj) {
		obj.innerHTML = item;
	}*/
}


function createRequestObject() {
	var http;


	if(window.XMLHttpRequest) { // Mozilla, Safari, ...
	    http = new XMLHttpRequest();
	} else if(window.ActiveXObject) { // Internet Explorer
	    http = new ActiveXObject("Microsoft.XMLHTTP");
	}

	return http;
}


function crossdomain_request(name) {
//alert('requesting');
	var s = document.createElement("script");
	s.type = "text/javascript";
	s.src=host+'/ws/card?name='+name+'&html&callback=mtga_callback';
	document.body.appendChild(s);
}


function mtga_callback(json) {
	mtga_popup_cache[json.name] = json.responseText;
	var obj = fetch_object('overDiv');
	if (obj) {
//		obj.innerHTML = mtga_popup_cache[json.name];
nd();
overlib(mtga_popup_cache[json.name], VAUTO, HAUTO, FULLHTML, STICKY);
	}
}


function _nd() {
	var obj = fetch_object('overDiv');
	obj.parentNode.removeChild(obj);
}


function mtga_show_popup(name, delay) {
	var timeout = window.setTimeout('overlib(\'<img src=\\\''+host+'/images/lc.gif\\\' alt=\\\'Chargement...\\\' />\', HAUTO, VAUTO, FULLHTML, STICKY);mtga_cardpopup(\''+name.replace(/\'/g,"\\'")+'\');',delay);
	mtga_popup_timeouts[name] = timeout;
}

function mtga_hide_popup(name) {
	if ( mtga_popup_timeouts[name]) {
		window.clearTimeout(mtga_popup_timeouts[name]);
		_nd();
	} else {
		_nd();
	}
}

function MTGACardPopup() {
	this.show = mtga_show_popup;
	this.hide = mtga_hide_popup;
}

function addslashes(str) {
	str=str.replace(/\'/g,'\\\'');
	str=str.replace(/\"/g,'\\"');
	str=str.replace(/\\/g,'\\\\');
	str=str.replace(/\0/g,'\\0');

	return str;
}

function stripslashes(str) {
	str=str.replace(/\\'/g,'\'');
	str=str.replace(/\\"/g,'"');
	str=str.replace(/\\\\/g,'\\');
	str=str.replace(/\\0/g,'\0');

	return str;
}