/**
 * Javascript Functions and extension for player handling via button
 * Here you can find extension lib which interact directly or indirectly whith prototype.js and prototype.function.js fremework
 * @see Prototype
 * 
 * @requires package Prototype (all classes)
 * @requires package PrototypeFunction (all classes)
 *
 * @author Lino Telera (collected by)
 *
**/

/**
 * Load directly external resources
 * @param {Object} sInputFile
 */
var PrototypePlayer = {
	
	sVersion: '1.0',
	sAuthor: 'Lino'
}


PrototypePlayer.wmp = Class.create();
PrototypePlayer.wmp.prototype = {
	
	
	elDiv: null,
	oOption:{
		sPlayerVideoUrl:'',
		sImgStandBy:'',
		iW : 300,
		iH: 200
	},
	sClassBtn: null,
	aBtnActive: new Array(),
	
	elIframe: null,
	
	elImgStby:null,
	
	/**
	 * Constructot
	 * @param Object html dom elDiv
	 * @param String sClassBtn
	 * @param {Object} oOption
	 */
	initialize: function(elDiv, sClassBtn, oOption){
		
		this.aBtnActive = new Array();
		
		this.sClassBtn = sClassBtn;
		this.elDiv = elDiv;
		
		this.setOption(oOption);
	},
	
	/**
	 * Set option
	 * @param {Object} oOption
	 */
	setOption: function(oOption){
		this.oOption = PrototypeData.Object.update(oOption);
		this._render();
	},
	
	/**
	 * Get all needed infos
	 */
	_render: function(){
		// Get al buttons 
		
		this._renderPlayer();
		
		this.aBtnActive = Element.getElementsByClassName(document, this.sClassBtn);
		for (var i =0 ; i < this.aBtnActive.length; i++){
			this.aBtnActive[i].style.cursor = "pointer";
			this.aBtnActive[i].onclick = this._evt_startPlay.bindAsEventListener(this);
		}
		
	},
	
	/**
	 * Render player and iframe
	 */
	_renderPlayer: function(){
		
		if (this.elIframe == null){
		
		
			this.elIframe = document.createElement("iframe");
			this.elDiv.appendChild(this.elIframe);	
			
			
			this.elIframe.setAttribute("width",  this.oOption.iW);
			this.elIframe.setAttribute("height",  this.oOption.iH);
			
			this.elIframe.setAttribute("SCROLLING", "no");
			this.elIframe.setAttribute("frameborder", "no");
			this.elIframe.setAttribute("border", "0");
			
			this.elIframe.style.display = "none";
		}
		if (this.oOption.sImgStandBy != ""){
			if (this.elImgStby == null){
				
				this.elImgStby = document.createElement("img");
				this.elDiv.appendChild(this.elImgStby);
				
				
			}
			
			this.elImgStby.setAttribute("src", this.oOption.sImgStandBy);
			
			
		}else{
			if (this.elImgStby != null){
				this.elDiv.removeChild(this.elImgStby);
				this.elImgStby = null;
			}
		}
		
		
	},
	
	_evt_startPlay: function(oEvt){
		
		
		// Stop prev play
		this._stopPlayer();
		
		// Start new play
		this._startPlayer(oEvt);
	},
	
	_startPlayer: function(oEvt){
		
		var elBtn = Event.element(oEvt);
		var sVideoSrc = elBtn.getAttribute("playersrc");
		
		if (sVideoSrc == undefined){
			this._stopPlayer();
			return;
		}
		
		if (this.elImgStby != null){
			this.elImgStby.style.display = "none";
		}
		this.elIframe.src = this.oOption.sPlayerVideoUrl+"?qual=1&width="+(this.oOption.iW-8 )+"&height="+(this.oOption.iH- 8 )+"&video="+sVideoSrc+"&"+Math.random();
		this.elIframe.style.display = "";
		
	},
	
	_stopPlayer: function(){
		this.elIframe.src = "";
		this.elIframe.style.display = "none";
		
		if (this.elImgStby != null){
			this.elImgStby.style.display = "";
		}
		
	}
	
}

