/***
*
*  Controls for Audio and Video  
*
***/
var PlayerControls = new Class({
        //
		//  initialize
		//
		Extends:Controls,
		//Implements: [Options,Events],
        options: {
				fps:100,
				quietSeek: false
		},
        initialize: function(media,container,options){
                this.setOptions(options)
				this.parent(media,container,options)
                this.media = media
		},
		//
		//  BUTTONS
		//
		muteButtons: function(mt,umt,container,ismuted) {
				if (!container) container = this.container
				if (container) {
						var box = document.id(container)
						this.mute_btn = new Element("div",{id:mt})
						this.unmute_btn = new Element("div",{id:umt})
						box.grab(this.mute_btn)
						box.grab(this.unmute_btn)
				} else {
						this.mute_btn = document.id(mt)
						this.unmute_btn = document.id(umt)
				}
				if (ismuted){
						this.mute_btn.setStyle("display","none")
						this.unmute_btn.setStyle("display","block")	
				} else {
						this.unmute_btn.setStyle("display","none")
						this.mute_btn.setStyle("display","block")
				}
				this.mute_btn.addEvent("click",this.mute.bind(this));
				this.unmute_btn.addEvent("click",this.unmute.bind(this));
				this.elements.push(this.mute_btn)
				this.elements.push(this.unmute_btn)
				return this
		},	
		timeDivs: function(tm,dur,container){
				if (!container) container = this.container
				if (container) {
						var box = document.id(container)
						this.time_div = new Element("div",{id:tm})
						this.duration_div = new Element("div",{id:dur})
						box.grab(this.time_div)
						box.grab(this.duration_div)
				} else {
						this.time_div = document.id(tm)
						this.duration_div = document.id(dur)
				}
				if (!this.hasProgress) {
						this.media.player.addEvent("PROGRESS",this.update.bind(this))
						this.hasProgress = true
				}
				if (!this.hasInfo) {
						this.media.player.addEvent("PLAYER_EVENT",this.updateInfo.bind(this))
						this.hasInfo = true
				}	
				return this
		},
		seekButtons: function(bwd,fwd,container){
				if (!container) container = this.container
				if (container) {
						if (bwd) {
								var b_btn = new Element("div",{id:bwd})
								document.id(container).grab(b_btn)
						}
						if (fwd) {
								var f_btn = new Element("div",{id:fwd})
								document.id(container).grab(f_btn)
						}		
				} else {
						if (bwd) var b_btn = document.id(bwd)
						if (fwd) var f_btn = document.id(fwd)
				}
				if (b_btn) b_btn.addEvent("mousedown",function() { this.enableSeek(-2) }.bind(this));
				if (f_btn) f_btn.addEvent("mousedown",function() { this.enableSeek(1) }.bind(this));
				return this
		},
		progressBar: function(bar,box,container,noscrub){
				if (!container) container = this.container
				if (container) {
						this.progress_bar = new Element("div",{id:bar})
						this.progress_box = new Element("div",{id:box})
						document.id(this.progress_box).grab(this.progress_bar)
						document.id(container).grab(this.progress_box)
				} else {
						this.progress_bar = document.id(bar)
						this.progress_box = document.id(box)
				}
				this.progress_x = this.progress_bar.getPosition().x	
				this.progress_width = this.progress_bar.getStyle("width").toInt()
				this.progress_bar.setStyle("width",1)
				if (!noscrub) this.progress_box.addEvent("mousedown",this.enableScrub.bind(this))
				if (!this.hasProgress) {
						this.media.player.addEvent("PROGRESS",this.update.bind(this))
						this.hasProgress = true
				}		
				return this
		},
		volumeScrubber: function(vol,cover,container){
				if (!container) container = this.container
				if (container) {
						this.volume_box = new Element("div",{id:vol})
						this.volume_cover = new Element("div",{id:cover})
						document.id(this.volume_box).grab(this.volume_cover)
						document.id(container).grab(this.volume_box)
				} else {
						this.volume_box = document.id(vol)
						this.volume_cover = document.id(cover)
				}
				this.volume_x = this.volume_box.getPosition().x	
				this.volume_width = this.volume_box.getStyle("width").toInt()
				this.setVolumeCover()
				this.volume_box.addEvent("mousedown",this.enableVolumeScrub.bind(this))
				return this
		},
		//
		//	UPDATE
		//
		update: function(){
				if (!this.media.player.isSeeking) {
						if (this.progress_bar){ 
								var w = (this.progress_width * this.media.player.playProgress).limit(1,this.progress_width)
								if (!w) w = 1
								this.progress_bar.setStyle("width",w)
						}
						if (this.time_div) this.time_div.set("text",this.media.player.time)
				}
		},
	 	updateInfo: function(){
				this.infoFields.each(function(obj){ obj.textDiv.set('html',this.media.info[obj.prop]) }.bind(this))
				if (this.duration_div) this.duration_div.set("text",this.media.player.totalTime)
				if (this.volume_cover) this.setVolumeCover()
				this.update()
		},
		//
		//  ACTIONS
		//
		mute: function(){
				this.mute_btn.setStyle("display","none")
				this.unmute_btn.setStyle("display","block")
				this.media.mute()
		},
		unmute: function(){
				this.unmute_btn.setStyle("display","none")
				this.mute_btn.setStyle("display","block")
				this.media.unmute()		
		},		
		enableScrub: function(e){
				this.media.player.pause(true)
				this.scrubEvent(e)
				window.addEvent('mousemove',this.scrubEvent.bind(this))
				window.addEvent('mouseup',this.stopScrub.bind(this))
		},
		scrubEvent: function(e){
				var ratio = ((e.event.pageX - this.progress_x)/this.progress_width).limit(0,1)
				var t = ratio * this.media.player.duration
				this.media.player.seek(t,true)
				this.progress_bar.setStyle("width", (this.progress_width * ratio).limit(0,this.progress_width))
				if (this.time_div) this.time_div.set("text",this.media.player.getTime(t))
        },
        stopScrub: function(){
				window.removeEvents('mousemove')
				window.removeEvents('mouseup')
				this.media.player.stopSeek()
                if (this.media.player.state == "playing") this.media.player.play(true)
        },
		enableSeek: function(sgn){
				if (!sgn) sgn = 1
				window.addEvent("mouseup",this.stopSeek.bind(this))
				this.seekTimer = this.seek.periodical(this.options.fps,this,sgn)
		},
		seek: function(sgn){
				if (this.options.quietSeek) this.media.player.pause(true)
				if (!sgn) sgn = 1
				this.media.player.seek(sgn)
				this.progress_bar.setStyle("width", (this.progress_width * this.media.player.playProgress).limit(0,this.progress_width))
				if (this.time_div) this.time_div.set("text",this.media.player.time)
		},
		stopSeek: function(){
				if (this.options.quietSeek) this.media.player.play()
				window.removeEvents("mouseup")
				if (this.seekTimer) clearTimeout(this.seekTimer)
				this.media.player.stopSeek()
                if (this.options.quietSeek && this.media.player.state == "playing") this.media.player.play(true)
		},
		setVolumeCover: function(){
				var w = this.media.player.volume == -1 ? this.volume_width * (1 - player.initVolume) : this.volume_width * (1 - this.media.player.volume)
				this.volume_cover.setStyles({
						width:w,
						left:this.volume_width - w
				})
		},
	    enableVolumeScrub: function(e){
				this.volumeScrubEvent(e)
				window.addEvent('mousemove',this.volumeScrubEvent.bind(this))
				window.addEvent('mouseup',this.stopScrub.bind(this))
		},
		volumeScrubEvent: function(e){
				var vol = (((e.event.pageX - this.volume_x)/this.volume_width)).limit(0,1)
				this.media.player.setVolume(vol)
				this.setVolumeCover()
        },
        stopVolumeScrub: function(){
				window.removeEvents('mousemove')
				window.removeEvents('mouseup')
        }
});
