var holderCast = null;
var highlightedArray = new Array();
function showPage(who) {
	var veil = document.getElementById("veil");
	if (veil.style.visibility != "hidden") {
		return true;
	}
	floater.style.display = "none";
	if (!window.XMLHttpRequest) {
		return false;
	}
	suspendAlert();
	newTargetName = who;
	var xmlhttp = getXmlHttp();
	xmlhttp.open("GET", "/cast/"+who+"/ajax_request=");
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
			var matches = xmlhttp.responseText.split("\t");
			if (matches) {
				if (matches[0] == "not found") {
					raiseAlert("We're sorry, but that character wasn't found!");
					replaceTarget();
					clearVeil();
				} else {
					castIcon = new Image();
					if (!window.opera) {
						
						castIcon.onload = function() {

							floater.innerHTML = "<div class=\"center\"><img id=\"cast_icon\" src=\"\" alt=\""+matches[0]+"\" /><br /><i>"+matches[2]+"</i><h2><i>"+matches[0]+"</i></h2>"+matches[3]+"<br /><br /><img class=\"clickable\" src=\"/client_include/images/x.gif\" alt=\"Close\" title=\"Close\" onclick=\"closeCastPage();\" /></div>";
							document.getElementById("cast_icon").src = castIcon.src;

							fadeIn(floater.id, 70, null, 5);
						}
					} else {
						castIcon.onload = function() {
							
							floater.innerHTML = "<div class=\"center\"><img id=\"cast_icon\" src=\""+matches[1]+"\" alt=\""+matches[0]+"\" /><br />\"<i>"+matches[2]+"</i>\"<h2><i>"+matches[0]+"</i></h2>"+matches[3]+"<br /><br /><img class=\"clickable\" src=\"/client_include/images/x.gif\" alt=\"Close\" title=\"Close\" onclick=\"closeCastPage();\" /></div>";
							floater.style.visibility = "visible";
							floater.style.display = "block";
							setOpacity(floater, 100);
						}
					}
					castIcon.src = matches[1];
				}
			}
		}
	}
	xmlhttp.send(null);
	//setOpacity(document.body, 100);

	openVeil("closeCastPage();", true);
	
	return true;
}

function closeCastPage() {
	replaceTarget();
	closeVeil();
}

addWindowOnload(function() {
	var ensembleImages = getElementsByClassName(document.getElementById("ensemble_images_wrapper"), "*", "ensemble_image");

	for (var i =0; i < ensembleImages.length; i++) {
		
		var iconLinks = ensembleImages[i].getElementsByTagName("a");
		for (var i2 =0; i2 < iconLinks.length; i2++) {
			if (iconLinks[i2].className == "dark_overlay") {
				var icon = iconLinks[i2].getElementsByTagName("img")[0];
				setOpacity(icon, 50);

				//iconLinks[i].style.left = findPosX(document.getElementById((icon.id.replace('_dark', '')))) +"px";
				//iconLinks[i].style.top = findPosY(document.getElementById((icon.id.replace('_dark', '')))) +"px";
				icon.style.display = "block";
				
			}
		}
	}
	clearVeil();
	var windowLocation = window.location.toString();
	var characterDirectLinkMatches = windowLocation.match(/#(.+)$/);

	if (characterDirectLinkMatches) {
		if (characterDirectLinkMatches[1]) {
			showPage(characterDirectLinkMatches[1]);
		}
	}

});

function deselect(obj) {
	if (lastOpacity[obj.id] != null && lastOpacity[obj.id] != undefined) {
		var startingOpacity = lastOpacity[obj.id];
	} else {
		var startingOpacity = 0;
	}
	var veil = document.getElementById("veil");
	if (veil.style.visibility == "visible") {
		haltFade(obj.id);
		setOpacity(obj, 50);
	} else {
		fadeIn(obj.id, startingOpacity, null, null, 50);
		floater.style.display = "none";
		floater.style.visibility = "hidden";
        floater.style.borderColor = "transparent";
        floater.style.borderWidth = "0px";
        floater.style.borderStyle = "none";
	}
	
}

function highlight(obj, e) {
	if (e == null) {
		e = window.event;
	}
	
	if (lastOpacity[obj.id] != null && lastOpacity[obj.id] != undefined) {
		var startingOpacity = lastOpacity[obj.id];
	} else {
		var startingOpacity = 50;
	}
	
	var veil = document.getElementById("veil");
	if (veil.style.width != "100%") {
		haltFade(floater.id);
		fadeOut(obj.id, startingOpacity, false, 0, 7, null, null, true);
		var cursorPositions = scavengeCursorPosition(e);
		window.setTimeout("showLabel('"+obj.alt.replace("'", "\\'")+"', "+cursorPositions["x"]+", "+cursorPositions["y"]+")", 5);
	}
}

function showLabel(text, posx, posy) {
	floater.innerHTML = text;
	posy += 10;
	floater.style.left = posx+"px";
	floater.style.top = posy+"px";
	floater.style.position = "absolute";
	floater.style.height = "auto";
	floater.style.width = "auto";
	floater.style.visibility = "visible";
	floater.style.display = "block";
	floater.style.backgroundColor = "#8F7C79";
	floater.style.color = "#FFFFFF";
    floater.style.borderColor = "#7F706D";
    floater.style.borderWidth = "2px";
    floater.style.borderStyle = "solid";
	floater.style.padding = "2px";

}
