/*
////////////////////////////////////////////////////
// External embed request js v01
// part of the metavid framework
// for details about the metavid code base and license 
// view: http://metavid.ucsc.edu/metavid_code
// for details about mv_embeds usage see:
// http://metavid.ucsc.edu/wiki/index.php/Mv_embed
//////////////////////////////////////////////

takes parameters by parsing the Div container which will be replaced with plugin detection code
this script is documented at: 
http://metavid.ucsc.edu/wiki/index.php/External_embed
*/


/* configuration */ 

//base server:
var base_metavid_server = 'http://metavid.ucsc.edu/';
//image path:
var img_path = 'images/';
/*
should not have to alter bellow this line: 
-------------------------------------------------------
*/ 

//some basic client detection: 
var agt=navigator.userAgent.toLowerCase();
var is_major = parseInt(navigator.appVersion);

var is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1)
&& (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1)
&& (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));

var is_nav4up = (is_nav && (is_major >= 4));
var is_nav6up = (is_nav && (is_major >= 5));

var is_safari = (agt.indexOf("safari") != -1);

var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
var is_ie5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.0")!=-1) );
var is_ie5_5 = (is_ie && (is_major == 4) && (agt.indexOf("msie 5.5") !=-1));
var is_ie6 = (is_ie && (is_major == 4) && (agt.indexOf("msie 6.0") !=-1));
var is_ie5up = (is_ie && (is_major == 4)
&& ( (agt.indexOf("msie 5.0")!=-1)
|| (agt.indexOf("msie 5.5")!=-1)
|| (agt.indexOf("msie 6.0")!=-1) ) );


/* mv_embed globals: */

var embed_type=null;
var embed_list= Array();

var sliders = new Array();
/*
* The main mv_embed detection script: 
*/
	  	  	  
function mv_embed(){ 
	//init local vars: 
	var i;
	//detect plugin type:
	embed_type = detect_client_plugins();	
	//get 
	
	//get all the embed videos for this page (use ieFix as IE does not like ElementsByNamed divs
	element_embed_list = getElementsByName_iefix("div", "mv_embed");
	
	for(i=0;i<element_embed_list.length;i++){		
		//replace the empty div with an image to play: 
		embed_list[i]= Array();
		embed_list[i]['div']=element_embed_list[i];
		
		//render the play button and image
		build_image_play_link(i);
	}
}

function getElementsByName_iefix(tag, name) {     
     var elem = document.getElementsByTagName(tag);
     var arr = new Array();
     for(i = 0,iarr = 0; i < elem.length; i++) {
          att = elem[i].getAttribute("name");
          if(att == name) {
               arr[iarr] = elem[i];
               iarr++;
          }
     }	 
     return arr;
}
//builds image link and places it in the div
function build_image_play_link(embed_id){
	embed_div = embed_list[embed_id]['div'];
	//init local vars: 
	var i; 		
	param_list = embed_div.getElementsByTagName("input");	
	for(i=0;i<param_list.length;i++){
		//parse params: 
		var param = param_list.item(i);		
		//set params in embed list array/object:
		eval('embed_list[embed_id][\''+param.getAttribute("name")+'\']=param.getAttribute("value");');
	}
	
	//check for required params: 
	if(!embed_list[embed_id]['media_url']){
		alert("error: missing media_url for " + embed_div);
		return null;
	}
	
	//check for control type: 
	if(!embed_list[embed_id]['display_control']){//default basic:
		embed_list[embed_id]['display_control']='basic';
	}	
	
	//get duration, start-end time: 
	parse_time(embed_id);		
	
	//if thumnail set: set thumnail
	if(embed_list[embed_id]['img_thumbnail']){
		//console.log('img: ' + embed_list[embed_id]['img_thumbnail']);
		//set the image a a div layer behind all. 
		/*var im = document.createElement("div");
		//im.innerHTML='<img src="'+img_thumbnail+'">';
		im.style.position='absolute';
		im.style.zindex=-1;
		im.style.top=0;
		im.style.left=0;
		im.style.width='100%';
		im.style.height='100%';
		im.style.backgroundimage='url("'+img_thumbnail+'")';*/
				
		var im_html= '<div style="position:relative; top:0px; left:0px; z-index:0;">'+
						 '<img src="' + embed_list[embed_id]['img_thumbnail'] + '">'+
					  '</div>';  
		embed_div.innerHTML+=im_html;
	}
				
	//add play button (if we have a server to get it from)
	//(you can host your own mv_embed_play.png icon on your own server)
	var tmp = new String();
	//tmp+='<div style="position:relative;top:-175px;left:100px;z-index:1">'
	tmp+='<div style="position:relative;top:-175px;left:100px;z-index:1">'
		+	'<a title="Play Media" href="javascript:auto_embed(\''+embed_id+'\')">';	

	if(is_ie){
		tmp+='<span style="display:inline-block;cursor:hand;width:109px;height:109px;'
			+ 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader'
			+ '(src=\'/mv_embed_play.png\', sizingMethod=\'scale\');"</span>';			
	}else{
		tmp+=	'<img id="img_'+embed_id+'" width="109" height="109" border="10" src="/mv_embed_play.png">';
	}					
	tmp+=	'</a>'
		+'</div>';			
	embed_div.innerHTML+=tmp; 	

	return null;
}
function parse_time(embed_id){
	//grab the media url
	var media_url = embed_list[embed_id]['media_url'];
	//http://metavid.ucsc.edu/m3u/clip/senate_11-14-05.ogg.anx?t=0:42:14/0:42:39
	var t_pos = media_url.indexOf("?t=");
	if(t_pos==-1){		// we don't have time info	(either an ogg file or live stream)		
		embed_list[embed_id]['duration']=0;
		embed_list[embed_id]['time_start_hr']='0:00:00';
		embed_list[embed_id]['time_start_sec']=0;
		return null;
	}
	var time_str = media_url.substring(parseInt(t_pos)+3);
	//strip out 'ntp:' if present (compatibility for (some)annodex time-urls)
	time_str = time_str.replace("ntp:", '');
	var tmp = new Array();
	tmp = time_str.split('/');
	//set human readable: 
	embed_list[embed_id]['time_start_hr']=tmp[0];
	embed_list[embed_id]['time_end_hr']=tmp[1];
	//set time in seconds: 
	embed_list[embed_id]['time_start_sec']=parseInt(ntp_to_sec(tmp[0]));
	embed_list[embed_id]['time_end_sec']=parseInt(ntp_to_sec(tmp[1]));
	//set duration:
	embed_list[embed_id]['duration']=parseInt([embed_id]['time_end_sec']-[embed_id]['time_start_sec']);
}
function ntp_to_sec(ntp_time){
	tp = ntp_time.split(':');
	return (parseInt(tp[0])*3600) + (parseInt(tp[1])*60) + parseInt(tp[2]);
}
function sec_to_ntp(sec){
	hours = Math.floor(sec/3600);
	minutes = Math.floor( (sec-(hours*3600))/60);
	sec = parseInt( sec-(hours*3600)-(minutes*60) );
	return hours.toString() + ':' + pad(minutes) + ':' + pad(sec);
}
function formatTime(timeVal)
{
    var timeHour = Math.round(timeVal / 1000);
    var timeSec = timeHour % 60;
    if( timeSec < 10 )
        timeSec = '0'+timeSec;
    timeHour = (timeHour - timeSec)/60;
    var timeMin = timeHour % 60;
    if( timeMin < 10 )
        timeMin = '0'+timeMin;
    timeHour = (timeHour - timeMin)/60;
     return timeHour+":"+timeMin+":"+timeSec;
};
function pad(num){
	return (num<10)?"0"+num:num;
}

function auto_embed(embed_id){
	opt= Array();
	if(embed_type){	
		eval(embed_type + "_embed(embed_id)");
	}else{
		plugin_missing_html(embed_id);
	}
}
function anx_embed(embed_id){
	//alert('anx_embed');
	embed_list[embed_id]['embed_type']='application/x-annodex-vlc-viewer-plugin';	
	vlc_embed(embed_id);
}

function jre_embed(embed_id){	
	var embed_div = embed_list[embed_id]['div'];

	var applet_code='<applet code="com.fluendo.player.Cortado.class" archive="http://mirror.linux.org.au/pub/linux.conf.au/2007/video/cortado-ovt-0.1.2.jar" width="320" height="240">'+	
		'<param name="url" value="' + embed_list[embed_id]['media_url']  + '" />'+
		'<param name="local" value="false"/>'+
		'<param name="keepaspect" value="true" />'+
		'<param name="video" value="true" />'+

		'<param name="audio" value="true" />'+
		'<param name="seekable" value="true" />'+		
		'<param name="bufferSize" value="200" />'+
	'</applet>';
	
	embed_div.innerHTML=applet_code;
	
	//note for remote embedding you have to use an iframe: to build an iframe include (for java security issues when remote embedding) 
	//@todo make sure the embed code is coming from the same server as the media
	/*var iframe = document.createElement("iframe");
	//alert('e width: ' + embed_div.style.width);
	iframe.width=embed_div.style.width;
	//add 4 pixles for the iframe controls and -20 pixles for the bottom white space. 
	iframe.height= (parseInt(embed_div.style.height)-20);
	iframe.frameborder=0;
	iframe.scrolling='no';
	iframe.MARGINWIDTH=0;
	iframe.MARGINHEIGHT=0;
	*/
	
	//var iframe_src = cortado_iframe;
	
	//@@todo check if its a sequence: 
	//replace 
	
	//@@TODO get cortado to support m3u files~!
	//@@TODO get cortado signed so it can load external clips
	//this is an ugly hack to deal with the lack of m3u support in cortado (generally not necessary) 
	/*s = new String(embed_list[embed_id]['media_url']);
	if(s.indexOf('/m3u/clip/') != -1){
		pos_s = s.indexOf('/m3u/clip/');		
		new_url = new String(base_metavid_server);
		new_url+='media/';
		new_url+=s.substring(pos_s+10);
		embed_list[embed_id]['media_url'] = new_url.toString();
		//alert('mediaurl: ' + embed_list[embed_id]['media_url'] );
	}			
	iframe_src+= "?media_url=" + embed_list[embed_id]['media_url'];
	//iframe_src+= "&width=" + opt['width'] + "&height=" + opt['height'];
	iframe_src+= "&duration=" + embed_list[embed_id]['duration'];
	
	//document.write(cortado_src);
	//alert('iframe: ' + iframe_src);
	iframe.src=iframe_src;
	
	embed_div.innerHTML="";
	embed_div.appendChild(iframe);*/
	
	//add info link: 
	if(embed_list[embed_id]['display_control']!="none"){
		var cnt=document.createElement("div");
		cnt.style.backgroundColor ='white';
		cnt.style.height='30px';
		cnt.innerHTML="";
		if(embed_list[embed_id]['info_page']){
			cnt.innerHTML+='<a title="media info" href="'+embed_list[embed_id]['info_page']+'">'+
			'	<img border="0" style="float:right"  src="'+img_path+'vid_info_sm.png" width="27" height="27" />'+
			'</a>';
		}
		embed_div.appendChild(cnt);
	}
}

//vlc embed: 
function vlc_embed(embed_id){	
	embed_div = embed_list[embed_id]['div'];
	//first insert the embed element
	if(!embed_list[embed_id]['embed_type']){
		embed_list[embed_id]['embed_type']='application/x-vlc-plugin';
	}	
	//make sure the embed div does not scroll: 
	embed_div.overflow="hidden";
	
	//build the embed obj: 
	var eb = document.createElement("div");
	eb.style.width='320px';
	eb.style.height='240px';
	eb.innerHTML='<object classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" '+
		'codebase="http://downloads.videolan.org/pub/videolan/vlc/latest/win32/axvlc.cab#Version=0,8,6,0" '+
		'id="video_'+embed_id+'" events="True" height="240" width="320">'+
'<param name="MRL" value="">'+
'<param name="ShowDisplay" value="True">'+
'<param name="AutoLoop" value="False">'+
'<param name="AutoPlay" value="False">'+
'<param name="Volume" value="50">'+
'<param name="StartTime" value="0">'+
'<embed pluginspage="http://www.videolan.org" type="application/x-vlc-plugin" progid="VideoLAN.VLCPlugin.2" name="video_'+embed_id+'" height="240" width="320">'+
'</object>';
	//replace the image with the embed
	embed_div.innerHTML="";
	
	embed_div.appendChild(eb);
	//manipulate the dom object to make sure it has the correct size: 
	vlc = getVLC('video_'+embed_id);
	vlc.style.width='320px';
	vlc.style.height='240px';
	//alert('embed html:\n' + eb.innerHTML);
	
	/*
	eb.type=embed_list[embed_id]['embed_type'];
	eb.pluginspage="http://www.videolan.org";
	eb.version="VideoLAN.VLCPlugin.2";
	
	eb.id="video_" + embed_id;
	//@@TODO make video res dynamic
	eb.width=320;
	eb.height=240;
	*/	
	
	//build the controls: 
	var cnt=document.createElement("div");
	cnt.style.backgroundColor ='white';
	if(embed_list[embed_id]['display_control']=="full"){
		cnt.style.height='40px';
	}else{
		cnt.style.height='30px';
	}
	cnt.innerHTML="";
	
	//assume vlc version > 8.6
	if(embed_type=='vlc'){
		var ct_playlist ='.playlist';
		var ct_video = '.video';
	}else{
		//legacy support for annodex extention:
		var ct_playlist = '';
		var ct_video = '.video';
	}
	
	if(embed_list[embed_id]['display_control']=="full"){
		//confirm the prototype libraries have been included: 
		if(typeof Scriptaculous == 'undefined'){
			console.log("Error: Scriptaculous required for full controls");
		}else{			
			cnt.innerHTML+=''+
			'<div id="track_'+embed_id+'" class="mv_track">'+
				'<div id="playhead_'+embed_id+'" class="mv_playhead"> </div>'+
			'</div>';
		}
	}	
	
	if(embed_list[embed_id]['display_control']=="basic" || embed_list[embed_id]['display_control']=="full"){
		cnt.innerHTML+=''+
		'<a title="play" href="javascript:;" onclick="video_action(\''+embed_id+'\',\'play\')">'+
		'	<img border="0" style="float:left"  src="'+img_path+'vid_play_sm.png" width="27" height="27" />'+
		'</a>'+
		'<a title="pause" href="javascript:;" onclick="video_action(\''+embed_id+'\',\'pause\')">'+
		'	<img border="0" style="float:left"  src="'+img_path+'vid_pause_sm.png" width="27" height="27" />'+
		'</a> '+
		'<a title="stop" href="javascript:;" onclick="video_action(\''+embed_id+'\',\'stop\')">'+
		'	<img border="0" style="float:left"  src="'+img_path+'vid_stop_sm.png" width="27" height="27" />'+
		'</a>'+
		'<a title="fullscreen" href="javascript:;" onclick="video_action(\''+embed_id+'\',\'fullscreen\')"">'+
		'	<img border="0" style="float:left" src="'+img_path+'vid_full_screen_sm.png" width="27" height="27" />'+
		'</a>'+
		'<span id="mv_status_'+embed_id+'" class="mv_status" style="float:left">0:00:00/0:00:00</span>';
	}
		
	//if info page & control_type!='none')
	if(embed_list[embed_id]['display_control']!="none"){
		if(embed_list[embed_id]['info_page']){
			cnt.innerHTML+='<a title="media info" href="'+embed_list[embed_id]['info_page']+'">'+
			'	<img border="0" style="float:right"  src="'+img_path+'vid_info_sm.png" width="27" height="27" />'+
			'</a>';
		}
	}
	
		
	//set the thumbnail as a background placeholder: 
	if(embed_list[embed_id]['img_thumbnail']){	
		embed_div.style.backgroundRepeat='no-repeat';
		embed_div.style.background='url("'+embed_list[embed_id]['img_thumbnail']+'")';
	}
	

	embed_div.appendChild(cnt);

	
	//set up the slider control:
	if(embed_list[embed_id]['display_control']=="full"){
		sliders[embed_id]= new Control.Slider('playhead_'+embed_id, 'track_'+embed_id,{
						 sliderValue:0,
						 onSlide:function(v){slide_clip(v, embed_id)},
						 onChange:function(v){jump_clip(v, embed_id)}
				 });		
		embed_list[embed_id]['slider_update']=true;		
	}else{
		embed_list[embed_id]['slider_update']=false;
	}
	//check to draw control.
	//if(embed_list[embed_id]['display_control']=="basic" || embed_list[embed_id]['display_control']=="full"){
	//}
	//alert(embed_div.innerHTML);	
	setTimeout('run_vlc(\''+embed_id+'\')', 200);
}
function video_action(embed_id, action){
	var vlc = getVLC("video_" +embed_id);
	if(!vlc){
		//console.log('wtf vlc missing');
	}else{
		//console.log('vlc playlist:' + playlist);
	}
	if(embed_type=='vlc'){
		switch(action){
			case 'play':vlc.playlist.play();break;
			case 'stop':vlc.playlist.stop();break;
			case 'pause':vlc.playlist.togglePause();break;
			case 'fullscreen':vlc.video.toggleFullscreen();break;
		}
	}else if(embed_type=='anx'){
		switch(action){
			case 'play':eval("document.video_" +embed_id+".play();");break;
			case 'stop':eval("document.video_" +embed_id+".stop();");break;
			case 'pause':eval("document.video_" +embed_id+".pause();");;break;
			case 'fullscreen':eval("document.video_" +embed_id+".fullscreen();");break;
		}
	}
}
function run_vlc(embed_id){
	var media_url = embed_list[embed_id]['media_url'];
	//assume vlc version > 8.6	
	if(embed_type=='vlc'){
		var vlc = getVLC("video_" +embed_id);
		vlc.playlist.items.clear();

		var itemId = vlc.playlist.add(media_url);
		if( itemId != -1 ){
			//play
			vlc.playlist.playItem(itemId);
		}else{
			alert("cannot play at the moment !");
		}
	}else if(embed_type=='anx'){
		//legacy support for annodex extention:
		var ct_playlist = '';
		eval("document.video_" +embed_id+".stop();");
		eval("document.video_" +embed_id+".clear_playlist();");
		eval("document.video_" +embed_id+".add_item(media_url);");
		eval("document.video_" +embed_id+".play();");
	}
	//do status updates for the browser: 
	setTimeout('run_browser_update(\''+embed_id+'\')',100);		
	//alert('target: ' +  target +" media:"+ media_url);
}
function getVLC(name){
	//return document.getElementById(name);
	//if (window.document[name]){
	//	return window.document[name];
	//}
	if (navigator.appName.indexOf("Microsoft Internet")==-1){
		if (document.embeds && document.embeds[name])
		return document.embeds[name];
	}else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	{
		return document.getElementById(name);
	}
}
/*function run_vlc(embed_id){
	//attempt to grab the vlc object and get version number: 
	var cur_vlc =document.getElementById("video_" + embed_id);
	if(cur_vlc){
		console.log('cur vlc html:' + cur_vlc.innerHTML);
		console.log('cur vlc found:' + cur_vlc.VersionInfo);
		//url = media_url;
		media_url = embed_list[embed_id]['media_url'];
		//alert('target: ' +  target +" media:"+ media_url);
		cur_vlc.clear_playlist();
		cur_vlc.add_item(media_url);
		cur_vlc.play();
	}
	
}*/
//updates the slider position based on the vlc time:
function run_browser_update(embed_id){	
	//console.log('browser update');
	//load the current video object: 	
	var vlc = getVLC("video_"+embed_id);
	if(vlc){			
	
		//check if playing via embed type:
		if(embed_type=='vlc'){
			var state = vlc.input.state;
			if(state==3)isPlaying=true;
			//var isPlaying = vlc.playlist.isPlaying;
		}else if(embed_type=='anx'){
			var isPlaying = vlc.isplaying();
		}
				
		if( isPlaying ){
			if(embed_type=='vlc'){
				var got_time = vlc.input.time;
			}else if(embed_type=='anx'){
				var got_time = vlc.get_time();
			}			
			//console.log("got time: " + got_time);
			
			document.getElementById('mv_status_'+embed_id).innerHTML=""+
				embed_list[embed_id]['time_start_hr'] + '/' + formatTime(got_time);
				
			//if durration is zero attemp to get the duration from the video file: 				
			if(embed_list[embed_id]['duration']==0){
				if(embed_type=='vlc'){
					var got_dur = vlc.input.length
				}else if(embed_type=='anx'){
					var got_dur = vlc.get_length();
				}
				embed_list[embed_id]['duration'] = got_dur;		
			}
			//get how far along we are: 
			var offset = parseInt(got_time) - parseInt(embed_list[embed_id]['time_start_sec']);
			if(	embed_list[embed_id]['slider_update']==true){
				//@@TODO build self contained controls or a way to dynamicly import scriptaculous
				embed_list[embed_id]['slider_set_type']='vlc';
				if(offset==0){
					sliders[embed_id].setValue(0);
				}else{									
					sliders[embed_id].setValue(offset / embed_list[embed_id]['duration']);
				}
				embed_list[embed_id]['slider_set_type']='manual';
			}
			//alert('got time: ' + got_time +"\n start time:" + embed_list[embed_id]['time_start_sec']);
			
		
		}else{
			//if video not playing set slider to start
			//again see abstract
			if(	sliders[embed_id] ) { 
				embed_list[embed_id]['slider_set_type']='vlc';
				sliders[embed_id].setValue(0);
				embed_list[embed_id]['slider_set_type']='manual';
			}
		}
		setTimeout('run_browser_update(\''+embed_id+'\')',100);			
	}
}
/*
slide clip: 
//@@TODO have frame updates as you slide
pause vlc video (if not already paused)
*/
function slide_clip(v, embed_id){
	//turn off slider updates
	embed_list[embed_id]['slider_update']=false;
	//if( eval("document.video_" +embed_id+"!=null") ){		
	//	if( eval("document.video_" +embed_id+".isplaying()")){
	//		eval("document.video_" +embed_id+".pause();");
	//	}
	//}
}
//jump to new location and play
function jump_clip(v, embed_id){
	if(embed_list[embed_id]['slider_set_type']=='manual'){
		//throw("Jump: v:"+ v + " embed: " + embed_id);
		//get time
		if(v==0){
			seek_time=0;	
		}else{
			offset = parseInt(v*embed_list[embed_id]['duration']);
			seek_time = embed_list[embed_id]['time_start_sec']+offset;
		}
		//alert('seek time: ' + seek_time);
		if( eval("document.video_" +embed_id+"!=null") ){		
			eval("document.video_" +embed_id+".seek("+seek_time+", false)");			
		}
	}
	embed_list[embed_id]['slider_update']=true;
}

function plugin_missing_html(embed_id){	
	embed_div = embed_list[embed_id]['div'];
	var out='<div style="margin:6px"><p>The '+
	' <a href="http://www.videolan.org/doc/play-howto/en/ch04.html#id293992">vlc mozilla plugin</a> or '+
	' <a href="http://java.com/en/download/index.jsp">java plug-in</a> is required to play this ogg theora encoded clip '+
	' <br>for more info <i>see metavid <a href="http://metavid.ucsc.edu/wiki/index.php/MetavidFAQ#How_can_I_play_back_Metavid_video">playback help</a></i>'+
	'<p><b>alternatively</b> you can <a href="'+embed_list[embed_id]['media_url'] +'">'+
	'download this clip</a> to be viewed outside the web browser with video application such as '+
		'<a href="http://www.videolan.org/vlc/">VLC</a>'+
	'</div>';
	embed_div.innerHTML=out;
}

function detect_client_plugins(){
	if(is_nav6up){
		//alert('is netscape');
		//vlcPlugin = navigator.plugins["VLC multimedia plugin"];
		//console.log( navigator.plugins);
		if( navigator.plugins["VLC Multimedia Plugin"] ){
			//alert('found vlc');
			//vlc found (use the the new api)
			return 'vlc';
		}else{
			if( navigator.plugins["VLC Annodex Viewer plugin"] ){
				//anx found
				return 'anx';
			}else{				
				//alert('looking for applet');
				//console.log('looking for applet');
				//try for a java-applet detection: (not yet supported)
				return detect_applet();				
			}
		}
	}else if(is_ie){
		//check for video lan: 
		var p;
		try {
			p = new ActiveXObject('VideoLAN.VLCPlugin.2');
		}
		catch (e) {
			//alert('vlc not found');
			return detect_applet();
		}
		if (p){
			//alert('vlc found');
			return 'vlc';
		}		
	}else{
		//other platforms look for java: 
		//try to detect the applet
		return detect_applet();
	}
}

function detect_applet(){	
	var pluginDetected = false;
	var activeXDisabled = false;
	
	// we can check for plugin existence only when browser is 'is_ie5up' or 'is_nav4up'
	if(is_safari){
		//safari has java by default
		return 'jre';
	}else if(is_nav4up) {
		// Refresh 'navigator.plugins' to get newly installed plugins.
		// Use 'navigator.plugins.refresh(false)' to refresh plugins
		// without refreshing open documents (browser windows)
		if(navigator.plugins) {
			navigator.plugins.refresh(false);
		}
		
		// check for Java plugin in installed plugins
		if(navigator.mimeTypes) {
			for (i=0; i < navigator.mimeTypes.length; i++) {
				if( (navigator.mimeTypes[ i].type != null)
						&& (navigator.mimeTypes[ i].type.indexOf(
						"application/x-java-applet;") != -1) ) {
					pluginDetected = true;
					break;
				}
			}
		}
	} else {
		//general way to check for java in IE & others
		if(navigator.javaEnabled() == 1){
			return 'jre';	
		}
	}			
	if (pluginDetected) {
		return 'jre';
	} else {
		return null;
	}
}
//dynamically include a javascript file:
function include_js(src){
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = src;
	document.getElementsByTagName('head')[0].appendChild(script);  
}
function $import(path){
    var i, base, src = "grid.js", scripts = document.getElementsByTagName("script");
    for (i=0; i<scripts.length; i++){if (scripts[i].src.match(src)){ base = scripts[i].src.replace(src, "");break;}}
    document.write("<" + "script src=\"" + base + path + "\"></" + "script>");
} 

/* auto load event register */
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}
addLoadEvent(mv_embed);


//extra functions: 

<!--[if lt IE 7]>
function correctPNG(imgID) // correctly handle PNG transparency in Win IE 5.5 & 6.
{	 
	var img = document.images[document.getElementyByID(imgID)];
        var imgName = img.src.toUpperCase()
        if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
        {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
}

