// (c) 2008 Live365, Inc. - Custom PRO Player generic include file
//
//******************************************************************************************************************
// Required - all versions of this page will require these parameters that identify which stream is to be launched.
//
var station_broadcaster		= 'live365premium';		// DJ name
var station_id				= 222733;				// station numeric ID
var stream_id				= 904867;				// stream numeric ID

//
// 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 web_server				= 'http://www.live365.com';	// web server to pull /play and PLS data from
var local_css				= web_server + '/mini/pls.css';
var imageDir				= web_server + '/mini/images/';

//******************************************************************************************************************
// 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 no_branding				= 1;			// true to suppress Live365 branding in PLS frame
var no_buttons				= 1;			// true to suppress buy buttons in PLS 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];
	}
}

var poc = 2;		// embedded WMP

ReadParameters();	// may override poc and vip

if (navigator.platform.indexOf("Win32") == -1 || 
	navigator.appName.indexOf("Microsoft") == -1) {
	poc = 7;	// use Flash player if not on IE on Windows (e.g. Mac iTunes)
} 

var player = GetPOCName(poc);

RememberItEx('box_mc', 'POC', poc);
RememberItEx('box_mc', 'LOK', 1);
RememberItEx('box_mc', 'beta', (vip) ? "mp3PRO" : '');
SetCookie("afl", afl);

// 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 plrFlag			= 'Y';

//*********************//
//** 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;
			}
		}
	}
}


//
// 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' || player == 'FLASH')
		launchUrl += "/play/";
	else {
		launchUrl += "/cgi-bin/play.pls?stationid=";
		conn = '&';
	}

	launchUrl += station_id;

	var visit_count		= GetCookie('VisitCount');
	var sane_ID			= GetCookie('SaneID');

	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;
}


//
// This will be called when the player control needs to reconnect to the Nanocaster.
//
function Retry()
{
	HandleAction('stop');

	InRetry = true;
	RetryCount++;

	HandleAction('play'); // delayed call
}



//
// Just a little local JS file to handle the little gray buttons on our test page.
//
var gOnLoadTimer = null;

function HandleAction(action)
{
	var launchUrl = GetLaunchURL();

	if (action == 'play')			// launch embedded player control after delay
		 gOnLoadTimer = window.setTimeout("DoOnLoadActions()", 100);	// workaround Firefox DOM issues
	else {
		if (player != "MP3") {
			if (action == 'playnow')// launch embedded player control immediately
				Play(launchUrl);
			else if (action == 'stop')
				Stop();
		}
		else {						// launch 3rd party player with PLS file
			if (action == 'playnow')
				window.location = launchUrl;
		}
	}
	
	return false;
}

function DoOnLoadActions() 
{
	window.clearTimeout(gOnLoadTimer);
	HandleAction('playnow');
	return true;
}


//
// This function is responsible for drawing the mp3PRO indicator lights.
//
function DrawPlayerIndicator()
{
	var img = "";

	if (gStreamFormat == 'MP3')
		img = "indicator-mp3pro-off.gif";
	else
		img = "indicator-mp3pro-on.gif";

	document.mp3pro_winkie.src = web_server + "/mini/images/" + img;
}


//
// DrawControls - draws player controls
//
var gPreBtnVertSpace = 8;
var gPlayerColor = '#000000';	// black by default

function DrawControls()
{
	var s = ''
	memberName	= station_broadcaster;		// make sure player control and page agree on what station we're playing!
	streamID	= stream_id;
	
	document.write('<tr valign="top">');
	
	if (player != "MP3") {					// Draw player; bypass check version/LS logic
		//
		// Draw volume bar and player control for embedded players
		//
		document.write('<td align="center">');
		DrawMeter(bVertical, imageDir);		// Draw volume meter
		DrawPlayer(0, gPlayerColor);		// Draw the embedded audio playback control
		s = '</td>';
	}

	//
	// Last (only one on Mac) row has the VCR buttons.
	//
	s += '<td>';
	s += '<img border="0" src="' + imageDir + 'dot.gif" width="81" height="' + gPreBtnVertSpace + '"><br>';
	s += '<img alt="PLAY" src="' + imageDir + 'radio-play.gif" width="33" border="0" height="34" onclick="HandleAction(\'playnow\')">';

	if (player != 'MP3')
		s += '<img alt="STOP" src="' + imageDir + 'radio-stop.gif" width="40" border="0" height="34" onclick="HandleAction(\'stop\')">';

	s += '</td></tr>';

	document.write(s);
}


//
// DrawPLS - display the playlist metadata in an iframe that will update itself.
//
//
var gPLSWidth = 290;
var gPLSHeight = 90;

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 + "/mini/playlist.html?station=";

	PLS_server += station_broadcaster;

	var ads = 1;
	
	if (afl && session_key)
		PLS_server += "afl%3A";
	else
		ads = 0;

	PLS_server += "&ads=" + ads;

	var hide = '';
	
	if (no_branding)
		hide += 'T';

	if (no_buttons)
		hide += 'BW';
		
	if (hide)
		PLS_server += "&hide=" + hide;
		
	if (local_css)
		PLS_server += "&css=" + escape(local_css);
		
	document.write('<iframe src="' + PLS_server + '" name="Live365PlayerPlaylist" scrolling="AUTO" noresize frameborder="No" marginwidth="0" marginheight="0" width=' + gPLSWidth + ' height=' + gPLSHeight + '></iframe>');
}

