// 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;
}

var nfCookie = GetCookie("nf");
var as = (nfCookie == "off") ? 0 : 1;
var toggletxt = ['Turn Sound On','Turn Sound Off'];
var mp3s = ['/a/DVDmusic160K-01.mp3','/a/DVDmusic160K-02.mp3','/a/DVDmusic160K-03.mp3','/a/DVDmusic160K-04.mp3','/a/DVDmusic160K-05.mp3'];
var r=Math.floor(Math.random()*5)
var s = location.search.substring(1);

ss = s.split("&");
for (var i=0; i<ss.length; i++){

	var pair = ss[i].split("=");
	if (pair[0]=='mp3'){
		r = parseInt(pair[1])-1;
	}
}
var mp3 = mp3s[r];

function nftoggle(link){
	niftyplayer('niftyPlayer1').playToggle();
	as = (as+1)%2;
	var str= toggletxt[as];
	var obj = document.getElementById('soundcontrol');
	obj.innerHTML = str;
	if (as) {
		SetCookie("nf","on");
	}else{
		SetCookie("nf","off");
	}
	link.className = "play"+as;
}


function SetCookie (name,value,expires,path,domain,secure){
	document.cookie = name + "=" + escape(value) + ((expires) ? "; expires=" + expires.toGMTString() : "") + ((path) ? "; path=" + path : "") + ((domain) ? "; domain=" + domain : "") + ((secure) ? ";secure" : "")
};

function getCookieVal(offset){
	var endstr = document.cookie.indexOf(";", offset);
	if (endstr == -1){
		endstr = document.cookie.length;
	}
	return unescape(document.cookie.substring(offset, endstr));
};

function GetCookie(name){
	var arg = name + "=";
	var alen = arg.length;
	var clen = document.cookie.length;
	var i = 0;
	while (i < clen){
		var j = i + alen;
		if (document.cookie.substring(i,j) == arg){
			return this.getCookieVal(j);
		}
		i = document.cookie.indexOf(" ", i) + 1;
		if (i === 0){
			break;
		}
	}
	return null;
};

	document.write('<a id="soundcontrol" class="play'+as+'" href="#" onclick="nftoggle(this);return false;">'+toggletxt[as]+'</a><br><br><br>');
	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="niftyPlayer1" align="">');
	document.write('<param name=movie value="/a/niftyplayer.swf?file='+mp3+'&as='+as+'">');
	document.write('<param name=quality value=high>');
	document.write('<param name=bgcolor value=#FFFFFF>');
	document.write('<embed src="/a/niftyplayer.swf?file='+mp3+'&as='+as+'" quality=high bgcolor=#FFFFFF width="0" height="0" name="niftyPlayer1" align="" type="application/x-shockwave-flash" swLiveConnect="true" pluginspage="http://www.macromedia.com/go/getflashplayer">');
	document.write('</embed>');
	document.write('</object>');
	


