/*
	Roku.PopupStation
*/

if(typeof(Roku) == "undefined")
    Roku = { classes : [] };

Roku.PopupStation =
{
	popupId				: "popup_station",
	contentClassName	: "content",
	queryKeyAttribName	: "query_key",
	entryClassName		: "entry",
	iconClassName		: "icon",
	textClassName		: "text",
	
	cmdIdPlayPC			: "cmdPlayPC",
	cmdIdPlaySoundbridge: "cmdPlaySoundbridge",
	cmdIdSaveAsPreset	: "cmdSaveAsPreset",
	cmdIdSaveAsFavorite	: "cmdSaveAsFavorite",
	cmdIdRemoveFavorite	: "cmdRemoveFavorite",
	cmdIdEdit			: "cmdEdit",
	cmdIdApprove		: "cmdApprove",
	cmdIdUnApprove		: "cmdUnApprove",
	cmdIdShowURLs		: "cmdShowURLs",
	cmdIdShowPlays		: "cmdShowPlays",
	cmdIdShowDetail		: "cmdShowDetail",
	
	popup				: null, 
	content				: null,
	queryKey			: ""

};

Roku.classes.push(Roku.PopupStation);

Roku.PopupStation.show = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var station = Roku.Util.parentClassElement(element, Roku.Station.stationClassName);
	
	if(!this.popup || !this.content || !station)
		return;

	var offsetX = 0;
	var stationRect = Roku.Util.elementRect(station);
	if(ev.offsetX && ev.offsetY)
	{
		// IE
		if(Roku.Util.CompareNoCase(element.tagName, "span"))
		{
			var parentRect = Roku.Util.elementRect(element.offsetParent);
			offsetX = ev.offsetX - (stationRect.left - parentRect.left);		
		}
		else
		{
			offsetX = ev.offsetX;		
		}
	}
	else if(ev.pageX && ev.pageY)
	{
		// Mozilla
		offsetX = ev.clientX + ev.pageX - ev.clientX - stationRect.left;
	}

	var clientPoint = { x : ev.clientX, y : ev.clientY };
	
	this.show1(station, stationRect, clientPoint, offsetX);
}

Roku.PopupStation.show1 = function(station, stationRect, clientPoint, offsetX)
{
	var custom_data = { station : station, stationRect : stationRect, clientPoint : clientPoint, offsetX : offsetX };

	var popup_type = Roku.Station.getPopupType(station);
	var id = Roku.Station.getId(station);

	var queryParam = this.queryKey + "=" + popup_type + "&id=" + id;
	
	Roku.Query.invoke(queryParam,
		function (content, custom_data) { Roku.PopupStation.fill_content(content, custom_data); },
		custom_data);
}

Roku.PopupStation.fill_content = function(content, custom_data)
{
	if(!content)
		return;

	this.content.innerHTML = content;
	Roku.Popup.initialize_popup(this.popup);
	this.initialize_content();

	// prepare calculations
	Roku.Popup.showArrow(this.popup, "ta", false);
	Roku.Popup.showArrow(this.popup, "ba", false);
	Roku.Popup.setArrowsOffset(this.popup, 0);
	this.popup.style.visibility = "hidden";
	this.popup.style.display = "block";

	var offsetX = custom_data.offsetX;

	var x = custom_data.stationRect.left;
	if(Roku.Util.clientWidth() < (custom_data.clientPoint.x - offsetX + this.popup.offsetWidth))
	{
		var popupWidth = this.popup.offsetWidth;

		offsetX += popupWidth - custom_data.stationRect.width;
		x += custom_data.stationRect.width - popupWidth;
	}
	
	var y = custom_data.stationRect.top;
	if(	(this.popup.offsetHeight < y) &&
		(Roku.Util.clientHeight() < (custom_data.clientPoint.y + custom_data.stationRect.height + this.popup.offsetHeight)))
	{
		y -= this.popup.offsetHeight;
		Roku.Popup.showArrow(this.popup, "ba", true);
	}
	else
	{
		y += custom_data.stationRect.height;
		Roku.Popup.showArrow(this.popup, "ta", true);
	}

	Roku.Popup.show(this.popup, custom_data.station, x, y, offsetX);
}

Roku.PopupStation.on_mouse_entry = function(ev)
{
	if(ev)
	{
		var element = Roku.Util.srcEventElement(ev);
		var hilight = !Roku.Util.CompareNoCase(ev.type, "mouseout");
		this.hilightEntry(element, hilight);
	}
}

Roku.PopupStation.hilightEntry = function(element, hilight)
{
	var entry = Roku.Util.parentClassElement(element, this.entryClassName);
	var text = Roku.Util.childClassElement(entry, this.textClassName);
	if(text)
	{
		text.style.textDecoration = hilight ? "underline" : "";
	}
}

Roku.PopupStation.on_click_entry = function(ev)
{
	var element = Roku.Util.srcEventElement(ev);
	var entry = Roku.Util.parentClassElement(element, this.entryClassName);
	if(entry)
		this.on_command(entry.id, ev);
}

Roku.PopupStation.on_command = function(cmd, ev)
{
	var station = this.popup ? this.popup.currentElement : null;
	Roku.Popup.hidePopup(this.popup);
	this.do_command(cmd, station, ev);
}

Roku.PopupStation.do_command = function(cmd, station, ev)
{
	if(!station)
		return;

	if(cmd == this.cmdIdPlayPC)
	{
		Roku.Util.Shared.playStation(ev, station);
	}
	else if(cmd == this.cmdIdPlaySoundbridge)
	{
		if(Roku.MySB && Roku.MySB.Radio && Roku.MySB.Radio.can_play())
		{
			Roku.MySB.Radio.play_station(station);
		}
	}
	else if(cmd == this.cmdIdSaveAsPreset)
	{
		Roku.DialogSavePreset.do_modal(station, ev);
	}
	else if(cmd == this.cmdIdSaveAsFavorite)
	{
		if(Roku.Util && Roku.Util.Shared && !Roku.Util.Shared.checkConfirmed(ev))
			return;
		
		Roku.Query.invoke("station_favorite=1&id=" + Roku.Station.getId(station), function(response) { if(Roku.Favorites) Roku.Favorites.update(); } );
	}
	else if(cmd == this.cmdIdRemoveFavorite)
	{
		Roku.Query.invoke("station_favorite=0&id=" + Roku.Station.getId(station), function(response) { if(Roku.Favorites) Roku.Favorites.update(); });
	}
	else if(cmd == this.cmdIdEdit)
	{
		if(Roku.Util && Roku.Util.Shared && !Roku.Util.Shared.checkConfirmed(ev))
			return;

		var edit_url = Roku.Util.Shared.getAttribute(station, "station_edit");
		if(!edit_url)
			edit_url = "addstream.php";
		
		window.location = edit_url + '?id=' + Roku.Station.getId(station);
	}
	else if(cmd == this.cmdIdApprove)
	{
		//if (confirm("Are you sure you want to approve this station?") == true)
			Roku.Query.invoke("approve_stream=1&id=" + Roku.Station.getId(station), function() { /*Roku.Body.doRefreshContent(null, station, Roku.PopupStation.cmdIdApprove); */});
	}
	else if(cmd == this.cmdIdUnApprove)
	{
		if (confirm("Are you sure you want to unapprove this station?") == true)
			Roku.Query.invoke("approve_stream=0&id=" + Roku.Station.getId(station), function() { Roku.Body.doRefreshContent(null, station, Roku.PopupStation.cmdIdUnApprove); });
	}
	else if(cmd == this.cmdIdShowURLs)
	{
		//if(Roku.Util && Roku.Util.Shared && !Roku.Util.Shared.checkConfirmed(ev))
		//	return;

		var showStreams_url = "streamurls.php";
		
		window.location = showStreams_url + '?id=' + Roku.Station.getId(station);
	}
	else if(cmd == this.cmdIdShowPlays)
	{
		//if(Roku.Util && Roku.Util.Shared && !Roku.Util.Shared.checkConfirmed(ev))
		//	return;

		var showPlays_url = "streamsplaystation.php";
		
		window.location = showPlays_url + '?id=' + Roku.Station.getId(station);
	}
	else if(cmd == this.cmdIdShowDetail)
	{
		//if(Roku.Util && Roku.Util.Shared && !Roku.Util.Shared.checkConfirmed(ev))
		//	return;

		var showPlays_url = "stationdetail.php";
		
		window.location = showPlays_url + '?sid=' + Roku.Station.getId(station);
	}
}

Roku.PopupStation.initialize = function()
{
	this.popup = document.getElementById(this.popupId);
	this.content = Roku.Util.childClassElement(this.popup, this.contentClassName);
	this.queryKey = this.popup ? this.popup.getAttribute(this.queryKeyAttribName) : "";
}

Roku.PopupStation.initialize_content = function()
{
	if(!this.content)
		return;

	var hilightEntryHandler = function(ev) { return Roku.PopupStation.on_mouse_entry(ev); };
	var clickEntryHandler = function(ev) { return Roku.PopupStation.on_click_entry(ev); };
	
	var entries = Roku.Util.childrenClassElements(this.content, this.entryClassName);
	var entries_count = entries.length;
	for(var iEntry = 0; iEntry < entries_count; iEntry++)
	{
		var entry = entries[iEntry]; 
		Roku.Util.eventHandler(entry, "click", clickEntryHandler);
		Roku.Util.eventHandler(entry, "mouseover", hilightEntryHandler);
		Roku.Util.eventHandler(entry, "mouseout", hilightEntryHandler);
	}
}

