// 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.playingClass = 'playing';
    this.pausedClass = 'paused';
    this.busyClass = 'busy';
    this.activeClass = 'active';
    this.soundClass = 'sound';
    
    this.div = $('#div_' + name);
    this.name = name;
    this.playlist = $('.playlist.' + this.activeClass); //active playlist element should have these classes

	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);
	};
    
    this.playOrPause = function() {
        var state = this.getState();
        
        if (state == 'empty' || state == 'error' )
        {
            this.playFirst();
        }
        else if ( state == 'finished' )
        {
            this.playNext();
        }
        else
        {
            this.playToggle();
        }
    }
    
    this.updateButtons = function() {
        var button = this.div.find('.play');
        if ( button ) {
            var state = this.getPlayingState();
            button.removeClass(this.pausedClass);
            button.removeClass(this.busyClass);
            button.removeClass(this.playingClass);
            if ( state == 'playing' )
            {
                button.addClass(this.playingClass);
            }
            else if ( state == 'loading' )
            {
                button.addClass(this.busyClass);
            }
            else {
                button.addClass(this.pausedClass);
            }
        }
        
    }
    
    this.updatePlaylist = function() {
        var active = this.findActiveSound();
        this.playlist.find('*').removeClass(this.activeClass);
        if ( activeElement )
        {
            activeElement.addClass(this.activeClass);
        }
    }
    
    this.updateAppearance = function() {
        updateButtons();
        //updatePlaylist();
    }
        
    this.registerStandardEvents = function() {
        if (this.obj.GetVariable('onSongOver')) {
            var player = "niftyplayer('" + this.name + "')";
            var update = player + ".updateAppearance()";
            registerEvent('onSongOver', player + ".playNext()");
            registerEvent('onStop', update);
            registerEvent('onPlay', update);
            registerEvent('onPause', update);
            registerEvent('onError', update);
            registerEvent('onBufferingComplete', update);
            registerEvent('onBufferingStarted', update);
        }
    }
    
    this.playAnchor = function(anchor) {
        var url = anchor.attr("href");
        if (url) {
            //set it to active
            this.playlist.find('*').removeClass(this.activeClass);
            anchor.closest("." + this.soundClass).addClass(this.activeClass);
            this.loadAndPlay(url);
        }
    }
    
    this.playElement = function(soundElement) {
        var anchor;
        if ( soundElement.is('a')) {
            anchor = sound;
        }
        else {
            anchor = soundElement.find('a').first();
        }
        playAnchor(anchor);
    }
    
    this.playByIndex = function(index) {
        var sound = findNth(index);
        playElement(sound);
    }
    
    this.playNext = function() {
        var next = this.findNext();
        if ( next.size() > 0 ) {
            this.playElement(next);
        }
        else {
            this.playlist.find('*').removeClass(this.activeClass);
            this.updateButtons();
        }
    }
    
    this.playFirst = function() {
        this.playElement(this.findFirst());
    }
    
    this.playPrevious = function() {
        var previous = this.findPrevious();
        this.playElement(previous);
    }
    
    this.findNth = function(index) {
        return this.playlist.find("." + this.soundClass).eq(index);
    }
    
    this.findFirst = function() {
        return this.playlist.find("." + this.soundClass).first();
    }
    
    this.findLast = function() {
        return this.playlist.find("." + this.soundClass).last();
    }
    
    this.findNext = function() {
        var active = this.findActiveSound();
        if (active.size() > 0){
            return active.next();
        }
        else {
            return this.findFirst();
        }
    }
    
    this.findPrevious = function() {
        var active = this.findActiveSound();
        if (active.size() > 0){
            return active.previous();
        }
        else {
            return this.findLast();
        }
    }
    
    this.findActiveSound = function() {
        return this.playlist.find("." + this.soundClass + "." + this.activeClass).first();
    }
    
    this.clearSelection = function() {
        updateAppearance($(''));
    }
    
    this.registerStandardEvents();
    
	return this;
}

function niftyClearSelection(query, activeClass)
{
    $(query).removeClass(activeClass);
}

function niftySimulateClickOnSong(query)
{
    $(query).click();
}


