var player = null;
function playerReady(thePlayer) {
	player = document.getElementById(thePlayer.id);
	addListeners();
//
//var duration  = player.getConfig().duration;
//alert(duration);
// if($('#waveform').length){
//	$('#waveform').html('Duration:'+duration);
// }
}


function addListeners() {
	if (player) { 
		player.addModelListener("STATE", "stateListener");
		player.addModelListener("TIME",  "timeListener");	// every 1/10th ofsecond !!!
	} else {
		setTimeout("addListeners()",100);
	}
}

var timeListenerPace=0;	// minimum interval in s between updates (0=always = every 0.1s)
var timeListenerlast=null;	// minimum interval in s between updates (0=always = every 0.1s)

function timeListener(obj) {
	var d = obj.duration; 
	var p = obj.position; 

	if(timeListenerlast==null||((p-timeListenerlast)>timeListenerPace) ||((p-timeListenerlast)<0) ) {
		timeListenerlast=p;

		if ($('#waveform').length) { 

			var w=320;	//width of waveform in pixels
			var pp=0;	//pos from left in px

			if (! $('#waveform #wavetime').length) { 
				$('#waveform').append('<span id=wavetime></span>');
		//Q&D
		if (! $.browser.msie) $('#waveform #wavetime').css('top','-52px'); 
			}
			//plots the position OVER the waveform

			pp=parseInt( parseFloat(p)/parseFloat(d)*parseFloat(w) );
			$('#waveform #wavetime').css('left',pp+'px'); 
			//--$('#waveform').html(''+p+' / '+d); 
			//TOOMUCH if(isd) dbg(''+p+' / '+d+' px='+pp); 
		}
	}

}

function stateListener(obj) { //IDLE, BUFFERING, PLAYING, PAUSED, COMPLETED

timeListenerlast=null;	//forces update

	currentState = obj.newstate; 
	previousState = obj.oldstate; 

	//var tmp = document.getElementById("stat");
	if (isd) { 
		dbg("PLY newstate:" + currentState + " oldstate:" + previousState); 
	}

	if ((currentState == "COMPLETED")&&(previousState == "PLAYING")) {
//alert(player+'\nCOMPLETED');
//		document.location.href="http://www.test.com/players/jw-flv-player/"; 
		fcs_play_unlight();	//:-) or suggestments!!!
	}

	//updates the playtime cursor
	if ((currentState == "PLAYING")||(currentState == "PAUSED")) {
		$('#waveform #wavetime').show(); 
	} else {
		$('#waveform #wavetime').hide(); 
	}

}


		var s1 = new SWFObject("/sym/player/player.swf","player1","100%","100%","9","#FFFFFF");
		s1.addParam("allowfullscreen","false");
		s1.addParam("allowscriptaccess","always");
		//s1.addParam("flashvars","file=**=filename**&autostart=true"); //&image=preview.jpg
		s1.addParam("flashvars","autostart=true&image=http://www.showyourmusic.com/sym/images/sym_nodetails.jpg");
//		s1.addParam("flashvars","autostart=true"); //&image=preview.jpg
		s1.write("theplayer_container");

function fcs_createPlayer(svideo,stitle) {
	var flashvars = {
		file:svideo,
		image:"http://www.showyourmusic.com/sym/images/sym_nodetails.jpg",
		autostart:"true", 
		playlist:"bottom",
		playlistsize:"100",
		shuffle:"false",
		repeat:"always"
	}

	var params = {
		allowfullscreen:"false", 
		allowscriptaccess:"always"
	}

	var attributes = {
		id:"player1",  
		name:"player1"
	}

//	swfobject.embedSWF("player.swf", "theplayer_container", "100%", "100%", "9.0.115", false, flashvars, params, attributes);

		var s1 = new SWFObject("player.swf", "theplayer_container", "100%", "100%", "9.0.115", false, flashvars, params, attributes);
		s1.write("theplayer_container");


}

function fcs_playKO(svideo,stitle) {
$('#player_div_hide').hide(); //if exists
fcs_createPlayer(svideo,stitle);

$('#player_title').html(stitle);
    $('.fcsuserlink').click(function() {
	fcsglobaljqo=$(this);
	$('#col23out').fadeTo("slow", 0, function(){
		fcs_user_show(fcsglobaljqo);
	});
    });

}

var fcs_player_lastmediaurl;
function fcs_play(domobject,svideo,stitle) {
	if(domobject) fcs_play_hilight($(domobject));

$('#player_div_hide').hide(); //if exists
  if( (svideo!=fcs_player_lastmediaurl) ) {

$('#player_div').show('slow',function(){

player = document.getElementById('player1');
player.sendEvent('LOAD', URLDecode(svideo));
//player.sendEvent("PLAY","true");
$('#player_title').html(stitle);

    $('.fcsuserlink').click(function() {
	fcsglobaljqo=$(this);
	$('#col23out').fadeTo("slow", 0, function(){
		fcs_user_show(fcsglobaljqo);
	});
    });
}); //.show

	fcs_player_lastmediaurl=svideo;
  } else {
		//toggles playing
		player = document.getElementById('player1');
		player.sendEvent('PLAY');
  } //end-of if( (svideo!=fcs_player_lastmediaurl) )

}

var fcs_player_currentactiveitem; //jqo
var fcs_player_currentactiveitem_btn; //jqo of last active play button
var fcs_player_currentactiveitem_dom; //last clicked dom obj

function fcs_play_hilight(jqo) {

	if (fcs_player_currentactiveitem_btn && (jqo!=fcs_player_currentactiveitem_btn) ) fcs_player_currentactiveitem_btn.addClass('sym_song_btnplay').removeClass('sym_song_btnstop');

	if (jqo!=fcs_player_currentactiveitem_btn) {
	 fcs_player_currentactiveitem_btn=jqo;
	 fcs_player_currentactiveitem_btn.removeClass('sym_song_btnplay').addClass('sym_song_btnstop');
	}

	jqo=jqo.parents(".sym_song,.sym_song_twocols");
	if (fcs_player_currentactiveitem) fcs_player_currentactiveitem.removeClass('player_activeitem');
	fcs_player_currentactiveitem=jqo;
	fcs_player_currentactiveitem.addClass('player_activeitem');

}

function fcs_play_unlight() {
	//unlights last played song

	if (fcs_player_currentactiveitem_dom) {
		fcs_player_currentactiveitem_dom.attivo=false;
		fcs_player_currentactiveitem_dom=false;
	}


	if (fcs_player_currentactiveitem_btn) fcs_player_currentactiveitem_btn.addClass('sym_song_btnplay').removeClass('sym_song_btnstop');
	fcs_player_currentactiveitem_btn=false;

	if (fcs_player_currentactiveitem) fcs_player_currentactiveitem.removeClass('player_activeitem');
	fcs_player_currentactiveitem=false;

}


function fcs_play3(domobject,svideo,stitle,imgurl) {
//imgurl=unescape(imgurl);
if (isd) dbg(imgurl);
if (domobject.attivo) {
	$(domobject).removeClass('sym_song_btnstop');
	$(domobject).addClass('sym_song_btnplay');
	domobject.attivo=false;

	if (fcs_player_currentactiveitem) fcs_player_currentactiveitem.removeClass('player_activeitem');
	fcs_player_currentactiveitem=false;

		player = document.getElementById('player1');
		player.sendEvent('PLAY');
} else {

	fcs_play_hilight($(domobject));
	domobject.attivo=true;

  if(!domobject.initialized || (domobject!=fcs_player_currentactiveitem_dom) ) {

	fcs_player_currentactiveitem_dom=domobject;

	fcs_play_hilight($(domobject));
	$('#player_div_hide').hide(); //if exists

	if ($.browser.mozilla) {
		$('#player_div').show();
		p31=domobject;p32=svideo;p33=stitle;p34=imgurl;
		//dbg('moz!')
		setTimeout('fcs_play3_async()',500);
	} else

	$('#player_div').show('slow',function(){

		player = document.getElementById('player1');
	        //alert('p='+typeof player);
		player.sendEvent('LOAD', URLDecode(svideo));
	        //alert(URLDecode(svideo));
		//player.sendEvent("PLAY","true");
		s=URLDecode(imgurl);s=escape(s);
	if(isd)dbg(s);
		$('#player_title').html('<img src=\''+s+'\' class=sym_song_img /><br />'+stitle);

		    $('.fcsuserlink').click(function() {
			fcsglobaljqo=$(this);
			$('#col23out').fadeTo("slow", 0, function(){
				fcs_user_show(fcsglobaljqo);
			});
		    });
	}); //.show
	domobject.initialized=true;
  } else {
		//toggles playing
		player = document.getElementById('player1');
		player.sendEvent('PLAY');
  } //end-of if(!domobject.initialized)

} //end-of if (domobject.attivo)

}

var p31,p32,p33,p34;
function fcs_play3_async() {
		player = document.getElementById('player1');
		player.sendEvent('LOAD', URLDecode(p32));
		$('#player_title').html('<img src=\''+p34+'\' class=sym_song_img /><br />'+p33);
}

function URLEncode( string )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = string;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};

function URLDecode( string )
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = string;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
   return plaintext;
};

// end URLencode-decode

