var browser = "NA";
var debuggerObj = "";
var maxleft = 300;
var BPselects = document.getElementsByTagName("select");
var BPiframe = document.getElementsByTagName("iframe");
var indexMicrosite = 0;
var microsite_off_swf;
var microsite_on_swf;
var livesquareHtml;

//DEBUG
function debugAff(val){
	if(debug == 0) return;
	var separateur = "\n*********************************************************************\n";
	if(debuggerObj == "" || debuggerObj == null){
		debuggerObj = window.open("#","DEBUG","width=610,height=440,scrollbars=no");
		debuggerObj.document.write("<textarea name=debug cols=70 rows=25></textarea>");
	}
	if(debuggerObj.document.getElementById('debug')) debuggerObj.document.getElementById('debug').value = separateur + val + separateur + debuggerObj.document.all.debug.value;
}

//TRACKING

function getSPTQS(){
	var result = "";
	for(i=0;i<SPTorder.length;i++){
		j = i + 2;
		result += "&" + SPTorder[i] + "=" + j;
	}
	return result;
}

function getSPT(tracerVar){
	if(track == 0) return "";
	var result = "";
	for(i=0;i<SPTorder.length;i++){
		if(tracerVar == SPTorder[i]){
			j = i + 2;
			result += j;
			break;
		}
	}
	return result;
}

function getUrlRedirectQS(){
	var result = "";
	for(i=0;i<url_redirect.length;i++){
		j = i + 1;
		if(url_redirect.length > 1){
			result += "&url" + j + "=" + escape(url_redirect[i]);
		}else{
			result += "&url=" + escape(url_redirect[i]);
		}
	}
	return result;
}

function sendStats(url){
 	if(track == 0) return;
 	if(url == "") return;
 	debugAff("SENDSTATS="+url);
 	if(document.all){
 		 document.all.SPT.src = url;
 	}else{
 		 document.getElementById ('SPT').src = url;
 	}
}

//LAYOUT
function displayFormat(){
	if(swfversion >= swfversionmin){
		//FLASH OK
		document.write("<div id='LIVESQUARE_PLACEMENT' style='position:relative;'><img id='LIVESQUARE0' src='"+url_download+"vide.gif' width='"+placement_width+"' height='"+placement_height+"' border='0'></div>");
		if(document.all){
			tid = setTimeout("fillFormat()",100);
		}else{
			tid = setTimeout("fillFormat()",500);
		}
	}else{
		//BACKUP
		document.write("<div id='LIVESQUARE'>" + getHtmlGif() + "<\/div>");
	}
}

function fillFormat(){
	var id = "LIVESQUARE_OFF";
	var startvisibility = "show";
	var startleft = -1000;
	var starttop = -1000;
	microsite_off_swf = (getWay() == "G") ? microsite_off_g_swf : microsite_off_d_swf;
	indexMicrosite = getRandom(microsite_off_swf.length);
	livesquareHtml = getHtmlFlash(microsite_off_swf[indexMicrosite],id,version,placement_width,placement_height,'');
	livesquareHtml += "<style type='text/css'>.LIVESQUARE {position:absolute ;visibility:"+startvisibility+"; left:"+startleft+"px; top:"+starttop+"px; z-index:10}<\/style>";
	livesquareHtml += "<div id='LIVESQUARE' class=LIVESQUARE><\/div>" + getSPTHtml();
	document.getElementById('LIVESQUARE_PLACEMENT').innerHTML = livesquareHtml;
}

function showFormat(url,rub){
	sendStats(url);
	var id = "LIVESQUARE_ON";
	microsite_on_swf = (getWay() == "G") ? microsite_on_g_swf : microsite_on_d_swf;
	var current_on_swf = microsite_on_swf[indexMicrosite];
	var currentway = getWay();
	debugAff("CURRENTWAY="+currentway);
	document.getElementById('LIVESQUARE').innerHTML = getHtmlFlash(current_on_swf,id,version,width,height,rub);
	updatePosition();
	FADhide_elements();
	tid = setInterval("updatePosition()",500);
}

function hideFormat(){
	document.getElementById('LIVESQUARE').innerHTML='';
	FADshow_elements();
}

function updatePosition(){
	document.getElementById('LIVESQUARE').style.zIndex = 10001;
	document.getElementById('LIVESQUARE').style.position = "absolute";
	document.getElementById('LIVESQUARE').style.left = getLeftPosition()+"px";
	document.getElementById('LIVESQUARE').style.top = getTopPosition()+"px";
	debugAff("getLeftPositionStyle()="+document.getElementById('LIVESQUARE').style.position+" getTopPositionStyle()="+document.getElementById('LIVESQUARE').style.top)
	debugAff("getLeftPosition()="+getLeftPosition()+" getTopPosition()="+getTopPosition())
}

function getWay(){
	if(way != 0) return way;
	if(left != 0){
		way = (left >  width - placement_width && left > maxleft) ? "G" : "D";
		return way;
	}

	var placementObj = document.getElementById('LIVESQUARE_PLACEMENT');
	var tmpleft = 0;
	while (placementObj != null){
		tmpleft += placementObj.offsetLeft;
		placementObj = placementObj.offsetParent;
	}
	way = (tmpleft >  width - placement_width  && tmpleft > maxleft) ? "G" : "D";
	return way;
}

function getLeftPosition(){
	if(left != 0){
		if(left >  width - placement_width  && left > maxleft){
			left -= width - placement_width;
			debugAff("force offset G " + left);
		}
		//return left;
	}
	var placementObj = document.getElementById('LIVESQUARE_OFF');
	var tmpleft = 0;
	while (placementObj != null){
		tmpleft += placementObj.offsetLeft;
		debugAff("offsetLeft de " + placementObj.tagName + " ID=" + placementObj.id + " = " + placementObj.offsetLeft + "/" + tmpleft);
		placementObj = placementObj.offsetParent;
		
	}
	left = tmpleft;
	if(left >  width - placement_width  && left > maxleft){
		debugAff("offset G = -" + eval(width - placement_width));
		left = - (width - placement_width);
	}else{
		left = 0;
	}
	debugAff("LEFT POSITION = " + left);
	return left;
}

function getTopPosition(){
	//if(top != 0) return top;
	var placementObj = document.getElementById('LIVESQUARE_PLACEMENT');
	var tmptop = 0;
	while (placementObj != null){
		tmptop += placementObj.offsetTop;
		placementObj = placementObj.offsetParent;
	}

	top = tmptop;
	top = 0;
	debugAff("TOP POSITION = " + top);
	return top;
}

//CORE
function getFlashPath(swffile,swfversion,version,url,rub){
	if(swfversion >= swfversionmin) path = url_download + swffile + "?url_download=" + url_download + "&url_tracer=" + escape(url_tracer) + getSPTQS() + "&debug=" + debug + getUrlRedirectQS();
	if(rub != "") path += "&rub="+rub;
	return path;
}

function getHtmlFlash(swffile,id,version,fwidth,fheight,rub){
	
	var flashswf = getFlashPath(swffile,swfversion,version,url_redirect,rub);
	flashswf += "&rnd="+1000*Math.random();
	var result = "";
	result += "<OBJECT classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'";
	result += " codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+swfversionmin+",0,0,0'";
	result += " WIDTH='" + fwidth +"' HEIGHT='" + fheight +"' id='"+id+"' ALIGN=''>";
	result += " <PARAM NAME=movie VALUE='" + flashswf +"'> <PARAM NAME=quality VALUE=high> <PARAM NAME=wmode VALUE=transparent> <PARAM NAME=bgcolor VALUE=#"+bgcolor+"> <EMBED src='" + flashswf +"' wmode='transparent' quality=high bgcolor=#"+bgcolor+"  WIDTH='" + fwidth +"' HEIGHT='" + fheight +"' NAME='"+id+"' ALIGN=''";
	result += " swLiveConnect='true' TYPE='application/x-shockwave-flash' PLUGINSPAGE='http://www.macromedia.com/go/getflashplayer'><\/EMBED>";
	result += "<\/OBJECT>";
	return result;
}

function getHtmlGif(){
	var result = "";
	result += "<a href='" + unescape(url_redirect_backup) + "' target='_blank'><img name='format' src='" + url_download + backup_src + "' border='0' width='"+placement_width+"' height='"+placement_height+"'></a>";
	result += "<img src='" + unescape(url_tracer) + getSPT('backup') + "' width=0 height=0>";
	return result;
}

function getSPTHtml(){
	if(track == 0) return "";
	var result = "";
	result += "<img id=SPT src='" + url_download + "vide.gif' width=0 height=0 border=0>";
	return result;
}

//PRIMITIVES
function getRandom(range){
	if(range == 1) return 0; 
	return Math.round(1000 * Math.random())%range;
}

function getFlashVersion() {
	var agent = navigator.userAgent.toLowerCase(); 
	
   // NS3 needs flashVersion to be a local variable
   if (agent.indexOf("mozilla/3") != -1 && agent.indexOf("msie") == -1) {
   	  browser = "NS";
      flashVersion = 0;
   }
   
	// NS3+, Opera3+, IE5+ Mac (support plugin array):  check for Flash plugin in plugin array
	if (navigator.plugins != null && navigator.plugins.length > 0) {
		browser = "NS";
		var flashPlugin = navigator.plugins['Shockwave Flash'];
		if (typeof flashPlugin == 'object') { 
			if (flashPlugin.description.indexOf('9.') != -1) flashVersion = 9;
			else if (flashPlugin.description.indexOf('8.') != -1) flashVersion = 8;
			else if (flashPlugin.description.indexOf('7.') != -1) flashVersion = 7;
			else if (flashPlugin.description.indexOf('6.') != -1) flashVersion = 6;
			else if (flashPlugin.description.indexOf('5.') != -1) flashVersion = 5;
			else if (flashPlugin.description.indexOf('4.') != -1) flashVersion = 4;
			else if (flashPlugin.description.indexOf('3.') != -1) flashVersion = 3;
		}else{
			flashVersion = -1;
		}
	}

	// IE4+ Win32:  attempt to create an ActiveX object using VBScript
	else if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) >= 4 && agent.indexOf("win")!=-1 && agent.indexOf("16bit")==-1) {
		browser = "IE";
		document.write('<scr' + 'ipt language="VBScript"\> \n');
		document.write('on error resume next \n');
		document.write('dim obFlash \n');
		document.write('set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.8") \n');
		document.write('if IsObject(obFlash) then \n');
		document.write('flashVersion = 8 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.7") end if \n');
		document.write('if flashVersion < 8 and IsObject(obFlash) then \n');
		document.write('flashVersion = 7 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.6") end if \n');
		document.write('if flashVersion < 7 and IsObject(obFlash) then \n');
		document.write('flashVersion = 6 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.5") end if \n');
		document.write('if flashVersion < 6 and IsObject(obFlash) then \n');
		document.write('flashVersion = 5 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.4") end if \n');
		document.write('if flashVersion < 5 and IsObject(obFlash) then \n');
		document.write('flashVersion = 4 \n');
		document.write('else set obFlash = CreateObject("ShockwaveFlash.ShockwaveFlash.3") end if \n');
		document.write('if flashVersion < 4 and IsObject(obFlash) then \n');
		document.write('flashVersion = 3 \n');
		document.write('end if');
		document.write('</scr' + 'ipt\> \n');
  }
		
	// WebTV 2.5 supports flash 3
	else if (agent.indexOf("webtv/2.5") != -1) flashVersion = 3;

	// older WebTV supports flash 2
	else if (agent.indexOf("webtv") != -1) flashVersion = 2;

	// Can't detect in all other cases
	else {
		flashVersion = -1;
	}
	return flashVersion;
}

function FADhide_elements() {
	for (BPfu=0;BPfu<BPselects.length;BPfu++) {
		BPselects[BPfu].style.visibility="hidden";
    }
    /*for (BPif=0;BPif<BPiframe.length;BPif++) {
       	BPiframe[BPif].style.visibility="hidden";
    }*/

}

function FADshow_elements() {
	for (BPfu=0;BPfu<BPselects.length;BPfu++) {
		BPselects[BPfu].style.visibility="visible";
	}
    	/*for (BPif=0;BPif<BPiframe.length;BPif++) {
    	BPiframe[BPif].style.visibility="visible";
  	}*/
}


swfversion = getFlashVersion();
//window.onload = displayFormat();
displayFormat();
//if(document.all) document.body.onload = showFormat;

