



jQuery.fn.popin = function(o) {

	var settings = jQuery.extend({
		width : 250,
		height : 250,
		className: "",
		loaderImg: "",
		opacity: .5,
		onStart: null,
		onComplete: null,
		onExit: null
	}, o);

	// Action ouverture
	jQuery(this).each(function() {
		jQuery(this).click(function() {
			PPNopen(jQuery(this).attr("href"));
			return false;
		});
	});

	// Popin Ouverture
	var Loader = new Image();
	Loader.src = settings.loaderImg;

	ie6 = (jQuery.browser.msie && (jQuery.browser.version == "6.0")) ? true : false;

	// CSS
	jQuery("body").css("position", "relative");


	function PPNopen(url) {

		if(settings.onStart != null) {
			settings.onStart();
		}

		if(ie6 == true) {
			jQuery("#PPNCSS").remove();
			jQuery("body").append(''
				+	'<style type="text/css" id="PPNCSS">'
				+	'.popin-voile {top:expression(documentElement.scrollTop + body.scrollTop + "px")}'
				+	'.popin {top:expression(documentElement.scrollTop + body.scrollTop + (documentElement.clientHeight/2) - ' + (settings.height/2) + ' + "px")}'
				+	'</style>'
				+	'');
		}


		// Insertion du voile & Verrouillage du scroll
		jQuery("body").prepend('<div class="popin-voile"></div>');

		// CSS du voile
		jQuery(".popin-voile")	.css("opacity",						0)
							.css("left",						0)
							.css("z-index",						"9000")
							.css("width",						"100%")
							.css("height",						0)
							.css("background-color",			"#000")
							.css("background-position",			"center center")
							.css("background-repeat",			"no-repeat")
							;
		if(ie6 == true) {
			jQuery(".popin-voile")		.css("position",					"absolute")
									;
		}
		else {
			jQuery(".popin-voile")		.css("top",							0)
									.css("position",					"fixed")
									;
		}

		// Patch IE6
		if(ie6 == true) {

			PPNhtmlScroll 			= document.getElementsByTagName("html")[0].scrollTop;
			var PPNbodyMargin 		= new Object();
			PPNbodyMargin.top 		= parseInt(jQuery("body").css("margin-top"));
			PPNbodyMargin.right 	= parseInt(jQuery("body").css("margin-right"));
			PPNbodyMargin.bottom 	= parseInt(jQuery("body").css("margin-bottom"));
			PPNbodyMargin.left 		= parseInt(jQuery("body").css("margin-left"));

			jQuery("html, body").css("height", "100%");
			jQuery("html, body").css("overflow", "hidden");
			jQuery("body").height(jQuery("body").height());
			PPNbodyHeight = parseInt(jQuery("body").height());
			jQuery("html, body").css("overflow", "visible");
			jQuery("html, body").css("overflow-x", "visible");

			PPNbodyTop = ((PPNbodyMargin.top + PPNbodyMargin.bottom) < PPNhtmlScroll) ? (PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) : 0;
			jQuery("body").css("top", PPNbodyTop );
			jQuery(".popin-voile").css("top", -(PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) );
			jQuery(".popin-voile").css("left", (- PPNbodyMargin.left) );
			jQuery(".popin-voile").css("width", jQuery("html").width());

		} else {
			jQuery("html, body").css("overflow", "hidden");
		}

		// Affichage du voile
		jQuery(".popin-voile").animate({opacity:settings.opacity, height:((ie6 == true) ? (PPNbodyHeight + PPNbodyMargin.top + PPNbodyMargin.bottom) : "100%")}, function() {

			// Loader
			jQuery(".popin-voile").css("background-image", "url('"+settings.loaderImg+"')");

			// Insertion de la popin et animation
			jQuery(".popin").css("height", jQuery("body").height() );

			// Requ�te
			jQuery.ajax({
				type: "GET",
				url: url,
				dataType: "html",
				success: function(m){

					// Cr�ation de la popin
					jQuery("body").prepend('<div class="popin ' + settings.className + '"><div class="popin-content"></div></div>');

					// CSS du voile
					jQuery(".popin")			.css("left",						"50%")
										.css("z-index",						"9500")
										.css("width",						settings.width)
										.css("height",						settings.height)
										.css("overflow",					"hidden")
										.css("margin-left",					-(settings.width/2))
										;
					jQuery(".popin-content")	.css("overflow",					"auto")
										.css("height",							jQuery(".popin").height()
																			- 	parseInt(jQuery(".popin").css("padding-top"))
																			- 	parseInt(jQuery(".popin").css("padding-bottom"))
																			)
										;
					if(ie6 == true) {
						jQuery(".popin")		.css("position",					"absolute")
										.css("margin-top",					0)
										;
					}
					else {
						jQuery(".popin")		.css("position",					"fixed")
										.css("top",							"50%")
										.css("margin-top",					-(settings.height/2))
										;
					}


					// Chargement du contenu
					jQuery(".popin-content").html(m);

				},
				complete: function(){

					// Loader
					jQuery(".popin-voile").css("background-image", "");

					// Affichage
					if(ie6 == true) {
						jQuery(".popin").css("top", parseInt(jQuery(".popin").css("top")) - PPNbodyTop );
					}
					jQuery(".popin").fadeIn("slow", function() {
						if(settings.onComplete != null) {
							settings.onComplete();
						}
					});

					// Action fermeture
					jQuery(".popin-close, .popin-voile").click(function() {
						PPNclose();
						return false;
					});
				}
			});


		});

		jQuery("html").keydown(function(e){
			if(e.keyCode == '27') {
				PPNclose();
			}
		});

	}

	// Popin fermeture
	function PPNclose() {

		jQuery("html").unbind("keydown");

		jQuery(".popin").fadeOut("slow", function() {

			jQuery(".popin-voile").animate({opacity:0, height:0}, function() {

				// Suppression du voile & D�verrouillage du scroll
				if(ie6 == true) {
					jQuery("html, body").css("height", "auto");
					jQuery("html, body").css("overflow", "auto");
					jQuery("html, body").css("overflow-x", "hidden");
					jQuery("body").css("top", 0);
					window.scrollTo(0, (PPNhtmlScroll) );
				} else {
					jQuery("html, body").css("overflow", "auto");
				}
				jQuery(".popin, .popin-voile").remove();

				if(settings.onExit != null) {
					settings.onExit();
				}

			});

		});

	}


};


//Custom par simon
// Allow to call a popin open within a script, without a click
jQuery.fn.popinOnDemand = function(o, url) {
	var settings = jQuery.extend({
		width : 250,
		height : 250,
		className: "",
		loaderImg: "",
		opacity: .5,
		onStart: null,
		onComplete: null,
		onExit: null
	}, o);

	PPNopen(url);


	// Popin Ouverture
	var Loader = new Image();
	Loader.src = settings.loaderImg;

	ie6 = (jQuery.browser.msie && (jQuery.browser.version == "6.0")) ? true : false;

	// CSS
	jQuery("body").css("position", "relative");


	function PPNopen(url) {

		if(settings.onStart != null) {
			settings.onStart();
		}

		if(ie6 == true) {
			jQuery("#PPNCSS").remove();
			jQuery("body").append(''
				+	'<style type="text/css" id="PPNCSS">'
				+	'.popin-voile {top:expression(documentElement.scrollTop + body.scrollTop + "px")}'
				+	'.popin {top:expression(documentElement.scrollTop + body.scrollTop + (documentElement.clientHeight/2) - ' + (settings.height/2) + ' + "px")}'
				+	'</style>'
				+	'');
		}


		// Insertion du voile & Verrouillage du scroll
		jQuery("body").prepend('<div class="popin-voile"></div>');

		// CSS du voile
		jQuery(".popin-voile")	.css("opacity",						0)
							.css("left",						0)
							.css("z-index",						"9000")
							.css("width",						"100%")
							.css("height",						0)
							.css("background-color",			"#000")
							.css("background-position",			"center center")
							.css("background-repeat",			"no-repeat")
							;
		if(ie6 == true) {
			jQuery(".popin-voile")		.css("position",					"absolute")
									;
		}
		else {
			jQuery(".popin-voile")		.css("top",							0)
									.css("position",					"fixed")
									;
		}

		// Patch IE6
		if(ie6 == true) {

			PPNhtmlScroll 			= document.getElementsByTagName("html")[0].scrollTop;
			var PPNbodyMargin 		= new Object();
			PPNbodyMargin.top 		= parseInt(jQuery("body").css("margin-top"));
			PPNbodyMargin.right 	= parseInt(jQuery("body").css("margin-right"));
			PPNbodyMargin.bottom 	= parseInt(jQuery("body").css("margin-bottom"));
			PPNbodyMargin.left 		= parseInt(jQuery("body").css("margin-left"));

			jQuery("html, body").css("height", "100%");
			jQuery("html, body").css("overflow", "hidden");
			jQuery("body").height(jQuery("body").height());
			PPNbodyHeight = parseInt(jQuery("body").height());
			jQuery("html, body").css("overflow", "visible");
			jQuery("html, body").css("overflow-x", "visible");

			PPNbodyTop = ((PPNbodyMargin.top + PPNbodyMargin.bottom) < PPNhtmlScroll) ? (PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) : 0;
			jQuery("body").css("top", PPNbodyTop );
			jQuery(".popin-voile").css("top", -(PPNbodyMargin.top + PPNbodyMargin.bottom - PPNhtmlScroll) );
			jQuery(".popin-voile").css("left", (- PPNbodyMargin.left) );
			jQuery(".popin-voile").css("width", jQuery("html").width());

		} else {
			jQuery("html, body").css("overflow", "hidden");
		}

		// Affichage du voile
		jQuery(".popin-voile").animate({opacity:settings.opacity, height:((ie6 == true) ? (PPNbodyHeight + PPNbodyMargin.top + PPNbodyMargin.bottom) : "100%")}, function() {

			// Loader
			jQuery(".popin-voile").css("background-image", "url('"+settings.loaderImg+"')");

			// Insertion de la popin et animation
			jQuery(".popin").css("height", jQuery("body").height() );

			// Requ�te
			jQuery.ajax({
				type: "GET",
				url: url,
				dataType: "html",
				success: function(m){

					// Cr�ation de la popin
					jQuery("body").prepend('<div class="popin ' + settings.className + '"><div class="popin-content"></div></div>');

					// CSS du voile
					jQuery(".popin")			.css("left",						"50%")
										.css("z-index",						"9500")
										.css("width",						settings.width)
										.css("height",						settings.height)
										.css("overflow",					"hidden")
										.css("margin-left",					-(settings.width/2))
										;
					jQuery(".popin-content")	.css("overflow",					"auto")
										.css("height",							jQuery(".popin").height()
																			- 	parseInt(jQuery(".popin").css("padding-top"))
																			- 	parseInt(jQuery(".popin").css("padding-bottom"))
																			)
										;
					if(ie6 == true) {
						jQuery(".popin")		.css("position",					"absolute")
										.css("margin-top",					0)
										;
					}
					else {
						jQuery(".popin")		.css("position",					"fixed")
										.css("top",							"50%")
										.css("margin-top",					-(settings.height/2))
										;
					}


					// Chargement du contenu
					jQuery(".popin-content").html(m);

				},
				complete: function(){

					// Loader
					jQuery(".popin-voile").css("background-image", "");

					// Affichage
					if(ie6 == true) {
						jQuery(".popin").css("top", parseInt(jQuery(".popin").css("top")) - PPNbodyTop );
					}
					jQuery(".popin").fadeIn("slow", function() {
						if(settings.onComplete != null) {
							settings.onComplete();
						}
					});

					// Action fermeture
					jQuery(".popin-close, .popin-voile").click(function() {
						PPNclose();
						return false;
					});
				}
			});


		});

		jQuery("html").keydown(function(e){
			if(e.keyCode == '27') {
				PPNclose();
			}
		});

	}

	// Popin fermeture
	function PPNclose() {

		jQuery("html").unbind("keydown");

		jQuery(".popin").fadeOut("slow", function() {

			jQuery(".popin-voile").animate({opacity:0, height:0}, function() {

				// Suppression du voile & D�verrouillage du scroll
				if(ie6 == true) {
					jQuery("html, body").css("height", "auto");
					jQuery("html, body").css("overflow", "auto");
					jQuery("html, body").css("overflow-x", "hidden");
					jQuery("body").css("top", 0);
					window.scrollTo(0, (PPNhtmlScroll) );
				} else {
					jQuery("html, body").css("overflow", "auto");
				}
				jQuery(".popin, .popin-voile").remove();

				if(settings.onExit != null) {
					settings.onExit();
				}

			});

		});

	}

};
/*
jQuery.fn.popinClose = function(o, url) {
	var settings = jQuery.extend({
		onExit: null
	}, o);
	
	PPNhtmlScroll = document.getElementsByTagName("html")[0].scrollTop;
	
	PPNclose();
	
	// Popin fermeture
	function PPNclose() {
		jQuery("html").unbind("keydown");

		jQuery(".popin").fadeOut("slow", function() {

			jQuery(".popin-voile").animate({opacity:0, height:0}, function() {

				// Suppression du voile & D�verrouillage du scroll
				if(ie6 == true) {
					jQuery("html, body").css("height", "auto");
					jQuery("html, body").css("overflow", "auto");
					jQuery("html, body").css("overflow-x", "hidden");
					jQuery("body").css("top", 0);
					window.scrollTo(0, (PPNhtmlScroll) );
				} else {
					jQuery("html, body").css("overflow", "auto");
				}
				jQuery(".popin, .popin-voile").remove();

				if(settings.onExit != null) {
					settings.onExit();
				}

			});

		});

	}
};*/