/*-------------------------------------------*/
/* Last update : 2009-01-22 Raynald Bertrand
/* - removed DE code
/*-------------------------------------------*/

// Function for DCOPT Variable

/* +-------------------------------------------------
   |  DCOPT handler
   +------------------------------------------------- */		
var _dcopt_data           = new Array();    // internal data
var _dcopt_html           = new Array();    // external html
var _dcopt_data_functions = new Array();    // internal functions


/*POUR BIGBOX & VIDEO*/
/***************************************************************************************************/
var CanoeAdCallVideo = 'http://ad.doubleclick.net/pfadx/can.'+MyGenericTagVar.language+'.'+MyGenericTagVar.division+'.'+MyGenericTagVar.mainsection;
        
 //this section check all section level to return a value if there's  one.
 if(MyGenericTagVar.sectionLevel2){
 CanoeAdCallVideo = CanoeAdCallVideo + '/bcplayer-'+MyGenericTagVar.sectionLevel2 + ';';
 }
 if(MyGenericTagVar.sectionLevel3){
 CanoeAdCallVideo = CanoeAdCallVideo + 'subz1=' + MyGenericTagVar.sectionLevel3 + ';';
 }
 if(MyGenericTagVar.sectionLevel4){
 CanoeAdCallVideo = CanoeAdCallVideo + 'subz2=' + MyGenericTagVar.sectionLevel4 + ';';
 }
 if(MyGenericTagVar.sectionLevel5){
CanoeAdCallVideo = CanoeAdCallVideo + 'subz3=' + MyGenericTagVar.sectionLevel5 + ';';
 }

 if(MyGenericTagVar.dfpSpekey != null){
     for (var itemKey in MyGenericTagVar.dfpSpeKey) {
        var CanoeAdCallVideo = CanoeAdCallVideo + itemKey + '=' + MyGenericTagVar.dfpSpeKey[itemKey] + ';';
     }
 }

 var CanoeAdCallBigbox = 'http://ad.doubleclick.net/adi/can.'+MyGenericTagVar.language+'.'+MyGenericTagVar.division+'.'+MyGenericTagVar.mainsection;
 
 //this section check all section level to return a value if there's  one.
 if(MyGenericTagVar.sectionLevel2){
 CanoeAdCallBigbox = CanoeAdCallBigbox + '/bcplayer-'+MyGenericTagVar.sectionLevel2 + ';';
 }
 if(MyGenericTagVar.sectionLevel3){
 CanoeAdCallBigbox = CanoeAdCallBigbox + 'subz1=' + MyGenericTagVar.sectionLevel3 + ';';
 }
 if(MyGenericTagVar.sectionLevel4){
 CanoeAdCallBigbox = CanoeAdCallBigbox + 'subz2=' + MyGenericTagVar.sectionLevel4 + ';';
 }
 if(MyGenericTagVar.sectionLevel5){
 CanoeAdCallBigbox = CanoeAdCallBigbox + 'subz3=' + MyGenericTagVar.sectionLevel5 + ';';
 }

 if(MyGenericTagVar.dfpSpekey != null){
     for (var itemKey in MyGenericTagVar.dfpSpeKey) {
        var CanoeAdCallBigbox = CanoeAdCallBigbox + itemKey + '=' + MyGenericTagVar.dfpSpeKey[itemKey] + ';';
     }
 }
/***************************************************************************************************/


/* +-------------------------------------------------
	|  fctDcopt()
	|    processes all declared dcopt
	+------------------------------------------------- */		
function fctDcopt() {
	var params = (arguments.length) ? arguments[0] : {};
	
	// for each dcopt
	//----------------------
	for (var type in _dcopt_html) {

		if ( typeof(_dcopt_html[type]) == "function" ) continue;
		
		var html = _dcopt_html[type];

		// div
		//----------------------
		var div = document.createElement('div');
		div.setAttribute('id', 'dcopt_'+type);
		document.body.appendChild(div);
		
		
		// specific function
		//----------------------
		if (typeof(_dcopt_data_functions[type]) == 'function') {
		    _dcopt_data_functions[type](params);
		}

		// append content
		//----------------------
		document.getElementById('dcopt_'+type).innerHTML = html;
	}

	return false;
}

function get_dcopt(mytile){
//	dcopt =  'ist';  
	if( mytile == 1 ) {
		dcopt =  'ist';  
	}
	else{
		dcopt =  ''; 
	}	    
	return dcopt;
}
// End function get_dcopt


function buildTag(tag_start,tag_end,tag_options,seperator) {
	var ad_tag = tag_start;
	for (var i in tag_options) {
		if(tag_options[i] != "" && ( typeof(tag_options[i]) == 'string' || typeof(tag_options[i]) == 'number')  ) {
			if (ad_tag != "") ad_tag += seperator;
			ad_tag = ad_tag + i + "=" + tag_options[i];
		}
	}
	ad_tag =  ad_tag + tag_end;
	return ad_tag;
}

function fctAdTag(position,objDfp,posId)
{				
	var size = GetDim(position); // Size Variable
	var my_dcopt =get_dcopt(objDfp.tile); // DCOPT Variable

	var my_keyword = get_keyword( 'q' );// motcle variable in LaToile
	my_keyword = my_keyword.replace(/%20/,"+");
	
	var tag_options = new Array();
			
	//-------------------------------------------------------------------------------
	// Special process to bypass dfp call when we switch for dfp for certain position
	// Take note that if dfpSwitch is on for a position we skip the call for dfp
	//-------------------------------------------------------------------------------
	if ( objDfp.dfpSwitch != null && objDfp.dfpSwitch[position] ) {
		return;
	} else {			

		var tag_start = '<script type="text\/javascript" src="http:\/\/ad.doubleclick.net\/adj\/'+objDfp.networkid+'.'+objDfp.language+'.'+objDfp.division+'.'+objDfp.mainsection+'\/'+objDfp.sectionLevel2;
		var tag_end = '?"><\/script>';
		var seperator = ";";
		
		tag_options["subz1"] = CheckProperties(objDfp,'sectionLevel3');
		tag_options["subz2"] = CheckProperties(objDfp,'sectionLevel4');
		tag_options["subz3"] = CheckProperties(objDfp,'sectionLevel5');
		tag_options["kw"] = CheckProperties(objDfp,'my_keyword');
		tag_options["tile"] = CheckProperties(objDfp,'tile');
		tag_options["sz"] = size;
		tag_options["dcopt"] = my_dcopt;
		tag_options["pos"] = posId;
		/* tag_options["hp"] = CheckProperties(objDfp,'hp'); */

		//---------------------------------//
		//-- optional: add special keys  --//
		//---------------------------------//
		if (objDfp.dfpSpeKey != null) {			
			for (var itemKey in objDfp.dfpSpeKey) {
				tag_options[itemKey] = objDfp.dfpSpeKey[itemKey];					
			}
		}
		
		if (CheckProperties(objDfp,'ForceHP') != "") { 
			tag_options["hp"] = CheckProperties(objDfp,'ForceHp');
		} else {
			if (( CheckProperties(objDfp,'sectionLevel2') != '' 
				&& CheckProperties(objDfp,'sectionLevel2') != 'accueil' 
				&& CheckProperties(objDfp,'sectionLevel2') != 'home'
				)
				
				|| 

				( CheckProperties(objDfp,'sectionLevel3') != '' 
				&& CheckProperties(objDfp,'sectionLevel3') != 'accueil'
				&& CheckProperties(objDfp,'sectionLevel3') != 'home' 
				)
				){
				tag_options["hp"] = 1;
			}			
		}
	
		tag_options["mk"] = CheckProperties(objDfp,'mk');
		tag_options["mdl"] = CheckProperties(objDfp,'mdl'); // note l is lower case L
		tag_options["prov"] = CheckProperties(objDfp,'prov');

		//ord must always be the last item in the array
		tag_options["ord"] = CheckProperties(objDfp,'rand_num');

		document.write(buildTag(tag_start,tag_end,tag_options,seperator));
	}
	
	objDfp.tile++;
}

var canoeCatfish = {};	

/* +--------------------------
   | properties
   +-------------------------- */
canoeCatfish._css      = '';
canoeCatfish._link     = '';
canoeCatfish._swf      = '';
canoeCatfish._height   = 0;
canoeCatfish._position = 0;
canoeCatfish._opened   = false;
canoeCatfish._interval = 0;





/* +----------------------------------------------------
   | private methods
   +---------------------------------------------------- */
/* +-------------------------
   | _generateHTML()
   +------------------------- */
canoeCatfish._generateHTML = function() {

	// add css
	var cssNode   = document.createElement('link');
	cssNode.type  = 'text/css';
	cssNode.rel   = 'stylesheet';
	cssNode.href  = canoeCatfish._css;
	cssNode.media = 'screen';
	document.getElementsByTagName("head")[0].appendChild(cssNode);


	// add html
	var content = '';

	if (canoeCatfish._swf) {
		content = ''+
			'swf: '+canoeCatfish._swf+
		'';
	} else {
		content = ''+
			'<div id="catfishInner">'+
			'	<div id="catfishTop"></div>'+
			'	<a href="'+ canoeCatfish._link +'" id="catfishLink" target="_blank"></a>'+
			'	<a href="#" onclick="return canoeCatfish.close();" id="catfishClose">close</a>'+
			'</div>'+
		'';
	}

	return '<div id="catfish">'+content+'</div>';
};

/* +-------------------------
   | _open()
   +------------------------- */
canoeCatfish._open = function() {
	canoeCatfish._position = -canoeCatfish._height;
	canoeCatfish._interval = setInterval('canoeCatfish._do_open()',10);
};

/* +-------------------------
   | _do_open()
   +------------------------- */
canoeCatfish._do_open = function() {
	canoeCatfish._position += 10;	
	
	if (canoeCatfish._position >= 0) {
		canoeCatfish._position = 0;
		canoeCatfish._opened = true;
		clearInterval(canoeCatfish._interval);
	}

	document.getElementById('catfish').style.bottom = canoeCatfish._position+'px';
};

/* +-------------------------
   | _do_close()
   +------------------------- */
canoeCatfish._do_close = function() {
	canoeCatfish._position -= 10;	
	
	if (canoeCatfish._position <= -canoeCatfish._height) {
		canoeCatfish._position = -canoeCatfish._height;
		canoeCatfish._opened = false;
		clearInterval(canoeCatfish._interval);
	}

	document.getElementById('catfish').style.bottom = canoeCatfish._position+'px';
};





/* +----------------------------------------------------
   | public methods
   +---------------------------------------------------- */
/* +-------------------------
   | isOpened()
   +------------------------- */
canoeCatfish.isOpened = function() {
	return canoeCatfish._opened;
};

/* +-------------------------
   | setCss(css)
   +------------------------- */
canoeCatfish.setCss = function(css) {
	canoeCatfish._css = css;
};

/* +-------------------------
   | setLink(link)
   +------------------------- */
canoeCatfish.setLink = function(link) {
	canoeCatfish._link = link;
};

/* +-------------------------
   | setHeight(height)
   +------------------------- */
canoeCatfish.setHeight = function(height) {
	canoeCatfish._height = height;
};

/* +-------------------------
   | setSwf(swf)
   +------------------------- */
canoeCatfish.setSwf = function(swf) {
	canoeCatfish._swf = swf;
};

/* +-------------------------
   | getHTML()
   +------------------------- */
canoeCatfish.getHTML = function() {
	return canoeCatfish._generateHTML();
};

/* +-------------------------
   | appendToDcopt()
   +------------------------- */
canoeCatfish.appendToDcopt = function() {
	_dcopt_html['catfish'] = canoeCatfish._generateHTML();
};





/* +-------------------------
   | open(timeout)
   +------------------------- */
canoeCatfish.open = function(timeout) {
	setTimeout('canoeCatfish._open()',(timeout) ? timeout : 0);
	return false;
};

/* +-------------------------
   | close()
   +------------------------- */
canoeCatfish.close = function() {
	canoeCatfish._position = 0;
	canoeCatfish._interval = setInterval('canoeCatfish._do_close()',10);
	return false;
};

/* +-------------------------
   | openOnLoad(timeout)
   +------------------------- */
canoeCatfish.openOnLoad = function(timeout) {
	timeout = (timeout) ? timeout : 0;
	
	if (window.addEventListener){
		eval("window.addEventListener('load', function() { canoeCatfish.open("+timeout+"); }, false);");
	} else if (window.attachEvent){
		eval("window.attachEvent('onload', function() { canoeCatfish.open("+timeout+"); });");
	}
	return false;
};

