var z=4;

function showPlayer(url)
{
	var win = window.open(url, "rteplayer", "height=560,width=860,status=yes,toolbar=no,menubar=no,location=no");
	win.focus();
}

function iAppear(id)
{
	document.getElementById(id).style.visibility = "visible";
	z++;
	document.getElementById(id).style.zIndex = z;
}


function cAppear(id)
{
	document.getElementById(id).style.visibility = "visible";
	z++;
	document.getElementById(id).style.zIndex = z;

}


var _url = "http://www.rte.ie/dynamic/rating/default.aspx?url=" + escape(location.href) + _getIdParamStr();

window.addEvent('domready', function() {
    new Request({
		url: _url,
        method: 'get',
        onSuccess: function(response){
			if($('starRating')) $('starRating').set('html',response);
		}
    }).send();
});

function Vote(i)
{
    new Request({
		url : _url + "&rating=" + i + "&service=7000000",
        method: 'get',
        onSuccess: function(response){
			$('starRating').set('html',response);
		}
    }).send();
}

function _getIdParamStr()
{
    var el = document.getElementsByName("DC.identifier");
    
    if(el.length > 0 && el[0].content)
    {
        el2 = el[0].content.split(":");
        if(el2.length == 3)
        {
        	return "&id=" + el2[2];
        }
    }
    
    return "";
}

function PrintPreview()
{
	var win = window.open();
	win.document.write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
	win.document.write("<html>");
	win.document.write("<head>");
	win.document.write("<link rel=\"stylesheet\" type=\"text/css\" href=\"/food/style/print.css\" />");
	win.document.write("</head>");
	win.document.write("<body>");
	win.document.write("<img src=\"/food/images/logo_printable_food.gif\">\n");
	win.document.write("<hr>");
	win.document.write(document.getElementById("primaryContent").innerHTML);
	win.document.write("<p><strong>Recipe from RT&Eacute; Food:<br /><a href=\"" + document.location + "\">"+document.location+"</a></strong></p>");
	win.document.write("<p><a href=\"javascript:window.print()\"><img src=\"http://www.rte.ie/images/icon_printable.gif\" border=\"0\"></a> <a href=\"javascript:window.print()\">print this page</a></p>");
	win.document.write("</body>");
	win.document.write("</html>");
	win.document.close();
}


//inject calculator into recipe page
window.addEvent('domready', function() {

		var el = $('RTE:Food:Ingredients');
		if(el)
		{
			el = el.getElement("h3");
			if(el)
			{
				var el3 = new Element('a').setProperty('href', 'javascript:void(window.open(\'\'+self.location+\'?view=print\'));').injectInside(el);
				el3.addClass("print_link");
				el3.set('text',"Print Recipe ");
				var el3 = new Element('img').setProperty('src', '/food//images/icons_print.gif').injectAfter(el3);

				//var el1 = new Element('a').setProperty('href', 'javascript:share()').injectInside(el);
				//el1.addClass("share_link");
				//el1.set('text',"Share Recipe ");
				//var el1 = new Element('img').setProperty('src', '/food//images/icons_share.gif').injectAfter(el1);

				var el2 = new Element('a').setProperty('href', 'javascript:openCalculator()').injectInside(el);
				el2.addClass("calc_link");
				el2.set('text',"Conversions ");
				var el2 = new Element('img').setProperty('src', '/food/images/calculator_icon.gif').injectAfter(el2);		
			}
		}
		loadRatings();
 });
 


function share()
{
	var win = MailToAFriend();
}



function openCalculator()
{
	var win = window.open("/food/conversion.html", "conversions", "height=560,width=690,status=yes,toolbar=no,menubar=no,location=no,scrollbars=yes");
	win.focus();
}

function loadRatings(){
	
	$$('div.featStars').each(function(div){
										
		var divId = div.getProperty('id');
		var docId = divId.substring(9, 19); 

		var pageUrl = "http://www.rte.ie"+$(divId).getParent().getElement('a[href$=html]').getProperty('href');
		
		var _url = "http://www.rte.ie/dynamic/rating/food_small.aspx?url=" + pageUrl + "&id=" + docId;
		
		var req = new Request.HTML({
			url: _url,					
			onSuccess: function(html) {
				$(divId).adopt(html);
				if ($(divId).getFirst().getStyle('width').toInt() != 0) {
					$(divId).setStyle('background', 'url(/food/images/stars_off_small.gif) no-repeat');
				}
			}
		}).send();
		
	});	
}
