/***
*
*  Audio Handler Class:  
* 	
***/
var VideoHandler = new Class({
        //
		//  initialize
		//
		Implements: [Options,Events,EventManager],
        options: {
                media_id:"media_player",
                folder:"audio/",
				init_index:0,
                rate:35,
                initVolume:.15,
                togglePlay: true,
                toggleMute: true,
                autoPlay: false,
                preload: true,
                continuousPlay: true,
                wrap: true
		},
        initialize: function(container,playlist,options){
				this.isSeeking = false
                this.setOptions(options)
				this.playlist = playlist;
				this.reset()
                this.container = document.id(container)
				this.setMedia()
				if (this.setType()) {
						this.altContent = this.container.empty()
						this.setPlayer()
				} else {
						this.failed = true;
				}
        },		
        //
        //  controls
        //
        play: function(){
				if (this.media){
						this.state = "play"
						this.media.play()
						this.fireEvent("PLAY")
				} else {
						this.setMedia()
						this.setPlayer(true)
				}
        },
        pause: function(){
                this.state = "paused"
                this.media.pause()
                this.fireEvent("PAUSE")
        },
		stop: function(){
				this.state = "stopped"
				this.pullTimers()
                this.reset()
                this.removeMedia()
                this.fireEvent("STOP")
        },
        mute: function(){
				this.isMuted = true
                this.media.volume = 0
                this.fireEvent("VOLUME")
        },
        unmute: function(isscrubber){
				this.isMuted = false
                if (!isscrubber) this.media.volume = this.volume
                this.fireEvent("VOLUME")
        },
        setVolume: function(vol){
                if ($type(vol) == "number") this.volume = vol
                else this.volume = this.options.initVolume
                this.media.volume = this.volume
                this.fireEvent("VOLUME")
        },
		next: function(){
				if (this.index + 1 >= this.playlist.length) {
						if (this.options.wrap) this.loadTrack(0)
						else this.stop()
				} else {		
						this.loadTrack(this.index+1)
				}
		},
		previous: function(){
				if (this.index - 1 < 0) {
						if (this.options.wrap) this.loadTrack(this.playlist.length)
						else this.stop()
				} else {		
						this.loadTrack(this.index-1)
				}
		},
		loadTrack: function(idx){
				this.pullTimers()
				this.removeMedia()
				this.index = idx.limit(0,this.playlist.length-1)
				this.setInfo()
				this.setMedia()
				this.pushTimers(this.prepare,this.options.rate,this,"meta")
				if (this.isMuted) this.media.volume = 0
                else this.media.volume = this.volume
				if (this.state=="play") this.play()
				this.fireEvent("TRACK_LOADED")
		},
		seek: function(t,seeking){
				if (seeking) this.isSeeking = true
				if (t) {
						t.limit(0,this.duration)
				} else {
						this.pause()
						this.fireEvent("ERROR")
						t = 0
				}		
                this.media.currentTime = this.secs = t
                this.time = this.getTime(t)
				this.update()
        },
		//
		// update functions
		//
		updateInfo: function(name,album,year,artist){
				this.name = name ? name : ""
				this.album = album ? album : ""
				this.year = year ? year : ""
				this.artist = artist ? artist : ""
				this.fireEvent("INFO")
		},
		update: function(){
                this.secs = this.media.currentTime
                this.time  = this.getTime(this.secs)
				this.playProgress = this.secs/this.duration
				if (this.secs >= this.duration * .999 && !this.isSeeking) this.next()
                this.fireEvent("UPDATE")
        },
		reset: function(){
                this.secs = 0
                this.duration = 0
                this.time = "00:00"
                this.totaltime = "00:00"
				this.playProgress = 0
				this.index = this.options.init_index
				var info = this.playlist[this.index]
				this.updateInfo(info.get("name"),info.get("album"),info.get("year"),info.get("artist"))
                this.fireEvent("UPDATE")
        },
		//
		//  inital setup
		//
		setMedia: function(){
				this.media = new Element("video",{width:"950px", height:"475px", id:this.options.media_id})
				this.media_src = new Element("source",{src:"video/trailer_test" + "." + this.fileType})
				this.media.grab(this.media_src)
				this.container.grab(this.media)
				if (this.options.continuousPlay) {this.pushEvents(this.media,"ended",this.next.bind(this),"ended")}
		},
		removeMedia: function(){
				if (this.media) this.container.empty()
				this.media = null
		},
		setType: function(){
				var canplay = false
                if (this.media.canPlayType("audio/ogg")=="maybe") {
						this.fileType = "ogg";
						canplay = true
				} else if (this.media.canPlayType("audio/mpeg")=="maybe") {
						this.fileType = "mp4";
						canplay = true
				}
				return canplay
        },
		setPlayer: function(autoplay){
				this.removeMedia()
				if (this.options.autoPlay || autoplay) this.state = "play"
				this.loadTrack(this.options.init_index)
				this.setVolume()
				this.media.play()
		},
		setInfo: function(){
				var info = this.playlist[this.index]
				if ($type(info) == "string") {
						this.filePath = this.options.folder+info + "." + this.fileType
						updateInfo()
				} else {
						this.filePath = this.options.folder+info.get("path") + "." + this.fileType
						this.updateInfo(info.get("name"),info.get("album"),info.get("year"),info.get("artist"))
				}		
		},
		prepare: function(){
                if ($type(this.media.duration) == "number" && this.media.duration > 0){
                        this.pullTimers("meta")
                        this.duration = this.media.duration
                        this.totaltime = this.getTime(this.duration)
                        this.pushTimers(this.update,this.options.rate,this,"update")
                        this.fireEvent("READY")
		                this.update()
                } 
        },
		//
		// utils
		//
		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;
        }
});
