var pl_hov="racingimages/play_hover.png"
var pl_down="racingimages/play_down.png"
var pl_norm="racingimages/play.png"
var pl_ds="racingimages/play_ds.png"

var ps_hov="racingimages/pause_hover.bmp"
var ps_down="racingimages/pause_down.bmp"
var ps_norm="racingimages/pause.bmp"
var ps_ds="racingimages/pause_ds.bmp"

var plps_hov=pl_hov;
var plps_down=pl_down;
var plps_norm=pl_norm;
var plps_ds=pl_ds;

var st_hov="racingimages/stop_hover.png"
var st_down="racingimages/stop_down.png"
var st_norm="racingimages/stop.png"
var st_ds="racingimages/stop_ds.png"

var mt_hov="racingimages/mute_hover.png"
var mt_down="racingimages/mute_down.png"
var mt_norm="racingimages/mute.png"
var mt_ds="racingimages/mute_ds.png"

var th_hov="racingimages/thumb_hover.bmp"
var th_down="racingimages/thumb_down.bmp"
var th_norm="racingimages/thumb.bmp"

var sc_hov="racingimages/schedule_over.png"
var sc_norm="racingimages/schedule.png"

var fr_hov="racingimages/racingforum_over.png"
var fr_norm="racingimages/racingforum.png"

var frm_hov="racingimages/racing_form_over.png"
var frm_norm="racingimages/racing_form.png"

var pdc_hov="racingimages/podcast_over.png"
var pdc_norm="racingimages/podcast.png"


var ch1_wmp_off 	= "racingimages/wmp_off.png";
var ch1_wmp_on 		= "racingimages/wmp_pressed.png";
var ch1_wmp_hover 	= "racingimages/wmp_on.png";
var ch1_flash_off 	= "racingimages/flash_off.png";
var ch1_flash_on 	= "racingimages/flash_pressed.png";
var ch1_flash_hover = "racingimages/flash_on.png";


//if(-1 == navigator.userAgent.indexOf("MSIE"))
{
	Prepare();
}
function Prepare()
{
	document.write('<p>');
	document.write('<table width="320" height="370" border="0" cellpadding="0" cellspacing="0" ID="Table1" style="position:absolute; left:0;top:0;">');
	document.write('<tr>');
	document.write('<td height="349" background="racingimages/player.png">');
	
    document.write('</td>');
	document.write('</tr>');
	
	document.write('<tr>');
	document.write('<td height="51" background="racingimages/foot.gif" class="text"" >');
	//ocument.write('<tr>');
	//document.write('<th height="39" scope="row"bgcolor="#E7E7E7" >');
    if(-1 == navigator.userAgent.indexOf("MSIE")) {
		var bWMPInst=0;
		for(var i=0;i < window.navigator.mimeTypes.length;i++) {
			try {
				if(window.navigator.mimeTypes.item(i).description=='np-mswmp') {
		 			bWMPInst=1;
		 		}
		 	} catch (e) {} // safari mac?	
		}
		if(bWMPInst==0) 
		{
			document.write('To use Windows Media on firefox <a href="http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx"onclick="openNewWindow(\'http://port25.technet.com/pages/windows-media-player-firefox-plugin-download.aspx\', \'_blank\');return false;">download this plugin</a>');
			//document.getElementById("MediaPlayer").innerHTML = ""; // blow away misconfigured wmp object.
		}
	}
	
	
	//document.write('<span id="errmessages" style="color:red;"></span><br><img id="hor_line1" name="hor_line1" src="racingimages/hor_line.png" border="0"/><br>');
	document.write('<span id="errmessages" style="color:red;"></span><br>');
	document.write(' For technical support contact <a href="mailto:radiosupport@timeform.com">radiosupport@timeform.com</a>');
	
    
    document.write('</td>');
	document.write('</tr>');
	
	document.write('</table>');
   

	document.write('<img id="play_img" name="play_img" src="racingimages/play.png" border="0" width="25px" height="25px" style="position:absolute; left:4;top:299;" onmousedown="OnPlayPause()" onmouseover="OnPlayPauseOver()" onmouseout="OnPlayPauseOut()" onmouseup="OnPlayPauseUp()"></img>');
	document.write('<img id="stop_img" name="stop_img" src="racingimages/stop.png" border="0" width="21px" height="21px" style="position:absolute; left:44;top:301;" onmousedown="OnStop()" onmouseover="changeImage(document.stop_img,\'stop\',st_hov)" onmouseout="changeImage(document.stop_img,\'stop\',st_norm)" onmouseup="changeImage(document.stop_img,\'stop\',st_hov)"></img>');
	
	document.write('<img id="mute_img" name="mute_img" src="racingimages/mute.png" border="0" width="21px" height="21px"style="position:absolute; left:238;top:301;"onmousedown="OnMute()" onmouseover="OnMuteOver()" onmouseout="OnMuteOut()" ></img>');
	//document.write('<img id="volume_img" name="volume_img" src="racingimages//volume_0.png" border="0" width="36px" height="19px"style="position:absolute; left:278;top:290;"onmousedown="OnMute()" onmouseover="OnMuteOver()" onmouseout="OnMuteOut()" ></img>');
	
	document.write('<img id="th_img" name="th_img" src="racingimages/thumb.bmp" border="0" width="9px" height="10px"style="position:absolute; left:265;top:308;z-index:3000"onmousedown="InitiateDrag_v(event)" onmouseover="document.th_img.src=th_hov" onmouseout="document.th_img.src=th_norm" onmouseup="document.th_img.src=th_norm" onchange="function{return}"></img>');
	
    document.write('<a href="http://www.timeform.com/display_articles.asp?page=Timeform_Radio.asp" target="_blank">');
	document.write('<img id="schedule_img" name="schedule_img" src="racingimages/schedule.png" border="0" width="191px" height="29px"style="position:absolute; left:64;top:161;"onclick="openNewWindow(\'http://www.timeform.com/display_articles.asp?page=Timeform_Radio.asp\', \'_blank\');return false;"onmousedown="" onmouseover="document.schedule_img.src=sc_hov" onmouseout="document.schedule_img.src=sc_norm" onmouseup="document.schedule_img.src=sc_hov"></img>');
	document.write('</a>');
	document.write('<a href="http://horseracing.betfair.com/form" target="_blank">');
	document.write('<img id="form_img" name="form_img" src="racingimages/racing_form.png" border="0" width="191px" height="29px"style="position:absolute; left:64;top:199;"onclick="openNewWindow(\'http://horseracing.betfair.com/form\', \'_blank\');return false;"onmousedown="" onmouseover="document.form_img.src=frm_hov" onmouseout="document.form_img.src=frm_norm" onmouseup="document.form_img.src=frm_hov"></img>');
	document.write('</a>');
	
	document.write('<a href="http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=302897742" target="_blank">');
	document.write('<img id="podcast_img" name="podcast_img" src="racingimages/podcast.png" border="0" width="191px" height="29px"style="position:absolute; left:64;top:238;"onclick="openNewWindow(\'http://itunes.apple.com/WebObjects/MZStore.woa/wa/viewPodcast?id=302897742\', \'_blank\');return false;"onmousedown="" onmouseover="document.podcast_img.src=pdc_hov" onmouseout="document.podcast_img.src=pdc_norm" onmouseup="document.podcast_img.src=pdc_hov"></img>');
	document.write('</a>');
	
	//document.write('<a href="http://horseracing.betfair.com" target="_blank">');
	//document.write('<img id="betfair_img" name="betfair_img" src="racingimages//betfair.gif" border="0" width="320px" height="22px"style="position:absolute; left:0;top:74;"onclick="openNewWindow(\'http://horseracing.betfair.com\', \'_blank\');return false;"onmousedown="" onmouseover="" onmouseout="" onmouseup=""></img>');
	//document.write('</a>');
	
	document.write('<a href="http://www.timeform.com" target="_blank">');
	document.write('<img id="timeform_img" name="timeform_img" src="racingimages/logo.png" border="0" width="320px" height="74px"style="position:absolute; left:0;top:0;"onclick="openNewWindow(\'http://www.timeform.com\', \'_blank\');return false;"onmousedown="" onmouseover="" onmouseout="" onmouseup=""></img>');
	document.write('</a>');
	
	document.write('<img id="ch1_flash" 	name="ch1_flash" 	src="racingimages/flash_off.png" 	border="0" width="190" height="31" style="cursor:pointer;position:absolute; left:64;top:89;" onmousedown="OnFlashChannel1()" onmouseover="if(currPlaying!=this.id){this.src=ch1_flash_hover}" onmouseout="if(currPlaying!=this.id){this.src=ch1_flash_off}" />');
	document.write('<img id="ch1_wmp" 		name="ch1_wmp" 		src="racingimages/wmp_off.png" 	border="0" width="190" height="31" style="cursor:pointer;position:absolute; left:64;top:120;" onmousedown="OnWMPChannel1()"  onmouseover="if(currPlaying!=this.id){this.src=ch1_wmp_hover}" onmouseout="if(currPlaying!=this.id){this.src=ch1_wmp_off}" />');

	



	document.write('<INPUT TYPE="Text" id="sttext" name="sttext" value="" style="font-size:10; position:absolute;border:none; left:55; top:330; color:#808080; background-color:black;width:250;height:17">')
	document.write('<INPUT TYPE="Text" id="tmtext" name="tmtext" value="" style="font-size:10; position:absolute;border:none; left:289; top:330; color:#808080; background-color:black;width:30;height:17">')
	document.stop_img.dsimg=st_ds;
	
	document.mute_img.dsimg=mt_ds;
	document.stop_img.enabledimg=st_norm;
	document.mute_img.enabledimg=mt_norm;
	document.play_img.enabledimg=pl_norm;
	document.play_img.dsimg=pl_ds;
	document.write('</p>');
	
	
	setTimeout("Timer_Check()",1000);
	setTimeout("OnTimer()",1000);
} 

function openNewWindow(url, target)
{
	window.open(url,target,'directories=yes,location=yes,menubar=yes,resizable=yes,scrollbars=yes,status=1,titlebar=yes,toolbar=yes',false);
}

function OnPlayPauseOver() {
	if (document.MediaPlayer.controls) {
		if(document.MediaPlayer.playState==3) {
			if (document.MediaPlayer.controls.isAvailable('pause'))	 {
				document.play_img.src=plps_hov;
				document.play_img.enabledimg=plps_hov;
			}
		} else {
			if (document.MediaPlayer.controls.isAvailable('play'))	 {
				document.play_img.src=plps_hov;
				document.play_img.enabledimg=plps_hov;
			}
		}
	}
}

function OnPlayPauseOut() {
	if (document.MediaPlayer.controls) {
		if(document.MediaPlayer.playState==3) {
			if (document.MediaPlayer.controls.isAvailable('pause'))	{
				document.play_img.src=plps_norm;
				document.play_img.enabledimg=plps_norm;
			}
		} else {
			if (document.MediaPlayer.controls.isAvailable('play')) {
				document.play_img.src=plps_norm;
				document.play_img.enabledimg=plps_norm;
			}
		}
	}
}

function OnPlayPauseUp() {
	if (document.MediaPlayer.controls) {
		if(document.MediaPlayer.playState==3) {
			if (document.MediaPlayer.controls.isAvailable('pause')) {
				document.play_img.src=plps_hov;
				document.play_img.enabledimg=plps_hov;
			}
		} else {
			if (document.MediaPlayer.controls.isAvailable('play')) {
				document.play_img.src=plps_hov;
				document.play_img.enabledimg=plps_hov;
			}
		}
	}
}

function CheckCtrl(imag,ctrl) {
	if(document.MediaPlayer.controls && document.MediaPlayer.controls.isAvailable(ctrl)) {
		imag.src=imag.enabledimg;
	} else {
		imag.src=imag.dsimg;
	}
}


function Timer_Check() {
	if (document.MediaPlayer.controls && !document.MediaPlayer.controls.isAvailable('pause')) {
		CheckCtrl(document.play_img,'play');
	}
	CheckCtrl(document.stop_img,'stop');
	if (document.MediaPlayer.settings) {
		var x=document.MediaPlayer.settings.volume*0.4+264;
		document.th_img.style.left = parseInt(x);// + 'px';
		document.getElementById("sttext").value=document.MediaPlayer.status;
		var p=document.MediaPlayer.controls.currentPosition;
		var m=parseInt(p/60);
		document.getElementById("tmtext").value=parseInt(m)+":"+parseInt(p-m*60);
	}
	//alert(document.MediaPlayer.status);
	setTimeout("Timer_Check()",1000);
}

function changeImage(imag,ctrl,newimg)
{
	imag.enabledimg=newimg;
	if(document.MediaPlayer.controls && document.MediaPlayer.controls.isAvailable(ctrl))
		imag.src=newimg;
	else
		imag.src=imag.dsimg;
}

function SetPlPs(bPlay)
{
	if(bPlay)
	{
		plps_hov=pl_hov;
		plps_down=pl_down;
		plps_norm=pl_norm;
	}
	else
	{
		plps_hov=ps_hov;
		plps_down=ps_down;
		plps_norm=ps_norm;	
	}
}

function OnPlayPause() {
	if (document.MediaPlayer.controls) {
		if(document.MediaPlayer.playState==3)
		{
			if (!document.MediaPlayer.controls.isAvailable('pause'))
				return;
			document.MediaPlayer.controls.pause();
			document.play_img.src=ps_down;
			SetPlPs(true);	
		}
		else
		{
			if (!document.MediaPlayer.controls.isAvailable('play'))
				return;
			document.play_img.src=pl_down;
			setTimeout("OnTimer()",500);
			document.MediaPlayer.controls.play();
			SetPlPs(false);
		}
	}
}

function OnMuteOver() {
	if (document.MediaPlayer.settings && !document.MediaPlayer.settings.mute) {
		document.mute_img.src=mt_hov;
	}
}

function OnMuteOut() {
	if (document.MediaPlayer.settings && !document.MediaPlayer.settings.mute) {
		document.mute_img.src=mt_norm;
	}
}

function OnTimer() {
	if (document.MediaPlayer.controls) {
		var p = document.MediaPlayer.controls.currentPosition;
		var l = 0;
		if(document.MediaPlayer.currentMedia) {
			l=document.MediaPlayer.currentMedia.duration;
		}
		if(l==0) {
			//document.seekth_img.style.left=16;
			return;
		}	
		var x=(288-15)*p/l+16;
		//document.seekth_img.style.left=x;
		var pls=document.MediaPlayer.playState;
		if(p!=l && (pls==3||pls==4||pls==5)) {
			SetPlPs(false);
			setTimeout("OnTimer()",500);
		} else {
			SetPlPs(true);
		}
	}
}

function OnStop() {
	changeImage(document.stop_img,'stop',st_down);
	document.MediaPlayer.controls.stop();
	SetPlPs(true);
	document.play_img.enabledimg=pl_norm;
	CheckCtrl(document.play_img,'play');
	
}

function hideWMPControls() {
	document.getElementById('play_img').style.display = "none";
	document.getElementById('stop_img').style.display = "none";
	document.getElementById('mute_img').style.display = "none";
	document.getElementById('th_img').style.display = "none";
	document.getElementById('sttext').style.display = "none";
	document.getElementById('tmtext').style.display = "none";
}

function showWMPControls() {
	document.getElementById('play_img').style.display = "";
	document.getElementById('stop_img').style.display = "";
	document.getElementById('mute_img').style.display = "";
	document.getElementById('th_img').style.display = "";
	document.getElementById('sttext').style.display = "";
	document.getElementById('tmtext').style.display = "";
}

function OnWMPChannel1() {
	currPlaying = "ch1_wmp";
	showWMPControls()
	document.getElementById("flashcontainer").style.left = "-500px";
    sendEvent('radioflash','stop');
    document.ch1_flash.src	=	ch1_flash_off;
    document.ch1_wmp.src	=	ch1_wmp_on;
    document.MediaPlayer.URL = "http://txradica.net/tune.php?c=betfair&.wax";
   	document.play_img.src=ps_norm;
	setTimeout("OnTimer()",500);
	tryPlayWMP()
    SetPlPs(false);
	setCookie("pchoice","wmp1",30);
}


var channel1Urls = [];
channel1Urls.push("rtmp://betfair1a.global-mix.net/radio1/instance1");
channel1Urls.push("rtmp://betfair1b.global-mix.net/radio1/instance1");


var delayForDeadServerCheck = 30000; // 30 seconds.

var initServerChoice = Math.floor(Math.random()*2); // 0 or 1. - basic load balancing.
var currServerChoice = initServerChoice;

function OnFlashChannel1() {
	var fileid = "audio1";
	currPlaying = "ch1_flash";
	hideWMPControls()
	tryStopWMP();
	document.getElementById("flashcontainer").style.left = "5px";
    document.ch1_flash.src	=	ch1_flash_on;
    document.ch1_wmp.src	=	ch1_wmp_off;
    sendEvent('radioflash','stop');
    loadFile('radioflash',{file:channel1Urls[currServerChoice],id:fileid})
    sendEvent('radioflash','playpause');
    setCookie("pchoice","flash1",30);
    window.setTimeout("checkChannel1OK()", delayForDeadServerCheck)
}

function checkChannel1OK() {
	if (currPlaying == "ch1_flash") {
		if (__flashplayerstate == 2 && __flashbuffering) {
			// everything is ok
		} else if (__flashplayerstate == 0 ) {
			// stopped on purpose
		} else { // either still buffering, or 'playing' a dead file (never went through buffering step.)
			// lets try the other server.
			if (currServerChoice==0) {
				currServerChoice = 1;
			} else {
				currServerChoice = 0;
			}
			OnFlashChannel1();
		}
	}
}


function tryStopWMP() {
	if (document.MediaPlayer.controls) {
		document.MediaPlayer.controls.stop();
	}
}
function tryPlayWMP() {
	if (document.MediaPlayer.controls) {
		document.MediaPlayer.controls.play();
	}
}

function OnMute() {
	if (document.MediaPlayer.settings) {
		if (document.MediaPlayer.settings.mute) {
			document.MediaPlayer.settings.mute=false;
			document.mute_img.src=mt_norm;
		} else {
			document.MediaPlayer.settings.mute=true;
			document.mute_img.src=mt_down;
		}
	}
}

function InitiateDrag(e)
{
    var evt = e||window.event;
    var startX = parseInt(evt.clientX-7);
	document.seekth_img.style.left = parseInt(startX);// + 'px'; 
    document.onmousemove = Drag;
    document.onmouseup = Drop; 
    return false;            
}

function Drag(e)
{
	
	var l=0;
	if(document.MediaPlayer.currentMedia)
	    l=document.MediaPlayer.currentMedia.duration;
	if(l==0)
	{
		document.seekth_img.style.left=16;
		return;
	}
	var p=document.MediaPlayer.controls.currentPosition;
	var x=(288-15)*p/l+16;
    var evt = e||window.event;
    var startX = parseInt(evt.clientX-7);
    if(startX>295)
    {
		document.MediaPlayer.controls.currentPosition=l;
		document.seekth_img.style.left = "288px";
		return;
    }
    if(startX<25)
    {
		document.MediaPlayer.controls.currentPosition=0;
		document.seekth_img.style.left = "16px";
		return;
    }
	document.seekth_img.style.left = parseInt(startX);// + 'px'; 
	document.MediaPlayer.controls.currentPosition=(startX+7-16)*l/(288-15);
}

function Drop(e) {
    var evt = window.event;
    {       
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

function InitiateDrag_v(e)
{
    var evt = e||window.event;
    var startX = parseInt(evt.clientX-4);
	document.th_img.style.left = parseInt(startX);// + 'px'; 
    document.onmousemove = Drag_v;
    document.onmouseup = Drop_v; 
    return false;            
}

function Drag_v(e) {
	if (document.MediaPlayer.settings) {
		var evt = e||window.event;
		var startX = parseInt(evt.clientX-4);
		 if(startX>303)
		{
			document.MediaPlayer.settings.volume=100;
			document.th_img.style.left = "304px";
			return;
		}
		if(startX<264)
		{
			document.MediaPlayer.settings.volume=0;
			document.th_img.style.left = "264px";
			return;
		}
		document.MediaPlayer.settings.volume=(startX-264)/0.4;
		document.th_img.style.left = parseInt(startX);// + 'px'; 
	}
}

function Drop_v(e) {
    var evt = window.event;
    {       
        document.onmouseup = null;
        document.onmousemove = null;
    }
}

function tripPlayer() {
	var pchoice = getCookie("pchoice");

	if (pchoice=="wmp1") {
		OnWMPChannel1();
	} else if (pchoice=="flash1") {
		OnFlashChannel1();
	} else {
		OnFlashChannel1();
	}
	
}









