﻿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 RatingVote(id)
{
	var el = document.getElementById(id+"dropdown");
	var ret = el.value;
	alert
	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;
	}
}


Sarissa.callRemote2 = function(sFromUrl, oTargetElement, callback, skipCache) {
	try{
		//Sarissa.updateCursor(oTargetElement, "wait");
		var xmlhttp = new XMLHttpRequest();
		xmlhttp.open("GET", sFromUrl, true);
		function _handler() {
			if (xmlhttp.readyState == 4) {
				if (callback) {
					callback(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, ans)
{
	var ret = ans=='yes' ? 1 : (ans=='no' ? 2 : 0)
	Sarissa.callRemote2("http://www.rte.ie/dynamic/rating/guidevote.aspx?url=" + id + "&rating=" + ret, document.getElementById("shouldPlay_yn"), ShowResults) ;
}

function ShowResults(val)
{
	var a = val.split(",");
	var res = document.getElementById("shouldPlay_yn");

	if(res)
	{
		res.innerHTML = "";
		
		if(a && !isNaN(parseInt(a[0])) && !isNaN(parseInt(a[1])) && !isNaN(parseInt(a[2])))
		{
			if(a[2] > 0)
			{
				res.innerHTML = "Yes: " + Math.round((a[0]/(a[2]-a[1])) * 100) + "% &nbsp; No: " + Math.round((a[1]/(a[2]-a[1])) * 100) + "%";
			}
		}
	}
}



function homeSwitch(tab) {
	if (tab == 'news') {
		document.getElementById("homeSwitchNav_news").className = "on";
		document.getElementById("homeSwitch_news").style.display = "block";
		document.getElementById("homeSwitchNav_av").className = "";
		document.getElementById("homeSwitch_av").style.display = "none";
	}
	else if (tab == 'av') {
		document.getElementById("homeSwitchNav_av").className = "on";
		document.getElementById("homeSwitch_av").style.display = "block";
		document.getElementById("homeSwitchNav_news").className = "";
		document.getElementById("homeSwitch_news").style.display = "none";
	}
}




