;(function($) {
jQuery.fn.popin = function(o) {
	
	var settings = jQuery.extend({
		width : 0,
		height : 0,
		opacity: .8,
		voileDuration : 'fast',
		innerContainer : 'div.spir_popinInnerContent',
		onStart: null,
		onExit: null
	}, o);
	
	var ie = ($.browser.msie) ? true : false;
	
		/*$(".bi-popin").hover(function() {
			alert(ie);
			if( $("#bt-selection").html().indexOf('(0)')>=0 ) $(".bi-popin-selection").css('cursor','default');
		});
		$(".bi-popin").click(function() {
			if( $("#bt-selection").html().indexOf('(0)')==-1 ) PPNopen(this);
			return false;
		});*/
					
	/* Action ouverture*/
	jQuery(this).each(function(index) {
		$(this).hover(function() { 
			if( $(this).attr("id")!="bi-btnAjoutSelection" && $("#bi-maSelection").html().indexOf('(0)')>=0 ) {
				$(".bi-popin-selection").css('cursor','default');
			} else {
				$(".bi-popin-selection").css('cursor','pointer');
			}
		});
		$(this).click(function() {
			if( $(this).attr("id")=="bi-btnAjoutSelection" || $("#bi-maSelection").html().indexOf('(0)')==-1 ) {
				PPNopen(this);
			}
			return false;
		});
	});

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

	function PPNopen(e) {
		var _self = $(e.hash);
		if(settings.onStart != null) {
			settings.onStart();
		}
		
		// mise en place du voile et affichage contenu
		$('<div />', {
			id:'voile',
			css: {
				width: '100%',
				height: $('body').height(), //Bug: espace visible uniquement, passage en fixed
				background: '#000',
				position:'fixed',
				top:0,
				left:0,
				opacity:0
			}
		}).appendTo('body').fadeTo(settings.voileDuration, settings.opacity, function() {
			_self.css({
				'margin-top':-(_self.height()/2),
				'margin-left':-(_self.width()/2)
			}).show();
			if(ie == true){
				_self.css({
					width : _self.find(settings.innerContainer).width(),
					'margin-left':-((_self.width()/2)/2)
				});
			}
		});
		settings.$voile = $('#voile');
		
		//Tag google analytics
		//pageTracker._trackEvent('AccesPanier','VersionB');
	
		/*Action fermeture*/
		$(".popin-close, #voile").click(function() {
			PPNclose(_self);
			return false;
		});
			
		$("html").keydown(function(k){
			if(k.keyCode == '27') {
				PPNclose(_self);
			}
		});
	}
	
	 /*Popin fermeture*/
	function PPNclose(e) {
		$("html").unbind("keydown");
		e.removeAttr('style');
		settings.$voile.fadeTo(settings.voileDuration, 0).remove();
		
		if(settings.onExit != null) {
			settings.onExit();
		}
	}
};
})(jQuery);
