// Video controlls
function videoControlls (_instanceName, _videoHolderId) 
{	

	// QuickTime
	this.playQuickTime = function(filename,objectHolder,width,height)
	{
		if(this.defaultControlls)
			QT_WriteOBJECT(objectHolder, filename, width, height, '','id',this.instanceName+'_QT','name',this.instanceName+'_QT','SHOWLOGO','false','AUTOPLAY','true','CONTROLLER','true','scale','ASPECT','QTSRCCHOKESPEED','movierate','ENABLEJAVASCRIPT','true','bgcolor','000000');		
		else
			QT_WriteOBJECT(objectHolder, filename, width, height, '','id',this.instanceName+'_QT','name',this.instanceName+'_QT','SHOWLOGO','false','AUTOPLAY','true','CONTROLLER','false','scale','ASPECT','QTSRCCHOKESPEED','movierate','ENABLEJAVASCRIPT','true','bgcolor','000000');
	}
	
	
	// Flash
	this.playFlash = function(filename,objectHolder,width,height)
	{
		objectHolder.innerHTML = '<div id="'+this.instanceName+'_FLASH_HOLDER"></div>';		
		var flashvars = {};
		var params = {};				
			//params.wmode = "transparent";
			params.allowFullScreen = true;	
		var attributes = {};		
			attributes.name = this.instanceName+'_FLASH';
			attributes.id = this.instanceName+'_FLASH';	
			attributes.bgcolor = "#000000";			
		if(this.defaultControlls)	
			swfobject.embedSWF(this.flashPlayer+"?file="+escape(filename)+"&format=fit", this.instanceName+'_FLASH_HOLDER', width, height, "10.0.0", false, flashvars, params, attributes);
		else
			swfobject.embedSWF(this.flashPlayer+"?file="+escape(filename)+"&format=fit&disablecontrolls=true", this.instanceName+'_FLASH_HOLDER', width, height, "10.0.0", false, flashvars, params, attributes);
	}
	
	// Flash video object
	this.flashVideo = function ()
	{
		var isIE = navigator.appName.indexOf("Microsoft") != -1;
		return (isIE) ? window[this.instanceName+'_FLASH'] : document[this.instanceName+'_FLASH'];
	}
	
	// Auto set type
	this.autoSetPlayerType = function ()
	{
		// Add html 5 object
		var vid = document.createElement('video');
		vid.id = this.videoObjectId;
		vid.width = document.getElementById(this.videoHolderId).offsetWidth;
		vid.height = document.getElementById(this.videoHolderId).offsetHeight;
		vid.onloadstart = new Function(this.instanceName+'.onLoadStart()');
		document.getElementById(this.videoHolderId).appendChild(vid);
		
		// Set video object
		this.videoObject = document.getElementById(this.videoObjectId);	
		
		// No html 5 support
		if(typeof this.videoObject.canPlayType != 'function' || !this.videoObject.canPlayType(this.canPlayType))
		{			
			// Flash	
			if (swfobject.getFlashPlayerVersion())	
				this.playbackType = 'flash';	
			
			// Check quicktime
			else 
			{
			this.playQuickTime(arguments[0],document.getElementById(this.videoHolderId),document.getElementById(this.videoHolderId).offsetWidth,document.getElementById(this.videoHolderId).offsetHeight);
			if(document[this.instanceName+'_QT'])			
				this.playbackType = 'qt';
			}
			
		}	
		
		else 
			this.playbackType = 'html';	
		
	}

	
	// Play
	this.play = function() 	
	{
		// Set player
		if(arguments.length > 0 && this.playbackType == null) 
			this.autoSetPlayerType(arguments[0]);	
		
		// Flash
		if(this.playbackType == 'flash')
		{				
			if(arguments.length > 0) 
				this.playFlash(arguments[0],document.getElementById(this.videoHolderId),document.getElementById(this.videoHolderId).offsetWidth,document.getElementById(this.videoHolderId).offsetHeight);
			else 			
				this.flashVideo().playVideo();
			
			this.isPaused = false;
			this.seekBar();					
		}	
		
		// Quicktime
		else if(this.playbackType == 'qt')
		{				
			if(arguments.length > 0) 
				this.playQuickTime(arguments[0],document.getElementById(this.videoHolderId),document.getElementById(this.videoHolderId).offsetWidth,document.getElementById(this.videoHolderId).offsetHeight);
			else 			
				document[this.instanceName+'_QT'].Play();
			
			this.isPaused = false;
			this.seekBar();					
		}
		
		// Html
		else 
		{				
			// Set video object
			if(!this.videoObject)
				this.videoObject = document.getElementById(this.videoObjectId);	
				
			this.videoObject.autoplay = true;
			
			// Controlls
			if(this.defaultControlls)	
				this.videoObject.controls = true;
			
			// Load file
			if(arguments.length > 0) 
				this.videoObject.src = arguments[0];
			
			// Play
			this.videoObject.play();	
			this.videoObject.style.display = "block";			
			this.isPaused = this.videoObject.paused;	
			this.seekBar();				
		}
		
		// On toggle play
		if(typeof this.onTogglePlay == 'function')
			eval('this.onTogglePlay(this.isPaused)');
	}
	
	
	// Stop
	this.stop = function() 
	{
		// Flash
		if(this.playbackType == 'flash')
		{			
			this.flashVideo().stopVideo();
			document.getElementById(this.videoHolderId).innerHTML = '';
			this.isPaused = true;	
		}
		
		// Quicktime
		else if(this.playbackType == 'qt')
		{
			document[this.instanceName+'_QT'].Stop();
			this.isPaused = true;
		}
		
		// Html	
		else 
		{
			this.videoObject.pause();
			this.videoObject.style.display = "none";
			this.videoObject.currentTime = 0;
			this.isPaused = this.videoObject.paused;
		};
		
		// Reset bar
		if(this.seekBarId != null)
			document.getElementById(this.seekBarId).style.width = "0px";
		
		// On toggle play
		if(typeof this.onTogglePlay == 'function')
			eval('this.onTogglePlay(this.isPaused)');
	}	
	
	
	// Full screen
	this.fullScreen = function()
	{
		if(this.playbackType == 'html' && typeof this.videoObject.webkitEnterFullscreen == 'function')
			this.videoObject.webkitEnterFullscreen();
	}
	
	// Toggle play
	this.togglePlay = function()
	{	
		// Flash
		if(this.playbackType == 'flash')
		{
			if(this.isPaused) 
			{
				this.flashVideo().playVideo();	
				this.isPaused = false;
				this.seekBar();	
			}
			
			else 
			{
				this.flashVideo().pauseVideo();
				this.isPaused = true;
			}		
		}
			
		// Quicktime
		else if(this.playbackType == 'qt')
		{
			if(this.isPaused) 
			{
				document[this.instanceName+'_QT'].Play(); 
				this.isPaused = false;
				this.seekBar();	
			}
			
			else 
			{
				document[this.instanceName+'_QT'].Stop(); 
				this.isPaused = true;
			}		
		}
		
		// Html
		else 
		{
			if(this.videoObject.paused) 
			{
				this.videoObject.play();
				
			}
			else
				this.videoObject.pause();
				
			this.isPaused = this.videoObject.paused;	
			if(!this.isPaused)
				this.seekBar();	
		}
		
		// On toggle play
		if(typeof this.onTogglePlay == 'function')
			eval('this.onTogglePlay(this.isPaused)');	
	}	
	
	
	// Set seek bar
	this.setSeekBar = function(_objectId, _width) 
	{
		this.seekBarId = _objectId;	
		this.seekBarWidth = _width;			
	}
	
	
	// Seek bar
	this.seekBar = function() 
	{		
		if(this.seekBarId != null)
		{				
				// Set
				var duration = 0;
				var current = 0;
				
				// Flash
				if(this.playbackType == 'flash')
				{		
					if(typeof this.flashVideo().getDuration == 'function')
					{			
						duration = this.flashVideo().getDuration();
						current = this.flashVideo().getCurrentTime();	
					}
				}
				
				// Quicktime
				else if(this.playbackType == 'qt')
				{	
					if (document[this.instanceName+'_QT'].GetPluginStatus() == 'Playable' || document[this.instanceName+'_QT'].GetPluginStatus() == 'Complete') 
					{
						duration = document[this.instanceName+'_QT'].GetDuration();
						current = document[this.instanceName+'_QT'].GetTime();
					}
				}
				
				// Html
				else 
				{
					duration = this.videoObject.duration;
					current = this.videoObject.currentTime;			
				}	
				
				var percentage = Math.round((current/duration)*100);
				if(percentage > 0)
					document.getElementById(this.seekBarId).style.width = (this.seekBarWidth*(percentage/100))+"px";	
				else 
					document.getElementById(this.seekBarId).style.width = "0px";
			
			// End
			
				// Flash		
				if(this.playbackType == 'flash' && typeof this.flashVideo().getDuration == 'function' && this.flashVideo().ended())
				{
					this.flashVideo().stopVideo();
					if(typeof this.onTogglePlay == 'function')
						eval('this.onTogglePlay(true)');
					this.onEnded();		
				}
			
				// Quicktime		
				else if(this.playbackType == 'qt' && (document[this.instanceName+'_QT'].GetPluginStatus() == 'Playable' || document[this.instanceName+'_QT'].GetPluginStatus() == 'Complete') && current == document[this.instanceName+'_QT'].GetEndTime())
				{
					document[this.instanceName+'_QT'].Stop();
					if(typeof this.onTogglePlay == 'function')
						eval('this.onTogglePlay(true)');
					this.onEnded();		
				}
				
				// Html
				else if(this.playbackType == 'html' && this.videoObject.ended && typeof this.onEnded == 'function')
				{
					this.videoObject.pause();
					if(typeof this.onTogglePlay == 'function')
						eval('this.onTogglePlay(true)');
					this.onEnded();	
				}
			
			// Interval
			else if(!this.isPaused)
				setTimeout(this.instanceName+'.seekBar()',10);
		}
	}
	
	
	// Seek 
	this.seek = function(evt,_object) 
	{					
		// Set action
		if(evt.type  == 'mousedown')
		{
			// Set clear overlay
			this.setOverlay(true);
			
			// Flash
			if(this.playbackType == 'flash')
			{
				this.flashVideo().pauseVideo();
				this.isPaused = true;
			}
			
			// Quicktime
			else if(this.playbackType == 'qt')
			{
				document[this.instanceName+'_QT'].Stop();
				this.isPaused = true;
			}
			
			// Html
			else
			{
				this.videoObject.pause();
				this.isPaused = this.videoObject.paused;
			}
			
			// On toggle play	
			if(typeof this.onTogglePlay == 'function')
				eval('this.onTogglePlay(this.isPaused)');
		}		
	
		// Set/Scroll position
		if(evt.type  == 'mousedown' || evt.type  == 'mousemove')
		{
			_object.blur();					
			evt=(evt)?evt:((window.event)?event:null);
			
			if(evt.offsetX && evt.type  == 'mousedown')
				var localX = evt.offsetX;				
			else
				var localX = evt.clientX-getElementPosition(_object).left;			
			if(localX >= 0 && localX <= this.seekBarWidth)
			{
				if(this.playbackType == 'flash')
					this.flashVideo().setCurrentTime(this.flashVideo().getDuration()*parseInt(localX)/this.seekBarWidth);	
				else if(this.playbackType == 'qt')
					document[this.instanceName+'_QT'].SetTime(document[this.instanceName+'_QT'].GetDuration()*parseInt(localX)/this.seekBarWidth);	
				else 
				{
					var setCurrentTime = this.videoObject.duration*parseInt(localX)/this.seekBarWidth;					
					if(setCurrentTime == 0)
						setCurrentTime = 0.1;					
						
					if(setCurrentTime > this.videoObject.seekable)
						this.videoObject.currentTime = this.videoObject.seekable;	
					else 
						this.videoObject.currentTime = setCurrentTime;						
				}
			}			
			this.seekBar();	
		}
		
		// Add listeners
		if(evt.type == 'mousedown')
		{
			
			if(document.body.addEventListener) 
			{
				_object.addEventListener("drag",function(){ return false; }, false);
				document.body.addEventListener("mousemove",seekOnMouseMove = function(evt){ videoplayer.seek(evt,_object); }, true);
				document.body.addEventListener("mouseup", seekOnMouseUp = function(evt){ videoplayer.seek(evt,_object); }, true);
			}
			
			else if (document.body.attachEvent) 
			{
				document.body.attachEvent("onmousemove",seekOnMouseMove = function(evt){ videoplayer.seek(evt,_object); });
				document.body.attachEvent("onmouseup", seekOnMouseUp = function(evt){ videoplayer.seek(evt,_object); });
			}
		}
			
		// Mouse up	
		else if(evt.type  == 'mouseup')
		{			
			// Remove listneres	
			if(document.body.addEventListener) 
			{
				document.body.removeEventListener("mousemove",seekOnMouseMove, true);
				document.body.removeEventListener("mouseup",seekOnMouseUp, true);
			}
			
			else if (document.body.detachEvent) 
			{
				document.body.detachEvent("onmousemove",seekOnMouseMove);
				document.body.detachEvent("onmouseup",seekOnMouseUp);	
			}
			
			// Flash
			if(this.playbackType == 'flash')
			{
				this.flashVideo().playVideo();
				this.isPaused = false;
			}	
			
			// Quicktime
			else if(this.playbackType == 'qt')
			{
				document[this.instanceName+'_QT'].Play();
				this.isPaused = false;
			}
			
			// Html
			else
			{
				this.videoObject.play();
				this.isPaused = this.videoObject.paused;
			}
			
			// Reset controlls
			this.seekBar();			
			this.setOverlay(false);
				
			// On toggle play
			if(typeof this.onTogglePlay == 'function')
				eval('this.onTogglePlay(this.isPaused)');				
		}
	}
	
	
	// Download current src
	this.download = function ()
	{
		if(this.playbackType == 'flash')
			var src = this.flashVideo().getCurrentSrc();
		else if(this.playbackType == 'qt')
			var src = document[this.instanceName+'_QT'].GetURL();
		else
			var src = this.videoObject.currentSrc;
		
		if(typeof this.onDownload == 'function')
			this.onDownload(src);
		else if(this.videoObject != null)
			location.href = src;		
	}
	
	
	// Get element position
	function getElementPosition(object)
	{
		var offsetTrail = object;
		var offsetLeft = 0;
		var offsetTop = 0;
		while(offsetTrail)
		{
			offsetLeft += offsetTrail.offsetLeft;
			offsetTop += offsetTrail.offsetTop;
			offsetTrail = offsetTrail.offsetParent;
		}
		if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != 'undefined')
		{
			offsetLeft += document.body.leftMargin;
			offsetTop += document.body.topMargin;
		}
		return {left:offsetLeft,top:offsetTop};
	}
	
	
	// On load start
	this.onLoadStart = function () 
	{		
		// Set video object
		if(!this.videoObject) 
			this.videoObject = document.getElementById(this.videoObjectId);	
			
		if(this.defaultControlls)	
			this.videoObject.controls = true;
			
		this.videoObject.style.display = "block";		
		
		if(this.seekBarId != null && this.videoObject && !this.videoObject.ended)
			this.seekBar();							
	}
	
	
	// Set overlay
	this.setOverlay = function(_show) 
	{		
		if(_show) 
		{	
			if(!document.getElementById('videoControllsHiddenBlocker')) 	
			{
				var object = document.createElement('div');
				object.id = 'videoControllsHiddenBlocker';
				object.style.position = 'fixed';
				object.style.display = 'none';
				object.style.top = '0px';
				object.style.left = '0px';
				object.style.width = '100%';
				object.style.height = '100%';
				object.innerHTML = '<div style="top:0px; left:0px; width:100%; height:100%; background-color:#000000; z-index: 99999; opacity:.0; filter: alpha(opacity=0); -moz-opacity:"></div>';
				document.getElementsByTagName('body')[0].appendChild(object);				
			}				
			document.getElementById('videoControllsHiddenBlocker').style.display = "block";
		}		
		else
			document.getElementById('videoControllsHiddenBlocker').style.display = "none";		
	}
	
	// Constructor	
	this.instanceName = _instanceName;
	this.videoObjectId = _instanceName+'_HTML5';
	this.videoHolderId = _videoHolderId;
	this.isPaused = false;
	this.playbackType = null;
	
	// Local veriables		
	this.flashPlayer = 'flash/video.swf';	
	this.canPlayType = 'video/quicktime';
	this.defaultControlls = true;	
	this.videoObject = null;	
	this.seekBarId = null;	
	this.seekBarWidth = null;		
	
}
