var f_cb_medrect1_served = false;
var f_cb_leaderboard1_served = false;
var f_need_cb = false;
var f_cb_medrect1_real = false;
var f_cb_leaderboard1_real = false;

function yume_backfill_banner(banner_div_id, imu_type) {
	var rc=0;
	var cb_width = document.getElementById(banner_div_id).style.width;
	var cb_height =document.getElementById(banner_div_id).style.height;
	var y_c_frame = document.getElementById(banner_div_id);
	var cb_w = cb_width.split("px", 1);
	var cb_h = cb_height.split("px", 1);
	var arg1 ="http://pl.yumenetworks.com/dynamic_banner_iframe.html?domain=132dfBfnwuc&width="+cb_w+"&height="+cb_h+"&imu="+imu_type;
	//var arg1 ="http://shadow01.yumenetworks.com/dynamic_banner_iframe.html?domain=88ElOFHxAh&width="+cb_w+"&height="+cb_h+"&imu="+imu_type;
	try {
		y_c_frame.innerHTML = "<iframe src='"+arg1+"'height='"+cb_height+"'width='"+cb_width+"' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' style='overflow:hidden'></iframe>";
		//y_c_frame.contentWindow.document.getElementsByTagName("body")[0].innerHTML = "<iframe src='"+arg1+"'height='"+cb_height+"'width='"+cb_width+"' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' style='overflow:hidden'></iframe>";
		rc = 1;
	} catch(e) {}
	return rc;
}
function yume_serve_cb_frame(banner_div_id, frame_src_url) {
	var rc=0;
	var cb_width = document.getElementById(banner_div_id).style.width;
	var cb_height =document.getElementById(banner_div_id).style.height;
	var y_c_frame = document.getElementById(banner_div_id);
	try {
		//y_c_frame.contentWindow.document.getElementsByTagName("body")[0].innerHTML = "<iframe src='"+frame_src_url+"'height='"+cb_height+"'width='"+cb_width+"' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' style='overflow:hidden'></iframe>";
		y_c_frame.innerHTML = "<iframe src='"+frame_src_url+"'height='"+cb_height+"'width='"+cb_width+"' frameborder='0' scrolling='no' marginheight='0' marginwidth='0' topmargin='0' leftmargin='0' style='overflow:hidden'></iframe>";
		rc = 1;
	} catch(e) {}
	return rc;
}
function yume_serve_cb_image(banner_div_id, frame_img, frame_link) {
	var rc=0;
	var cb_width = document.getElementById(banner_div_id).style.width;
	var cb_height =document.getElementById(banner_div_id).style.height;
	var y_c_frame = document.getElementById(banner_div_id);
	try {
		y_c_frame.contentWindow.document.getElementsByTagName("body")[0].innerHTML = "<a href='"+frame_link+"' target='_blank'><img src='"+frame_img+"' border='0' width='"+cb_width+"' height='"+cb_height+"'></a>";
		rc = 1;
	} catch(e) {}
	return rc;
}
// return 1 if img_url is a SWF, 0 otherwise
function yume_img_is_swf(img_url) {
	var ix_getElement = img_url.indexOf("/getElement?", 0);
	var ix_extSWF = img_url.indexOf("ext=.swf", 0);
	var rc=0;

	if (ix_getElement > 0 && ix_extSWF > ix_getElement) {
		rc=1;
	}
	return rc;
}
function yume_flash_callback(command, arg1, arg2, arg3){
	//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2 + "," + arg3);
	if (command == 'companionbanner/iframe')
	{
		if (arg3=="roll_1") {
			if (yume_serve_cb_frame("yume_roll1_div", arg1) > 0) {
				showRollDiv();
			}
		}
	}
	if (command == 'companionbanner/iframe'
			|| (command == 'companionbanner/image' && yume_img_is_swf(arg1) > 0) // serve SWFs as an Iframe
			)
	{
		//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2 + "," + arg3);
		if (arg3=="cb1" || arg3=="cb_medrect1") {
			$('#adDiv').css({'width' : '300px', 'height' : '250px'})
			if (yume_serve_cb_frame("adDiv",  arg1) > 0) {
				f_cb_medrect1_served = true;
				f_cb_medrect1_real = true;
			}
		} else
		if (arg3=="cb_leaderboard1") {
			if (yume_serve_cb_frame("cb_leaderboard1_div", arg1) > 0) {
				f_cb_leaderboard1_served = true;
				f_cb_leaderboard1_real = true;
			}
		}
	} else if (command == 'companionbanner/image') {
		//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2 + "," + arg3);
		if (arg3=="cb1" || arg3=="cb_medrect1") {
			$('#adDiv').css({'width' : '300px', 'height' : '250px'})
			if (yume_serve_cb_image("adDiv", arg1, arg2) > 0) {
				f_cb_medrect1_served = true;
				f_cb_medrect1_real = true;
			}
		} else
		if (arg3=="cb_leaderboard1") {
			if (yume_serve_cb_image("cb_leaderboard1_div", arg1, arg2) > 0) {
				f_cb_leaderboard1_served = true;
				f_cb_leaderboard1_real = true;
			}
		}
	} else
	if(command == "yume_preroll_start" || command == "yume_postroll_start" || command == "yume_midroll_start") {
		f_need_cb = true;

		// ADDED
		hideRollDiv();
		// END OF ADDED

		//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2);
	} else
	if(command == "yume_preroll_end" || command == "yume_postroll_end" || command == "yume_midroll_end") {
		f_need_cb = false;

		//ADDED
		hideRollDiv();
		//END OF ADDED

		//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2);
		if(command == "yume_postroll_end"){
			f_cb_medrect1_real = false;
			f_cb_leaderboard1_real = false;
		}
	} else
	if(command == "yume_ad_end") {
		f_cb_medrect1_served = false;
		f_cb_leaderboard1_served = false;
		//alert("yume_flash_callback:" + command +"," + arg1 + "," + arg2);
	} else
	if( (command == "yume_ad_start" && f_need_cb == true)
			||(command == "yume_end" && arg1 == "0" && f_need_cb == true) ) {
		//alert("check for backfill");
		if ((f_cb_medrect1_served == false)&&(f_cb_medrect1_real == false)) {
			$('#adDiv').css({'width' : '300px', 'height' : '250px'})
			if (yume_backfill_banner("adDiv", "medrect") > 0) {
				f_cb_medrect1_served = true;
			}
		}
		if ((f_cb_leaderboard1_served == false)&&(f_cb_leaderboard1_real == false)) {
			if (yume_backfill_banner("cb_leaderboard1_div", "leaderboard") > 0) {
				f_cb_leaderboard1_served = true;
			}
		}
	}
}
function hideRollDiv(){
	var theDiv = document.getElementById("yume_roll1_div");
	theDiv.style.visibility="hidden";
	var theDiv1 = document.getElementById("yume_roll1_frame");
	theDiv1.src="http://www4.filmannex.com/empty.html";
	return true;
}
function showRollDiv(div_w, div_h){
	var theDiv = document.getElementById("yume_roll1_div");
	theDiv.style.visibility="visible";
}
function playContent(width, height){
	document.getElementById('playerDiv').innerHTML =
	'  <div id="flvPlayerDiv" style="position: absolute; width: '+width+'px; height: '+height+'px; float:left; z-index: 1; background-color:gray;" >' +
	'   Please install Adobe Flash player'+
	'  </div> '+
	'  <div  id="yume_roll1_div" style="position: absolute; width: 300px; height: 250px; margin-left: '+((width-300)/2)+'px; margin-top: '+((height-250)/2-10)+'px; float:left; z-index: 2; overflow:hidden; visibility:hidden; ">' +
	'    <iframe name="yume_roll1_frame" id="yume_roll1_frame" src="http://www4.filmannex.com/empty.html" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" topmargin="0" leftmargin="0" allowtransparency="true" width="300" height="250">    </iframe> ' +
	'  </div> ';
}
function fa_prep_ad_div() {
		var fa_cmp_top = "-94px";
		var fa_cmp_left = "-35px";
		var fa_prep = 1;
		if (!fa_prep) return;
		$('#ad_div_container').html ("<div id=\"adDiv\"><\/div>");
		$('#ad_div_container').css({'position' : 'absolute', 'top' : fa_cmp_top, 'left' : fa_cmp_left, 'z-index' : '100'});
		if ($('#ad_div_container #fa_app_close_button').length == 0) {
			$('#ad_div_container').append("<div id=\"fa_app_close_button\" style=\"position:absolute;top:250px;z-index:110; padding:0px; margin: 0px;\"><a href=\"#\" onclick=\"$('#wrap_ad_div').css({'width':'320px','height':'250px'}); $('#ad_div_container').css({'top':'0px','left':'0px'});\">Close<\/a><\/div>");
			}
};

var adaptvCompanionAdTag = '<div style="width:320px;height:250px;background-color:black;" ><\/div>';