
/* Fonction générale de cration de popin */
/*
	mClickedObject = lien ou bouton sur lequel lier l'ouverture de la popin
	mPopinContent = contenu à insérer dans la popin
	mOpenCallbackFn = fonction lancée à l'ouverture de la popin
	mCloaseCallbackFn = fonction lancée à la fermeture de la popin
	
	Utilisations :
	
		Ouverture directe:
		------------------
		new nedPopin(null, $('#ContenuAInserer').html(), fnOuverture, fnFermeture).openPopin();
		
		Ouverture depuis un lien:
		-------------------------
		new nedPopin($('#lien'), $('#ContenuAInserer').html(), fnOuverture, fnFermeture);
		
*/
var nedPopin = function (mClickedObject, mPopinContent, mOpenCallbackFn, mCloaseCallbackFn) {
	
	this.clickedObject = mClickedObject;
	this.popinContent = mPopinContent;
	this.openCallbackFn = mOpenCallbackFn;
	this.btClose = null;
	this.cloaseCallbackFn = mCloaseCallbackFn;
	
	this.openPopin = function () {		
		
		// Inseertion du contenu
		$('#popinNED .contentPopin').html(this.popinContent);
			
		// Fix background height on IE6
		if ($.browser.msie && navigator.appVersion.indexOf('MSIE 6') != -1) {
			$('select').css('visibility', 'hidden');
		}
		
		
		$('#bgPopin').css({ height: $('#page').height()});
		
		// Display Background
		$('#bgPopin').show();
		
		// Display Popin
		$('#popinNED').show();

		// Init Popin size and position
		
		
		var clientHeight = 0;
		if( typeof( window.innerWidth ) == 'number' ) {
			//Non-IE
			clientHeight = window.innerHeight;
		} else if( document.documentElement && (document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			clientHeight = document.documentElement.clientHeight;
		} else if( document.body && (document.body.clientHeight ) ) {
			//IE 4 compatible
			clientHeight = document.body.clientHeight;
		}

		
		var scrollTopOffset = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			//Netscape compliant
			scrollTopOffset = window.pageYOffset;
		} else if( document.body && (document.body.scrollTop ) ) {
			//DOM compliant
			scrollTopOffset = document.body.scrollTop;
		} else if( document.documentElement && (document.documentElement.scrollTop ) ) {
			//IE6 standards compliant mode
			scrollTopOffset = document.documentElement.scrollTop;
		}

		
		$('#popinNED').css('left', ($('body').width()/2-$('#popinNED').width()/2)); 
		$('#popinNED').css('top', (document.documentElement.clientHeight/2-$('#popinNED').height()/2) + scrollTopOffset); 
		
		if (this.openCallbackFn != null && typeof(this.openCallbackFn) === 'function') {
				this.openCallbackFn();
		}
	}
	
	this.openPopinHandler = function (e) {
		e.preventDefault();
		e.data.popinObject.openPopin();
	}
	
	this.closePopin = function () {
		$('#bgPopin').hide();
		$('#popinNED').hide();
		
		// Fix background height on IE6
		if ($.browser.msie && navigator.appVersion.indexOf('MSIE 6') != -1) {
			$('select').css('visibility', 'visible');
		}
		
		if (this.cloaseCallbackFn != null && typeof(this.cloaseCallbackFn) === 'function') {
				this.cloaseCallbackFn();
		}
	}
	
	this.closePopinHandler = function (e) {
		e.preventDefault();
		e.data.popinObject.closePopin();
	}
		
	this.init = function () {
		
		// Add Background, if not already added
		if ($('#bgPopin').size() === 0) {
			$('body').append('<div id="bgPopin"></div>');
			$('#bgPopin').hide();
		}
		// Add Popin, if not already added
		if ($('#popinNED').size() === 0) {
			$('body').append('<div id="popinNED"><div class="popinTop"><p class="mClose"><a id="popinNEDClose" href="#"> x Fermer</a></p><div class="contentPopin"></div></div><div class="popinBottom"></div></div>');
			$('#popinNED').hide();
		}
		
		$('#popinNEDClose').bind ('click', {popinObject: this}, this.closePopinHandler);
		
		if (this.clickedObject)
			this.clickedObject.bind('click', {popinObject: this}, this.openPopinHandler);
		
	}
		
	this.init();
}



/* ------------------------------------------------------------------------------------------------------------------- */
/* Fonction spécifique à la popin de redirection */


var timeoutID = 0;

/* Redirige ver l'url souhaitée */
function NEDRedirect () {
	window.open("http://eulerian.bluestreak.com/click/lactalis/9WEzt.Ng_QO_4I.lc2tpa9u8TNIgLw_8sQs69SptpGU-/");
}
// Delay avant la redirection
function NEDRedirectHandler () {
	timeoutID = setTimeout (NEDRedirect, 3000)
}
// Stoppe la redirection
function NEDStopRedirectHandler () {
	clearTimeout(timeoutID);
}


// utilisé pour les tests
$(document).ready (function(){
							
	// Exemple 1
	//var popNED = new nedPopin(null, $('#NEDPopinContent').html(), NEDRedirectHandler, NEDStopRedirectHandler);
	//popNED.openPopin();
	
	// Exemple 2
	//new nedPopin(null, $('#NEDPopinContent').html(), NEDRedirectHandler, NEDStopRedirectHandler).openPopin();
	
	// Exemple 3 (avec clic sur bouton)
//	if($('.jsOpenNEDPopin').size() > 0) {
//		new nedPopin($('.jsOpenNEDPopin'), $('#NEDPopinContent').html(), NEDRedirectHandler, NEDStopRedirectHandler);
	//}
	
});

