//we ll need some functions for strings
String.prototype.endsWith = function(str){ return this.substring(this.length - str.length) == str ? true : false; }
String.prototype.getExtension = function(){ return this.substring(this.lastIndexOf(".", this.length-1)+1)}

//the supported video formats
var supportedVideoFormats = ['flv', 'wmv', 'avi', 'mpg', 'mpeg', 'asf'];
var formatTester = new RegExp('^(' + supportedVideoFormats.join('|') + ')$', 'i');

//since we change the innerHTML of the parent node of the anchor, we must check that its ok to change the parent element.
var allowedParentTags = ['p', 'li'];
var parentTagTester = new RegExp('^('+ allowedParentTags.join('|') + ')$', 'i');




/******************************************************************************************************
This function converts all anchors to video files. It checks the target attribute and does the following:
	- if target=_self then replace the anchor with the video player
	- else if target=_blank, replaces the href with a function that opens the video player in a new window
	- else (or if no target attribute is found) the link stays as is  */

convertVideoAnchors = function(){

	var anchors = document.getElementsByTagName('a');
	var videoId = 0;
	
	for(i=0; i<anchors.length; i++){
		var href = anchors[i].href;
		var extension = href.getExtension();
		var target = anchors[i].target;
		
		if (formatTester.test(extension)){

			if( target == '_self'){
				var parentElement = anchors[i].parentNode;
				
				//it must be a p element or an li
				if( ! parentTagTester.test(parentElement.tagName))
					continue;
				
				parentElement.innerHTML = '';
				parentElement.id='playerContainer_'+ (videoId++);
				i--; //because anchors.length just got shorter by 1				
				embedVideoPlayer(parentElement.id, href);
			}				
			else if( target == '_blank'){
				anchors[i].href = "javascript:playVideo('"+href+"')";
				anchors[i].target = "_self";
			}
			
		}		
	}
}


embedVideoPlayer = function(playerContainerId, videoUri){

	var width = typeof(__WIDTH__) == 'undefined' ? 400 : __WIDTH__;
	var height = typeof(__HEIGHT__) == 'undefined' ? 300 : __HEIGHT__;
	
	//if video is flv then ok proceed with ac_fl_run_content
	if( videoUri.endsWith('.flv'))
		AC_FL_RunContent(
			'containerelementid',playerContainerId,
			'codebase', 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0',
			'width', ''+width,
			'height', ''+height,
			'src', '/eot/flash/flvplayer',
			'quality', 'high',
			'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
			'align', 'middle',
			'play', 'true',
			'loop', 'false',
			'scale', 'showall',
			'wmode', 'window',
			'devicefont', 'false',
			'id', 'flvplayer',
			'bgcolor', '#ffffff',
			'name', 'flvplayer',
			'menu', 'true',
			'allowFullScreen', 'false',
			'allowScriptAccess','sameDomain',
			'flashVars','movie='+videoUri,
			'movie', '/eot/flash/flvplayer',
			'salign', ''
		);
	
	//if video is win then we embed directly  
	else
		document.getElementById(playerContainerId).innerHTML = 
			'<object id="playerobject" width="+width+" height="+height+" type="application/x-oleobject" standby="Loading Windows Media Player components..." classid="CLSID:6BF52A52-394A-11d3-B153-00C04F79FAA6"> '+
			'<param name="AnimationatStart" value="false" /> '+
			'<param name="TransparentatStart" value="true" /> '+
			'<param name="ShowControls" value="true" /> '+
			'<param name="ShowDisplay" value="true" /> '+
			'<param name="ShowStatusBar" value="true" /> '+
			'<param name="autoStart" value="true" /> '+
			'<param name="stretchToFit" value="false" /> '+
			'<param name="uiMode" value="none" /> '+
			'<param name="filename" value="'+videoUri+'" /> '+
			'<param name="url" value="'+videoUri+'" /> '+
			'<param name="enableContextMenu" value="true"> '+
			'<embed type="application/x-ms-wmp" pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/" '+
			'filename="'+videoUri+'" '+
			'src="'+videoUri+'" '+
			'showcontrols="1" '+
			'showdisplay="0" '+
			'showstatusbar="1" '+
			'enableContextMenu="1" '+
			'width="+width+" height="+height+"></embed> '+
			'</object> ';
		
		
}


playVideo = function(videoUri){
	var newWin = window.open('','','width=700,height=500,scrollbars=yes,resizable=yes');
	newWin.document.write(
		'<html>'+
			'<head>'+
				'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+
				'<title>Video Player</title>'+
				'<script type="text/javascript" src="/eot/scripts/jquery-1.4.2.min.js"></script>'+
				'<script type="text/javascript" src="/eot/scripts/AC_RunActiveContent.js"></script>'+
				'<script type="text/javascript">__WIDTH__ = 640; __HEIGHT__= 385;</script>'+
				'<script type="text/javascript" src="/eot/scripts/gg.utils.video.js"></script>'+				
			'</head>'+
			'<body><center><p id="playerContainer"><a href="'+videoUri+'" target="_self"></a></p></center></body>'+			
		'</html>');

	newWin.document.close();
}


embedYoutube = function(videoUri, width, height){
	var newWin = window.open('','','width='+(100+width)+',height='+(60+height)+',scrollbars=yes,resizable=yes');
	newWin.document.write(
		'<html>'+
			'<head>'+
				'<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />'+
				'<title>Video Player</title>'+				
			'</head>'+
			'<body><center><p>'+
				'<object width="'+width+'" height="'+height+'">'+
					'<param name="movie" value="'+videoUri+'"></param>'+
					'<param name="allowFullScreen" value="true"></param>'+
					'<param name="allowscriptaccess" value="always"></param>'+
	
					'<embed '+
						'src="'+videoUri+'" '+
						'type="application/x-shockwave-flash" '+
						'allowscriptaccess="always" '+
						'allowfullscreen="true" '+
						'width="'+width+'" '+
						'height="'+height+'">'+
					'</embed>'+
				'</object>'+
			'</body>'+
		'</html>'
	);

	newWin.document.close();
}

jQuery(document).ready(function(){
	convertVideoAnchors();
});