//Setup unique xmlHTTP object 
try	{	// Firefox, Opera 8.0+, Safari
	xmlHttpCard=new XMLHttpRequest(); 
}catch (e){
	// Internet Explorer 
	try{
		xmlHttpCard=new ActiveXObject("Msxml2.XMLHTTP");
	}catch (e){

		try{
			xmlHttpCard=new ActiveXObject("Microsoft.XMLHTTP");
		}catch (e){
		}
	}
}
	
function viewCard(evt, thespan, cardname){

	//keep a string with proper wording
	var searchname = cardname
	//replaces all whitespace with "," - then all ",,,,," (n # of commas) with "," - then removes all leading commas - then removes all trailing commas
	cardname = cardname.replace(/[\s\-]+/g, '_').replace(/[',]+/g, '').replace(/[Ææ]/g, 'Ae').replace(/[áàâ]/g, 'a').replace(/[é]/g, 'e').replace(/[í]/g, 'i').replace(/[ö]/g, 'o').replace(/[úû]/g, 'u').replace(/[^0-9A-Za-z_]+/g, '')
	var	xPos = evt.clientX
	var	yPos = evt.clientY
	var posH = thespan.offsetHeight
	var posW = thespan.offsetWidth
	var adjY = 0-posH
	var adjW = 0
	for( var posX = 0, posY = 0; thespan; thespan = thespan.offsetParent ) {
    	posX += thespan.offsetLeft;
		posY += thespan.offsetTop;
		
    }
	//alert(document.documentElement.clientWidth  + " | " + xPos + " | " + (document.documentElement.clientWidth - xPos))
	if(document.documentElement.clientHeight - yPos - posH < 285){
		adjY = -285
	}
	
	if(document.documentElement.clientWidth - xPos - posW < 200){
		adjW = -200-posW
	}
	
	if(!(document.getElementById("cardshow_div"))){
		NewDiv = document.createElement("div");
		NewDiv.setAttribute("id","cardshow_div")
		NewDiv.height = 285
		NewDiv.width = 200
		NewDiv.style.zIndex = 1
		NewDiv.style.visibility = "hidden"
		NewDiv.style.position = "absolute"
		NewDiv.style.left = (posX + posW + adjW).toString() +  "px"
		NewDiv.style.top = (posY + posH + adjY).toString() +  "px"
		document.getElementsByTagName("body")[0].appendChild(NewDiv);
		NewIMG = document.createElement("img");
		NewIMG.setAttribute("id","cardshow_img")
		NewIMG.src = "http://www.wizards.com/global/images/magic/general/" + cardname + ".jpg"
		NewDiv.appendChild(NewIMG);
		NewDiv.style.visibility = "visible"
		
		NewDiv = document.createElement("div");
		NewDiv.setAttribute("id","carddata3_div")
		NewDiv.height = 29
		NewDiv.width = 198
		NewDiv.style.zIndex = 2
		NewDiv.style.visibility = "hidden"
		NewDiv.style.position = "absolute"
		NewDiv.style.border = "1px solid #000000"
		NewDiv.style.filter = "alpha(opacity=80)"  
		NewDiv.style.opacity = 0.8
		NewDiv.style.paddingLeft ="2px"
		NewDiv.style.fontSize = "10px"
		NewDiv.style.fontFamily = "arial,sans-serif"
		NewDiv.style.color="#666666"
		NewDiv.style.backgroundColor="#666666"
		NewDiv.style.left = (posX + posW + adjW + 2).toString() +  "px"
		NewDiv.style.top = (posY + posH + adjY+256).toString() +  "px"
		document.getElementsByTagName("body")[0].appendChild(NewDiv);
		
		NewDiv = document.createElement("div");
		NewDiv.setAttribute("id","carddata2_div")
		NewDiv.height = 28
		NewDiv.width = 200
		NewDiv.style.zIndex = 3
		NewDiv.style.visibility = "hidden"
		NewDiv.style.position = "absolute"
		NewDiv.style.paddingLeft ="5px"
		NewDiv.style.paddingTop ="1px"
		NewDiv.style.fontSize = "10px"
		NewDiv.style.fontFamily = "arial,sans-serif"
		NewDiv.style.color="#000000"
		NewDiv.style.left = (posX + posW + adjW).toString() +  "px"
		NewDiv.style.top = (posY + posH + adjY+256).toString() +  "px"
		document.getElementsByTagName("body")[0].appendChild(NewDiv);		
		
		NewDiv = document.createElement("div");
		NewDiv.setAttribute("id","carddata_div")
		NewDiv.height = 28
		NewDiv.width = 200
		NewDiv.style.zIndex = 4
		NewDiv.style.visibility = "hidden"
		NewDiv.style.position = "absolute"
		NewDiv.style.paddingLeft ="4px"
		NewDiv.style.fontSize = "10px"
		NewDiv.style.fontFamily = "arial,sans-serif"
		NewDiv.style.color="#FFFFFF"
		NewDiv.style.left = (posX + posW + adjW).toString() +  "px"
		NewDiv.style.top = (posY + posH + adjY+256).toString() +  "px"
		document.getElementsByTagName("body")[0].appendChild(NewDiv);

		
	}else{
		document.getElementById("cardshow_img").src = ""
		document.getElementById("cardshow_img").src = "http://www.wizards.com/global/images/magic/general/" + cardname + ".jpg"
		document.getElementById("cardshow_div").style.left = (posX + posW + adjW).toString() + "px"
		document.getElementById("cardshow_div").style.top = (posY + posH + adjY).toString() + "px"
		document.getElementById("carddata_div").style.left = (posX + posW + adjW).toString() + "px"
		document.getElementById("carddata_div").style.top = (posY + posH + adjY+256).toString() + "px"
		document.getElementById("carddata2_div").style.left = (posX + posW + adjW).toString() + "px"
		document.getElementById("carddata2_div").style.top = (posY + posH + adjY+256).toString() + "px"
		document.getElementById("carddata3_div").style.left = (posX + posW + adjW + 2).toString() + "px"
		document.getElementById("carddata3_div").style.top = (posY + posH + adjY+256).toString() + "px"
	}
	
	document.getElementById("carddata_div").innerHTML = "Loading...<br/> "
	document.getElementById("carddata2_div").innerHTML = "Loading...<br/> "
	document.getElementById("carddata3_div").innerHTML = "Loading...<br/> "
	xmlHttpCard.abort()
	var dseason = new Date();
	var tseason = dseason.getFullYear()
	var params = "CardName=" + escape(searchname) + "&Season=" + tseason
	xmlHttpCard.open("POST","/tournaments/ajax_get_carddata.asp",true);
	xmlHttpCard.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xmlHttpCard.setRequestHeader("Content-length", params.length);
	xmlHttpCard.setRequestHeader("Connection", "close");
	xmlHttpCard.onreadystatechange=function(){
		if(xmlHttpCard.readyState==4){
			var carddata = xmlHttpCard.responseText.split("|")
			if(carddata[3] === undefined){
			}else{
				document.getElementById("carddata_div").innerHTML = "Season: " + tseason + " Rankings<br/>Total: #" + carddata[3] + "&nbsp; Appear: #" + carddata[4] + "&nbsp; Deck: #" + carddata[5]
				document.getElementById("carddata2_div").innerHTML = "Season: " + tseason + " Rankings<br/>Total: #" + carddata[3] + "&nbsp; Appear: #" + carddata[4] + "&nbsp; Deck: #" + carddata[5]
				document.getElementById("carddata3_div").innerHTML = "Season: " + tseason + " Rankings<br/>lTotal: #" + carddata[3] + "&nbsp; Appear: #" + carddata[4] + "&nbsp; Deck: #" + carddata[5]
			}
		}
	}
	xmlHttpCard.send(params);
	
	document.getElementById("carddata_div").style.visibility = "visible"
	document.getElementById("carddata2_div").style.visibility = "visible"
	document.getElementById("carddata3_div").style.visibility = "visible"
	document.getElementById("cardshow_div").style.visibility = "visible"
}
function hideCard()
{
	document.getElementById("cardshow_div").style.visibility = "hidden"
	document.getElementById("carddata_div").style.visibility = "hidden"
	document.getElementById("carddata2_div").style.visibility = "hidden"
	document.getElementById("carddata3_div").style.visibility = "hidden"
}
