/*
	Roku.Util.Shared
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };
	
if(typeof(Roku.Util) == "undefined")
	Roku.Util = {};
	
Roku.Util.Shared =
{
	pcPlayerTarget			: "pcplayer",
	pcPlayerUrl				: "",
	pcPlayerWindow			: null
};

Roku.Util.Shared.getAttribute = function(station, attribute)
{
	var value = "";
	if(station)
		if(typeof(station.getAttribute) == "function")
			value = station.getAttribute(attribute);
		else
			value = station[attribute];
	if(!value)
		value = "";
	return value;
}


Roku.Util.Shared.playStation = function(ev, stationElement, table, field, id, station_title, station_url)
{
	if(!id)
		id = this.getAttribute(stationElement, "station_id");

	if(id && !table)
		table = this.getAttribute(stationElement, "station_table");
	if(id && !table)
		table = "stations";
	
	if(id && !field)
		field = this.getAttribute(stationElement, "station_field");
	if(id && !field)
		field = "id";
	
	if(!station_title)
		station_title = this.getAttribute(stationElement, "station_title");
	
	if(!station_url)
		station_url = this.getAttribute(stationElement, "station_url");

	if(!id && !station_url)
		return;	// can't play anything

	var scrollX = 0, scrollY = 0;
	if(ev && ev.offsetX && ev.offsetY)
	{
		// IE
		var element = Roku.Util.srcEventElement(ev);
		var rectElement = (Roku.Util.CompareNoCase(element.tagName, "span") || Roku.Util.CompareNoCase(element.tagName, "a")) ? element.offsetParent : element;
		var rect = Roku.Util.elementRect(rectElement);
		  
		scrollX = rect.left - (ev.clientX - ev.offsetX);
		scrollY = rect.top - (ev.clientY - ev.offsetY);
	}
	else if(ev && ev.pageX && ev.pageY)
	{
		// Mozilla
		scrollX = ev.pageX - ev.clientX;
		scrollY = ev.pageY - ev.clientY;
	}
	
	var alignRect = Roku.Util.elementRect(stationElement);
	var x = alignRect.left - scrollX + (ev ? (ev.screenX - ev.clientX) : 0); 
	var y = alignRect.top + alignRect.height - scrollY + (ev ? (ev.screenY - ev.clientY) : 0); 
	var w = 250;
	var h = 240;
	
	if(!this.pcPlayerUrl)
		this.pcPlayerUrl = this.pcPlayerTarget + ".php";
	

	var url = this.pcPlayerUrl;

	var url_params = ""
	if(table)
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "table=" + table;
	}
	
	if(field)
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "field=" + field;
	}
	
	if(id)
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "id=" + id;
	}
	
	if(station_title)
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "title=" + encodeURI(station_title);
	}

	if(station_url)
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "url=" + encodeURIComponent(station_url);
	}

	if(this.getAttribute(stationElement, "single_url"))
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "single_url=1";
	}

	if(Roku.Util.CompareNoCase(window.navigator.appName, "Microsoft Internet Explorer"))
	{
		h += 20;	// IE normally does not hide statusbar.
	}
	else
	{
		if(0 < url_params.length)
			url_params += "&"
		url_params += "embed=true";
	}
	
	
	if(0 < url_params.length)
		url += ("?" + url_params);
	
	//try { this.pcPlayerWindow.close(); } catch(e) {}
	this.pcPlayerWindow = window.open(url, this.pcPlayerTarget, "width=250, height=" + h + ", menubar=no, toolbar=no, location=no, directories=no, status=no, resizable=no, scrollbars=no"); //"left=" + x + ", top=" + y + 
	this.pcPlayerWindow.focus();
}

Roku.Util.Shared.checkConfirmed = function(ev)
{
	var need_confirm = Roku.DialogRegistrationRequired ? document.getElementById(Roku.DialogRegistrationRequired.id) : null;
	if(!need_confirm)
		return true;

	if(ev)
		Roku.Util.eventCancel(ev);
	
	Roku.DialogRegistrationRequired.do_modal(ev);
	return false;
}

