/*******************************************************************************
 *  BoomerRadio Player JavaScript
 *  
 * Copyright (c) 2001-2003 Abacast, Inc. All rights reserved World Wide.
 * Author: J Morris
 * Version 1.6
 * Modified 09 Sept 2003 - Added player check
 * Modified 09 Oct  2003 - relaunch() procedure could cause loop if no format
 * Modified 26 Jan  2004 - Added thePlatform URLs to local streams 
 *                         Changed ASX file locations
 * Modified 03 Feb  2004 - Modified load_stream procedure and added sec check
 * Modified 30 Dec  2004 - Updated Server Urls
 * Modified 09 Aug  2005 - WMP 7 Code
 * Modified 11 Nov  2006 - Added AllHitOldies
 * Modified 05 Mar  2008 - Added Sizzlin 70s and HTMLView Mods
 ******************************************************************************/
 
var updates	     = 0;
var done         = false;
var g_use_wmp7   = false; //aba_HasURLParam('player=6')?false:true;
var firstRun     = true;
var playing      = false;
var pageload     = false;
var uniasx       = '';
var lastcaption  = '';
var lastfmt      = null;
var tmo_done     = null;
var defaultcaptions = 'Artist and Title information will be available at the beginning of the next song';
var defaultcaptions_ns = 'Artist and Title information is currently<br>only available to Internet Explorer users';
var current_fmt  = '';
var thePlatform  = aba_HasURLParam('ref=msft');
var aba_is_nav4  = (aba_is_nav && (parseInt(navigator.appVersion)==4))?true:false;
var g_htmlview   = aba_HasURLParam("htmlview=1");



/* security update workaround 2004-02-02 */
//g_use_wmp7 = false;

/* start the captions timer, this number can be turned down to speed caption reaction */
//
// This is now done via event handler.  I suspect implemented by the folks that did
// the amazon buy-now code.  This is probably nice in that there is no caption update delay, 
// but probably breaks whatever caption support existed for non Internet Explorer browsers.  
// This checkCaptions function now returns after default
// captions have been set -- it no longer sets actual caption data.  This is done in javascript
// from player.html
//
var captimer    = setInterval('checkCaptions()', 2000);


/* start the playerstate timer, this number can be adjusted */
//var pstatetimer = setInterval('checkPlayerState()', 30000);

/* do the callback */
if (window.opener && window.opener.PlayerCallBack) {
	window.opener.PlayerCallBack();	
}
else if (top.window.opener && top.window.opener.PlayerCallBack) {
	top.window.opener.PlayerCallBack();	
}
	
/* grab the stream url parm and call load_stream if the page was loaded with stream/format attributes */
function doPageLoad() {

	pageload = true;

	/* load the stream */
	s = getURLParm('stream');
	u = getURLParm('u');
	f = getURLParm('format');
	
	if (f != "") {
		selectFormat (f);
	}	
	
	if (aba_is_ie) {
		if (s) {
			if (u=="true") {
				load_stream (s, '',true);	
			}
			else {
				load_stream (s,'',false);
			}
		}
	}

}


/* reload the player w/ the last format */
function relaunch() {
	done = true;
	if (!current_fmt) cmd = "document.location.reload(true);";
	else cmd  = "document.location.href='player.html?stream="+current_fmt+"';";
	setTimeout(cmd, 100);
	return true;
}



/* get a pararm value from the url */
function getURLParm(parm, seperator) {
	if (!parm) return '';
	if (!seperator) seperator = '&';
	parm += '=';
	
	
	/* search this doc */
	var s = String(window.location).toLowerCase();
	var i = s.indexOf(parm,0);
	
	/* search the frameset */
	if (i == -1) {
		s = String(top.window.location).toLowerCase();		
		i = s.indexOf(parm,0);
	}

	/* found parm, now get the value */
	if (i != -1) {
		/* move the index to the end of the keyword */
		i += parm.length;
		if (i == s.length) return '';
		
		/* find the ampersand */
		var ii = s.indexOf(seperator, i);
		if (ii == -1) ii = s.length;
		
		/* return the value */
		return s.substring(i, ii);
	}
	else return '';
		
}

function load_unicast_stream(fmt, description)
{
	load_stream (fmt, description, true);
}


/* start the stream.  If ref=msft, play thePlatform for the first stream only */
function load_stream(fmt, description, is_unicast) {
	/* if no format selected, return */
	if (!fmt) return;
	
	if (g_htmlview) return;

	/* store the current format, then set the selection images */
	if (tmo_done) clearTimeout(tmo_done); done = true;
	
	/* set formats, and format selection images */
	//current_fmt = fmt;
	//selectFormat (fmt);
	if (aba_is_ie) setHTML('HiddenCaptionDiv', 'default');
	if (aba_is_ie) setHTML('CaptionDiv', defaultcaptions);
		
	/* start the appropriate stream */
	if (!g_htmlview && (aba_DoUnicast() || is_unicast)) {
	//if (aba_DoUnicast() || is_unicast) {
		if ((firstRun&&playing) || !document.aba_MediaPlayer) { /* do nothing */ }
		else {
			setUniAsx(fmt);
			if (g_using_wmp7) {
				aba_MediaPlayer.URL = g_uniasxurl;
				setTimeout("aba_MediaPlayer.controls.Play();", 100);
			}
			else setTimeout("aba_MediaPlayer.Open('"+g_uniasxurl+"');", 100);
		}
	} 
	else {
		if (!playing) gateway = 'http://release.theplatform.com/content.select?pid=hi4MVMqSU0tfkKnLsbOaFtlzhxJxKVIH&UserName=Unknown';
		else gateway = '';
		
		if (g_using_wmp7) {
			aba_MediaPlayer.URL = fmt;
			setTimeout("aba_MediaPlayer.controls.Stop();", 100);
		}
		else setTimeout("aba_MediaPlayer.Stop();", 100);
	
		switch (fmt) {
	
			case 'cafe': 		if (thePlatform) GoAbacast('cafe.boomerradio.abacast.com:80/boomerradio-cafe-20','boomerradio-cafe-20',true,'http://asx.abacast.com/production/boomerradio/aba_cafe_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('cafe.boomerradio.abacast.com:80/boomerradio-cafe-20','boomerradio-cafe-20',true,'http://asx.abacast.com/production/boomerradio/aba_cafe_noad.asx', gateway);
								break;
							
			case 'cafebb': 		GoAbacast('cafe.boomerradio.abacast.com:80/boomerradio-cafe-48','boomerradio-cafe-48',true,'http://asx.abacast.com/production/boomerradio/aba_cafe_noad.asx', gateway);
								break;
							
			case 'sweetsoul':	if (thePlatform) GoAbacast('sweetsoul.boomerradio.abacast.com:80/boomerradio-sweetsoul-20','boomerradio-sweetsoul-20',true,'http://asx.abacast.com/production/boomerradio/aba_soul_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('sweetsoul.boomerradio.abacast.com:80/boomerradio-sweetsoul-20','boomerradio-sweetsoul-20',true,'http://asx.abacast.com/production/boomerradio/aba_soul_noad.asx', gateway);
								break;
	
			case 'classicmix':	if (thePlatform) GoAbacast('classicmix.boomerradio.abacast.com:80/boomerradio-classicmix-20','boomerradio-classicmix-20',true,'http://asx.abacast.com/production/boomerradio/aba_mix_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('classicmix.boomerradio.abacast.com:80/boomerradio-classicmix-20','boomerradio-classicmix-20',true,'http://asx.abacast.com/production/boomerradio/aba_mix_noad.asx', gateway);
								break;
	
			case 'classicmixbb':if (thePlatform) GoAbacast('classicmixbb.boomerradio.abacast.com:80/boomerradio-classicmixbb-48','boomerradio-classicmix-20',true,'http://asx.abacast.com/production/boomerradio/aba_mixbb_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('classicmixbb.boomerradio.abacast.com:80/boomerradio-classicmixbb-48','boomerradio-classicmix-20',true,'http://asx.abacast.com/production/boomerradio/aba_mixbb_noad.asx', gateway);
								break;
	
			case 'boomerjazz':	if (thePlatform) GoAbacast('smoothjazz.boomerradio.abacast.com:80/boomerradio-smoothjazz-20','boomerradio-smoothjazz-20',true,'http://asx.abacast.com/production/boomerradio/aba_jazz_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('smoothjazz.boomerradio.abacast.com:80/boomerradio-smoothjazz-20','boomerradio-smoothjazz-20',true,'http://asx.abacast.com/production/boomerradio/aba_jazz_noad.asx', gateway);
								break;
	
			case 'allhitoldies':if (thePlatform) GoAbacast('allhitoldies.boomerradio.abacast.com:80/boomerradio-allhitoldies-32','boomerradio-allhitoldies-32',true,'http://asx.abacast.com/production/boomerradio/aba_allhitoldies_noad.asx','http://release.theplatform.com/content.select?pid=bjoIJad3CSr7_S1C0Pz1nNhO4QyeKrkc&UserName=Unknown');
								else GoAbacast('allhitoldies.boomerradio.abacast.com:80/boomerradio-allhitoldies-32','boomerradio-allhitoldies-32',true,'http://asx.abacast.com/production/boomerradio/aba_allhitoldies_noad.asx', gateway);
								break;
	
			default:		 	alert ('invalid stream selection: ' + fmt);	
								break;
		}
	}
	
	
	/* dont run thePlatform more than once */
	thePlatform = false;
	firstRun = false;
	playing  = true;
	lastfmt  = fmt;
	tmo_done = setTimeout("done=false;", 5000);
	
	// reset default captions in the event that we're switching songs
	resetCaps();
}


function resetCaps()
{
	var l = document.getElementById("CaptionDiv");
	if (l) {
		l.innerTEXT = defaultcaptions; 
	}
}


/* parse the captions string */
function parseCaps(caps) {

	l = q = ai = 0;
	a = new Array();
	
	for (i = 0; i <= caps.length; i++) {
		
		c = caps.charAt(i);
		if (c=='"') q++;
		if (c==',' || (caps.length == i)) {
			if (q%2 == 0) { 
				a[ai++] = caps.substring(l, i);
				l = i+1;
			}
		}
	}
	return a;
}


function Status(msg) {
	window.status = msg;
}


/* check the hidden caption div for an update */
function checkCaptions() { 

	/* read the hidden Caption div contents */
	caps = String(getTEXT('HiddenCaptionDiv'));
	
	//Status(caps);
	
	/* dont do anyting if the caps haven't been updated */
	if (caps == lastcaption) return;

	/* set default caps if appropriate */
	if (caps == 'default') {
		if (g_using_wmp7 || aba_has_wmp9_up) { 
			setHTML('CaptionDiv', defaultcaptions);
			lastcaption = defaultcaptions;
			clearInterval(captimer);
		}
		else {
			if (aba_is_ie) setHTML('CaptionDiv', 'Captions are currently unavailable.');
			else setHTML(defaultcaptions_ns);
			clearInterval(captimer);
			return;
		}
		
	}
	else if (caps) {
	
		// dont execute here... all caption setting is done in javascript on player.html
		return;
	
		/* parse the media player captions */
		//c = parseCaps(caps);
		c = caps.split(';');

		for (i = 0; i < c.length; i++) {
			if (c[i] == 'undefined' || c[i] == '' || c[i] == ' ') c[i] = null;
		}

		/* build the new captions div innerHTML */
		r = '';
		if (c[0]) r += '<font face="Verdana, Arial, Helvetica, sans-serif" color="#E85E1D"><b>Artist:&nbsp;&nbsp;</b></font><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000">' + c[0] + '</font><br>';
		if (c[1]) r += '<font face="Verdana, Arial, Helvetica, sans-serif" color="#E85E1D"><b>Title:&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000">' + c[1] + '</font><br>';
		if (c[2]) r += '<font face="Verdana, Arial, Helvetica, sans-serif" color="#E85E1D"><b>Album:&nbsp;</b></font><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000">' + c[2] + '</font><br>';
		if (c[3]) r += '<font face="Verdana, Arial, Helvetica, sans-serif" color="#E85E1D"><b>Year:&nbsp;&nbsp;&nbsp;&nbsp;</b></font><font face="Verdana, Arial, Helvetica, sans-serif" color="#000000">' + c[3] + '</font><br>';
		
		/* write the formatted div innerHTML, and save the last caption */
		setHTML('CaptionDiv', r);
		lastcaption = r;
	}
}


/* get the innerHTML from a div on the page */
function getTEXT(id) 
{ 
	result = null;

	if (!document.layers) { 
		var l = null;
		if (document.getElementById) l = document.getElementById(id);
		else if (document.all) l = document.all[id];
		if (l && l.innerText) result = l.innerText;
	} else { 
		var l = document[id]; 
		if (l && l.innerText) { 
			result = l.innerText;
		} 
	} 
	if (!result) result = '';
	return result;
}


/* get the innerHTML from a div on the page */
function getHTML(id) 
{ 
	result = null;


	if (!document.layers) { 
		var l = null;
		if (document.getElementById) l = document.getElementById(id);
		else if (document.all) l = document.all[id];
		if (l && l.innerHTML) result = l.innerHTML;
	} else { 
		var l = document[id]; 
		if (l && l.innerHTML) { 
			result = l.innerHTML;
		} 
	} 
	if (!result) result = '';
	return result;
}


/* add to the innerHTML in a div on the page */
function addHTML(id,sHTML) 
{ 
	if (!document.layers) { 
		var l = null;
		if (document.getElementById) l = document.getElementById(id);
		else if (document.all) l = document.all[id];
		if (l && l.innerHTML) l.innerHTML += sHTML; 
	} else { 
		var l = document[id]; 
		if (l && l.innerHTML) { 
			l.innerHTML = l.innerHTML + sHTML; 
			l.document.write(l.innerHTML); 
			l.document.close(); 
		} 
	} 
}


/* set the innerHTML from a div on the page */
function setHTML(id,sHTML) 
{ 
	if (!document.layers) { 
		var l = null;
		if (document.getElementById) l = document.getElementById(id);
		else if (document.all) l = document.all[id];
		if (l && l.innerHTML) l.innerHTML = sHTML; 
	} else { 
		var l = document[id]; 
		if (l && l.innerHTML) { 
			l.innerHTML = sHTML; 
			l.document.write(l.innerHTML); 
			l.document.close(); 
		} 
	} 
}


/* set the last format image to the default white */
function resetFormatSelection() {
}


/* toggle the format image */
function selectFormat(stream){
	if (document.images) {
		var img = document.getElementById ("id210x78_channeldisplay");
		var img_name = "channels/" + stream + ".jpg";
		img.src = img_name;
	}
}


/* pushes play if the media player isn't paused or playing */
function checkPlayerState() {
	
	/* IE */
	if (aba_is_ie) { 
		pstate = aba_MediaPlayer.PlayState;
		
		/* if the player isn't playing or paused, restart it */
		if (pstate != 2 && pstate != 3) {
			Status('player inactive - restarting...');
			if (g_asxurl) aba_LaunchASX();
			else if (g_using_wmp7) aba_MediaPlayer.controls.Play();
			else aba_MediaPlayer.Play(); 
		}
	}

	/* Netscape */
	else { 
		pstate = document.aba_MediaPlayer.GetPlayState();
		if( pstate != 2 && pstate != 3 ) { 
			Status('player inactive - restarting...');
			if (g_asxurl) aba_LaunchASX();
			else document.aba_MediaPlayer.Play();
		}
	}
}


function setUniAsx (fmt) {
	/* set the unicast needed at the embedded player on page load */
	var str, uniasx;
	if (fmt) str = fmt;
	else str = getURLParm('stream', '&');
	
/*	
	if (str=='cafe')              uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/cafe.asx';
	else if (str=='cafebb')       uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/cafe.asx';
	else if (str=='sweetsoul')    uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/soul.asx';
	else if (str=='classicmixbb') uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/mix.asx';
	else if (str=='classicmix')   uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/mix.asx';
	else if (str=='boomerjazz')   uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/sjazz.asx';
	else if (str=='smoothjazz')   uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/sjazz.asx';
	else if (str=='allhitoldies') uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/hitoldies.asx';    
	else if (str=='vintagerock')  uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/vintagerock.asx';    
	else if (str=='lushlife')     uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/lushlife.asx';    
	else if (str=='sizzlinseventies')  uniasx = 'http://www1.abacast.com/demo/boomerradio/20080114/asx/seventies.asx'; 
	else uniasx = str;            // assume whatever we got was properly formatted
*/
	
	if (str=='cafe')              uniasx = 'http://www.boomerradio.com/cafe.asx';
	else if (str=='cafebb')       uniasx = 'http://www.boomerradio.com/cafe.asx';
	else if (str=='sweetsoul')    uniasx = 'http://www.boomerradio.com/soul.asx';
	else if (str=='classicmixbb') uniasx = 'http://www.boomerradio.com/mix.asx';
	else if (str=='classicmix')   uniasx = 'http://www.boomerradio.com/mix.asx';
	else if (str=='boomerjazz')   uniasx = 'http://www.boomerradio.com/sjazz.asx';
	else if (str=='smoothjazz')   uniasx = 'http://www.boomerradio.com/sjazz.asx';
	else if (str=='allhitoldies') uniasx = 'http://www.boomerradio.com/hitoldies.asx';    
	else if (str=='vintagerock')  uniasx = 'http://www.boomerradio.com/vintagerock.asx';    
	else if (str=='lushlife')     uniasx = 'http://www.boomerradio.com/lushlife.asx';    
	else if (str=='sizzlinseventies')  uniasx = 'http://www.boomerradio.com/seventies.asx'; 
	else if (str.indexOf ("http://") != -1) {
		// it has http in it, lets assume its the full URL
		uniasx = str;
	}
	else {
		// unrecognized stream.  lets assume that its sitting at the root of the boomer web server
		uniasx = 'http://www.boomerradio.com/' + str + '.asx';            
	}
	
	// set the global g_uniasxurl
	setUnicastASX(uniasx);
}

setUniAsx();
