/**
copyright 2010 Stefan Paduraru - paduraru.com
**/

var h5p = function(id, options, sources, vn) {
	
	this.id = "video_"+vn;
	this.parentid = id;
	this.object = document.getElementById(this.id);
	this.isPlaying = false;
	this.options = options;
	this.sources = sources;
	this.vn = vn;
	this.timerID = 0;
	this.transitionID = 0;
	this.canplay = false;
	this.rot = 1;
	this.dragObject = null;
	this.fullscreen = false;
	this.cbtranslate = {};
	this.sBars = false;
	this.vtimer = 0;
	this.mmTimer = 0;
	this.rfTimer = 0;
	this.browser = BrowserDetect.browser;
	this.os = BrowserDetect.os;
	this.filepath = this.getScriptPath("html5player.js");
	this.dragState = 0;
	this.watchBuffer = 0;
	
	
	if (typeof $== 'function')
		this.jquery = true;
	else
		this.jquery = false;

		
	if ((this.browser == "Safari" &&  this.os == "iPhone/iPod") || this.isIpad())
	{
		this.nocontrols = true;	
	} else {
		this.nocontrols = false ;	
	}
	
	//test 4 html 5
	this.supportsVideo = this.browserSupportsVideo();
	
	if (this.supportsVideo && this.browser != "Opera")
	{
		this.createVideoTag();
		this.setTagEvents();
		
		if (!this.nocontrols) {
			this.createControls();
			this.pb = this.createPB();
		}
		
		this.setOptions(options);
		this.setListeners();
	} else {
		this.createFlashObject();
	}
};

	h5p.prototype.focus = function() {
		try {
			this.object.focus();
		} catch(e) {}
	}
	
	h5p.prototype.play = function() {
		if (!this.isPlaying)
		{
			this.object.play();
			this.togglePB(false);
			this.isPlaying = true;
			this.startTimer();
			
			if (this.nocontrols) {
				return;
			}
			//updatez buttonul de play
			document.getElementById('play_pause_button_'+this.vn).className = "play_pause_button pause"; 
			this.focus();
		}
	}
	
	h5p.prototype.pause = function() {
		if (this.isPlaying)
		{
			this.object.pause();
			this.togglePB(true);
			this.isPlaying = false;
			this.stopTimer();
			if (this.nocontrols) {
				return;
			}
			//updatez buttonul de play
			document.getElementById('play_pause_button_'+this.vn).className = "play_pause_button"; 
			this.focus();
		}
	}
	
	h5p.prototype.togglePlay = function() {
		if (!this.isPlaying)
		{
			this.play();
		} else {
			this.pause();
		}
	}
	
	h5p.prototype.createPB = function() {
		var pb = document.createElement('span');
		pb.setAttribute("class", "play_button");
	
		pb.setAttribute("onclick", 'eval("'+this.vn+'.togglePlay()")');
		
		pb.setAttribute("id", "play_button_"+this.vn);
		pb.setAttribute("onmouseover", 'eval("'+this.vn+'.showControls()")');
		pb.setAttribute("onmouseout", 'eval("'+this.vn+'.hideControls()")');
		
		//aaa
		pb.setAttribute('style', 'display:none');
		
		this.object.parentNode.appendChild(pb);
	
		return pb;
	}
	
	h5p.prototype.togglePB = function(t) {
		if (this.nocontrols) {
			return;
		}
		this.pb.setAttribute('style', 'display:none');
		return;
		if (!this.fullscreen)
		{
			if (t) {
				this.pb.setAttribute('style', 'display:block');
			} else {
				this.pb.setAttribute('style', 'display:none');
			}
		}
		this.focus();
	}
	
	h5p.prototype.setTagEvents = function()
	{
		//this.object.setAttribute('ondblclick', this.vn+'.toggleFullscreen()');
		this.object.setAttribute('onclick', this.vn+'.togglePlay()');
		this.object.setAttribute('onmouseover', this.vn+'.showControls()');
		this.object.setAttribute('onmouseout', this.vn+'.hideControls()');
		this.object.setAttribute('onpause', this.vn+'.pause()');
		this.object.setAttribute('onplay', this.vn+'.play()');
		this.object.setAttribute('onprogress', this.vn+'.loadProgress(event)');
		this.watchBuffer = setInterval(this.vn+'.updateBufferedTotal()', 33);
		
		this.object.setAttribute('onended', this.vn+'.end(event)');
		//this.object.setAttribute('waiting', this.vn+'.waiting(event)');
		this.object.setAttribute('onwaiting', this.vn+'.waiting(event)');
		this.object.setAttribute('onseeked', this.vn+'.seeked(event)');
		this.object.setAttribute('onseeking', this.vn+'.seeking(event)');
		this.object.setAttribute('oncanplay', this.vn+'.canPlay(event)');
		this.object.setAttribute('onerror', this.vn+'.error(event)');
		this.object.setAttribute('ondurationchange', this.vn+'.setTotalTime(event)');
		this.object.setAttribute('ontimeupdate', this.vn+'.updateControls(1)');
	}
	
	h5p.prototype.loadProgress = function(event) 
	{
		if (this.nocontrols)
		return true;
		
		w = parseInt(event.loaded*100/event.total);
		document.getElementById('progress_buffered_'+this.vn).style.width = w+"%";
	}
	
	h5p.prototype.updateBufferedTotal = function()
	{
		if (this.nocontrols) {
			clearInterval(this.watchBuffer);
			return true;
		}
		
		if (this.object.buffered) {
			if (this.object.buffered.length >= 1) 
			{
				w = parseInt(((this.object.buffered.end(0) / this.object.duration) * 100));
				document.getElementById('progress_buffered_'+this.vn).style.width = w+"%";
			    if (this.object.buffered.end(0) == this.object.duration) 
			      clearInterval(this.watchBuffer);
			}
		} else {
		  clearInterval(this.watchBuffer);
		}
	}
  
	h5p.prototype.canPlay = function(event) {
		this.canplay = true;
	}
	
	h5p.prototype.end= function(event) {
		//media ended
		if (this.fullscreen)
		{
			this.toggleFullscreen();
		}
		if (!this.fullscreen)
			this.pb.style.display = "block";
		
		document.getElementById('play_pause_button_'+this.vn).className = "play_pause_button";
		
		this.pause();
	}
	
	h5p.prototype.error = function(event) {
		var obj = document.getElementById('error_'+this.vn);
		
		try {
			switch (this.object.error.code)  {
			   case 1:
			      msg = "Error: Playback stopped.";
			      obj.style.display = "block";
			      break;
			   case 2:
			      msg =  ("Error: Network error. Please try again later.");
			      obj.style.display = "block";
			      break;
			   case 3:
			      msg = ("Error: Decoding error, video is broken or the codec makes problems");
			      obj.style.display = "block";
			      break;
			   case 4:
			   	if (this.object.networkState == 4) {
			      msg = ("Error: The video is not available.");
			      obj.style.display = "block";
			   	}
			    else {
			      msg = ("Error: The format is not supported.");
			      obj.style.display = "block";
			    }
			      break;
			 }
			 obj.innerHTML = msg;
		} catch(e) {}
	}
	
	h5p.prototype.waiting = function(event) {
		this.canplay = false;
	}
	
	h5p.prototype.seeking = function(event) {
		if (this.nocontrols)
		return;
		document.getElementById('progress_loading_stripes_'+this.vn).style.display = "block";
	}
	
	h5p.prototype.seeked = function(event) {
		if (this.nocontrols)
		return;
		
		document.getElementById('progress_loading_stripes_'+this.vn).style.display = "none";
	}
	
	h5p.prototype.changeRoT = function() {
		if (this.rot == 1) this.rot = 2;
		else this.rot = 1;
		this.focus();
	}
	
	h5p.prototype.createControls = function()
	{
		if (this.options.width)
		{
			var w = parseInt(this.options.width);
		} else {
			var w = 640;	
		}
		
		if (this.options.height)
		{
			var h = parseInt(this.options.height);
		} else {
			var h = 640;	
		}
		
		var c = document.createElement('div');
		c.setAttribute('class', 'controlsbar fullscreen');
		c.setAttribute('id', 'controls_'+this.vn);
		c.style.left = (parseInt(w - 321)/2)+"px";
		/**
		@todo design ptr fullscreen 
		**/
		var s = document.createElement('div');
		s.setAttribute('class', 'errorwindow');
		s.setAttribute('id', 'error_'+this.vn);
		s.style.width=(w-5)+"px";
		c.appendChild(s);
		
		//tre sa creeze rewind & shit
		var s = document.createElement('div');
		s.setAttribute('class', 'rewind_button');
		s.setAttribute('id', 'rewind_button_'+this.vn);
		//s.setAttribute('onclick', 'eval("'+this.vn+'.rewind()")');
		s.setAttribute('onmousedown', 'eval("'+this.vn+'.rewind()");this.className = "rewind_button click"');
		s.setAttribute('onmouseup', 'eval("'+this.vn+'.cancelRF()");this.className = "rewind_button"');
		c.appendChild(s);
			
		var s = document.createElement('div');
		s.setAttribute('class', 'play_pause_button');
		s.setAttribute('id', 'play_pause_button_'+this.vn);
		s.setAttribute('onclick', 'eval("'+this.vn+'.togglePlay()")');
		s.setAttribute('onmousedown', 'if (this.className == "play_pause_button") this.className="play_pause_button click"; else this.className = "play_pause_button pause click"');
		s.setAttribute('onmouseup', 'if (this.className == "play_pause_button click") this.className="play_pause_button"; else this.className = "play_pause_button pause"');
		c.appendChild(s);
		
		var s = document.createElement('div');
		s.setAttribute('class', 'forward_button');
		s.setAttribute('id', 'forward_button_'+this.vn);
		s.setAttribute('onmousedown', 'eval("'+this.vn+'.forward()");this.className = "forward_button click"');
		s.setAttribute('onmouseup', 'eval("'+this.vn+'.cancelRF()");this.className = "forward_button"');
		//s.setAttribute('onclick', 'eval("'+this.vn+'.forward()")');
		c.appendChild(s);
		
		
		var bp = document.createElement('div');
		bp.setAttribute('class', 'progress_bar');
		bp.setAttribute('id', 'progress_bar_'+this.vn);
		
			this.width = w;
			this.height = h;
			this.object.parentNode.style.width = w+'px';
			this.object.parentNode.style.height = h+'px';
			
			bp.setAttribute('style', 'width: '+(w-160)+'px');
		
			var s = document.createElement('div');
			s.setAttribute('id', 'progress_back_'+this.vn);
			s.setAttribute('class', 'progress_back');
			s.setAttribute('vn', this.vn);
			s.setAttribute('onclick', 'handleBarClick(event, "'+this.vn+'")');
			
				var s1 = document.createElement('div');
				s1.setAttribute('class', 'progress_loading_wrapper');
				
				
				var s12 = document.createElement('div');
				s12.setAttribute('class', 'progress_loading_stripes');
				s12.setAttribute('id', 'progress_loading_stripes_'+this.vn);
				
			
				s1.appendChild(s12);
				s.appendChild(s1);
				
				var s1 = document.createElement('div');
				s1.setAttribute('class', 'progress_buffered');
				s1.setAttribute('id', 'progress_buffered_'+this.vn);
				s.appendChild(s1);
				
				var s1 = document.createElement('div');
				s1.setAttribute('class', 'progress_elapsed_time');
				s1.setAttribute('id', 'progress_elapsed_time_'+this.vn);
				s.appendChild(s1);
				
				var s1 = document.createElement('div');
				s1.setAttribute('class', 'progress_indicator');
				s1.setAttribute('id', 'progress_indicator_'+this.vn);
				s1.setAttribute('vn', this.vn);
				s1.setAttribute('candrag', 'true');
				s.appendChild(s1);
				
			bp.appendChild(s);
			
			/*var s = document.createElement('div');
			s.setAttribute('class', 'remaining_time');
			s.setAttribute('id', 'remaining_time_'+this.vn);
			s.innerHTML = "00:00";
			bp.appendChild(s);*/
		
		c.appendChild(bp);
		
		var s = document.createElement('div');
		s.setAttribute('class', 'elapsed_time');
		s.setAttribute('style', 'cursor: pointer');
		s.setAttribute('id', 'elapsed_time_'+this.vn);
		s.setAttribute('onclick', this.vn+".changeRoT()");
		if (this.object.duration >= 3600)
			s.innerHTML = "00:00:00";
		else
			s.innerHTML = "00:00";
		c.appendChild(s);
		
		var s = document.createElement('div');
		s.setAttribute('class', 'total_time');
		s.setAttribute('style', 'cursor: pointer');
		s.setAttribute('id', 'total_time_'+this.vn);
		s.innerHTML = "00:00";
		c.appendChild(s);
		
		
		var s = document.createElement('div');
		s.setAttribute('class', 'fullwindow_button');
		s.setAttribute('id', 'fullwindow_button_'+this.vn);
		s.setAttribute('onclick', 'eval("'+this.vn+'.toggleFullscreen()")');
		s.setAttribute('onmousedown', 'this.className = "smallwindow_button click"');
		s.setAttribute('onmouseup', 'this.className = "smallwindow_button"');
		c.appendChild(s);
		
		var s = document.createElement('div');
		s.setAttribute('class', 'volume_button');
		s.setAttribute('id', 'volume_button_'+this.vn);
		//s.setAttribute('onclick', this.vn+'.toggleVolume()');
		s.setAttribute('onmouseover', this.vn+'.showVolume()');
		s.setAttribute('onmouseout', this.vn+'.hideVolume()');
		
		/*s.setAttribute('onmouseover', this.vn+'.showVolume()');
		s.setAttribute('onmouseout', this.vn+'.hideVolume()');*/
		
		var ss = document.createElement('div');
		ss.setAttribute('class', 'volume_bar');
		ss.setAttribute('id', 'volume_bar_'+this.vn);
		ss.setAttribute('onmouseover', this.vn+'.showVolume()');
		ss.setAttribute('onmouseout', this.vn+'.hideVolume()');
		ss.setAttribute('onclick', this.vn+'.updateVolume(event)');
		
		
		var vc = document.createElement('div');
		vc.setAttribute('class', 'volume_indicator');
		vc.setAttribute('id', 'volume_indicator_'+this.vn);
		vc.setAttribute('onmouseover', this.vn+'.showVolume()');
		vc.setAttribute('onmouseout', this.vn+'.hideVolume()');
		vc.setAttribute('vn', this.vn);
		//vc.setAttribute('candrag', 'true');
		
		ss.appendChild(vc);
		s.appendChild(ss);
		c.appendChild(s);
		
		
		
		this.object.parentNode.appendChild(c);
		c.setAttribute('onmouseover', 'eval("'+this.vn+'.showControls()")');
		c.setAttribute('onmouseout', 'eval("'+this.vn+'.hideControls()")');
		this.controls = c;
		
	}
	
	h5p.prototype.handleResize = function() {
			window.fullscreenElement = this.vn;
			document.getElementById('fullwindow_button_'+this.vn).className = "smallwindow_button";
			
			//scrollez la zero
			window.scrollTo(0,0);
			document.body.style.overflow = "hidden";
			
			var cover = document.getElementById(this.id+"_cover");
			cover.setAttribute('style', "width:100%; height: 100%; position:absolute; left:0; top:0; background: #000; opacity:1; z-index:999998;");
			
			this.object.parentNode.style.position = "relative";
			this.object.parentNode.style.zIndex = "999999";
			
			this.fullscreen = true;
			var d = getDocDimensions(1);
			var d2 = getDocDimensions(1);
			
			//tre sa calculez procentul de scalare incat sa ajung la max cu una din dimensiuni
			var hm = d.height/this.height;
			var wm = d.width/this.width;
			var scale = Math.min(hm, wm);
			
			var tx = 0;
			var ty = 0;
			
			/*var offLeft = this.controls.parentNode.offsetLeft - (this.width * scale - this.width)/2 ;
			var offTop = this.controls.parentNode.offsetTop - (this.height * scale - this.height)/2 ;*/
			var offLeft = this.controls.parentNode.offsetLeft ;
			var offTop = this.controls.parentNode.offsetTop;
			
			try {
					var offLeft = this.controls.parentNode.getBoundingClientRect().left ;
					var offTop = this.controls.parentNode.getBoundingClientRect().top ;
			}catch(e) {}
			
			tx = d.width/2 - (offLeft + this.width/2) - 0.5;
			ty = d.height/2 - (offTop + this.height/2) - 0.5;
			this.object.style.MozTransform  = "translate("+tx+"px, "+ty+"px) scale("+scale+")";
			this.object.style.webkitTransform  = "translate("+tx+"px, "+ty+"px) scale("+scale+")";
			
			//repozitionez bara
			var diff = d.height - this.height * scale;
			var dh = (diff/this.height)*50;
			var ty = (this.height * scale - this.height) / 2 + ty;
			//var tx = (this.width* scale - this.width) / 2 + tx;
			
			this.cbtranslate = {x:tx, y:ty};
			
			/**@todo resetez left si top**/
			
			this.controls.style.top = "auto";
			this.controls.style.left = "0px";
			var ot = document.getElementById(this.parentid).getBoundingClientRect().top
			var ox = this.controls.getBoundingClientRect().left;
			this.controls.style.left = ((d.width-321)/2)-ox+"px";
			this.controls.style.top = ((d.height-81)) - ot+"px";
			window.pxoffset = ((d.width-321)/2)-ox;
			window.pyoffset = ((d.height-81)) - ot;
			
			/*this.controls.style.MozTransform = "translate(0px, "+ty+"px)";
			this.controls.style.webkitTransform = "translate(0px, "+ty+"px)";*/
			this.controls.style.MozTransform = "translate(0px, 0px)";
			this.controls.style.webkitTransform = "translate(0px, 0px)";
			this.updateControls(1);
			
			this.object.focus();
			
			
			
	}
	
	h5p.prototype.toggleFullscreen = function() {
		if ( (this.object.style.MozTransform != '' && this.browser == "Firefox") 
			|| (this.object.style.webkitTransform != '' && (this.browser == "Safari" || this.browser == "Chrome")))
		{ 
			this.object.style.MozTransform = '';
			this.object.style.webkitTransform = '';
			this.fullscreen = false;
			this.updateControls(1);
			
			//resetez controalele
			this.controls.style.MozTransform = "";
			this.controls.style.webkitTransform = "";
			this.cbtranslate = {};
			
			if (this.sBars) {
				window.scrollTo(this.sBars.w,this.sBars.h);
				document.body.style.overflow = "auto";
				this.sBars = false;
			}
			//remove cover div
			document.getElementById(this.id+'_cover').parentNode.removeChild(document.getElementById(this.id+'_cover'));
			
			this.object.parentNode.style.position = "relative";
			this.object.parentNode.style.zIndex = "inherit";
			
			document.getElementById('fullwindow_button_'+this.vn).className = "fullwindow_button";
			window.fullscreenElement = false;
			this.controls.className = "controlsbar fullscreen";
			this.controls.style.left =  (parseInt(this.width - 321)/2)+"px";
			this.controls.style.opacity = 1;
			this.controls.style.top = 'auto';
			this.controls.removeAttribute('candrag');
		} else {
			this.mouseMove();
			this.controls.setAttribute('candrag', 'true');
			window.fullscreenElement = this.vn;
			document.getElementById('fullwindow_button_'+this.vn).className = "smallwindow_button";
			
			var sBars = getScrollPosition();
			//retin scroll-ul
			this.sBars = sBars;
			//scrollez la zero
			window.scrollTo(0,0);
			document.body.style.overflow = "hidden";
			
			var cover = document.createElement('div');
			cover.setAttribute('style', "width:100%; height: 100%; position:absolute; left:0; top:0; background: #000; opacity:1; z-index:999998;");
			cover.setAttribute("id", this.id+"_cover");
			document.body.appendChild(cover);
			
			this.object.parentNode.style.zIndex=999999;
			this.fullscreen = true;
			var d = getDocDimensions(1);
			var d2 = getDocDimensions(1);
			
			//tre sa calculez procentul de scalare incat sa ajung la max cu una din dimensiuni
			var hm = d.height/this.height;
			var wm = d.width/this.width;
			var scale = Math.min(hm, wm);
			
			var tx = 0;
			var ty = 0;
			
			/*var offLeft = this.controls.parentNode.offsetLeft - (this.width * scale - this.width)/2 ;
			var offTop = this.controls.parentNode.offsetTop - (this.height * scale - this.height)/2 ;*/
			var offLeft = this.controls.parentNode.offsetLeft ;
			var offTop = this.controls.parentNode.offsetTop;
			
			try {
					var offLeft = this.controls.parentNode.getBoundingClientRect().left ;
					var offTop = this.controls.parentNode.getBoundingClientRect().top ;
			}catch(e) {}
			
			tx = d.width/2 - (offLeft + this.width/2) - 0.5;
			ty = d.height/2 - (offTop + this.height/2) - 0.5;
			this.object.style.MozTransform  = "translate("+tx+"px, "+ty+"px) scale("+scale+")";
			this.object.style.webkitTransform  = "translate("+tx+"px, "+ty+"px) scale("+scale+")";
			
			//repozitionez bara
			var diff = d.height - this.height * scale;
			var dh = (diff/this.height)*50;
			var ty = (this.height * scale - this.height) / 2 + ty;
			//var tx = (this.width* scale - this.width) / 2 + tx;
			
			this.cbtranslate = {x:tx, y:ty};
			this.controls.className = "controlsbar fullscreen";
			
			var ot = document.getElementById(this.parentid).getBoundingClientRect().top
			var ox = this.controls.getBoundingClientRect().left;
			this.controls.style.left = ((d.width-321)/2)-ox+"px";
			this.controls.style.top = ((d.height-81)) - ot+"px";
			window.pxoffset = ((d.width-321)/2)-ox;
			window.pyoffset = ((d.height-81)) - ot;
			
			/*this.controls.style.MozTransform = "translate(0px, "+ty+"px)";
			this.controls.style.webkitTransform = "translate(0px, "+ty+"px)";*/
			this.controls.style.MozTransform = "translate(0px, 0px)";
			this.controls.style.webkitTransform = "translate(0px, 0px)";
			this.updateControls(1);
			
			window.fxoffset =document.getElementById('progress_indicator_'+this.vn).getBoundingClientRect().left - parseInt(document.getElementById('progress_indicator_'+this.vn).style.left);
		}
		this.focus();
	}
	
	h5p.prototype.toggleVolume = function() {
		if (document.getElementById('volume_bar_'+this.vn).style.display == "block")
			this.hideVolume();
		else
			this.showVolume();
	}
	
	
	h5p.prototype.updateVolume = function(event) {
		//var objY = findPosY(document.getElementById('volume_bar_'+this.vn));
		/*if (this.fullscreen)
			var y = event.pageY + this.sBars.h;
		else
			var y = event.pageY;*/
			
			
		//var y = event.pageY;	
		//var diff = y - objY;
		
		
		var diff = event.layerY;
		
		try {
				if (this.browser == "Chrome" || this.browser == "Safari") {
					thetarget = event.srcElement;
				}
				else 
					thetarget = event.explicitOriginalTarget;
					
				if (thetarget.className == "volume_indicator") {
					var diff = event.layerY + parseInt(thetarget.style.top);
				}
		} catch (e) {}
		
		
		var x = 1 - diff/60;
		if (x > 1) {x = 1;diff = 0}
		if (x >= 0.9) {x = 1;diff = 0}
		if (x <= 0.08) {x = 0;diff = 60}
		
		this.adjustVolume(x);
		
		
		document.getElementById('volume_indicator_'+this.vn).style.height = (60 - diff)+"px";
		document.getElementById('volume_indicator_'+this.vn).style.top = (diff)+"px";
		
		this.focus();
		
	}
	h5p.prototype.showVolume = function() {
		window.clearTimeout(this.vtimer);
		this.vtimer = 0;
		document.getElementById('volume_bar_'+this.vn).style.display = "block";
	}
	
	h5p.prototype.hideVolume = function(n) {
		if (n)
			document.getElementById('volume_bar_'+this.vn).style.display = "none";
		else {
			if (!this.vtimer) {
				this.vtimer = window.setTimeout(this.vn+'.hideVolume(1)', 200);
			}
		}
		
		this.focus();
	}
	
	h5p.prototype.adjustVolume = function(value) {
		if (value == 0)
		{
			if (!this.nocontrols) {
				document.getElementById('volume_button_'+this.vn).className = "volume_button muted";
			}
		} else {
			this.object.muted = false;
			if (value > 0 && value <= 0.33)
			{
				if (!this.nocontrols) {
					document.getElementById('volume_button_'+this.vn).className = "volume_button l1";	
				}
			}
			  
			if (value > 0.33 && value <= 0.66)
			{
				if (!this.nocontrols) {
					document.getElementById('volume_button_'+this.vn).className = "volume_button l2";	
				}
			}
			
			if (value > 0.66)
			{
				if (!this.nocontrols) {
					document.getElementById('volume_button_'+this.vn).className = "volume_button l3";	
				}
			}
		}
		this.object.volume = value;
	}
	
	h5p.prototype.showControls = function() {
		if (this.nocontrols) {
			return;
		}
		if (!this.fullscreen) {
			if (this.jquery) {
				$('#controls_'+this.vn).stop(1);
				$('#controls_'+this.vn).animate({opacity: 1}, 1000);
			}
			else
				this.controls.style.opacity = 1;
		}
		
	}
	
	h5p.prototype.hideControls = function() {
		if (this.nocontrols) {
			return;
		}
		
		if (window.dragvn == this.vn && window.dragObject != null)
		return;
		if (!this.fullscreen) {
			if (this.jquery) {
				$('#controls_'+this.vn).stop(1);
				$('#controls_'+this.vn).animate({opacity: 0}, 1000);
			}
			else
				this.controls.style.opacity = 0;
		}
	}
	
	
	h5p.prototype.transHideControls = function() {
		if (window.dragvn == this.vn && window.dragObject != null)
		return;
		
		if (this.jquery)
			$('#controls_'+this.vn).animate({opacity: 0}, 1000);
		else
			this.controls.style.opacity = 0;
	}
	
	h5p.prototype.transShowControls = function() {
		if (this.jquery)
			$('#controls_'+this.vn).animate({opacity: 1}, 1000);
		else
			this.controls.style.opacity = 1;
	}
	
	h5p.prototype.mouseMove = function() {
		if (this.fullscreen)
		{
			/**
			@debug
			**/
			if (this.controls.style.opacity == 0)
				this.transShowControls();
				
			//this.controls.style.opacity = 1;
			window.clearTimeout(this.mmTimer);
			this.mmTimer = window.setTimeout(this.vn+'.transHideControls()', 5000);
		}
	}
	
	
	h5p.prototype.toggleControls = function() {
		if (this.controls.style.display == "none")
		{
			this.showControls();
		} else {
			this.hideControls();
		}
	}
	
	h5p.prototype.startTimer = function() {
		//this.stopTimer();
		this.updateControls();
	}
	
	h5p.prototype.updateControls = function(skip) {
		if (this.nocontrols) { 
			return;
		}
		if (!this.canplay)
		{
				document.getElementById('progress_loading_stripes_'+this.vn).style.display = "block";
		} else {
			if (document.getElementById('progress_loading_stripes_'+this.vn).style.display == "block" || document.getElementById('progress_loading_stripes_'+this.vn).style.display == "")
				document.getElementById('progress_loading_stripes_'+this.vn).style.display = "none";
		}
		
		if (!skip)
		{
			//this.stopTimer();
			//this.timerID = window.setTimeout(this.vn+'.updateControls()', 100);
		}
		//window.xoffset = 0;
		//window.fxoffset = 0;
		//check to see if the movie finished playing
		if (this.object.ended) this.pause();
		
		//update the timings
		var d = parseInt(this.object.duration);
		var c = parseInt(this.object.currentTime);
		
		if (isNaN(this.object.duration)) d = 0;
		if (isNaN(this.object.currentTime)) c = 0;
		
		if (d >= 3600)
		{
			var rh = parseInt(c/3600);
			if (rh < 10) rh = "0"+rh;
			
			var rm = parseInt((c%3600)/60);
			if (rm < 10) rm = "0"+rm;
			
			var rsc = parseInt((c%3600)%60);
			if (rsc < 10) rsc = "0"+rsc;
			
			
			var th = parseInt((d-c)/3600);
			if (th < 10) th = "0"+th;
			
			var tm = parseInt(((d-c)%3600)/60);
			if (tm < 10) tm = "0"+tm;
			
			var tsc = parseInt(((d-c)%3600)%60);
			if (tsc < 10) tsc = "0"+tsc;
			
			
			if (this.rot == 1) 
				document.getElementById('elapsed_time_'+this.vn).innerHTML = rh+":"+rm+":"+rsc;
			else 
				document.getElementById('elapsed_time_'+this.vn).innerHTML = "-"+th+":"+tm+":"+tsc;
				
			//if (this.fullscreen) {
			if (1) {
				document.getElementById('total_time_'+this.vn).innerHTML = "-"+th+":"+tm+":"+tsc;
				document.getElementById('elapsed_time_'+this.vn).innerHTML = rh+":"+rm+":"+rsc;
			}/* else {
				var rh = parseInt(d/3600);
				if (rh < 10) rh = "0"+rh;
				var rm = parseInt((d%3600)/60);
				if (rm < 10) rm = "0"+rm;
				var rsc = parseInt((d%3600)%60);
				if (rsc < 10) rsc = "0"+rsc;
				document.getElementById('total_time_'+this.vn).innerHTML = "/&nbsp;"+rh+":"+rm+":"+rsc;		
			}*/
		} else {
		
			var rm = parseInt(c / 60);
			if (rm < 10) rm = "0"+rm;
			
			var rs = parseInt(c % 60);
			if (rs < 10) rs = "0"+rs;
			
			
			var tm = parseInt((d-c) / 60);
			if (tm < 10) tm = "0"+tm;
			
			var ts = parseInt((d-c) % 60);
			if (ts < 10) ts = "0"+ts;
			
			if (this.rot == 1)
				document.getElementById('elapsed_time_'+this.vn).innerHTML = rm+":"+rs;
			else 
				document.getElementById('elapsed_time_'+this.vn).innerHTML = "-"+tm+":"+ts;
			
			//if (this.fullscreen) {
			if (1) {
				document.getElementById('total_time_'+this.vn).innerHTML = "-"+tm+":"+ts;
				document.getElementById('elapsed_time_'+this.vn).innerHTML = rm+":"+rs;
			} /*else {
				var rm = parseInt(d / 60);
				if (rm < 10) rm = "0"+rm;
				var rs = parseInt(d % 60);
				if (rs < 10) rs = "0"+rs;
				document.getElementById('total_time_'+this.vn).innerHTML = "/&nbsp;"+rm+":"+rs;	
			}*/
		}
		/*document.getElementById('remaining_time_'+this.vn).innerHTML = m+":"+s;*/
		
		
		var w = parseInt((parseInt(c)*100)/d);
		var l = (((this.width-150) * w)/100)-5;
		
		var l = ((c*(this.width - 159))/d)-10;
		
		if (l < 0) l = 0;
		
		//update the bars
		document.getElementById('progress_elapsed_time_'+this.vn).style.width = w+"%";
		
		//if (this.fullscreen) {
		if (1) {
			var l = ((c * 200)/d ) - 5;
			
			if (l <= 0) l = -1;
			
			if (l >= 190) l = 190;
					document.getElementById('progress_indicator_'+this.vn).style.left= l+"px";
		}
		/*else {
			if (l >= this.width - 160) l = this.width - 160;
			document.getElementById('progress_indicator_'+this.vn).style.left= l+"px";
		}*/
	}
	
	
	h5p.prototype.stopTimer = function() {
		window.clearTimeout(eval(eval('"'+this.vn+'.timerID"')));
		this.timerID = 0;
	}
	
	h5p.prototype.setOptions = function(options) {
		
		this.object.setAttribute('vn', this.vn);
		
		for (i in options)
		{
			if (i == "poster")
			{
				if (!this.nocontrols) {
					this.object.poster = options[i];
				}
			} else 
				if (i == "volume")
				{
					//set the drag object
					var d = (1 - parseFloat(options[i]));
					if (d < 0) d = -1 * d;
					
					x = d*60;
					if (!this.nocontrols) {
						document.getElementById('volume_indicator_'+this.vn).style.height = (60 - x)+"px";
						document.getElementById('volume_indicator_'+this.vn).style.top = x+"px";
					}
					this.adjustVolume(parseFloat(options[i]));
					
					if (options.muted == true)
					{
						document.getElementById('volume_indicator_'+this.vn).style.top = "0px";
						document.getElementById('volume_indicator_'+this.vn).style.height = "0px";
						this.adjustVolume(0);
					}
				}
			else
				if (i == "muted")
				{
					if (options[i] == true)
					{
						if (options.muted)
						{
							document.getElementById('volume_indicator_'+this.vn).style.top = "0px";
							document.getElementById('volume_indicator_'+this.vn).style.height = "0px";
							this.adjustVolume(0);
						}
					}
				}
			else
				if (i == "bg")
				{
					this.object.style.background = options[i];
				}
			else 
				this.object[i] = options[i];	
		}
	}
	
	h5p.prototype.jumpTo = function(val, x) {
		x = x - 5;
		if (x < 0) x = -1;
		
		this.object.currentTime = parseInt(this.object.duration*val);
		document.getElementById('progress_indicator_'+this.vn).left = x+"px";
		this.updateControls();
		this.focus();
	}
	h5p.prototype.setListeners = function() {
		try {
			document.addEventListener('keypress',this.handleKeyPress,false)
		} catch(e) {}
		/*document.addEventListener('mousedown',this.handleMouseDown,false)	
		document.addEventListener('mouseup',this.handleMouseOup,false)	
		document.addEventListener('mousemove',this.handleMouseMove,false)	*/
	}
	
	h5p.prototype.handleKeyPress = function(event) {
		try {
			if (event.originalTarget.hasAttribute('vn'))
			{
				if (event.charCode == 32)
				{
					//space
					eval(event.originalTarget.getAttribute('vn')+'.togglePlay()');
				}
				
				if (event.keyCode == 27)
				{
					//escape
					if (eval(event.originalTarget.getAttribute('vn')).fullscreen)
					eval(event.originalTarget.getAttribute('vn')+'.toggleFullscreen()');
				}
				
				if (event.keyCode == 37)
				{
					//left
					event.originalTarget.currentTime -= 1;
				}
				
				if (event.keyCode == 39)
				{
					//right
					event.originalTarget.currentTime += 1;
				}
				//eval(event.originalTarget.getAttribute('vn')+''
			}
		} catch(e) {}
	}
	
	h5p.prototype.createFlashObject = function() {
		//embed flash	
		var height = this.options.height? this.options.height: 480;
		var width = this.options.width? this.options.width: 640;
		
		document.getElementById(this.parentid).innerHTML = '<object style="margin:0px; padding:0px;" id="video_overlay" \
			height="'+height+'" width="'+width+'" \
			type="application/x-shockwave-flash" \
			data="/jwplayer.swf" \
			wmode="transparent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"\
codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" \
			>\
			<param name="movie" value="/jwplayer.swf">\
			<param name="quality" value="high"> \
			<param name="allowScriptAccess" value="always"/>\
			<param name="allowFullScreen" value="true"/>\
			<param name="flashvars" value="file='+this.sources.flash+'">\
			<embed src="/jwplayer.swf" style="margin:0px; padding:0px;" \
				quality="high" \
				pluginspage="http://www.macromedia.com/go/getflashplayer" \
				type="application/x-shockwave-flash" \
				flashvars="'+this.sources.flash+'"\
				height="'+height+'" width="'+width+'" ></embed> \
		</object>';	
	}
	h5p.prototype.createVideoTag = function()
	{
		var tag = document.createElement('video');
		tag.height = this.options.height? this.options.height+"px": '480px';
		tag.width = this.options.width? this.options.width+"px": '640px';
		
		if (this.nocontrols == 0) {
			tag.poster = this.options.poster? this.options.poster: '';
		}
		
		for (i in this.sources.html5)
		{
			var t = document.createElement('source');
			t.src=this.sources.html5[i];
			tag.appendChild(t);
		}
		document.getElementById(this.parentid).innerHTML = '';
		document.getElementById(this.parentid).appendChild(tag);
		this.object = tag;
	}
	
	h5p.prototype.debug = function(text) {
		document.getElementById("debug").innerHTML += "<br />"+text;	
	}
	
	h5p.prototype.setTotalTime = function(text) {
		if (this.nocontrols) {
			return;
		}
		var d = this.object.duration;
		if (d >= 3600)
		{
			var h = parseInt(d/3600);
			if (h < 10) h = "0"+h;
			
			var m = parseInt((d%3600)/60);
			if (m < 10) m = "0"+m;
			
			var sc = parseInt((d%3600)%60);
			if (sc < 10) sc = "0"+sc;
			
			document.getElementById('elapsed_time_'+this.vn).innerHTML = "00:00:00";
			document.getElementById('total_time_'+this.vn).innerHTML = "/&nbsp;&nbsp;"+h+":"+m+":"+sc;
			
			document.getElementById('progress_bar_'+this.vn).style.width = (this.width-190)+'px';
				
		} else {
			
			var m = parseInt((d%3600)/60);
			var sc = parseInt((d%3600)%60);
			
			var m = parseInt((d%3600)/60);
			if (m < 10) m = "0"+m;
			
			var sc = parseInt((d%3600)%60);
			if (sc < 10) sc = "0"+sc;
			document.getElementById('elapsed_time_'+this.vn).innerHTML = "00:00";
			document.getElementById('total_time_'+this.vn).innerHTML = "/&nbsp;&nbsp;"+m+":"+sc;
		}
	}
	
	h5p.prototype.forward = function() {
		this.mouseMove();
		var interval = parseInt(this.object.duration/20);
		
		if (this.object.currentTime + interval <= this.object.duration)
			this.object.currentTime += interval;
		else
			this.object.currentTime = this.object.duration;
			
		this.updateControls();
		this.rfTimer = window.setTimeout(this.vn+'.forward()', 500);		
	}
	
	h5p.prototype.rewind = function() {
		this.mouseMove();
		var interval = parseInt(this.object.duration/20);
		if (this.object.currentTime - interval >= 0)
			this.object.currentTime -= interval;
		else 
			this.object.currentTime = 0;
			
		this.updateControls();
		this.rfTimer = window.setTimeout(this.vn+'.rewind()', 500);		
	}
	
	h5p.prototype.cancelRF = function() {
		window.clearTimeout(this.rfTimer);		
	}
	
	h5p.prototype.browserSupportsVideo = function() {
  		return !!document.createElement('video').canPlayType;
	}
	
	h5p.prototype.basename = function(path, suffix) {
	    var b = path.replace(/^.*[\/\\]/g, '');
	    
	    if (typeof(suffix) == 'string' && b.substr(b.length-suffix.length) == suffix) {
	        b = b.substr(0, b.length-suffix.length);
	    }
	    
	    return b;
	}
 
	h5p.prototype.getScriptPath = function(scriptname) {
		scriptobjects = document.getElementsByTagName('script');
		for (i=0; i<scriptobjects.length; i++) {
			if(this.basename(scriptobjects[i].src)==scriptname){
				return scriptobjects[i].src.substring(0, scriptobjects[i].src.lastIndexOf('/'));
			};
		}
		return "";
	}
	
	h5p.prototype.finishedDragging = function() {
		if (this.dragState == 1)
		this.play();
		this.dragState = 0;	
	}
	
	h5p.prototype.startDragging = function() {
		this.dragState = this.isPlaying;
		this.pause();
		this.pb.style.display = "none";
	}
	
	h5p.prototype.isIpad = function()
	{ 
		return navigator.userAgent.match(/iPad/i) != null; 
	}

 
window.fullscreenElement = false;	
window.addEventListener('resize',resize,false);
function resize() {
	if (window.fullscreenElement !== false)
	{
		eval(window.fullscreenElement+".handleResize()");
	}
}