//
//  Controls: class to load controls for Slideshows (PlayerControls EXTENDS for video/music)
//      -- assumes that the actions of the player (play, pause, next, prev, mute, unmute, skip )
//         are controled by methods of the same name (play, pause, next, prev, mute, unmute, skip)
//         this can be easily modified by changing the ACTIONS section below,
//         either to other specific methods or to except functions as a vars.
//    
var Controls = new Class({	
	    //
		//  initialize
		//
		Implements: [Options,Events],
		options: {
				on_opacity:1,
				off_opacity:.25
		},
		initialize: function(media,container,options){
				this.media = media
				this.container = container
				this.elements = []
		},
		//
		//   
		//		
		playButtons: function(ply,pau,container,isplaying) {
				if (!container) container = this.container
				if (container) {
						var box = document.id(container)
						this.play_btn = new Element("div",{id:ply})
						this.pause_btn = new Element("div",{id:pau})
						box.grab(this.play_btn)
						box.grab(this.pause_btn)
				} else {
						this.play_btn = document.id(ply)
						this.pause_btn = document.id(pau)
				}
				if (isplaying){
						this.play_btn.setStyle("display","none")
						this.pause_btn.setStyle("display","block")	
				} else {
						this.pause_btn.setStyle("display","none")
						this.play_btn.setStyle("display","block")
				}
				this.play_btn.addEvent("click",this.play.bind(this));
				this.pause_btn.addEvent("click",this.pause.bind(this));
				this.elements.push(this.play_btn)
				this.elements.push(this.pause_btn)
				return this
		},
		stopButton: function(stp,container) {
				if (!container) container = this.container
				if (container) {
						this.stop_btn = new Element("div",{id:stp})
						document.id(container).grab(this.stp_btn)
				} else {
						this.next_btn = document.id(stp)
				}
				this.stop_btn.addEvent("click",this.stop.bind(this));
				this.elements.push(this.stop_btn)
				return this
		},
		nextButtons: function(prv,nxt,container) {
				if (!container) container = this.container
				if (container) {
						var box = document.id(container)
						this.next_btn = new Element("div",{id:nxt})
						this.prev_btn = new Element("div",{id:prv})
						box.grab(this.next_btn)
						box.grab(this.prev_btn)
				} else {
						this.next_btn = document.id(nxt)
						this.prev_btn = document.id(prv)
				}
				this.next_btn.addEvent("click",this.next.bind(this));
				this.prev_btn.addEvent("click",this.prev.bind(this));
				this.elements.push(this.next_btn)
				this.elements.push(this.prev_btn)
				return this
		},
		infoText: function(tdiv,prp,init_txt,container){
				if (!container) container = this.container
				if (container) {
						var txt_div = new Element("div",{id:tdiv})
						document.id(container).grab(txt_div)
				} else {
						var txt_div = document.id(tdiv)
						this.prev_btn = document.id(prv)
				}
				if (init_txt) txt_div.set("html",init_txt)
				if (!this.infoFields) {
						this.infoFields = []
						this.media.addEvent("UPDATE",this.updateInfo.bind(this))
				}
				var obj = {
						prop: prp,
						textDiv:txt_div
				}
				this.infoFields.push(obj)
				this.elements.push(this.media)
				return this
		},
		//
		//  Actions
		//
		play: function(){
				this.play_btn.setStyle("display","none")
				this.pause_btn.setStyle("display","block")
				if (this.stop_btn) this.stop_btn.setStyle("opacity",this.options.on_opacity)
				this.media.play()
		},
		pause: function(){
				this.pause_btn.setStyle("display","none")
				this.play_btn.setStyle("display","block")
				this.media.pause()		
		},
		stop: function(){
				this.pause_btn.setStyle("display","none")
				this.play_btn.setStyle("display","block")
				this.stop_btn.setStyle("opacity",this.options.off_opacity)
				this.media.pause()		
		},
		next: function(){
				this.media.next()
		},
		prev: function(){
				this.media.prev()
		},
		//
		//  listeners
		//
		updateInfo: function(){
				this.infoFields.each(function(obj){ obj.textField.set('html',this.media.info[obj.prop]) }.bind(this))
				if (!this.hasInfo) {
						this.media.player.addEvent("PLAYER_EVENT",this.updateInfo.bind(this))
						this.hasInfo = true
				}
		},
		//
		//
		//
		detach: function(){
				this.elements.each(function(elm){
						if (elm) elm.removeEvents()
				}.bind(this))
		}		
});

