// Script for NiftyPlayer 1.7, by tvst from varal.org
// Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

var FlashHelper =
{
	movieIsLoaded : function (theMovie)
	{
		if (typeof(theMovie) != "undefined") return theMovie.PercentLoaded() == 100;
		else return
		false;
	},

	getMovie : function (movieName)
	{
  	if (navigator.appName.indexOf ("Microsoft") !=-1) return window[movieName];
	  else return document[movieName];
	}
};
var nifty_created_players = new Array();
var nifty_obj = new nifty();
function nifty()
{
	this.current_song=-1;
	this.playlist = new Array();
	this.play_time=0;
	this.play_start=0;
	this.current_player = null;
	setInterval('nifty_obj.ontimer();',100);

	
	this.get_player = function(id)
	{
		for (var i=0;i<nifty_created_players.length;i++)
		{
			if (nifty_created_players[i].id == id) 
			{
				if (!nifty_created_players[i].obj)
					nifty_created_players[i].init();
				if (nifty_created_players[i].obj)
					return nifty_created_players[i];
			}
		}
		return null;
	}
	this.playNextSong = function () 
	{
		var next_index=0;
		if (this.current_player)
		for (var i=0;i<nifty_created_players.length;i++)
			if (nifty_created_players[i].id == this.current_player.id) 
				next_index = i+1;
		if (next_index>=nifty_created_players.length) return;
		if (!nifty_created_players[next_index]) return;
		this.play_by_id(nifty_created_players[next_index].id);
	};
	this.add_song = function (file,id,length,player_name) 
	{
		var a = {'length':length,'file':file,'id':id};
		this.playlist[this.playlist.length] = a;
		var np = new niftyplayer(player_name,id);
		np.song = a;
		nifty_created_players[nifty_created_players.length] = np;
		
	};
	this.play_by_id = function (id) 
	{
		if (this.current_player) this.current_player.pause();
		var np = this.get_player(id);
		if (!np) return;
		if (np.getLoadingState() == "empty")
			np.load(np.song.file);
		if (np.getPlayingState() == "finished")
			np.setSongPos(0);

		np.play();
		this.current_player = np;
		return false;
	};
	this.getX = function (e)
	{
		var x=0;
		while (e)
		{
			x+=e.offsetLeft;
			e = e.offsetParent;
		}
		return x;
	}

	this.skip_by_id = function(id,event,w)
	{ 
		var e;
		if (event) e = event; else e = window.event;
		var d = (e.srcElement)?e.srcElement:e.target;
		if (!this.current_player || this.current_player.id != id) 
			this.play_by_id(id);
		if (this.current_player && this.current_player.obj)
		{
			this.current_player.pause();
			var x = e.clientX;
			x = x-this.getX(d);
			var sd = this.current_player.song.length;
			var val = Math.floor(x*sd/w);
			this.current_player.setSongPos(val);
			this.current_player.play();
		}
		return false;
	}
	this.pause = function () {
		if (this.current_player) this.current_player.pause();
		return false;
	};


	this.ontimer = function () 
	{
		for (var i=0;i<nifty_created_players.length;i++)
			if (nifty_created_players[i].getLoadingState() == "loading") 
				nifty_created_players[i].ontimer();
		if (this.current_player)
			this.current_player.ontimer();
	};


	
}

function niftyplayer(name,nid)
{
	this.name = name;
	this.id = nid;
	this.song = null;
	this.obj = null;


	this.init = function() 
	{
		this.obj = FlashHelper.getMovie(name);
		if (!FlashHelper.movieIsLoaded(this.obj)) return;

		
		this.registerEvent('onplay','if (nifty_obj.get_player('+this.id+')) nifty_obj.get_player('+this.id+').onplay();');
		this.registerEvent('onstop','if (nifty_obj.get_player('+this.id+')) nifty_obj.get_player('+this.id+').onstop();');
		this.registerEvent('onpause','if (nifty_obj.get_player('+this.id+')) nifty_obj.get_player('+this.id+').onpause();');
		this.registerEvent('onerror','if (nifty_obj.get_player('+this.id+')) nifty_obj.get_player('+this.id+').onerror();');
		this.registerEvent('onSongOver','if (nifty_obj.get_player('+this.id+')) nifty_obj.get_player('+this.id+').onsongover();');
	}


	this.onplay = function () 
	{
	};
	this.onstop = function () 
	{
		
	};
	this.onpause = function () {
	};
	this.onerror = function () {
		nifty_obj.playNextSong();
	};
	this.onsongover = function () {
		this.show_stop();
		this.pause();
		this.setSongPos(0);
		this.play();
		this.pause();

		nifty_obj.playNextSong();
	};
	this.ontimer = function()
	{
		var l = this.song.length*1000;
		this.play_time = this.getSP();
		var st = this.getST();
		var sl = this.getSL();
       		var ii = document.getElementById('time'+this.id);
		if (ii && l>0 && this.play_time<l) 
		{
			if (ii.parentNode)
			{
				var w = ii.parentNode.offsetWidth;
				ii.style.width=this.play_time*w/l+"px";
			}
		}
       		var ii = document.getElementById('progress'+this.id);
		if (ii && sl>0 && sl<=st) 
		{
			if (ii.parentNode)
			{
				var w = ii.parentNode.offsetWidth;
				ii.style.width=sl*w/st+"px";
			}
		}


	}



	this.show_stop = function()
	{
		var ii = document.getElementById('pause_button'+this.id);
		if (ii) ii.style.display='none';
		var ii = document.getElementById('play_button'+this.id);
		if (ii) ii.style.display='block';
		var ii = document.getElementById('time'+this.id);
		if (ii) ii.style.width='0px';
	}
	this.show_pause = function()
	{
		var ii = document.getElementById('pause_button'+this.id);
		if (ii) ii.style.display='none';
		var ii = document.getElementById('play_button'+this.id);
		if (ii) ii.style.display='block';
	}

	this.show_play = function()
	{
		var ii = document.getElementById('pause_button'+this.id);
		if (ii) ii.style.display='block';
		var ii = document.getElementById('play_button'+this.id);
		if (ii) ii.style.display='none';
		var ii = document.getElementById('progress'+this.id);
		if (ii) ii.style.width='0px';
	}
	this.play = function () {
		this.obj.TCallLabel('/','play');
		this.show_play();
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
		this.show_stop();
	};

	this.pause = function () {
		this.obj.TCallLabel('/','pause');
		this.show_pause();
	};

	this.playToggle = function () {
		this.obj.TCallLabel('/','playToggle');
	};

	this.reset = function () {
		this.obj.TCallLabel('/','reset');
	};

	this.load = function (url) {
		this.obj.SetVariable('currentSong', url);
		this.obj.TCallLabel('/','load');
	};

	this.loadAndPlay = function (url) {
		this.load(url);
		this.play();
	};

	this.getSP = function () {
		//song position
		if (this.obj)
			return this.obj.GetVariable('sP');

	};
	this.getSD = function () 
	{
		//song duration
		if (this.obj)
			return this.obj.GetVariable('sD');
	};
	this.getST = function () {
		//song total
		if (this.obj)
			return this.obj.GetVariable('sT');
	};
	this.getSL = function () 
	{
		//song loaded
		if (this.obj)
			return this.obj.GetVariable('sL');
	};
	this.getSongPos = function () 
	{
		//song loaded
		if (this.obj)
			return this.obj.GetVariable('songPosition');
	};
	this.setSongPos = function (p) 
	{
		//song loaded
		if (this.obj)
			return this.obj.SetVariable('songPosition',p);
	};

	this.getState = function () {
		var ps = this.obj.GetVariable('playingState');
		var ls = this.obj.GetVariable('loadingState');

		// returns
		//   'empty' if no file is loaded
		//   'loading' if file is loading
		//   'playing' if user has pressed play AND file has loaded
		//   'stopped' if not empty and file is stopped
		//   'paused' if file is paused
		//   'finished' if file has finished playing
		//   'error' if an error occurred
		if (ps == 'playing')
			if (ls == 'loaded') return ps;
			else return ls;

		if (ps == 'stopped')
			if (ls == 'empty') return ls;
			if (ls == 'error') return ls;
			else return ps;

		return ps;

	};

	this.getPlayingState = function () {
		// returns 'playing', 'paused', 'stopped' or 'finished'
		if (this.obj)
			return this.obj.GetVariable('playingState');
		return "";
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		if (this.obj)
			return this.obj.GetVariable('loadingState');
		return "";
	};

	this.registerEvent = function (eventName, action) {
		// eventName is a string with one of the following values: onPlay, onStop, onPause, onError, onSongOver, onBufferingComplete, onBufferingStarted
		// action is a string with the javascript code to run.
		//
		// example: niftyplayer('niftyPlayer1').registerEvent('onPlay', 'alert("playing!")');

		this.obj.SetVariable(eventName, action);
	};
	return this;
}
