/*
################################################################
#Page: player.js
#Purpose: 
#Description: 
#Caveats: 
#Dependancies:
#Create Date: 23rd March 2007
#History:
#Author: Paul Hesketh
#params:
################################################################
*/
var isInternetExplorer = navigator.appName.indexOf("Microsoft") != -1;
var localTime = new Date()
var randomnumber=Math.floor(Math.random()*1000000) 
var fwto;
var fCounter = 0;

function fnWritePlayer(height,width) {
	var so = new SWFObject(sitePath+"flash/preload.swf", "webplayer", height, width, "8", "#000000");
	//var so = new SWFObject(sitePath+"flash/homePreload.swf", "webplayer", height, width, "8", "#000000");
	so.addParam("menu", "false");
	so.addParam("play", "true");
	so.addParam("scale", "noscale");
	so.addParam("salign", "lt");
	so.addParam("wmode", "transparent");
	//so.addVariable("bStream", "false");
	//so.addVariable("sConnectionName", "defected");
	so.addVariable("sDebug", "true");
	//so.addVariable("sSession", sSession);
	so.useExpressInstall(sitePath+'flash/expressinstall.swf');
	//document.write(so.write("flashPlayer"));
	so.write("flashPlayer");
	fnLoadFlashBanners();
	
	//manual call to preload tracks
	//fwto = window.setTimeout("loadPreselectedTracks()", 3000);
}


function fnWriteMiniPlayer(height,width) {
	
	var so = new SWFObject(sitePath+"flash/home/preload.swf?r="+randomnumber, "miniplayer", height, width, "8", "#000000");
	so.addParam("menu", "false");
	so.addParam("play", "true");
	so.addParam("scale", "noscale");
	so.addParam("salign", "lt");
	so.addParam("wmode", "transparent");
	so.addVariable("sDebug", "true");
	so.useExpressInstall(sitePath+'flash/expressinstall.swf');
	so.write("flashPlayer");
	fnLoadFlashBanners();
}


function loadPreselectedTracks() {
	if(fwto){window.clearTimeout(fwto)};
	//if we've tried 3 times, give up
	if(fCounter <4){
		// try loading the xml this time
		fnPreloadTracks();
		fCounter++;
	}
}


function fnLoadFlashBanners(){
	// now get the section and load the flash banners - 
	$loc = window.location.href;
	$vars = $loc.split('/');
	switch($vars[3]){
		case "store":	$location ="store";
		break;
		case "": $location ="home"
		break;
		default: $location ="general";
		break;
	}
	if($('banner1')){
		fnWriteBanner(1,$location,300,250);
	}
	if($('banner2')){
		fnWriteBanner(2,$location,728,90);
	}
	if($('banner3')){
		fnWriteBanner(3,$location,120,600);
	}
	if($('banner4')){
		fnWriteBanner(4,"store",120,600);	
	}
	//if($('banner5')){
	//	fnWriteBanner(5,"home",300,250);	
	//}	
}

var timeoutID = null;
var playerFocus = null;


function StartPlayer(){
	pwto = window.setTimeout("play()", 2000);	
}

function play(){
	if(pwto){window.clearTimeout(fwto)};
	if(document.webplayer.play){
		document.webplayer.play();
	}else{
		//we will use the embed instead;
		if(document.getElementById('webplayer')){
			document.getElementById('webplayer').play();
		}else{
			
		}
	}	
}



function StartHomePlayer(){
	pwto = window.setTimeout("homeplay()", 2000);	
}

function homeplay(){
	if(pwto){window.clearTimeout(fwto)};
	if(document.miniplayer.play){
		document.miniplayer.play();
	}else{
		//we will use the embed instead;
		if(document.getElementById('miniplayer')){
			document.getElementById('miniplayer').play();
		}else{
			
		}
	}	
}

function addTrackQS(qs){
	if(document.webplayer.addTrackQS){
		document.webplayer.addTrackQS(qs);
	}else{
		//we will use the embed instead;
		if(document.getElementById('webplayer')){
			document.getElementById('webplayer').addTrackQS(qs);
		}else{
			
		}
	}
}

function addTrackXMLAjax(id,play){
	var url = "/scripts/ajax/player_addTracks.php";
	var pars = "id="+id+"&play="+play;
	new Ajax.Request(url,{ 
			method:'post', 
			parameters: pars, 
			onComplete: addTrackXML
	});
}

function addTrackXML(http) {
	xml =http.responseText
	if(document.webplayer){
		if(document.webplayer.addTrackXML){
			document.webplayer.addTrackXML(xml);
			//alert("document.webplayer");
		}else{
			//we will use the embed instead;
			if(document.webplayer && document.webplayer.addTrackXML){
				document.webplayer.addTrackXML(xml);
				//alert("not document.webplayer");
			}else{
				
			}
		}
	}
}

function fnPreloadTracks(){
	if(xml>''){
		$found=0;
		if(document.miniplayer.addTrackXML){
			$found++;
			document.miniplayer.addTrackXML(xml);
		}else{
			//we will use the embed instead;
			if(document.getElementById('miniplayer')){
				document.getElementById('miniplayer').addTrackXML(xml);
				$found++;
			}
		}

		if($found==0){	
			fwto = window.setTimeout("loadPreselectedTracks()", 1500);	
		}else{
			//alert("found player on round "+fCounter);	
		}
	}
}

function radioClicked(){
	if(document.webplayer.addTrackQS){
		document.webplayer.addTrackQS($radioXML);
	}else{
		//we will use the embed instead;
		if(document.getElementById('webplayer')){
			document.getElementById('webplayer').addTrackQS($radioXML);
		}else{
			
		}
	}
	
}

function buyTrack(id){
	$vars = id.split('-');
	fmt = $vars[0];
	track = $vars[1];
	//alert("called "+track);
	
	var url = "/scripts/ajax/cart-add/"+fmt+"/d/"+track  //url+ '//' + bundle146;
	 var dummy = new Date().getTime(); // dummy value to avoid caching.
	 var pars = "action=test" + "&dummy=" + dummy;
	 new Ajax.Request(url,{
	 method:'post',
	 parameters: pars,
	 onComplete: fnLoadCartSuccessGeneric
	 });
	
	 return false;

}

function buyAllTracks(dbids){
	var str = "";
	for(i=0;i<dbids.length;i++){
		vars = dbids[i].split('_');
		var id = vars[0];
		if(str.indexOf(id+",")==-1){
			buyTrack(id);
			str+=id+",";
		}
	}
}

function fnUpdateCart(http){
	//alert(http.responseText);
	var url = "/include/ajaxFunctions.php";
	var pars = "action=editCart"
	new Ajax.Request(url,{ 
				method:'post', 
				parameters: pars, 
				onComplete: updateCart
		});
	return false;	
}
// Handle all the FSCommand messages in a Flash movie.
function main_DoFSCommand(command, args) {
	var mainObj = isInternetExplorer ? document.all.main : document.main;
	if ((command == "buyAction") || (command == "FSCommand:buyAction")){
		//code here
	}
	if ((command == "removeAction") || (command == "FSCommand:removeAction")){
		//code here
	}
	if ((command == "addAction") || (command == "FSCommand:addAction")){
		//code here
	}
	if ((command == "infoAction") || (command == "FSCommand:infoAction")){
		//code here
	}
}
	// Hook for Internet Explorer.
if (navigator.appName && navigator.appName.indexOf("Microsoft") != -1 && navigator.userAgent.indexOf("Windows") != -1 && navigator.userAgent.indexOf("Windows 3.1") == -1) {
	document.write('<script language=\"VBScript\"\>\n');
	document.write('On Error Resume Next\n');
	document.write('Sub main_FSCommand(ByVal command, ByVal args)\n');
	document.write('Call main_DoFSCommand(command, args)\n');
	document.write('End Sub\n');
	document.write('</script\>\n');
}
	