var youtube_url = "http://www.youtube.com";
//===================================================================
//  
//  postEditors: do all the action! 
//  
//===================================================================
function postEditors() {
	
	// video, audio, flash, youtube
	var flvs = document.getElementsByName("flvImage");
	if (flvs) {
		insertFileElement("/htm-webaxy/flowplayer/flowplayer-3.0.0-rc5.min.js","js");
		
		var flvIDs = new Array();
		for (var flv = 0; flv < flvs.length;flv++) {
			setImageWH(flvs[flv].id);
			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++) {
	  		video=document.getElementById(flvIDs[flv]);
		    var flvflv = video.getAttribute("flv");
		    
		    if ((flvflv.search(/^\s*</) != -1) || (flvflv.indexOf(youtube_url) == 0)) { // youtube
		    	embedExternalElement(video);	
		    }
		    else if (flvflv.search(/\.swf$/i) != -1) {	// flash banner
		    	embedFlashElement(video);
		    	continue;
		    }
		    else {
		    	embedVideoElement(video);
		    	continue;
		    }
		    
			
	  }	// for (var flv=0
	}	// if (flvs)
	
	// contacts
	var contactViews = document.getElementsByName("contactView");
	if (contactViews.length) {
		insertFileElement("/htm-webaxy/skins/standard/standard_new.css","css");
		if (typeof(overlib) != 'function') {
			insertFileElement("/htm-webaxy/javascript/overlibmws/overlibmws.js","js");
		}
		try {
			$(document)
		} catch (e) { insertFileElement("/htm-webaxy/javascript/jquery-1.3.2.min.js","js");}
		insertFileElement("/htm-webaxy/tel/mig-tel.js","js");
		
		for (var contact=0; contact<contactViews.length; contact++) {
			var telAppearence = contactViews[contact].getAttribute("telAppearence");
			var contactID = contactViews[contact].getAttribute("telSearchWords");
			insertFileElement("/Contact/tel"+telAppearence+".css","css");
			
			var getContactCode = "tel"+contactID+" = ''; ";
			getContactCode += 'get_ajax_popup("", "","'+webaxyHiddenProperties+'&telID='+telAppearence+'&recordID='+contactID+'&only_data=1", "", "tel'+contactID+'","popupRecord");';
			waitAndRun(escape("typeof get_ajax_popup == 'function' "),escape(getContactCode),200);
			
			contactViews[contact].onmouseout = new Function("nd(15000);");
			contactTitle = contactViews[contact].innerHTML;
			//contactTitle = '';
			var closeimg = '<img src="/htm-webaxy//icons/close.png" border="0">';
			//contactViews[contact].onmouseover = new Function("overlib(tel"+contactID+", REF, 'telLink"+contactID+"', CAPTION, '<center>"+contactTitle+"</center>', STICKY, CLOSETEXT, '"+closeimg+"', CLOSECLICK);FixDoar();overlibPost();");
			contactViews[contact].onmouseover = new Function("overlib(tel"+contactID+", CAPTION, '<center>"+contactTitle+"</center>', STICKY, CLOSETEXT, '"+closeimg+"', CLOSECLICK);FixDoar();overlibPost();");
			contactViews[contact].id = 'telLink-'+contactID;
						
		}
	}
}

//===================================================================
function overlibPost() {
	
	try {
		document.getElementById('overDiv').firstChild.cellPadding = 0;
		document.getElementById('overDiv').firstChild.bgColor = '';
		//document.getElementById('overCap').bgColor = '';
		document.getElementById('overCap').className += ' tel-category_title';
		document.getElementById('overCap').parentNode.firstChild.nextSibling.cellPadding = 0;
		document.getElementById('overCap').rows[0].cells[0].firstChild.style.color = '';
		document.getElementById('overCap').rows[0].cells[0].firstChild.style.fontFamily = '';
		document.getElementById('overCap').rows[0].cells[0].firstChild.style.fontSize = '';
		document.getElementById('overCap').rows[0].cells[0].firstChild.style.fontWeight = '';
	} catch (e){}
		
}
//===================================================================
//  
//  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 (code.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><param name="wmode" value="transparent"></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>';
    }
    else {
    	if (code.search(/width=\D*(\d+)/i) != -1) {
    		flvdiv.style.width = RegExp.$1+"px";
    	}
    	if (code.search(/height=\D*(\d+)/i) != -1) {
    		flvdiv.style.height = RegExp.$1+"px";
    	}
	}
   
    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);
	    flva.setAttribute("wmode","transparent");
	    /*
	    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")));
	   flva.style.display = "block"; 
	   if ((flvProperties.indexOf("nobuttons") != -1) && (flvProperties.indexOf("autoplay") != -1) && (parseInt(flva.style.height) < 25)) {	// hide player
		 	flva.style.width    = "0px";
		 	flva.style.height   = "0px";
	   		flva.style.position = "absolute"; 
	   		flva.style.top        = "0px"; 
	    }
	    //alert(video)
	   	video.parentNode.replaceChild(flva,video);
	   	waitAndRun(escape("typeof flowplayer == 'function' "),escape("startPlayer('"+flva.id+"')"),200);
}

//===================================================================
//  
//  startPlayer: start video player 
//  
//===================================================================
function startPlayer(id) {
	var flva = document.getElementById(id);
	var flvProperties = flva.getAttribute("flvProperties");
	
	    flowplayer(flva.id, "/htm-webaxy/flowplayer/flowplayer-3.0.0-rc4.swf", {
		    clip: { 
		        url: flva.getAttribute("flv"), 
		        autoPlay: (flvProperties.indexOf("autoplay") != -1), 
		        //autoBuffering: !flva.getAttribute("audio"), 
		        autoBuffering: ((flva.getAttribute("audio")==0) && (flvProperties.indexOf("noimage") == -1)),
		        bufferLength: 10,
		        
		        //	 when clip starts, do something 
		        
		        onLastSecond: function(clip) {   
		        	if (flvProperties.indexOf("loop") != -1) {
			            this.play();
			        }
		        }  
		    },
		    play: { 
		    	label:"",
		    	replayLabel:"", 
		        fadeSpeed: 1000 
		    }, 
		     plugins: { controls: { 
		     	display: (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(
	
	 
	    

}
//===================================================================
//  
//  insertFileElement: insert css or js file ref into page header 
//  
//===================================================================
function insertFileElement(path,ext) {
	try {
		var headerTag = document.getElementsByTagName("head")[0];
	}
	catch(no_header) {
		return;
	}
	var fileObject = null;
	if (ext == "css") {
		fileObject = document.createElement("link");
		fileObject.setAttribute("comment","postEditors");
		fileObject.setAttribute("rel", "stylesheet");
		fileObject.setAttribute("type", "text/css");
		fileObject.setAttribute("href",path);
	}
	else if (ext == "js") {
		fileObject = document.createElement("script");
		fileObject.setAttribute("comment","postEditors");
		fileObject.setAttribute("type","text/javascript");
		fileObject.setAttribute("src",path);
	}
	
	if (fileObject) {
		
		var place = null;
		
		for (var h=0; h < headerTag.childNodes.length; h++) {
			if (!headerTag.childNodes[h].getAttribute) {
				continue;
			}
			if (headerTag.childNodes[h].getAttribute("comment") && (headerTag.childNodes[h].getAttribute("comment") == fileObject.getAttribute("comment"))) {
				continue;
			}
			
			if ((headerTag.childNodes[h].tagName == "STYLE") || (headerTag.childNodes[h].tagName == "LINK")) {
				place = headerTag.childNodes[h];
				break;
			}
		}
		
		if (place) {
			headerTag.insertBefore(fileObject,place);
		}
		else {
			headerTag.appendChild(fileObject);
		}
		
		//headerTag.appendChild(fileObject);
	}

}
//===================================================================
//  
//  waitAndRun: wait and run when condition is true 
//	USAGE: waitAndRun(escape("get_ajax_popup"),escape(getContactCode),200);
//  
//===================================================================
function waitAndRun(runIf,runCode,milisec) {
	
	try {
		if (eval(unescape(runIf))) {
			eval(unescape(runCode));
			return;
		}
	}
	catch(notyet) {}
	self.setTimeout("waitAndRun('"+runIf+"','"+runCode+"','"+milisec+"')",milisec);
}
//===================================================================
//  
//  setImageWH: set img width and height in style
//  
//===================================================================

function setImageWH(id) {
	var img = document.getElementById(id);
	if (!img) {
		return;
	}
	
	if (!img.style.width) {
		img.style.width = img.clientWidth + "px";
	}
	
	if (!img.style.height) {
		img.style.height = img.clientHeight + "px";
	}
}


//==========================
/*
var pageOldOnLoad = window.onload;

if (typeof pageOldOnLoad != 'function') {
	window.onload = new Function("postEditors();");
}
else {
	if (pageOldOnLoad.toString().indexOf("postEditors()") == -1) {
		window.onload = new Function("if (pageOldOnLoad) { pageOldOnLoad(); } postEditors(); ");
   }
}
*/
try {
	if (postEditorsTimeout) {
		clearTimeout(postEditorsTimeout);
	}
}
catch(peError) {}
postEditorsTimeout = setTimeout(" postEditors(); ",500);
