/**
 * HTML5 video tag support
 * @copyright monad - www.monad.pl, 2010
 */

/**
 * options: {
		overlaySpec: {
		  	src: url do obrazka
		  	style: { lista deklaracji... }
	  },
	  flashvars: {},
	}
	
	
 */

/*
x = flowplayer(
		"player", 
		"http://releases.flowplayer.org/swf/flowplayer-3.2.5.swf", 
		{clip: {
			provider:"pseudostreaming",
			url:"http://vod01.netdna.com/vod/demo.flowplayer/Extremists.flv"},
			screen:{
				height:"100pct",
				top:0
			},
			plugins:{
				controls: {
					timeColor:"#ffffff",
					borderRadius:0,
					slowForward:false,
					bufferGradient:"none",
					backgroundColor:"rgba(0, 0, 0, 0)",
					volumeSliderGradient:"none",
					slowBackward:false,
					time:false,
					height:33,
					volumeColor:"#4599ff",
					tooltips:{
						scrubber:false,
						volume:false,
						buttons:false
					},
					opacity:1,
					fastBackward:false,
					volumeSliderColor:"#ffffff",
					border:"0px",
					buttonColor:"rgba(189, 189, 189, 1)",
					mute:true,
					autoHide:{enabled:false},
					backgroundGradient:"none",
					width:"100pct",
					display:"block",
					sliderBorder:"1px solid rgba(128, 128, 128, 0.7)",
					buttonOverColor:"#ffffff",
					fullscreen:false,
					borderWidth:0,
					scrubberBarHeightRatio:0.2,
					bottom:0,
					stop:false,
					zIndex:1,
					sliderColor:"#000000",
					scrubberHeightRatio:0.6,
					tooltipTextColor:"#ffffff",
					spacing:{time:6,volume:8,all:2},
					sliderGradient:"none",
					timeBgHeightRatio:0.8,
					volumeSliderHeightRatio:0.6,
					name:"controls",
					timeSeparator:" ",
					volumeBarHeightRatio:0.2,
					left:"50pct",
					tooltipColor:"rgba(0, 0, 0, 0)",
					playlist:false,
					durationColor:"#b8d9ff",
					play:false,
					fastForward:true,
					timeBorder:"0px solid rgba(0, 0, 0, 0.3)",
					progressColor:"#4599ff",
					scrubber:false,
					volume:false,
					builtIn:false,
					margins:[2,6,2,12]},
					rtmp:{
						netConnectionUrl:"rtmp://vod01.netdna.com/play",
						url:"http://builds.flowplayer.netdna-cdn.com/flowplayer.rtmp-3.2.3.swf"}
					}
			}
		
);
*/

function VideoLoad(containerId, mp4Url, ogvUrl, swfUrl, imgUrl, options) {
	var ct = this.ct = document.getElementById(containerId);
	ct._video = this
	
	this.toggleMute = function(forceTo) {
		switch (this.playerType) {
			case 'flowplayer':
				if (this.flowplayer.getStatus().muted) {
					this.flowplayer.unmute();
				} else {
					this.flowplayer.mute();
				}
			break;
			
			case 'htmlplayer':
				
			break;
		}
	}
	
	this.destroy = function() {
		/*for(var i=0; i<this.ct.childNodes.length; i++) {
			this.ct.removeChild(this.ct.item(i));
		}*/
		while(this.ct.childNodes.length > 0) {
			this.ct.removeChild(this.ct.firstChild);
		}
	}
	
	var flashVars = this.flashVars = options.flashVars;
	var overlaySpec = this.overlaySpec = options.overlaySpec;
	this.startMuted = options.startMuted || false;
	
	if (!ct) {
		return false;
	}
	
	this.containerId = containerId;
	
	var width = options.width || 622;
	var height = options.height || 311;
	var ogvString = ogvUrl ? '<source src="'+ ogvUrl +'" type="video/ogg" />' : '';
			
	var template = '<video width="'+ width +'" height="'+ height +'" style="display: block; position: absolute; left: 0px; top:-7px" preload="auto" autoplay controls >\
		<source src="'+ mp4Url +'" type="video/mp4" />\
		'+ogvString+'\
		</video>\
	';
		
	ct.innerHTML = null;
	ct.innerHTML = template;
		
	function on_canplaythrough(e) {
		var v = e.target;
		v.play();		
	}

	
	var v = ct.firstChild;
	var overlay = null;
	if(overlaySpec && overlaySpec.src) {
		overlay = document.createElement('img');
		overlay.src = overlaySpec.src;
		if (overlaySpec.style) {
			for(var i in overlaySpec.style) {
				overlay.style[i] = overlaySpec.style[i];
			}
		} else {
			overlay.style.position = 'absolute';
			overlay.style.left = '0px';
			overlay.style.top = '0px';
		}
	}
	ct.style.position = 'relative';
	ct.overlay = overlay;

	if(! options.hideControls) {
		this.controls = document.createElement('div');
		this.controls.player = this;
		this.controls.muted = this.startMuted;
		this.controls.innerHTML = '<a href="javascript:void(0) class="playerControls_mute" onclick="javascript:this.parentNode.toggleMute()">';
		this.controls.button_mute = this.controls.firstChild;
		if (this.controls.muted) {
			this.controls.button_mute.className += 'playerControls_mute muted'; 
		}
		this.controls.className = 'playerControls';
		this.controls.style.width = width + 'px';
		this.controls.toggleMute = function() {
				if(this.muted) {
					this.button_mute.className = "playerControls_mute"
				} else {
					this.button_mute.className += ' muted'; 
				}
				
				this.player.toggleMute();
				this.muted = ! this.muted;
		}
	}
	
	this.playerType = 'htmlplayer';
	if(v.currentSrc.length < 4) {
		this.playerType = 'flowplayer';
		
		ct.overlay = overlay;
		ct.removeChild(ct.firstChild);
		
		var flowControls = {"borderRadius":0,"timeColor":"#ffffff","bufferGradient":"none","slowForward":true,"backgroundColor":"rgba(0, 0, 0, 0)","volumeSliderGradient":"none","slowBackward":false,"timeBorderRadius":20,"time":false,"progressGradient":"none","height":26,"volumeColor":"#4599ff","tooltips":{"marginBottom":5,"scrubber":false,"volume":false,"buttons":false},"fastBackward":false,"opacity":0,"timeFontSize":12,"border":"0px","volumeSliderColor":"#ffffff","bufferColor":"#a3a3a3","buttonColor":"#ffffff","mute":true,"autoHide":{"enabled":false,"hideDelay":500,"mouseOutDelay":500,"hideStyle":"fade","hideDuration":400,"fullscreenOnly":true},"backgroundGradient":"none","width":"100pct","display":"block","sliderBorder":"1px solid rgba(128, 128, 128, 0.7)","buttonOverColor":"#ffffff","fullscreen":false,"timeBgColor":"rgb(0, 0, 0, 0)","borderWidth":0,"scrubberBarHeightRatio":0.2,"bottom":0,"stop":false,"sliderColor":"#000000","zIndex":1,"scrubberHeightRatio":0.6,"tooltipTextColor":"#ffffff","sliderGradient":"none","spacing":{"time":6,"volume":8,"all":2},"timeBgHeightRatio":0.8,"volumeSliderHeightRatio":0.6,"name":"controls","timeSeparator":" ","volumeBarHeightRatio":0.2,"left":"50pct","tooltipColor":"rgba(0, 0, 0, 0)","playlist":false,"durationColor":"#b8d9ff","play":false,"fastForward":true,"timeBorder":"0px solid rgba(0, 0, 0, 0.3)","progressColor":"#4599ff","scrubber":false,"volume":false,"builtIn":false,"volumeBorder":"1px solid rgba(128, 128, 128, 0.7)","margins":[2,6,2,12]};
		
		this.flowplayer = flowplayer(containerId, {
				src: "lib/flowplayer-3.2.5.swf", 
				wmode: 'opaque',
				width: width,
				height: height,
			}, {
				buffering: false,
				onLoad: function(ctx) { 
					return function() {
						if(ctx.startMuted) {
							this.mute();
						} else {
							this.unmute();
						}
						
						if (ctx.ct.overlay) {
							ctx.ct.appendChild(ctx.ct.overlay);
						}
						if (ctx.controls) {
							ctx.ct.appendChild(ctx.controls);
						}
					};
				}(this),
				
				clip: {
					url: swfUrl,
					autoPlay: true,
					bufferLength: 10,
					fadeInSpeed: 0, fadeOutSpeed:0,
					onBeforeFinish: function() {return false;},
					onBeforePause: function() {return false;},
					onBeforeFullscreen: function() {return false;},
				},
				
				plugins: {
					controls: null
				}
			}
		);

	} else {
		if(ct.overlay) {
			ct.appendChild(ct.overlay);
		}
		v.addEventListener('ended', function(e) {e.target.play(); }, false);
		/*
		v.addEventListener('error', on_error, false);
		v.addEventListener('abort', on_abort, false);
		*/
		if (v.readyState != v.HAVE_ENOUGH_DATA) {
			v.addEventListener('canplaythrough', on_canplaythrough, false);
		} else {
			v.play();		
		}
		
		this.htmlplayer = v;
		if(this.controls) {
			this.ct.appendChild(this.controls);
		}

	}


	

}
