// 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];
	}
};

function niftyplayer(name)
{
	this.obj = FlashHelper.getMovie(name);

	if (!FlashHelper.movieIsLoaded(this.obj)) return;

	this.play = function () {
		this.obj.TCallLabel('/','play');
	};

	this.stop = function () {
		this.obj.TCallLabel('/','stop');
	};

	this.pause = function () {
		this.obj.TCallLabel('/','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.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'
		return this.obj.GetVariable('playingState');
	};

	this.getLoadingState = function () {
		// returns 'empty', 'loading', 'loaded' or 'error'
		return this.obj.GetVariable('loadingState');
	};

	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;
}




// The options for a button press are:
//   Press play, nothing currently playing
//   Press play, currently on another player
//   Press stop
function buttonpress(song, buttonname)
{
	this.obj = FlashHelper.getMovie("niftyPlayer");

	pos = document[buttonname].src.indexOf('play.png');
	if (pos == -1) 
	{
		// Stop button pressed

		// stop playing
		this.obj.TCallLabel('/','stop');

		// change logo back to play
		document[buttonname].src="/ck-data/music/mp3/play.png";
	}
	else
	{
		// Play button pressed

		// scan through the document, and set any other logos back to play
		// this is in case 
		for (n = 0; n < 20; n++)
		{
			name = "play" + n;
			x = document.getElementsByName(String(name));
			if (x.length > 0)
			{
				document[name].src="/ck-data/music/mp3/play.png";
			}
		}

		// change this logo back to stop
		document[buttonname].src="/ck-data/music/mp3/stop.png";

		// start playing
		this.obj.SetVariable('currentSong', song);
		this.obj.TCallLabel('/','load');

		this.obj.TCallLabel('/','play');

	}

	return;
}


// Write in the niftyplayer flash object
document.write('<div id="player1">');
document.write('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="0" height="0" id="niftyPlayer" align="">');
document.write('<param name="quality" value="high" />');
document.write('<param name="movie" value="/cgi-bin/niftyplayer.swf" />');
document.write('<embed src="/cgi-bin/niftyplayer.swf" quality="high" type="application/x-shockwave-flash" height="0" width="0" name="niftyPlayer" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>');
document.write('</object>');
document.write('</div>');



