/**
  * mp3playa - Streaming Flash Mp3 player
  *
  * Copyright (C) 2004,2005  troels knak-nielsen <troels@kyberfabrikken.dk>
  * 
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation;
  * version 2.1 of the License.
  * 
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  * 
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  * @version 1.2
  *
  */

Playa = {};

/**
  * Internal function to retrieve player-object
  * @private
  */
Playa.__getPlaya = function() {
	if (navigator.appName.indexOf ("Microsoft") !=-1) {
		var p = window["playa"];
	} else {
		var p = document["playa"];
	}
	if (typeof(p) != "undefined") {
		if (p.PercentLoaded() == 100) {
			return p;
		}
	}
	return null;
};

/**
  * This is the main loop
  * @private
  */
Playa.poll = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		var params = {};
		params.title = p.GetVariable("jsTitle");
		params.playListPosition = parseInt(p.GetVariable("jsPlayListPosition"));
		params.path = p.GetVariable("jsPath");
		params.btnState = p.GetVariable("jsBtnState");
		params.playlistSize = parseInt(p.GetVariable("jsPlaylistSize"));

		Playa.updateInterface(params);
	}
	setTimeout("Playa.poll()", 200);
};

/**
  * Starts the playa
  * @public
  */
Playa.start = function() {
	if (Playa.__isStarted) {
		return;
	}
	Playa.poll();
	Playa.__isStarted = true;
};

/**
  * Fire this event when user press the play/stop button
  * @public
  */
Playa.doPlayStop = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "PlayStop");
	}
};

/**
  * Starts playing, if the playback is stopped
  * @public
  */
Playa.doPlay = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Play");
	}
};

/**
  * Stops playback if it's playing
  * @public
  */
Playa.doStop = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Stop");
	}
};

/**
  * Fire this event when user press the next button
  * @public
  */
Playa.doNext = function() {
	var p = Playa.__getPlaya();
	if (p != null) {
		p.SetVariable("remoteRequest", "Next");
	}
};

/**
  * Update interface here
  * Most likely this is the only function you will need to tamper with
  *
  * params has the following attributes :
  *     playListPosition    Integer   The current index of the playlist (0-based)
  *     playlistSize        Integer   The total length of the playlist
  *     path                String    URL to the current playing file
  *     title               String    Title of the current playing file
  *     btnState            String    Play | Stop
  * @public
  */
Playa.updateInterface = function(params) {
	sTitle = (params.playListPosition+1) + "/" + params.playlistSize + " ";
	if (params.path == "") {
		sTitle += params.title;
	} else {
		sTitle += "<a href=\"" + params.path + "\" target=\"_blank\" title=\"Click to download\">" + params.title + "</a>";
	}

	document.getElementById("songTitle").innerHTML = sTitle;
	document.getElementById("btnPlayStop").value = params.btnState;
}

/*** Start processing */
Playa.start();


/*** Echange images */

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}