/**

 * Affiche une div à la manière d'un popup

 *

 * @example jQuery.jPopup.show(titre du popup, contenu, largeur, hauteur);

 * @example jQuery.jPopup.remove();

 *

 * @name jPopup

 * @type jQuery

 * @param html: code html à afficher dans le popup 

 * @param settings: Object

 *          caption: titre de la fenetre,

 *          dimensions: Object { 

 *                      width: largeur de la zone d'affichage

 *                      height: hauteur de la zone d'affichage

 *                    },

 *          sizeAuto: true|false 

 *          css: Object, propriétés css de la fenetre,   

 *          displayClose: determine où afficher le bouton fermer

 * @cat Plugins/jPopup

 * @author Antoine Marcadet

 */

(function($) {

    $.extend({

        jPopup: {

            settings: { },

            show: function(html, settings) { //caption, divContent, width, height, oCss) {//function called when the user clicks on a thickbox link

            

            	this.settings = jQuery.extend({

            	   width: window.innerWidth,

            	   height: window.innerHeight

                }, settings);

	

            	if(typeof document.body.style.maxHeight === "undefined") {//if IE 6

            		$('body','html').css({height: '100%', width: '100%'});

            		$('html').css('overflow','hidden');

            		if(document.getElementById('jPopup_HideSelect') === null) {//iframe to hide select elements in ie6

            			$('body').append('<iframe id="jPopup_HideSelect"></iframe><div id="jPopup_overlay"></div><div id="jPopup_window"></div>');

            			$('#jPopup_overlay').click(this.remove);

            		}

            	}

                else { //all others

            		if(document.getElementById('jPopup_overlay') === null) {

            			$('body').append('<div id="jPopup_overlay"></div><div id="jPopup_window">');

            			$('#jPopup_overlay').click(this.remove);

            		}

            	}

            	

            	// positionnement

            	$('#jPopup_overlay').css({

                	position: 'fixed',

                	zIndex: 100,

                	top: 0,

                	left: 0,

                	height: '100%',

                	width: '100%'

                });

                $('#jPopup_window').css({

                	position: 'absolute',

                	zIndex: 102,

                	margin: 'auto',

                    height: settings.height,

                    width: settings.width

                });

                

                if(settings.center) {

                    $(window).resize(function() {

                        $('#jPopup_window').css({

                        	top: ($(window).height() - settings.height) / 2,

                        	left: ($(window).width() - settings.width) / 2

                        });

                    });

                    $('#jPopup_window').css({

                    	top: ($(window).height() - settings.height) / 2,

                    	left: ($(window).width() - settings.width) / 2

                    });

                }

                else {

                    $('#jPopup_window').css({

                    	top: 58,

                    	left: (($(window).width() - settings.width) / 2)-10

                    });

                }

                /*

                windowWidth  = ajaxContentW + 30;

        	    windowHeight = ajaxContentH + 45;

                //$('#TB_window').css({marginLeft: '-' + parseInt((windowWidth / 2),10) + 'px', width: windowWidth + 'px'});

            	//$('#TB_window').css({marginTop: '-' + parseInt((windowHeight / 2),10) + 'px'});

                */

                

                // contenu

                if(settings.caption) // affichage du titre

                    $('#jPopup_window').append('<div id="jPopup_caption">'+settings.caption+'</div>');

                

                $('#jPopup_window').append('<div id="jPopup_global"></div>');

                $('#jPopup_global').append('<div id="jPopup_closeWindow"><a href="#" id="jPopup_closeWindowButton" title="Fermer"><img src="/design/fermer.gif" /> Fermer</a></div><br/>');

                $('#jPopup_global').append('<div id="jPopup_windowContent">'+html+'</div>');

            	$('#jPopup_window').fadeIn('slow');

                $("#jPopup_closeWindowButton").click(this.remove);

                

            	document.onkeyup = function(e){ 	

            		if(e == null) { // ie

            			keycode = event.keyCode;

            		} else { // mozilla

            			keycode = e.which;

            		}

            		if(keycode == 27){ // close

            			this.remove();

            		}	

            	};

            },

            remove: function() {

            	$("#jPopup_overlay").unbind("click");

            	$("#jPopup_closeWindowButton").unbind("click");

            	$("#jPopup_window").fadeOut("slow",function(){$('#jPopup_window,#jPopup_overlay,#jPopup_HideSelect').remove();});

            	if(typeof document.body.style.maxHeight == "undefined") { //if IE 6

            		$("body","html").css({height: "auto", width: "auto"});

            		$("html").css("overflow","");

            	}

            	document.onkeydown = "";

            	return false;

            },

            update: function() {

            }

        }

    });

    

})(jQuery);


