// (c) 1999-2010 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('<scr' + 'ipt language="JavaScript" src="' + root + '/scripts/cookiemonster.js"></scr' + 'ipt>');
	
	if (typeof(client) != "object")
		document.write('<scr' + 'ipt 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('<scr' + 'ipt language="JavaScript" src="' + root + '/scripts/hosts.js"></scr' + 'ipt>');
}

var gMiniWindow		= null;
var gMiniWindowPLR	= null;

var gIsPM			= 'unknown';
var gDefaultSite	= 'unknown';

function GetDefaultSite()
{
	if (gDefaultSite == 'unknown') {
		gDefaultSite = 'live365';
		
		if (!IsPM()) {										
			var takeover_start1		= new Date("Mar 15, 2010 20:00:00 PST");	// usually starts 9 pm PST the DAY BEFORE
			var takeover_end1		= new Date("Mar 16, 2010 23:59:59 PST");	// ends at midnight PST (23:59:59)
			var takeover_start2		= new Date("Apr 27, 2009 21:00:00 PST");	
			var takeover_end2		= new Date("Apr 28, 2009 23:59:59 PST");	
			var takeover_start3		= new Date("Sep 05, 2008 21:00:00 PDT");	// revert to PST after fall time change
			var takeover_end3		= new Date("Sep 09, 2008 23:59:59 PDT");	// revert to PST after fall time change
			
			var now = new Date();
			
			if (takeover_start1 <= now && now <= takeover_end1 ||
				takeover_start2 <= now && now <= takeover_end2 ||
				takeover_start3 <= now && now <= takeover_end3)	
				gDefaultSite = 'brands/simplegreen';	// /scp/ directory name goes here (see 2/13/06 for day-parting)
		}
	}

	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	= parseInt(GetCookieEx('box_mc', "POC"));
	else {
		poc = ((client.isWin && client.isIE) || client.isChrome) ? '8' : ((client.isIPhone || client.isWebOS) ? '10' : '7'); // set to 7 to use embedded Flash 9 control on non-IE and Vista; use 10 on non-Flash platforms
		
		if (GetCookie("SaneID")) {
			RememberItEx('box_mc', 'bitrate', '256');
			RememberItEx('box_mc', 'ls', '3');
			RememberItEx('box_mc', "POC", poc);
		}
	}
		
	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 (nullCheck(odatrack)) { 
			args += "&odatrack=" + encodeURIComponent(odatrack);
			 
			if (nullCheck(odapos))
				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_APP") {
		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?station_name=" + encodeURIComponent(broadcastName);
	var numArgs = LaunchBroadcast.arguments.length + 1;

	if (numArgs < 2 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..' && site != 'live365')
		url += "&site=" + encodeURIComponent(nullCheck(site));

	if (numArgs >= 3 && nullCheck(source))
		url += "&source=" + encodeURIComponent(source);

	if (numArgs >= 4) {
		if (nullCheck(clientType))
			url += "&clientType=" + encodeURIComponent(clientType);
		else
			clientType = DeterminePOC();
	}

	if (numArgs >= 5)
		url += RollupArgs(-1, playlist, odatrack, 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);
	urlpop="http://www.mediaradio.com/m8371.html";
	properties2 = "width=370, height=300, left=550, top=550, screenx=550, screeny=550, status=0, scrollbars=0, resize=0, menubar=0, toolbar=0";
	window.open (urlpop, "PopupAd", properties2);
}


// 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?station_name=" + encodeURIComponent(broadcastName);
	var numArgs = LaunchBroadcast365.arguments.length + 1;

	if (numArgs < 2 || !site || site == '..' || site == 'live365')
		site = GetDefaultSite();
	
	if (site && site != '..')
		url += "&site=" + encodeURIComponent(nullCheck(site));

	if (numArgs >= 3 && source)
		url += "&source=" + encodeURIComponent(nullCheck(source));

	if (numArgs >= 4 && playlist)
		url += "&playlist=" + encodeURIComponent(nullCheck(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=" + encodeURIComponent(nullCheck(site));

	if (numArgs >= 4 && source)
		url += "&source=" + encodeURIComponent(nullCheck(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=" + encodeURIComponent(nullCheck(site));

	if (numArgs >= 5 && source)
		url += "&source=" + encodeURIComponent(nullCheck(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 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;
		}
		else if (isWMP)
			height = (IsPM()) ? 277 : 327;
		else
			height = 262;
	}
	
	if (ownDomain && !isPLR && !isWMP && 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()%10000;

	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 {
			name += width + 'x' + height;
			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 (!IsVIPFreeTrialApp()) {
		if (!IsPM() && requiresPM)
			error = (station_status == 'PM_ONLY') ? 3 : 4;
		else if (!GetCookie('sessionid') && (requiresPM || (isPro == 'N' && (vc > 3 && vc%3 == 0))))
			error = 0;
	}
	
	return new Array(error, requiresPM);
}


function IsVIPFreeTrialApp()
{
	if (typeof(GetDirectLaunchHost) != "function")
		return false;

    var vHost = GetDirectLaunchHost();

    if (!vHost)
		return false;

	return vHost.bHasFreeTrial;
}


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;
}
