/***** 
Q: How to embed a single vodcast into the page?
-----------------------------------------------
1. include this SSI into the page
<!--#include virtual="/global/common/inc/global_media.html" -->

2. add this code to the body (remember to change the FLV url)
  <script><!--
  var mediaType="vodcast";
  embedMediaFlash("/xxx.flv", "true", "targetDivId");
  //-->
  </script>

Q: Possible parameters for controlling vodcast/podcast?
-------------------------------------------------------
one or more of the following (note, do NOT incl file extension):
1. playlistPath=xxx
2. startIndex=2 (buffers 2nd playlist item)
3. flvPath=xxx
4. mp3Path=xxx

*****/

var xmlUrl, xmlDoc;
var mediaType;
var startIndex;
var errorMsg = false;
var domainName = window.location.host;
//var redirectDomain = (domainName.indexOf("omy")==-1) ? "http://www.omydev.sg" : ""; //if called from other domain (eg. mypaper.sg) then use SWF files from omy server - else this is blank
var redirectDomain = "";

//uses SWFObject to embed flash vodcast/podcast/gallery into the element with id=trgId, or "mediaHolder" as default trgId
function embedMediaFlash(media_src, media_autoplay, trgId, image_src) {
	//vodcast/podcast alr does this checking in generateHTML() this is primarily for gallery which does not run generateHTML()
	if (errorMsg) {
		displayError(errorMsg);
		return; //dont continue to load XML or flash
	}
	
	if (mediaType=="vodcast" || mediaType=="podcast") {
		var width = "320";
		var height = "306";
	}
	else if (mediaType=="gallery") {
		var width = "615";
		var height = "430";
	}
	else if (mediaType=="vodcast_180x190") {
		var width = "180";
		var height = "190";
	}
	else if (mediaType=="vodcast_240x215") {
		var width = "240";
		var height = "215";
	}
	
	//populate defaults
	var media_autoplay = (typeof(media_autoplay)=="undefined") ?  true : media_autoplay; //default is to autoplay
	var divId = (trgId) ? trgId : "mediaHolder"; //default is to embed into <div id="mediaHolder">
	var image_src = (image_src) ?  image_src : ''; //no preview img if not specified
	var swfName = divId+"_SWFObj";
	
	//tmp: force a new SWF to download
	var my_Date = new Date();
	var now = my_Date.getTime();
	
	var so = new SWFObject(redirectDomain+"/global/common/media/swf/"+mediaType+".swf?_="+now, swfName, width, height, "8", ""); //redirectDomain is blank if called frm omy server, else is omy server
	so.addParam("wmode", "transparent");
	if (mediaType=="gallery") so.addVariable("xmlresponse", unescape(media_src)); //pull a global var
	else {
		if (media_src.substr(0,7)!="http://") media_src = "http://"+domainName+media_src;
		so.addVariable("firstMediaSrc", media_src);
		so.addVariable("autoPlayMedia", media_autoplay);
		if (image_src && image_src!='') so.addVariable("firstImageSrc", image_src);
	}
	so.useExpressInstall(redirectDomain+"/global/common/swf/expressinstall.swf");
	
	so.write(divId);
}

//the function that calls the AS function "sendFlvToAS()"
function loadFlv(src, title, trgId) {
	var swfName = (trgId) ? trgId+"_SWFObj" : "mediaHolder_SWFObj";
	document.getElementById(swfName).sendFlvToAS(src);
	
	var captionName = (trgId) ? trgId+"_title" : "mediaItemTitle";
	if (document.getElementById(captionName)) document.getElementById(captionName).innerHTML = unescape(title);
	
	//urchinTracker(); //call google analytics
	if (typeof(pageTracker)!='undefined') pageTracker._trackPageview();
}

function getTitleLink(media_title) {
	var maxChineseChars = 5;
	var chineseCharsLength = 6; //eg. "%u5730" - must handle 6 chars at once in case there are chinese chars
	var maxChars = maxChineseChars*chineseCharsLength;
	
	var str = unescape(media_title);
	if (str.length>maxChars) return unescape(str.substr(0,maxChars))+'...';
	else return str;
}

function displayError(msg) {
	document.getElementById("mediaHolder").innerHTML = "<div class='errorMsg'><h4>"+msg+"</h4></div>";
	document.title += " - "+msg; //change title as well to track hits in google analytics
}

/***** MISC FUNCTIONS *****/
//used only by gallery, to custom-escape an XML string
//used because an unescaped string is passed to Flash (since AS cannot unescape a JS escape())
function customEscape(str) {
	str = str.replace(/"/g, "'" ); //cannot have double quotes else will break the text when sending to flash
	str = str.replace(/%u201C/g, "{quot}"); //cannot have open qoutes, replace with single quote
	str = str.replace(/%u201D/g, "{quot}"); //cannot have close quotes
	str = str.replace(/%22/g, "{quot}"); //cannot have double quotes
	str = str.replace(/%27/g, "{quot}"); //cannot have single quotes	
	str = str.replace(/%26/g, "{amp}"); //cannot have & characters
	str = str.replace(/&amp;/g, "{amp}"); //cannot have & characters	
	str = str.replace(/&lt;/g, "{lt}"); //cannot have < characters
	str = str.replace(/&gt;/g, "{gt}"); //cannot have > characters
	return str;
}

// ***** embed an mp3 player (identical to t2s cept doesnt redirect path) ***** //
function embedMp3Flash(media_src, trgId, saveAsFilename, media_autoplay) {
	var so = new SWFObject(redirectDomain+"/global/common/media/swf/t2s.swf", "mp3Obj"+trgId, "227", "43", "8", "");
	so.addParam("wmode", "transparent");
	so.addVariable("mp3player", true);
	so.addVariable("t2sFilename", media_src);
	so.addVariable("saveAsFilename", saveAsFilename);
	so.addVariable("autoPlayMedia", media_autoplay);
	so.useExpressInstall('/global/common/swf/expressinstall.swf');
	so.write(trgId);
}
