// Javascript for the Winners Page

// Handler in case we turn off this partner. We don't want to display this page or
// support this contest
// Implement '''handleInactivePartner''' if you want to inform the user of this state.
// Parameter passed in is the pid value in the java script include for photocontest.js
function handleInactivePartner(partnerId) {
	alert("Do your error handling if the partner is not active. Partnerid: " + partnerId);
}

function handleContestReady() {
	getLatest();
}

function getLatest() {
	getRecentEntries(11);
}

// Generic routine to illustrate the error map
function handleError(errors) {
	if (errors.NO_ENTRIES != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "There are no entries in this contest";
	} else if (errors.INVALID_CONTEST_ID != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "Contest id value is not a number";
	} else if (errors.CONTEST_ID_NOT_FOUND != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "Contest id could not be found";
	} else if (errors.CONTEST_NOT_ACTIVE != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "This contest is no longer active";
	} else if (errors.INVALID_PARTNER_ID != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "Partner id value is not a number";
	} else if (errors.PARTNER_ID_NOT_FOUND != undefined) {
		document.getElementById("recent_entries_out").innerHTML = "Partner id could not be found";
	} else {
		for (var i in errors) {
			if (errors.hasOwnProperty(i)) {
				alert("Error: " + errors[i]);
			}
		}
	}
}

function handleGetRecentEntries(result) {
	if (result.status == "ERROR") {
		handleError(result.errors);
	} else {
		var entries = result.data.entries;
		var length = 0;
		if (entries != undefined) {
			length = entries.length;
		}
		var entriesHtml = "";
		for (var i = 0; i < length; i++) {
			var entry = entries[i];
			entriesHtml += "<div class=\"imageholder\" id=\"imagediv"+i+"\" style=\"background-image: url('";
			entriesHtml += entry.thumbUrl;
			entriesHtml += "');\" ><a href=\"entries.html?selectedentryid=" + entry.id + "\" target=\"_parent\" ><img class=\"entryimage\" src=\"images/1x1.gif\" height=\"68\" width=\"68\" /></a></div>&nbsp;";				}
		if (length == 0) {
			entriesHtml = "Should have been caught as an error. No entries in this contest";
		}
		document.getElementById("recent_entries_out").innerHTML = entriesHtml;
	}
	// check every 5 seconds
	setTimeout("getLatest()", 5000);
}
