/***
*
*  Audio Player Class:  
* 	
***/
var AudioPlayer = new Class({
        //
		//  initialize
		//
		Implements: [Options,Events],
        options: {
                media_id:"media_player",
                folder:"audio/",
				init_index:0,
                fps:150,
                initVolume:.75,
                togglePlay: true,
                toggleMute: true,
                autoPlay: false,
                preload: true,
                continuousPlay: true,
                wrap: true,
				seek_incr:750
		},
        initialize: function(container,options){
				if (!this.hasHTML5(container)) this.failed = true
				else {
						this.failed = false
						this.setOptions(options)
						this.container = document.id(container)
						this.container.empty()
						this.isSeeking = false
						this.reset()
				}
		},
		loadFile: function(pth){
				this.removeMedia()
				this.setMedia()
		        this.media.set("src",pth)
				this.metaTimer = this.meta.periodical(this.options.fps,this)
				if (this.isMuted) this.media.volume = 0
                else this.media.volume = this.volume
		},
        //
        //  PLAYER
        //
        play: function(){
				this.state = "playing"
				this.media.play()
				this.eventType = "play"
				this.fireEvent("PLAYER_EVENT")
        },
        pause: function(pass){
                if (!pass) this.state = "paused"
                this.media.pause()
                this.eventType = "pause"
				this.fireEvent("PLAYER_EVENT")
        },
		stop: function(){
				this.state = "stopped"
				if (this.updateTimer) clearTimeout(this.updateTimer)
                this.reset()
                this.removeMedia()
                this.eventType = "stop"
				this.fireEvent("PLAYER_EVENT")
        },
        mute: function(){
				this.isMuted = true
                this.media.volume = 0
                this.eventType = "mute"
				this.fireEvent("PLAYER_EVENT")
        },
        unmute: function(isscrubber){
				this.isMuted = false
                if (!isscrubber) this.media.volume = this.volume
                this.eventType = "unmute"
				this.fireEvent("PLAYER_EVENT")
        },
        setVolume: function(vol){
                if (typeOf(vol) == "number") this.volume = vol
                else this.volume = this.options.initVolume
                this.media.volume = this.volume
                this.eventType = "volume"
				this.fireEvent("PLAYER_EVENT")
        },
		seek: function(num,istime){
				this.isSeeking = true
				var t = istime ? num : this.media.currentTime + num * this.options.seek_incr/1000
				t.limit(.5,this.media.duration - 1)
				this.media.currentTime = this.secs = t
                this.time = this.getTime(t)
				this.update()
        },
		stopSeek: function(){
				this.isSeeking = false
				this.eventType = "seek"
				this.fireEvent("PLAYER_EVENT")
		},
		//
		//  LISTENERS 
		//
		meta: function(){
                if (typeOf(this.media.duration) != "number" || this.media.duration <= 0) this.fireEvent("LOADING")
				else {
						if (this.metaTimer) clearTimeout(this.metaTimer)
                        this.duration = this.media.duration
                        this.totalTime = this.getTime(this.duration)
						this.updateTimer = this.update.periodical(this.options.fps,this)
						if (this.state=="playing" || (!this.state && this.options.autoPlay)) this.play()
						this.eventType = "meta"
						this.fireEvent("PLAYER_EVENT")
                } 
        },
		update: function(){
                this.secs = this.media.currentTime
                this.time  = this.getTime(this.secs)
				this.playProgress = this.secs/this.duration
                this.fireEvent("PROGRESS")
        },
		//
		//	INTERNAL 
		//
		reset: function(state){
				if (!(this.volume || this.volume===0)) this.volume = this.options.initVolume
                this.secs = 0
                this.duration = 0
                this.time = "00:00"
                this.totalTime = "00:00"
				this.playProgress = 0
                this.eventType = "reset"
				this.fireEvent("PLAYER_EVENT")
        },
		setMedia: function(){
				this.media = new Element("audio",{id:this.options.media_id})
				this.container.grab(this.media)
		},
		removeMedia: function(){
				if (this.media) this.container.empty()
				this.media = null
		},
		//
		// UTILS
		//
		hasHTML5: function(container){
				var tmp = new Element("audio")
				tmp.setStyle("display","none")
				document.id(container).grab(tmp)
				var canplay = false
				try { 
						if (tmp.canPlayType("audio/mpeg")=="probably") {
								this.fileType = "mp3";
								canplay = true
						} else if (tmp.canPlayType("audio/ogg")=="probably") {
								this.fileType = "ogg";
								canplay = true
						} else if (tmp.canPlayType("audio/mpeg")=="maybe") {
								this.fileType = "mp3";
								canplay = true
						} else if (tmp.canPlayType("audio/ogg")=="maybe") {
								this.fileType = "ogg";
								canplay = true
						} 
				} catch (err) {
						this.error = err
				}
				tmp.destroy()
				return canplay
		},
		getTime: function(t){
                mins = Math.floor(t / 60);
                secs = Math.round(t % 60);
                mins = (mins >= 10) ? mins : "0" + mins;
                secs = (secs >= 10) ? secs : "0" + secs;
                return mins + ":" + secs;
        }
});
