
var playerState = 0;
var currentPlaylistUrl = "";
var createdPlayer = false;

jQuery(
	   function() {
		

		 $(".roundedAliased").corner(
			{
				tl: { radius: 4 },
				tr: { radius: 4 },
				bl: { radius: 4 },
				br: { radius: 4 },
				antiAlias: true
			}
		);
	
		
		$(".playButton").live("click", play);
		
		
		if ($.browser.msie && ($.browser.version < 7)) {	// horrid hack for IE6 which doesn't position:fixed
			$("#mediaPlayerContainerContainer").css("position", "absolute");
			$(window).bind("scroll", setPlayerPosition);
			
			function setPlayerPosition() {
				var offset = $("html").scrollTop();
				$("#mediaPlayerContainerContainer").css("top", offset+10+"px");
			}
		}

	}
);


function createPlayer(playListUrl, playButton) {
	if (!createdPlayer) {
		var s1 = new SWFObject('/flash/mp3player.swf','player','300','100','8');
		s1.addParam('allowscriptaccess','always');
		s1.addVariable('width','300');
		s1.addVariable('height','100');
		s1.addVariable('javascriptid','player');
		s1.addVariable('enablejs','true');
		s1.addVariable("file",playListUrl);
		s1.addVariable("repeat","true");
		s1.addVariable("showdigits","true");
		s1.addVariable("showstop","false")
		s1.addVariable("autoscroll","false");
		s1.addVariable("autostart", "true");
		s1.addVariable("showeq","true");
		s1.addVariable("showicons","true");
		s1.addVariable("lightcolor","0x3366CC");
		s1.addVariable("backcolor","0xF0ECE6");
		s1.addVariable("displayheight","70");
		s1.addVariable("displaywidth","100");
		s1.addVariable("shuffle","false");
		s1.addVariable("thumbsinplaylist","false");
		s1.write('mediaPlayerContainer');
		createdPlayer = true;

		$("#mediaPlayerContainerContainer").show(750);
		
		
	}
}

function removePlayer() {
	stopPlaying();
	$("#mediaPlayerContainerContainer").hide();
	createdPlayer = false;
}

function stopPlaying() {
	if (playerState > 0) { 	// player is currently playing, so we can stop it now.
		sendEvent("player", "stop");
	}
}

function play() {
	// find the play list url...

	var prevPlayList = currentPlaylistUrl;
	var playListUrl = $(this).parents(".searchResult").find(".playListUrl").val();
	

	currentPlaylistUrl = playListUrl; 
	
	if (!createdPlayer) {
		
		createPlayer(playListUrl, this);
		return false;
	}

	var start = false;
	
	if (playerState > 0) { 	// player is currently playing, so we can stop it now.
		sendEvent("player", "stop");

	} else {	// player is not currently playing, so we need to start it, no matter which button we clicked
	
		if (currentPlaylistUrl == prevPlayList) {	
			start = true;	// there is no need to manually start the player if we are about to load a new play list
		}		
	}
	
	if (  (currentPlaylistUrl != prevPlayList) ) {	// we clicked on a different button, so we need to load a new play list and start playing 
		loadFile("player", {file:playListUrl});	// the plaer is set to auto play so we don't need to start it manually.
	}
	
	if (start) {		
		sendEvent("player", "playitem",0);
	}
	
	return false;
	
}

function loadFile(swf,obj) { 
	thisMovie(swf).loadFile(obj); 
};

function sendEvent(swf,typ,prm) { 
	thisMovie(swf).sendEvent(typ,prm); 
};

function getUpdate(typ,pr1,pr2,swf) { 
	//console.log(typ + " : " + pr1 + " : " + pr2 + " : " + swf);
	if (typ == "state") {	// we have started or stopped - update the buttons appropriatly.
		//console.log(pr1);
		playerState = pr1;	// 0 = stopped, 2 = buffering, 2 = playing
		
		if (playerState ==0) {	
			$(".playButton").val("Listen...");
		} else {
			
			$("input[value=" + currentPlaylistUrl + "]").parents(".resultContainer").find(".playButton").val("Stop...");

		}
	}
};

function thisMovie(swf) {
	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[swf];
	} else {
		return document[swf];
	}
};

	

