//
// @desc	Manage the adcorner 
// 		Flash or img for the folded/unfolded state of the adcorner
// 		Manage a cookie to limit the display of the adcorner on loading page
// @need	gestion_cookie.js
//
// @author	khai TA
// @date	2007
//

//***********************//
// CLASS OBJECT //
//***********************//
function GestionAdcorner()
{
	// [begin] attributes ***very important***
	this._adCorner_InstanceName =  '';
	this._adCorner_Id =  '';	// id dans la base
	this._adCorner_StateFolded_Name = '';				// folded <=> img
	this._adCorner_StateFolded_Width = 0;
	this._adCorner_StateFolded_Height = 0;
	this._adCorner_StateUnfolded_Name = '';				// unfolded <=> swf
	this._adCorner_StateUnfolded_Width = 0;
	this._adCorner_StateUnfolded_Height = 0;
	// format : 'clicktag=....&adcorner_id=...&lien=...&target=....&AdcornerInstanceName=' + monAdcorner._adCorner_InstanceName
	this._adCorner_FlashParams = '' + this._adCorner_InstanceName;	// lien flash
	this._adCorner_DivId =  '';							// div id
	/* cookie conf */
	this._adCorner_CookieName =  '';					// cookie name
	this._adcorner_CookieNameExpireDate =  '';				// cookie's name of expiration date
										// this value permit to update the '_adCorner_CookieName' with always the original expiration date
	this._adCorner_CookieExpiresAfter = 0;			// cookie time to live
											// ex: 1 year = 365*24*60*60*1000
	this._adCorner_CookieDateExpiresAfter = this._adCorner_CookieExpiresAfter;	// cookie time to live
									// this value must be the same as '_adCorner_CookieExpiresAfter'
	this._adCorner_TimeToDisplay =  0;					// Time to display the unfolded state (msec)
	this._adCorner_NumberOfDisplay =  0;					// Number of display of adcorner unfolded stat
	// [end] attributes ***very important***

	
	// methods
	this.changeToSwf = changeToSwf; 
	this.changeToImg = changeToImg;
	this.kcornerclose = kcornerclose;
	this.timer = timer;
	this.startAdcorner = startAdcorner;
	this.createAdcornerDivTag = createAdcornerDivTag;

//debug
//alert(this + 'GestionAdcorner');	
}

//***********************//
// MAIN METHOD//
//***********************//
// @desc : Manage the cookie and the display of adcorner
function startAdcorner( )
{
//debug	
//alert( this + ' gestionCookie');

	// *** object GestionCookie ***
	var cookie_adcorner = new GestionCookie();

	// get cookie value and expiration date
	var valeur_cookie = ( cookie_adcorner.getCookie(this._adCorner_CookieName) ) ;	// une variable pour la valeur
	var valeur_cookie_dateExpire = cookie_adcorner.getCookie(this._adcorner_CookieNameExpireDate) ;// une variable pour la date d'expiration
	//
	var new_valeur_cookie_expire = cookie_adcorner.dateExpire( this._adCorner_CookieExpiresAfter);	// date expiration initiale du cookie
	var new_valeur_cookie_dateExpire = cookie_adcorner.dateExpire( this._adCorner_CookieDateExpiresAfter );// date expiration de la date initiale
	//
	var date_du_jour = cookie_adcorner.dateExpire( 0 );

	// construct the div for adcorner if it doesn't exist
	this.createAdcornerDivTag();
	
	// init
	if( isNaN(valeur_cookie) || valeur_cookie==null )
	{
		// init the cookie
		// init the value
		valeur_cookie = 0;
		//alert('new : '+this._adCorner_CookieName+', '+parseInt(0)+', '+new_valeur_cookie_expire );
		cookie_adcorner.setCookie ( this._adCorner_CookieName, parseInt(valeur_cookie), new_valeur_cookie_expire, '', '', '');
		
		// init the cookie date expire
		//alert('new 2 : '+this._adcorner_CookieNameExpireDate+', '+new_valeur_cookie_expire+', '+new_valeur_cookie_dateExpire );
		cookie_adcorner.setCookie ( this._adcorner_CookieNameExpireDate, new_valeur_cookie_expire, new_valeur_cookie_dateExpire, '', '', '');
		valeur_cookie_dateExpire = new_valeur_cookie_expire;

	}// endof if( isNaN(valeur_cookie) || valeur_cookie==null )

	//debug
	//alert(valeur_cookie+'\n'+this._adCorner_NumberOfDisplay);
	// On loading page, displays 'this._adCorner_NumberOfDisplay' times the adcorner unfolded
	if( valeur_cookie < this._adCorner_NumberOfDisplay )
	{
		
		// Adcorner unfolded
		this.changeToSwf();
		
		// cookie 's inital date has expired
		if( date_du_jour > valeur_cookie_dateExpire && valeur_cookie>0 )
		{
			// new expiration date
			alert('maj 1 : '+this._adcorner_CookieNameExpireDate+', '+new_valeur_cookie_expire+', '+new_valeur_cookie_dateExpire );
			cookie_adcorner.setCookie ( this._adcorner_CookieNameExpireDate, new_valeur_cookie_expire, new_valeur_cookie_dateExpire, '', '', '');
			valeur_cookie_dateExpire = new_valeur_cookie_expire; // new inital cookie's expiration date
		}
		
		// increment the cookie's value
		//alert('maj 2 : ' + this._adCorner_CookieName+', '+(parseInt(valeur_cookie)+1)+', '+valeur_cookie_dateExpire) ;
		cookie_adcorner.setCookie ( this._adCorner_CookieName, parseInt(valeur_cookie)+1, valeur_cookie_dateExpire, '', '', '');
			
		// Adcorner folded after 'this._adCorner_TimeToDisplay' msec
		this.timer(this._adCorner_InstanceName, this._adCorner_TimeToDisplay);	
	}	
	else // On loading page, displays the adcorner folded
	{
		// Adcorner folded
		this.changeToImg();
	}// endof if( valeur_cookie < this._adCorner_NumberOfDisplay )
	
	//alert( this._adCorner_CookieName + ' : ' + valeur_cookie);
}

/* ****************************************** */
/* METHODS FOR UNFOLDED / FOLDED ADCORNER & OTHERS */
/* ****************************************** */

//
// @desc : Into div, change to swf
function changeToSwf( )
{
	
//debug
//alert('changeToSwf');

	// OnMouseout event
	var evt_onmouseout = 'onmouseout="javascript:'+this._adCorner_InstanceName+'.changeToImg();" ';
	
	var bug_IE = 0;
	
	// [begin] DIV
	var line = '<DIV ';
	
	if( document.all )	// correct IE bug : onmouseout event isn't correctly executed
	{
		line += evt_onmouseout;
		bug_IE = 1;			
	}
	
	// [continue] DIV
	line += ' style="left:0px;width:'+ this._adCorner_StateUnfolded_Width+ ';height:'+ this._adCorner_StateUnfolded_Height+	';position:absolute;top:0px;"  >';
			
	//debug
	//alert(this._adCorner_StateUnfolded_Width + '\n' + this._adCorner_StateUnfolded_Height);		
			
	// [begin] OBJECT		
	line += '<OBJECT ';
	
	if( bug_IE == 0 )	// for browsers <> IE
	{
		line += evt_onmouseout;
	} 
	// [continue to end] OBJECT
	line+= ' id="ad_corner" classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab// version=5,0,0,0" width="'+
	this._adCorner_StateUnfolded_Width+
	'" height="'+
	this._adCorner_StateUnfolded_Height+
	'" >';
	line += '<PARAM name="movie"  value="'+	this._adCorner_StateUnfolded_Name + '">';
	line += '<PARAM name="wmode" value="transparent"><PARAM name="quality" value="high"><PARAM name="bgcolor" value="#FFFFFF">';
	
	//line += '<!--<PARAM name="flashvars" value="'+ this._adCorner_FlashParams + '">-->';
	
	line += '<PARAM name="swremote" value="'+ this._adCorner_FlashParams + '">';
	
	
	line += '<EMBED src="'+	this._adCorner_StateUnfolded_Name+ '" quality="high" bgcolor="#FFFFFF" wmode="transparent" width="'+ this._adCorner_StateUnfolded_Width+ '" height="'+ this._adCorner_StateUnfolded_Height+ '" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash" flashvars="'+ this._adCorner_FlashParams +'" ></EMBED>';
	
	// [end] OBJECT
	line += '</OBJECT>';
	
	// [end] DIV
	line += '</DIV>';
	
	//debug
	//alert (line);	
	
	// construct the div for adcorner if it doesn't exist
	//this.createAdcornerDivTag();
	
	//alert(document.getElementById(this._adCorner_DivId));
	// *** we display the new div code ***
	document.getElementById(this._adCorner_DivId).innerHTML = line;
	
	// correct IE bug on sending flash vars 
	document.getElementById("correction_bug_activex").src = document.getElementById("correction_bug_activex").src;
	
	// fix z-index property when 2 swf are displayed in the same zone
	// we hide the non adcroner swf
	document.getElementById("pub_sous_logo").style.display='none';	
	document.getElementById("pub_sous_logo_hide").style.display='block';	
	document.getElementById("annuaire_localisations").style.display='none';	
	
}


//
// @desc : Into div, change to img
function changeToImg()
{
//debug
//alert(this + 'changeToImg')

	var line = '<DIV style="left:0px;width:'+
		this._adCorner_StateFolded_Width+
		'px;position:absolute;top:0px;height:'+
		this._adCorner_StateFolded_Height+
		'px" ><img src="'+
		this._adCorner_StateFolded_Name+
		'" width="'+
		this._adCorner_StateFolded_Width+
		'px" height="'+
		this._adCorner_StateFolded_Height+
		'px" onmouseover="javascript:'+this._adCorner_InstanceName+'.changeToSwf();" ></DIV>';

	// construct the div for adcorner if it doesn't exist
	//this.createAdcornerDivTag();				
	
	document.getElementById(this._adCorner_DivId).innerHTML = line;
	
	// fix z-index property when 2 swf are displayed in the same zone
	// we show the non adcroner swf
	document.getElementById("pub_sous_logo_hide").style.display='none';
	document.getElementById("pub_sous_logo").style.display='block';		
	document.getElementById("annuaire_localisations").style.display='block';		
}	

//
// @desc : Make the adcorner to folded state
function kcornerclose()
{
	this.changeToImg();
}

//
// @desc : Into div change to img after 'msec' milliseconds
// @param : callback_object -> object instance name
// @param : msec -> nb of milliseconds
function timer( callback_object, msec)
{   
	var timeoutID;
		
	timeoutID = window.setTimeout(callback_object + ".changeToImg()", msec); // ok
}

//
// @desc : create the div that contains the adcorner
function createAdcornerDivTag()
{
	// Does the div which contain the adcorner exist ?
	if( document.getElementById(this._adCorner_DivId) == null )
	{
		// if doesn't exist yet, we create it
		var newDivTag = document.createElement("div");		// create the div for adcorner
		newDivTag.setAttribute("id", this._adCorner_DivId);	// set attribute id
		newDivTag.setAttribute("style", "z-index=20");	// set the div above all
		
		document.body.appendChild(newDivTag);			// attach the new div to the body tag
	}
}

// -----------------------------------------------------------------------------------------------
