//
//  This tool replaces content written in the correct format with embedded objects
//	It makes use of the following open source tools:
//	jquery - http://jquery.com/
//	swfObject - http://code.google.com/p/swfobject/
//	flowplayer - http://flowplayer.org/
//	XSPF Player - http://musicplayer.sourceforge.net/
//
$(document).ready(function() {
	//".each" sets up a loop  through each of the zems
	$("a.zem").each(function() {
		var zem = $(this);
		
		//user needs to fill in the href or we skip
		if(zem.attr("href") != undefined) {
		
			//first set up all the variables we will use to embed things
			var cUrl = zem.attr("href");
			var cUrl = $.trim(cUrl);
			var cExt = getExtension(cUrl);
			var cExt = $.trim(cExt);
			var cMethod = "embed";
			var cWidth = 446;
			var cHeight = 334;
			var cInnerValue = zem.html();
				//get a big random number
				var rand_no = Math.random();
				rand_no = rand_no * 100000000;
				rand_no = Math.ceil(rand_no);
			var cUniqueId = "z" + rand_no;
			var cBackgroundColor = "transparent";
			var cWmode = "transparent";
			var cSplashUrl = "";
			//get data from braces using metadata jquery plugin
			var data = zem.metadata();
			if ( data.method && data.method == "pop" ) {
				cMethod = "pop";
			}
			if ( data.splashurl ) {
				cMethod = "splash";
				cSplashUrl = data.splashurl;
			}
			if ( data.width ) {
				cWidth = data.width;
			}
			if ( data.height ) {
				cHeight = data.height;
			}
			if (data.backgroundColor) {
				cWmode = "opaque";
				cBackgroundColor = data.backgroundColor;
			}
			
			//variables are all set at this point, begin embedding stuff based on which variables are set
			
			//embed buttonMp3Player
			if (cExt == ".mp3") {
				zem.after("<span class=\"buttonMp3Player\"><span id=\"" + cUniqueId + "\"></span><span> " + cInnerValue + "</span></span>").remove();
				embedButtonMp3Player(cUniqueId, cUrl);
			}
			
			//embed video player
			if(cExt == ".flv") {
				if(cMethod == "embed") {
					//embed flowplayer
					zem.after("<span id=\"" + cUniqueId + "\" class=\"embeddedVideoPlayer\"></span>").remove();
					embedVideoPlayer(cUniqueId, cUrl, cWidth, cHeight, false);
				}
				if(cMethod == "splash") {
					//embed flowplayer where only an image is displayed first until click
					zem.after("<span id=\"" + cUniqueId + "\" class=\"embeddedVideoPlayer\"></span>").remove();
					embedVideoPlayer(cUniqueId, cUrl, cWidth, cHeight, false, cSplashUrl);
				}
				if(cMethod == "pop") {
					//embed link that causes a popup with flowplayer on it
					var cUniqueId2 = "z" + cUniqueId; //1 = content to be put into the popup  2 = video itself
					
					//create two elements one to hold the content that ThinBox is going to pull in, and another to apply the ThinBox click event to.
					var clickableLink = "<a href=\"#TB_inline?height=" + cHeight + "&width=" + cWidth + "&inlineId=" + cUniqueId + "\" class=\"thickbox\">" + cInnerValue + "</a>";
					var replaceableDivs ="<span id=\"" + cUniqueId + "\" style=\"display:none;\"><span id=\"" + cUniqueId2 + "\">replacing</span></span>";
					zem.after(clickableLink + replaceableDivs).remove();
					
					//setup flowplayer into a hidden div created by the previous "after" method, then TB_inline (thinbox) can pull it into its block
					embedVideoPlayer(cUniqueId2, cUrl, cWidth, cHeight, true);
				}
			}
			//embed swf file (flash file)
			if(cExt == ".swf") {
				if(cMethod == "embed") {
					
					//create an element with an id that we can use swfobject on
					zem.after("<span id=\"" + cUniqueId + "\"></span>").remove();
					
					var flashvars = {};
					var params = {"wmode": cWmode, "bgcolor": cBackgroundColor};
					var attributes = {};

					swfobject.embedSWF(cUrl, cUniqueId, cWidth, cHeight, "9.0.0", "/Zondervan/Include/swfobject2.1/expressInstall.swf", flashvars, params, attributes);
				}
				if(cMethod == "pop") {
					var cUniqueId2 = "z" + cUniqueId; //1 = content to be put into the popup  2 = swf replacement itself
					
					//create two elements one to hold the content that ThinBox is going to pull in, and another to apply the ThinBox click event to.
					zem.after("<a href=\"#TB_inline?height=" + cHeight + "&width=" + cWidth + "&inlineId=" + cUniqueId + "\" class=\"thickbox\">" + cInnerValue + "</a><span id=\"" + cUniqueId + "\" style=\"display:none\"><span id=\"" + cUniqueId2 + "\">replacing</span></span>").remove();
					
					var flashvars = {};
					var params = {"wmode": cWmode, "bgcolor": cBackgroundColor};
					var attributes = {};
					
					//setup the swf into a hidden span created by the previous "after" method, then TB_inline (thinbox) can pull it into its block
					swfobject.embedSWF(cUrl, cUniqueId2, cWidth, cHeight, "9.0.0", "/Zondervan/Include/swfobject2.1/expressInstall.swf", flashvars, params, attributes);
				}
			}
		}
	});	
});

//Helpers

function getExtension(url) 
{ 
 var dot = url.lastIndexOf("."); 
 if( dot == -1 ) return ""; 
 var extension = url.substr(dot,url.length); 
 return extension; 
} 

function embedVideoPlayer(idToReplace, vvideo, vwidth, vheight, vautoPlay, splashUrl) {
//this player is FlowPlayer - http://flowplayer.org/

	var cSplashUrl = "";
	var cVAutoPlay = vautoPlay;
	var cSplashUrlConfigTxt = "";
	if(splashUrl != null)
	{
		cSplashUrl = splashUrl;
		cVAutoPlay = false;
		cSplashUrlConfigTxt = "splashImageFile: '" + cSplashUrl + "',"; 
	}
	var flashvars = {
		 config: "{ " + cSplashUrlConfigTxt + " showVolumeSlider: false, loop: false, controlsOverVideo: 'ease', controlBarBackgroundColor: -1, controlBarGloss: 'low', showFullScreenButton: false, showMenu: false, showVolumeSlider: true, autoPlay: " + cVAutoPlay + ", initialScale: 'fit', videoFile: '" + vvideo + "' }" 
	};
	var params = {"wmode":"transparent"};
	var attributes = {};

	swfobject.embedSWF("/Zondervan/include/flowplayer2/FlowPlayerDark.swf", idToReplace, vwidth, vheight, "9.0.0", "/Zondervan/Include/swfobject2.1/expressInstall.swf", flashvars, params, attributes)
}


function embedButtonMp3Player(idToReplace, mp3Url) {
//this player is one of the versions of XSPF Web Music Player - http://musicplayer.sourceforge.net/
	var flashvars = {
		song_url: mp3Url,
		b_bgcolor: 'ffffff',
		b_fgcolor: '7a1600'
	};
	var params = {"wmode":"transparent"};
	var attributes = {};

	swfobject.embedSWF("/Zondervan/include/buttonMp3Player.swf", idToReplace, "17", "17", "9.0.0", "/Zondervan/Include/swfobject2.1/expressInstall.swf", flashvars, params, attributes)
}


