﻿Sarissa.callRemote = function(sFromUrl, oTargetElement, callback, id, rating, skipCache) {
	try{
		//Sarissa.updateCursor(oTargetElement, "wait");
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", sFromUrl, true);
		function _handler() {
			if (xmlhttp.readyState == 4) {
				if (callback) {
					if (xmlhttp.responseText != null) {
						callback(id, rating, xmlhttp.responseText);
					};
				};
			};
		};
		xmlhttp.onreadystatechange = _handler;
		if (skipCache) {
			 var oldage = "Sat, 1 Jan 2000 00:00:00 GMT";
			 xmlhttp.setRequestHeader("If-Modified-Since", oldage);
		};
		xmlhttp.send("");
	}
	catch(e){
		Sarissa.updateCursor(oTargetElement, "auto");
		throw e;
	};
};

function Vote(id)
{
	var el = document.getElementById(id+"dropdown");
	var ret = el.value;
	Sarissa.callRemote("http://www.rte.ie/dynamic/rating/playerratings.aspx?url=" + id + "&rating=" + ret, document.getElementById(id+"dropdownHolder"), UpdateResults, id, ret) ;
}

function initScore(id)
{
	Sarissa.callRemote("http://www.rte.ie/dynamic/rating/playerratings.aspx?url=" + id, document.getElementById(id+"dropdownHolder"), initResults, id, "0") ;
}

function initResults(id, rating, val)
{
	var el = document.getElementById(id+"Score");
	var a = val.split(",");	
	
	if(a && !isNaN(parseInt(a[0])) && !isNaN(parseInt(a[1]))) {
		el.innerHTML = Math.round(a[0]*10/a[1])/10;
	}
}

function UpdateResults(id, rating, val)
{
	var el = document.getElementById(id+"Score");
	var drop = document.getElementById(id+"dropdownHolder");
	var a = val.split(",");	
	
	if(a && !isNaN(parseInt(a[0])) && !isNaN(parseInt(a[1]))) {
		el.innerHTML = Math.round(a[0]*10/a[1])/10;
		drop.innerHTML = "Rating: "+rating;
	}
}


