// Script for NiftyPlayer 1.7, by tvst from varal.org
// Released under the MIT License: http://www.opensource.org/licenses/mit-license.php

var newFile=0;
var oldFile=0;

function getWindowSize() {
  var size = { w:0, h:0 };
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    size.w = window.innerWidth;
    size.h = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    size.w = document.documentElement.clientWidth;
    size.h = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    size.w = document.body.clientWidth;
    size.h = document.body.clientHeight;
  }
 return size;
}

function getPosition(obj) {
  var pos = { x:-830, y:-112 };
  var win = getWindowSize().w;
  do {
    pos.x += obj.offsetLeft;
    pos.y += obj.offsetTop;
  } while (obj = obj.offsetParent);
  pos.x -= (win-1975)/2;
  return pos;
}


function toggle(who, what){
	obj=document.getElementById(who);
	if(what=='pause'){
		obj.src="images/pause.png";
	}else{
		obj.src="images/play.png";
	}
}

function placePlayer(file){
	var obj = document.getElementById(file);
	var pos = getPosition(obj);
	var player = document.getElementById("player");
	player.style.left = (pos.x)+"px";
	player.style.top = pos.y+"px";
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
	player.style.left = (pos.x)+"px";
	player.style.top = (pos.y-18)+"px";
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
	player.style.left = (pos.x)+"px";
	player.style.top = pos.y+"px";
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
	player.style.left = (pos.x)+"px";
	player.style.top = pos.y+"px";
  }
}

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.getFile = function (url) {
		var file = this.obj.GetVariable('currentSong');
		return file;
	};

	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);
	};
	
	this.loadAndToggle = function (file) {
		oldFile = this.getFile();
		newFile = file;
		var obj = document.getElementById(file);
		var pos = getPosition(obj);
		var player = document.getElementById("player");
		player.style.visibility = "visible";
		if (this.getState() != "playing" && this.getState() != "paused"){
			this.loadAndPlay(file);
			placePlayer(newFile);
		} else {
			if (this.getFile() == file){
				this.playToggle();
			} else {
				this.loadAndPlay(file);
				placePlayer(newFile);
			}
		}
	}
	return this;
}

window.onload = function() {
	niftyplayer('niftyPlayer1').registerEvent('onPlay', 'if(newFile!=oldFile){toggle(newFile, "pause");toggle(oldFile, "play");}else{toggle(newFile, "pause");}');
	niftyplayer('niftyPlayer1').registerEvent('onStop', 'toggle(oldFile, "play")');
	niftyplayer('niftyPlayer1').registerEvent('onPause', 'toggle(oldFile, "play")');
	niftyplayer('niftyPlayer1').registerEvent('onError', 'toggle(oldFile, "play")');
	niftyplayer('niftyPlayer1').registerEvent('onSongOver', 'toggle(newFile, "play")');
}
