// 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];
	}
};

/* Hacked by Rob */
// http://stackoverflow.com/questions/901115/get-querystring-with-jquery
$.urlParam = function(name){
var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
if (!results) { return 0; }
return results[1] || 0;}

function LU_np(e, id) {
    var pos = $(e).offset();

    var as = 0; //$.urlParam('play') ? 1 : 0;
    if($("#NP").length == 0) {
	var np_html = '<div id="NP" style="display: block; position: absolute; top:'+pos.top+'px; left:'+pos.left+'px;">'+
	    '<h3>Eat the music with your ears!</h3>'+
	    '<p class="close"><a href="javascript:;">Close</a></p>'+
	    '<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="170" height="40" id="nPlayer" align="">'+
	    '<param name="movie" value="js/niftyplayer.swf?as='+as+'">'+
	    '<param name="quality" value="high">'+
	    '<param name="wmode" value="transparent">'+
	    '<embed src="js/niftyplayer.swf?as='+as+'" quality="high" wmode="transparent" width="170" height="40" name="nPlayer" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">'+
	    '</embed></object></div>';    
	$("body").append(np_html);
	$("#NP p.close a").click(function() {
	    LU_stop();
	    // not sure why I can't use $("#NP").hide() selector. Oh well
	    $(this).parent().parent().remove();
	});
    }
    else {
	// player already exists, just move to current Location
	//$("#NP").css('top:'+pos.top+'px; left: '+pos.left+'px'); // doesn't work?!
	$("#NP").css('left', pos.left+'px');
	$("#NP").css('top', pos.top+'px');
    }
    LU_play(id)
}
function LU_play(id) {
    if(niftyplayer("nPlayer") == undefined) {
	setTimeout('LU_play('+id+')', 100);
    }
    else {
	niftyplayer('nPlayer').loadAndPlay('stream.php?id='+id)
    }
}
function LU_stop() {
    if(niftyplayer("nPlayer") == undefined) {
	setTimeout('LU_stop()', 100);
    }
    else {
	niftyplayer('nPlayer').stop();
    }
}

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;
}

