/** 
* Video Ad Embedder.
* These function require the varibles: 
* dc_ref (reference to the doubeclick XML) 
* gameName (reference to the Flash game to be played). 
* 
* Execution begins with writeDivAndPlayAd -- both the player div and the ad-receiving div are written
* then autoplayer.swf is embedded with the param tag "dc_ref". Flash retrieves the XML and in turn calls setDartCommercial.  
* There are three exit paths - player detects the video ends, timer runs out or error conditions. All Paths lead to 
* embedGameInDiv which swaps in the game for the video player.
* alerts disabled for production testing
*/

//holds a reference to the video player.
var player;
//makes a reference to the end tag accessible after timeout or video 'ended'.
var endTag="";
var timeoutID;
var playerReady;
var duration;

//begin by writing a div which will hold the player and then the game embed.
function writeDivAndPlayAd(dc_ref){
	//alert(dc_ref);
	
	//small div holds retreived startTag and endTag
	document.write('<div id="ad1"></div>');
	document.getElementById("ad1").style.visibility = "hidden";
	//holds the video player and then the game
	document.write('<div id="play1"></div>');
	if (navigator.userAgent.indexOf("Mac") != -1){
		//alert("it's a mac!");
		embedGameInDiv()
	} else {
		document.getElementById ( 'play1' ).innerHTML = getFlashEmbedHtml(dc_ref);
		player = window.document.flashVideo;
  	}
}

/**
 * autoplayer.swf is the Flash Video Player adn XML parser. This javascript function
 * writes the object tag which embeds the flash player. It does not load an flv file into
 * the object tag. Note use of dc_ref to pass in the location of the XML. 
 */
function getFlashEmbedHtml ()
{

	var flashHtml = "";
	flashHtml += '<object id="flashVideo" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"';
	flashHtml += 'codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0"';
	flashHtml += 'width="480" height="360" align="middle">';
	flashHtml += '<param name="allowScriptAccess" value="always" />';
	flashHtml += '<param name="movie" value="/jsource/autoplayer.swf" />';
	flashHtml += '<param name="quality" value="high" />';
	flashHtml += '<param name="bgcolor" value="#000000" />';
	flashHtml += '<param name="FlashVars" value="clickTag=http://www.adobe.com&debug=false&dc_ref=' + dc_ref + '">';
	flashHtml += '<embed src="/jsource/autoplayer.swf" quality="high" bgcolor="#000000" width="480" height="360" name="flashVideo" align="middle"';
	flashHtml += ' FlashVars="clickTag=http://www.adobe.com&debug=false&dc_ref=' + dc_ref + '" '; 
	flashHtml += 'align="middle" allowScriptAccess="always" type="application/x-shockwave-flash"';
	flashHtml += 'pluginspage="http://www.macromedia.com/go/getflashplayer" />';
	flashHtml += '</object>';
	return flashHtml;
}

//starts the video, requests startTag, sets Timer for div switch and endTag request 
function setDartCommercial(dc_startTag, dc_endTag, dc_type, dc_duration, dc_src, clickTag){
	if(!dc_src){
		//alert("dc_src is : " + dc_src);
		embedGameInDiv();
	} else {
		//alert("playerReady is " +playerReady);
		endTag = dc_endTag;
		
		//alert("player: " + player);
		player.loadAndPlay(dc_src, 0);
		
		//alert(dc_startTag);
		//alert("dc_duration is: " + dc_duration);
		dc_duration=parseInt(dc_duration);
		dc_duration += 2;
		duration=dc_duration*1000;
		//write startTag
		timeoutID = setTimeout("embedGameInDiv(gameName)", duration);
		//timeoutID = setTimeout("timerFired()", duration);
		
		document.getElementById ( 'ad1' ).innerHTML = '<img src="' + dc_startTag + '">';	
	}
}

function handlePlayStateChangeEvent(state)
{
	if (state == "ready")
	{
		playerReady=true;
		//need to ensure current state is ready before calling ad placement. 	
	}
	if (state == "ended")
	{
		embedGameInDiv();
	}
	if (state == "buffering")
	{
		//alert("buffering");
	}
}
function playerTrouble(desc){
	//alert(desc);
	embedGameInDiv();
	//alert("playerTrouble called embedGameInDiv which returned to playerTrouble");
}
//gameName is set at embedding
function embedGameInDiv(){	
	//alert("embedGameInDiv");
	//clear the timeout just in case
	if(timeoutID) {clearTimeout(timeoutID);}
	//alert(timeoutID);
	
	document.getElementById ( 'ad1' ).innerHTML = '<img src="' + endTag + '">';	
	flashContents=writeFlash(gameName, gameWidth, gameHeight);
	//alert(flashContents);
	document.getElementById ('play1').innerHTML = flashContents;
}

function divContents(){
	alert(document.getElementById('play1').innerHTML);
}

//callable by the Flash if it has something to say.
function triggerAlert(alertText){
	//alert(alertText);
}

//standard embed does the document.write, I just need the assembled string.
function writeFlash(path, width, height)
{
	writeVar='<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,22,0" WIDTH="' + width;
	writeVar+='" HEIGHT="' + height + '" id="FlashContent">';
	writeVar+='<PARAM NAME="movie" VALUE="' + path + '"><PARAM NAME="quality" VALUE="high">';
	writeVar+='<PARAM NAME="AllowScriptAccess" VALUE="never"><EMBED src="' + path + '"';
	writeVar+=' quality="high" WIDTH="' + width + '" HEIGHT="' + height + '" NAME="FlashContent"';
	writeVar+=' AllowScriptAccess="never" TYPE="application/x-shockwave-flash" PLUGINSPAGE="http://www.macromedia.com/go/getflashplayer">';
	writeVar+='</EMBED></OBJECT>';
	return writeVar;
}
