
var flvs = document.getElementsByName("flvImage");
var youtube_url = "http://www.youtube.com";
if (flvs) {
	var flvIDs = new Array();
	for (var flv = 0; flv < flvs.length;flv++) {
		flvs[flv].id = flvs[flv].id.replace(/[\- ]/g,"_");
  		flvIDs[flvIDs.length] = flvs[flv].id;
  	}
  	var video = null;
  	for (var flv = 0; flv < flvIDs.length;flv++) {
	  	try {
		  	if (!flowplayer) {
		  		break;
		  	}
		}
		catch(e) {
			break;
		}
  		video=document.getElementById(flvIDs[flv]);
	    var flvflv = video.getAttribute("flv");
	    if ((flvflv.search(/^\s*</) != -1) || (flvflv.indexOf(youtube_url) == 0)) {
	    	embedExternalElement(video);	
	    }
	    else if (flvflv.search(/\.swf$/i) != -1) {	// flash banner
	    	embedFlashElement(video);
	    	continue;
	    }
	    else {
	    	embedVideoElement(video);
	    	continue;
	    }
	    
		
  }	// for (var flv=0
}	// if (flvs)
//===================================================================
//  
//  embedExternalElement: do 
//  
//===================================================================
function embedExternalElement(video) { 
	
	var flvdiv = document.createElement("div");
    flvdiv.id = video.id+"_";
    flvdiv.style.width = video.style.width;
    flvdiv.style.height = video.style.height.replace(/\s/,"");
    
    flvdiv.style.margin = "0px";
    var code = video.getAttribute("flv");
    if (flvflv.indexOf(youtube_url) == 0) {
    	var youcode = "";
    	var youstyle = "width:"+flvdiv.style.width+"; height:"+flvdiv.style.height+"; ";
    	if (	code.search(/v=([^&]+)/) != -1) {
    		youcode = RegExp.$1;
    	}
    	else {
    		return;
    	}
    	code = '<object wmode="transparent" width="425" height="344" style="'+youstyle+'"><param name="movie" value="http://www.youtube.com/v/'+youcode+'></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed src="http://www.youtube.com/v/'+youcode+'" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true"  wmode="transparent" width="425" height="344"  style="'+youstyle+'"></embed></object>';
    }
   
    flvdiv.innerHTML = code;
    //alert(video)
   	video.parentNode.replaceChild(flvdiv,video);
}

//===================================================================
//  
//  embedFlashElement: embed flash code to page 
//  
//===================================================================
function embedFlashElement(flash) {
		var swfid = flash.id;
	    var swfurl = flash.getAttribute("flv");
	    var swfProperties = flash.getAttribute("flvProperties");
	    if (!swfProperties) {
	    	swfProperties = "";
	    }
	    
	    var embed = document.createElement("embed");
	    embed.setAttribute("type","application/x-shockwave-flash");
	    embed.setAttribute("pluginspage","http://www.macromedia.com/go/getflashplayer");
	    embed.setAttribute("loop",(swfProperties.indexOf("loop") != -1));
	    embed.setAttribute("play",true);
	    embed.setAttribute("menu",true);
	    embed.setAttribute("wmode","transparent");
	    embed.id = swfid;
	    embed.src = swfurl;
	    embed.style.width = flash.style.width;
	    embed.style.height = flash.style.height;
	    
	    embed.style.margin = 0;
	    //<embed width="500" height="80" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" src="http://www.maianot.co.il/media/newspaper_banner.swf" play="true" loop="true" menu="true">
	   	flash.parentNode.replaceChild(embed,flash);
}
//===================================================================
//  
//  name: embed video in page 
//  
//===================================================================
function embedVideoElement(video) {
	    var flvProperties = video.getAttribute("flvProperties");
	    if (!flvProperties) {
	    	flvProperties = "";
	    }
		var flva = document.createElement("a");
	    //var audio = (video.src.indexOf("audio") != -1);
	    flva.href = "javascript:void(0);";
	    flva.id = video.id+"_";
	    flva.style.width = video.style.width;
	    flva.style.height = video.style.height.replace(/\s/,"");
	    flva.setAttribute("flv",video.getAttribute("flv"));
	    flva.setAttribute("flvProperties",flvProperties);
	    flva.setAttribute("audio",(video.src.indexOf("audio") != -1) ? 1 : 0);
	    /*
	    if (video.style.border) {
	    	flva.style.border = video.style.border;
	    }
	    */
	    //if (video.style.margin) {
	    flva.style.margin = "0px";//video.style.margin;
	   // }
	    //flva.setAttribute("style",(video.getAttribute("style")));
	    if ((flvProperties.indexOf("nobuttons") != -1) && (flvProperties.indexOf("autoplay") != -1) && (parseInt(flva.style.height) < 25)) {
	    	flva.style.display = "none"; 
	    }
	    else {
	    	flva.style.display = "block"; 
	    }
	    //alert(video)
	   	video.parentNode.replaceChild(flva,video);
	   	if (navigator.userAgent.indexOf("MSIE 7") == -1)  {
	   		startPlayer(flva.id);
	   	}
	   	else {		// bypass IE7 bug (Kibbutz Dalia)
	   		setTimeout('startPlayer("'+flva.id+'")',1000);
	   	}
}

//===================================================================
//  
//  startPlayer: start video player 
//  
//===================================================================
function startPlayer(id) {
	var flva = document.getElementById(id);
	    flowplayer(flva.id, "/htm-webaxy/flowplayer/flowplayer-3.0.0-rc4.swf", {
		    clip: { 
		        url: flva.getAttribute("flv"), 
		        autoPlay: (flva.getAttribute("flvProperties").indexOf("autoplay") != -1), 
		        //autoBuffering: !flva.getAttribute("audio"), 
		        autoBuffering: ((flva.getAttribute("audio")==0) && (flva.getAttribute("flvProperties").indexOf("noimage") == -1)),
		        bufferLength: 10,
		        
		        //	 when clip starts, do something 
		        
		        onLastSecond: function(clip) {   
		        	if (flva.getAttribute("flvProperties").indexOf("loop") != -1) {
			            this.play();
			        }
		        }  
		    },
		    play: { 
		    	label:"",
		    	replayLabel:"", 
		        fadeSpeed: 1000 
		    }, 
		     plugins: { controls: { 
		     	display: (flva.getAttribute("flvProperties").indexOf("nobuttons") != -1) ? 'none' : 'block',
		    	autoHide: 'never',
		    	play:true,      
		        volume:true, 
		        mute:true,  
		        time:true,  
		        stop:true, 
		        playlist:false,  
		        fullscreen:true, 
		         
		        // scrubber is a well known nickname to the timeline/playhead combination 
		        scrubber: true 
		      }  }     
		}
		
	);	// flowplayer(

}

