function AutoCardGatherer(cardname, set) {
	cardname = cardname.replace(/ /g, "_");
	cardname = cardname.replace(/\'/g, "[");
	params = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1, width=740, height=670";
	win = window.open("http://ww2.wizards.com/gatherer/CardDetails.aspx?name="+cardname, 'autocard', params);
}

function AutoCardMagicCardsInfo(cardname, set) {
	if (set == undefined) set = '';
	uri = cardname.replace(/\//g,'[') + '|' + set;
	params = 'width=950, height=500, scrollbars=1, resizable=1, menubar=0, status=0';
	win = window.open('http://www.magiccards.info/autocard.php?card=' + uri, 'autocard', params);
}

function AutoCardWizards(cardname, set) {
	params = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=0, resizable=0, width=450, height=400";
	win = window.open("http://www.wizards.com/magic/autocard.asp?name="+cardname, 'autocard', params);
}

//

function getRandId () {
	var rn = $A($R(1, 9));
	var rl = $A($R('a', 'z'));
	var i = 10;
	var id = '';

	for (; i > 0; --i)
		id += rl[getRandom (0, rl.length)] + rn[getRandom (0, rn.length)];

	return id;
}

function getRandom (min, max) {
	return Math.floor (Math.random() * max + min);
}

function setPopupable (idLink, idImgContainer, idImg) {
	var link = $(idLink);
	var imgContainer = $(idImgContainer);

	Event.observe (window, 'load', function () {
		link.observe ('mouseover', showPopup.curry (link, imgContainer, idImg));
	});
}

function showPopup (link, imgContainer, idImg, e) {
	var tp = window.scrollY + e.clientY - 200;
	var lf = e.clientX + 30;
	var img = $(idImg);

	if (tp < 0)
		tp += 200;

	imgContainer.writeAttribute ({style: 'left: '+lf+'px; top: '+tp+'px'});
	//imgContainer.setStyle ({left: lf+'px', top: tp+'px'});
	$$ ('.cinfo').invoke ('hide');

	imgContainer.show ();
	img.fire ('widget:view');

	link.observe ('mouseout', hidePopup.curry (imgContainer, link));
}

function hidePopup (imgContainer, link, e) {
	if (!e.target.descendantOf (imgContainer)) {
		imgContainer.hide ();
		link.stopObserving ('mouseout', hidePopup);
	}

	e.stop ();
}

function setInfos (line, idImgContainer, idLinkContainer, idBefore, idImg) {
	var url = './wl/wl-functions.php';

	new Ajax.Updater (idImgContainer, url, {
		method: 'get',
		parameters: {content: line, idImgContainer: idImgContainer, linkContainer: idLinkContainer, beforeContainer: idBefore, idImg: idImg},
		evalScripts: true
	});
}

function makeCard (line) {
	set = '';
	uri = 'http://www.magiccards.info/autocard.php?card=' + line.replace(/\//g,'[') + '|' + set;
	params = 'width=950, height=500, scrollbars=1, resizable=1, menubar=0, status=0';
	document.write ("<div onClick=\"window.open(\"http://www.wizards.com/magic/autocard.asp?name=Tarmogoyf\", 'autocard', 'width=950, height=500, scrollbars=1, resizable=1, menubar=0, status=0');\">" + line + "</div>");
}

function makeDeck (deck) {
	var arr = deck.split ("<br />");

	arr.each (function (line) {
		if (!line.blank ())
			makeCard (line);

		document.write ("<br />");
	});
}
