/***
*
*  Media Handler Class:  for use with audio/video players
* 	
***/
var MediaHandler = new Class({
        //
		//  initialize
		//
		Implements: [Options,Events],
        options: {
                media_id:"media_player",
                folder:"audio",
                fileProperty:"file",
                autoPlay: true,
                continuousPlay: true,
                wrap: true
		},
        initialize: function(player,medialist,options){
				this.setOptions(options)
				this.player = player
				this.player.addEvent("PROGRESS",this.progress.bind(this))
				if (medialist) this.setMedia(medialist)
				this.loadCount = 0
				if (this.options.autoPlay) this.load(0)
		},
		setMedia: function(medialist){
				this.mediaList = typeOf(medialist) == "array" ? medialist : [medialist]
		},
		//
		//	ACTIONS
		//
		load: function(idx,pass){
				this.loadCount++
				if (this.loadCount>1) this.error = "LOOP ERROR"
				else {
						if (!(idx || idx===0)) this.resetPlayer()
						else {
								this.loadCount = 0
								this.index = idx
								this.setCurrent()
								this.player.loadFile(this.path)
								this.eventType = "load"
								if (!pass) this.fireEvent("HANDLER_EVENT")
						}
				}
				return this
		},
		play: function(pass){
				this.player.play()
				this.eventType = "play"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return
		},
		pause: function(pass){
				this.player.pause()
				this.eventType = "pause"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		stop: function(pass){
				this.player.stop()
				this.eventType = "stop"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		next: function(pass){
				if (this.index + 1 < this.mediaList.length) this.load(this.index+1)
				else {
						if (this.options.wrap) this.load(0)
						else this.stop()
				}
				this.eventType = "next"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		prev: function(pass){
				if (this.index - 1 >= 0) this.load(this.index-1)
				else {
						if (this.options.wrap) this.load(this.mediaList.length)
						else this.stop()
				}
				this.eventType = "prev"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		mute: function(pass){
				this.player.mute()
				this.eventType = "mute"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		unmute: function(pass){
				this.player.unmute()
				this.eventType = "unmute"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		resetPlayer: function(pass){
				if (this.options.autoPlay) this.load(0,!pass)
				else this.load(0,true).pause(!pass)
				this.eventType = "reset"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},		
		//
		//  INTERNAL
		//
		setCurrent: function(pass){
				this.info = this.mediaList[this.index]
				this.path = this.options.folder + "/" + this.info[this.options.fileProperty] + "." + this.player.fileType
				this.eventType = "new"
				if (!pass) this.fireEvent("HANDLER_EVENT")
				return this
		},
		progress: function(pass){
				if (this.player.secs >= this.player.duration * .999 && !this.player.isSeeking) {
						this.eventType = "finished"
						if (!pass) this.fireEvent("HANDLER_EVENT")
						if (this.options.continuousPlay) this.next()
				}		
                this.fireEvent("PROGRESS")
        }
});
