//****************//
//** Parameters **//
//****************//
//
// Optional - most applications will want to provide these parameters with 
// every call.  
//	'afl'		 - indicates whether we use try to connect to the AFL version of a station.
//	'vip'		 - indicates whether we use the Player365 or Player365-VIP ActiveX control.
//	'session_id' - specifies the listener's login information, typically returned from an api_login.cgi call
//	'tag'		 - indicates which service is being credited for this stream launch
//	'web_server' - specifies domain for /play and PLS services and also for graphics images
//	'no_branding'- true to suppress Live365 branding in PLS frame
//	'no_buttons' - true to suppress buy buttons in PLS frame
//
var afl						= 0;			// attach to the AFL version of the station
var vip						= 0;			// use VIP or regular Player365
var session_id				= GetCookie('sessionid');	// listener's sessionid (member_name:session_key); will use cookie if not provided
var tag						= '';			// unique client organization id
var token					= '';			// unique client organization authentication token
var web_server				= 'http://www.live365.com';	// web server to pull /play and PLS data from
var no_branding				= 1;			// true to suppress Live365 branding in PLS frame
var station_id				= 344519;							// station numeric ID
var stream_id				= 1350581;							// stream numeric ID
var AuthType				= 'NONE';
var playlist				= '';			// default ODA playlist to start station with

//
// Alternatively, you can define your own stylesheet to control the display of the playlist information.
// If you do that, you'll need to define some other JS variables, too.  The CSS file should define all
// of the styles required to draw the playlist frame.
//

var member_name				= '';
var session_key				= '';

if (session_id) {
	var sess = session_id.split(':');
	if (sess && sess.length > 1) {
		member_name = sess[0];
		session_key = sess[1];
	}
}


//*********************//
//** Basic functions **//
//*********************//
function ReadParameters()
{
	var url = window.location.href;

	url = url.replace(/\+/g, " ");

	var arg_list = url.split("?");

	if (arg_list.length <= 1)
		return;

	var nv_pairs = arg_list[1].split("&");
	var i;
	
	for (i = 0; i < nv_pairs.length; i++) {	// for each name=value pair
		var nv_pair = nv_pairs[i].split("=");

		if (nv_pair.length > 1) {
			var val = nv_pair[1];
			
			switch(nv_pair[0]) {
				case "station_broadcaster":
				station_broadcaster = val;
				break;

				case "station_id":
				station_id = parseInt(val);
				break;

				case "stream_id":
				stream_id = parseInt(val);
				break;

				case "poc":
				poc = parseInt(val);
				break;

				case "vip":
				vip = parseInt(val);
				break;

				case "afl":
				afl = parseInt(val);
				break;

				case "session_id":
				session_id = val;
				break;

				case "tag":
				tag = val;
				break;

				case "token":
				token = val;
				break;

				case "web_server":
				web_server = unescape(val);
				break;

				case "no_branding":
				no_branding = parseInt(val);
				break;

				case "no_buttons":
				no_buttons = parseInt(val);
				break;

				case "displayType":
				displayType = unescape(val);
				break;

				case "site":
				site = unescape(val);
				break;
				
				case "playlist":
				playlist = unescape(val);
				break;
			}
		}
	}
}


//
// Build a /play url with all of the parameters necessary to yield a successful stream launch.
//
function GetLaunchURL()
{
	var conn			= '?';
	var launchUrl		= web_server;

	if (player == '365' || player == 'WMP')
		launchUrl += "/play/";
	else {
		launchUrl += "/cgi-bin/play.pls?stationid=";
		conn = '&';
	}

	launchUrl += station_id;

	var visit_count		= GetCookie('VisitCount');
	var sane_ID			= GetCookie('SaneID');

	if (playlist) {
		launchUrl += conn + "playlist=" + escape(playlist);
		conn = '&';
	}

	if (tag) {
		launchUrl += conn + "tag=" + tag;
		conn = '&';
	}

	if (token) {
		launchUrl += conn + "token=" + token;
		conn = '&';
	}

	if (session_key) {
		launchUrl += conn + "session=" + session_key;
		conn = '&';
	}	

	if (member_name) {
		launchUrl += conn + "membername=" + member_name;
	}	

	if (visit_count) {
		launchUrl += conn + "VisitCount=" + visit_count;
		conn = '&';
	}	

	if (sane_ID) {
		launchUrl += conn + "SaneID=" + sane_ID;
		conn = '&';
	}

	if (player == 'G2' || player == 'MP3')
		launchUrl += conn + "filename.pls";
	else if (player == 'WMP')
		launchUrl += conn + "filename.m3u";

	return launchUrl;
}


//**************************//
//** Set-up for .js files **//
//**************************//

var gVersion = "0.0.0.0";
var gPlayers = new Array("MP3", "G2", "WMP", "365");
var gPlayersDisplayNames = new Array("External MP3", "RealOne", "MS WMP", "Player365");
var poc = 3;	

ReadParameters();	// may override poc and vip

if (navigator.platform.indexOf("Win32") == -1 || 
	navigator.appName.indexOf("Microsoft") == -1) {
	poc = 0;	// use 3rd party M3U player if not on IE on Windows (e.g. Mac iTunes)
} 

var player = gPlayers[poc];
RememberItEx('box_mc', 'POC', poc);
RememberItEx('box_mc', 'beta', (vip) ? "mp3PRO" : '');

SetCookie("afl", afl);
SetCookie("SuccessfulListen", 1);

// Some other cookies you can tweak to adjust Player365 performance
//
//	RememberItEx("player_mc","BufSize", 4);
//	RememberItEx("player_mc","ProxyPort", '0');
//	RememberItEx("player_mc","ProxyServer", '0');
//	RememberItEx("player_mc","UseProxy", 'N');

//
// These variables are required by vb_sniff_lite.js and player.js.
//
var memberName		= station_broadcaster;
var streamID		= stream_id;
var gMP3Pro			= vip;					// default to use VIP player
var bVertical		= 0;					// which way to draw volume bar
var gPLRflag		= "N";
var gSetupFlag		= 0;
var live365VerifyMessage = 0;
var launchUrl		= GetLaunchURL();
var plrFlag			= 'Y';

// end scripting -->

document.write('<script language="JavaScript" src="http://www.live365.com/scripts/vb_sniff_lite.js"></script>');

//
// This will be called when the player control needs to reconnect to the Nanocaster.
//
function Retry()
{
	HandleAction('stop');

	InRetry = true;
	RetryCount++;

	HandleAction('play')
}


//
// This is called when logic requires the current page to be blown away.  If you
// use an onbeforeunload event handler, this is the place to disable it if you want.
//
function KillWarning()
{
}

function ShouldShowButtons()
{
	if (no_buttons)
	   return false;
	else
	   return true;
}

//
// Just a little local JS file to handle the little gray buttons on our test page.
//
function HandleAction(action)
{
	if (player != "MP3") {
		if (action == 'play')	// launch embedded Player365 ActiveX control
			Play(launchUrl);
		else if (action == 'stop')
			Stop();
	}
	else {						// launch 3rd party player with PLS file
		if (action == 'play')
			window.location = launchUrl;
	}

	return false;
}


//
// This function is responsible for drawing the mp3PRO indicator lights.
//
function DrawPlayerIndicator()
{
	var img = "";

	if (gStreamFormat == 'MP3')
		img = "images/indicator-mp3pro-off.gif";
	else
		img = "images/indicator-mp3pro-on.gif";

//	document.mp3pro_winkie.src = web_server + "/mini/images/" + img;
}


//
// DrawControls - draws player controls
//
function DrawControls()
{
	var s = ''
	
	if (player != "MP3") {					// Draw player; bypass check version/LS logic
		//
		// First row is the mp3PRO indicator light
		//
//		s += '';
//		s += '<img width=110 height=12 name="mp3pro_winkie" src="' + web_server + '/mini/images/indicator-mp3pro-off.gif" alt="Indicates if broadcast is in mp3PRO format." border="0">';
//		s += '';

		//
		// Second row has the volume bar and hosts ActiveX player control
		//
		document.write(s + '');

		DrawMeter(bVertical, imageDir);		// Draw volume meter

		DrawPlayer(0);						// Draw the Player365 ActiveX control

		GetVersion();						// Determine what version of embedded player is installed

		s = '</td>';
	}

	//
	// Last (only one on Mac) row has the VCR buttons.
	//
	s += '';
	s += '';
	s += '<img alt="PLAY" src="live365/images/radio-play.jpg" width="148" border="0" height="72" style="cursor:hand" onclick="HandleAction(\'play\')">';

	if (player != 'MP3')
		s += '<a href="#"><img alt="STOP" src="images/radio-stop.jpg" width="148" border="0" height="72" onclick="HandleAction(\'stop\')"></a>';

	s += '';

	document.write(s);
}


//
// DrawPLS - display the playlist metadata in an iframe that will update itself.
//
//
function DrawPLS()
{
	//
	// PLS_server - this provides the playlist information to the web page.  Slightly
	// different forms if pulling from the ad-free stream:  use "afl:" prefix.
	//
	var PLS_server = web_server + "/pls/front?handler=playlist&cmd=view&handle=";

	if (afl && session_key)
		PLS_server += "afl%3A";

	PLS_server += station_broadcaster;

	if (no_branding)
		PLS_server += "&noBranding=1";

	if (no_buttons)
		PLS_server += "&noBuyButtons=1";
		
	if (local_css)
		PLS_server += "&css=" + escape(local_css);
		
	if (displayType != 'LIVE365')
		PLS_server += "&displayType=" + escape(displayType);
				
	PLS_server += "&site=" + escape(site);

	document.write('<iframe src="' + PLS_server + '" name="Live365PlayerPlaylist" scrolling="AUTO" noresize frameborder="No" marginwidth="0" marginheight="0" width=' + iwidth + ' height=' + iheight + '></iframe>');
}
