var assetDir = "";

function voteChange(item)
{
	try {
		var ourId = document.getElementById("id_" + item).value;
		var ourRank = document.getElementById("rank_" + item).value;
		var check;
		var col;
		var medal;
		
		switch(ourRank) {
		case "1":
			col = "#fff";
			medal = "trophy1.gif";
			break;
		case "2":
			col = "#ccc";
			medal = "trophy2.gif";
			break;
		case "3":
			col = "#999";
			medal = "trophy3.gif";
			break;
		default:
			col = "#333";
			medal = "shim.gif";
			break;
		}

		$(document.getElementById("wrapper_" + item)).css("border-color", col);
		$(document.getElementById("medal_" + item)).attr("src", assetDir + "/" + medal);
		
		if(ourRank != "0") {
			for(i = 1; check = document.getElementById("id_" + i); i++) {
				id = document.getElementById("id_" + i).value;
				rank = document.getElementById("rank_" + i).value;
			
				if(ourRank == rank && id != ourId) {
					document.getElementById("rank_" + i).selectedIndex = 0;
					$(document.getElementById("wrapper_" + i)).css("border-color", "#333");
					$(document.getElementById("medal_" + i)).attr("src", assetDir + "/shim.gif");
				}
			}
		}
	} catch(e) {
		alert("Vote change failed - something went wrong!");
	}
}

function initVoteStyles()
{
	var check;
	
	for(j = 1; check = document.getElementById("id_" + j); j++) {
		voteChange(j);
	}

}


function validateVotes()
{
	try {
		
		document.getElementById("first").value = -1;
		document.getElementById("second").value = -1;
		document.getElementById("third").value = -1;
		
		for(i = 1; check = document.getElementById("id_" + i); i++) {
			id = document.getElementById("id_" + i).value;
			rank = document.getElementById("rank_" + i).value;

			switch(rank) {
			case "1":
				document.getElementById("first").value = id;
				break;

			case "2":
				document.getElementById("second").value = id;
				break;

			case "3":
				document.getElementById("third").value = id;
				break;

			default:
				break;
			}
		}
		
		if(document.getElementById("first").value == -1 ||
		   document.getElementById("second").value == -1 ||
	           document.getElementById("third").value == -1) {
			alert("You haven't chosen three images to vote for yet.\n\nPlease rank three images then try again.");
			return false;
		}
		
		return true;
	} catch(e) {
		alert("Vote validation failed - something went wrong!");
		return false;
	}
}

function openPopup(url, name, width, height)
{
	newwindow=window.open(url, name, "width=" + width + ",height=" + height);
	
	if (window.focus)
		newwindow.focus();
	
	return false;
}

