// (c) 1999-2007 Live365, Inc.
   var gHostServer = "www.live365.com";
// var gHostServer = 'devweb01.nanocosm.com';

{
	var root = (self.location.protocol != 'https:') ? 'http://' + gHostServer : '';

	if (typeof(GetCookieEx) != "function")
		document.write('<script language="JavaScript" src="' + root + '/scripts/cookiemonster.js"></scr' + 'ipt>');
	
	if (typeof(client) != "object")
		document.write('<script language="JavaScript" src="' + root +'/scripts/clientdetect.js"></scr' + 'ipt>');
}

// exclude host caps on 3rd party sites; if you want host caps on your site include /scripts/hosts.js
if (window.location.host == gHostServer)
	document.write('<script language="JavaScript" src="http://' + gHostServer + '/scripts/hosts.js"></scr' + 'ipt>');

var gMiniWindow		= null;
var gMiniWindowPLR	= null;

var gIsPM			= 'unknown';
var gDefaultSite	= 'unknown';

function GetDefaultSite()
{
	if (gDefaultSite == 'unknown')
		gDefaultSite = IsPM() ? 'live365' : 'tgif';
		
	// plug your /scp/customskin directory name into the second location like below
	// gDefaultSite = IsPM() ? 'live365' : 'customskin';
	// if you're looking for the day-parting, Coke skin code, grab the 2/13/06 version

	return gDefaultSite;
}

function IsPM()
{
	if (gIsPM == 'unknown')
		gIsPM = (GetCookie("afl") != null);

	return gIsPM;
}


function DeterminePOC()			
{
	var ownDomain = (self.location.host.indexOf(parseHost(gHostServer)) != -1);
	
	if (!ownDomain)
		return 100;

	var poc		= -1;
	var box_mc	= GetCookie('box_mc');

	if (box_mc && box_mc.indexOf('POC=') != -1)
		poc	= GetCookieEx('box_mc', "POC");
	else {
		// Use embedded WMP control as default on IE on Windows; otherwise use 3rd party mp3 player
		poc = (client.isWin && client.isIE) ? '2' : '0';
		
		if (GetCookie("SaneID")) {
			RememberItEx('box_mc', 'bitrate', '256');
			RememberItEx('box_mc', 'ls', '3');
			RememberItEx('box_mc', "POC", poc);
			
			try {
				if (typeof(SetRMACookie) == "function")
					SetRMACookie(2, .20);
			}
			catch(e) {
			}
		}
	}
		
	return poc;
}


function RollupArgs(error, playlist, odatrack, odapos) 
{
	var args = '';
	
	if (error != -1)
		args += "&error=" + error;

	if (playlist && playlist != 'undefined') { 
		args += "&playlist=" + encodeURIComponent(playlist); 

		if (odatrack && odatrack != 'undefined') { 
			args += "&odatrack=" + encodeURIComponent(odatrack);
			 
			if (odapos && odapos != 'undefined')
				args += "&odapos=" + encodeURIComponent(odapos); 
		}
	}

	return args;
}

function DirectLaunch(id, id_type, station_status, site, clientType, playlist, odatrack, odapos, isPro)
{
	if (typeof(GetDirectLaunchHost) != "function")
		return false;

    var vHost = GetDirectLaunchHost();

    if (!vHost)
		return false;

	var values = GetErrorStatus(station_status, isPro);
	var error = values[0];

	if (vHost.Name == "WMP") {
		if (station_status == 'SUBSCRIPTION' || playlist)
			return false;
	}
	else if (vHost.Name != "RealOne") {
		if (error != -1 || station_status == 'SUBSCRIPTION' || playlist)
			return false;
	}

	var args = RollupArgs(error, playlist, odatrack, odapos);
	
	if (id_type == 'str')
   		return vHost.LaunchStreamByID(id, site, args);
   	else if (id_type == 'nam')
   		return vHost.LaunchStreamByName(id, site, args); 
   		
	return false;
}


// LaunchBroadcast - launch locally served stream by name.
function LaunchBroadcast(broadcastName, site, source, clientType, playlist, odatrack, odapos, station_status, isPro)
{
	SetLastStationCookie(broadcastName, 'nam');
	var url = "http://" + gHostServer + "/cgi-bin/mini.cgi?membername=" + encodeURIComponent(broadcastName);
	var numArgs = LaunchBroadcast.arguments.length + 1;

	if (numArgs < 2 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..')
		url += "&site=" + nullCheck(encodeURIComponent(site));

	if (numArgs >= 3 && source)
		url += "&source=" + nullCheck(encodeURIComponent(source));

	if (numArgs >= 4)
		url += "&clientType=" + nullCheck(clientType);

	if (numArgs >= 5)
		url += "&playlist=" +  nullCheck(encodeURIComponent(playlist));
	else
		playlist = "";

	if (numArgs >= 6)
		url += "&odatrack=" +  nullCheck(encodeURIComponent(odatrack));
	else
		odatrack = "";

	if (numArgs >= 7)
		url += "&odapos=" +  nullCheck(encodeURIComponent(odapos));
	else
		odapos = "";

	if (numArgs < 8 || !station_status)
		station_status = 'OK';

	if (numArgs < 9)
		isPro = (site.indexOf('pro') == 0 || site.indexOf('PLR-') != -1) ? 'Y' : 'N';
		
	if (DirectLaunch(broadcastName, "nam", station_status, site, clientType, playlist, odatrack, odapos, isPro))
		return;
		
	LaunchMiniDecide(url, site, isPro, true, station_status);
}


// LaunchBroadcast365 - launch Co-Lo stream.  Does not get munged and does not vary on test systems.
function LaunchBroadcast365(broadcastName, site, source, playlist, isPro)
{
	SetLastStationCookie(broadcastName, 'nam');
	var url = "http://www.live" + "365.com/cgi-bin/mini.cgi?membername=" + encodeURIComponent(broadcastName);
	var numArgs = LaunchBroadcast365.arguments.length + 1;

	if (numArgs < 2 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..')
		url += "&site=" + nullCheck(encodeURIComponent(site));

	if (numArgs >= 3 && source)
		url += "&source=" + nullCheck(encodeURIComponent(source));

	if (numArgs >= 4 && playlist)
		url += "&playlist=" + nullCheck(encodeURIComponent(playlist));

	isPro = nullCheck(isPro);
	if (!isPro)
		isPro = 'N';
		
	if (DirectLaunch(broadcastName, "nam", "OK", site, DeterminePOC(), playlist, '', 0, isPro))
		return;

	LaunchMiniDecide(url, site, isPro);
}


// Launch - launch locally served stream.
function Launch(streamID, genre, site, source, station_status, isPro)
{
	SetLastStationCookie(streamID, 'str');
	var url = "http://" + gHostServer + "/cgi-bin/mini.cgi?stream=" + streamID + "&genre=" + encodeURIComponent(genre);
    var numArgs = Launch.arguments.length + 1;

	if (numArgs < 3 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..')
		url += "&site=" + nullCheck(encodeURIComponent(site));

	if (numArgs >= 4 && source)
		url += "&source=" + nullCheck(encodeURIComponent(source));

	if (numArgs < 5 || !station_status)
		station_status = 'OK';

	site = nullCheck(site);

	isPro = nullCheck(isPro);	
	if (!isPro)
		isPro = 'N';

	if (DirectLaunch(streamID, "str", station_status, site, DeterminePOC(), '', '', 0, isPro))
		return;
		
	LaunchMiniDecide(url, site, isPro, true, station_status);
}


// Launch365 - launch Co-Lo stream.  Does not get munged and does not vary on test systems.
function Launch365(streamID, genre, isPro)
{
	SetLastStationCookie(streamID, 'str');
	var url = "http://www.li" + "ve365.com/cgi-bin/mini.cgi?stream=" + streamID + "&genre=" + encodeURIComponent(genre);
	
	isPro = nullCheck(isPro);
	if (!isPro)
		isPro = 'N';
	
	LaunchMiniDecide(url, null, isPro);
}


// LaunchPlayer - launch locally served stream with specified player type.
function LaunchPlayer(streamID, genre, clientType, site, source, station_status, isPro)
{
	SetLastStationCookie(streamID, 'str');
	var url = "http://" + gHostServer + "/cgi-bin/mini.cgi?clientType=" + clientType + "&stream=" + streamID + "&genre=" + encodeURIComponent(genre);
	var numArgs = LaunchPlayer.arguments.length + 1;
	var poc = clientType;

	if (poc == "")
		poc = DeterminePOC();

	if (numArgs < 4 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..')
		url += "&site=" + nullCheck(encodeURIComponent(site));

	if (numArgs >= 5 && source)
		url += "&source=" + nullCheck(encodeURIComponent(source));

	if (numArgs < 6 || !station_status)
		station_status = 'OK';
		
	isPro = nullCheck(isPro);	
	if (!isPro)
		isPro = 'N';

	if (DirectLaunch(streamID, "str", station_status, site, poc, '', '', 0, isPro))
		return;

	LaunchMiniDecide(url, site, isPro, true, station_status);
}


// LaunchMiniDecide() Determines whether to launch the PLR or Normal/SCP version
function LaunchMiniDecide(url, site, isPro, lsOK, station_status)
{
	var numArgs = LaunchMiniDecide.arguments.length + 1;

	if (numArgs < 2 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();

	if (numArgs < 3 || !isPro)
		isPro = "N";

	if (numArgs < 4)
		lsOK = true;

	if (numArgs < 5)
		station_status = 'OK';

	LaunchMini(url, isPro, lsOK, station_status, site);
}


function PopupBlockerAlert()
{
	alert("We were unable to start the Live365 player.\nIf you have installed a popup-blocker, turn\nit off, or hold down the Ctrl-key, and try again.");
}


// LaunchMini - launch Live365.com playlist window given full url.
function LaunchMini(url, isPro, lsOK, station_status, site) 
{
	var normalWin = true;
	var win = gMiniWindow;
	var name = "Live365PlayerWindow";
	var width = 780;
	var height = 262;
	var ownDomain	= (self.location.host.indexOf(parseHost(gHostServer)) != -1);
	var escURL		= encodeURIComponent(url);
	var POC			= DeterminePOC();

	var isWMP = (GetCookie("platform") == 'wmp10');
	var isPLR = (isPro == 'Y' && site && (site.indexOf("pro") == 0 || site.indexOf("PLR-") != -1));
	
	if (isPLR || IsPM() || isWMP) {
		width = 468;
		
		if (isPLR) {
			height		= 332;
			normalWin	= false;
			win			= gMiniWindowPLR;
			name		= "Live365PlayerWindowPLR";
		}
		else if (isWMP)
			height = (IsPM()) ? 277 : 327;
		else
			height = 262;
	}
	
	if (ownDomain && !isPLR && lsOK) {
		var values = GetErrorStatus(station_status, isPro);

		if (values[0] != -1 && station_status != 'SUBSCRIPTION') {
			width = 468;
			height = 323;
			url	= "http://" + gHostServer + "/mini/login.live?error=" + values[0];
			
			if (values[1])
				url += "&PMonly=1";

			url += "&station_status=" + station_status;

			var v = GetCookieEx('box_mc', 'str');
			if (v)
				url += '&streamid=' + v;
			else {
				v = GetCookieEx('box_mc', 'nam');
				if (v)
					url += '&station_name=' + v;
			}
			
			url += "&url=" + escURL;
		}
	}

	var tm = new Date();
	url += "&tm=" + tm.getTime();

	if (win != null) {
		if (win.closed == true)
			win = null;
		else {
			if (ownDomain) {
				if (win.ActivateWarning)
					win.ActivateWarning(false);
			}
			win.location = url;
		}
	}
	
	if (win == null) {
		try {
			win = open(url, name, 'width=' + width + ',height=' + height + ',toolbar=no,scrollbars=no,location=no,resizable=no,status=no,menubar=no');
		}
		catch(e) {
			win = null;
		}

		if (win == null)	
			PopupBlockerAlert();
			
		if (normalWin)
			gMiniWindow = win;
		else
			gMiniWindowPLR = win;
	}

	if (win != null && win.focus) {
		try {
			win.focus();
		}	
		catch(e) {
			PopupBlockerAlert();
		}
	}
}


function GetErrorStatus(station_status, isPro)
{
	var error = -1;
	var requiresPM = (station_status == 'PM_ONLY' || station_status == 'SOLD_OUT');
	var vc = GetCookie("VisitCount");

	if (station_status == 'PRO_SOLD_OUT')
		error = 10;
	else if (station_status == 'OFF')
		error = 11;
	else if (station_status == 'TOO_FAST')
		error = 12;
	else if (!IsPM() && requiresPM)
		error = (station_status == 'PM_ONLY') ? 3 : 4;
	else if (!GetCookie('sessionid') && (requiresPM || (!isPro && vc >= 10)))
		error = 0;

	return new Array(error, requiresPM);
}


function SetLastStationCookie(stn, field)
{
	if (SetLastStationCookie.arguments.length + 1 < 2)
		field = 'str';
		
	RememberItEx('box_mc', field, stn);
}


function nullCheck(str)
{
	if (str+"" == "undefined" || str+"" == "null" || str+"" == "")
		return '';
	
	return str;
}