$(document).ready( function () {
	fnPreloadImage('/t-annonces/images/fermer.gif');
	fnPreloadImage('/t-annonces/images/fermer-clair.gif');
	fnPreloadImage('/t-annonces/images/fermer-sombre.gif');
} ) ;

jQuery.fn.popup = function (oOptions) {
	var oOptionsParDefaut = {
		iWidth: 0, iHeight: 0, iMaxWidth: 0, iMaxHeight: 0, iMinWidth: 0, iMinHeight: 0,
		sTitre : '', bTitreArrondis: false, bFondNoir : true, sMode : 'fixed' // fixed ou draggable
	};
	oOptions = jQuery.extend(oOptionsParDefaut, oOptions);
	
	  // La popup
	if (jQuery("#popup").length)
		jQuery("#popup").empty();
	else
		jQuery("body").append('<div id="popup"></div>');
	
	  // On cache la popup pour le moment
	jQuery("#popup").hide();
	
	  // Titre
	if (oOptions.sTitre) {
		if (oOptions.bTitreArrondis)
			jQuery("#popup").append('<h2 id="popupTitle" class="titreEnImageArrondis"><span class="arrondisGauche"></span>' + oOptions.sTitre + '<span class="arrondisDroit"></span></h2>');
		else
			jQuery("#popup").append('<h2 id="popupTitle" class="titreEnImage">' + oOptions.sTitre + '</h2>');
	}
	
	  // Contenu
	jQuery("#popup").append('<div id="popupContent"></div>');
	this.clone(true).show().appendTo("#popupContent");
	
	  // Le bouton fermer
	jQuery("#popup").append('<img src="/t-annonces/images/fermer.gif" alt="Fermer" id="fermer" />')
	.find("#fermer").css({ position: 'absolute', top: '4px', right: '4px' })
	.hover(	function () { this.src = '/t-annonces/images/fermer-clair.gif'; }, function () { this.src = '/t-annonces/images/fermer.gif'; } )
	.mousedown(	function () { this.src = '/t-annonces/images/fermer-sombre.gif'; } )
	.mouseup(	function () { this.src = '/t-annonces/images/fermer.gif'; } )
	.click(function () {
		jQuery("#popup, #blackScreen").remove();
		
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
			jQuery("img.select").remove();
			jQuery("select").css('display', 'inline');
		}
	} ) ;
	
	  // Taille initiale
	var iPopupWidth = this.width() + 4;
	var iPopupHeight = this.height() + jQuery("#titre-zoom").height() + 4;
	
	  // On ajoute les espaces
	var pEspaces = {
		padding: { iHaut: 0, iDroite: 0, iBas: 0, iGauche: 0 },
		margin: { iHaut: 0, iDroite: 0, iBas: 0, iGauche: 0 }
	};
	
	for (sNomEspace in pEspaces) {
		if (this.css(sNomEspace)) {
			var aValeurs = this.css(sNomEspace).split(' ', 4);
			if (aValeurs.length == 1)
				pEspaces[sNomEspace].iHaut = pEspaces[sNomEspace].iDroite = pEspaces[sNomEspace].iBas =
				pEspaces[sNomEspace].iGauche = aValeurs[0];
			else if (aValeurs.length == 2) {
				pEspaces[sNomEspace].iHaut = pEspaces[sNomEspace].iBas = aValeurs[0];
				pEspaces[sNomEspace].iDroite = pEspaces[sNomEspace].iGauche = aValeurs[1];
			} else if (aValeurs.length == 3) {
				pEspaces[sNomEspace].iHaut = aValeurs[0];
				pEspaces[sNomEspace].iDroite = pEspaces[sNomEspace].iGauche = aValeurs[1];
				pEspaces[sNomEspace].iBas = aValeurs[2];
			} else {
				pEspaces[sNomEspace].iHaut = aValeurs[0];
				pEspaces[sNomEspace].iDroite = aValeurs[1];
				pEspaces[sNomEspace].iBas = aValeurs[2];
				pEspaces[sNomEspace].iGauche = aValeurs[3];
			}
		}
		
		if (this.css(sNomEspace + '-top')) pEspaces[sNomEspace].iHaut = this.css(sNomEspace + '-top');
		if (this.css(sNomEspace + '-right')) pEspaces[sNomEspace].iDroite = this.css(sNomEspace + '-right');
		if (this.css(sNomEspace + '-bottom')) pEspaces[sNomEspace].iBas = this.css(sNomEspace + '-bottom');
		if (this.css(sNomEspace + '-left')) pEspaces[sNomEspace].iGauche = this.css(sNomEspace + '-left');
		
		  // Transformer les strings en int
		for (sNomDirection in pEspaces[sNomEspace])
			pEspaces[sNomEspace][sNomDirection] = parseInt(pEspaces[sNomEspace][sNomDirection]);
	}
	
	iPopupWidth += pEspaces.padding.iGauche + pEspaces.padding.iDroite;
	iPopupWidth += pEspaces.margin.iGauche + pEspaces.margin.iDroite;
	
	iPopupHeight += pEspaces.padding.iHaut + pEspaces.padding.iBas;
	iPopupHeight += pEspaces.margin.iHaut + pEspaces.margin.iBas;
	
	if (oOptions.iWidth)
		iWidth = oOptions.iWidth;
	else if (oOptions.iMaxWidth && iPopupWidth > oOptions.iMaxWidth)
		iWidth = oOptions.iMaxWidth;
	else if (oOptions.iMinWidth && iPopupWidth < oOptions.iMinWidth)
		iWidth = oOptions.iMinWidth;
	else
		iWidth = iPopupWidth;
	
	if (oOptions.iHeight)
		iHeight = oOptions.iHeight;
	else if (oOptions.iMaxHeight && iPopupHeight > oOptions.iMaxHeight)
		iHeight = oOptions.iMaxHeight;
	else if (oOptions.iMinHeight && iPopupHeight < oOptions.iMinHeight)
		iHeight = oOptions.iMinHeight;
	else
		iHeight = iPopupHeight;
	
	jQuery("#popup").css({ position: 'absolute', 'z-index': 500, width: iWidth, height: iHeight });
	jQuery("#popup #popupContent").css({ height: iHeight - jQuery("#popupTitle").height() - 2 });
	
	  // En position fixe dans la page ou bien déplaçable
	if (oOptions.sMode == 'fixed') {
		if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7) {
			jQuery(window).scroll(function () { jQuery("#popup").centrer(); });
			jQuery(window).resize(function () { jQuery("#popup").centrer(); });
		} else
			jQuery("#popup").css('position', 'fixed');
	} else if (oOptions.sMode == 'draggable')
		jQuery("#popup").draggable( {
			handle: jQuery("#popup > *:first-child"),
			drag: function (e, ui) {
				var oPosition = { top: ui.position.top, left: ui.position.left };
				
				if (ui.position.left - jQuery(document).scrollLeft() + iWidth > jQuery(window).width()) // Dépasse à droite
				oPosition.left = jQuery(window).width() + jQuery(document).scrollLeft() - iWidth;
				else if (ui.position.left - jQuery(document).scrollLeft() < 0) // Dépasse à gauche
					oPosition.left = 0 + jQuery(document).scrollLeft();
				
				if (ui.position.top - jQuery(document).scrollTop() + iHeight > jQuery(window).height()) // Dépasse en bas
					oPosition.top = jQuery(window).height() + jQuery(document).scrollTop()  - iHeight;
				else if (ui.position.top - jQuery(document).scrollTop() < 0) // Dépasse en haut
					oPosition.top = 0 + jQuery(document).scrollTop();
				
				return oPosition;
			}
		} ) ;
	
	  // On centre la popup
	jQuery("#popup").centrer();
	
	  // On affiche la popup
	jQuery("#popup").show(null, function () { jQuery("#popup").centrer(); } );
	
	  // L'arrière plan noir
	if (oOptions.bFondNoir)
		jQuery.fondNoir();
	
	  // Anti-bue IE 6
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)
		jQuery("select").css('display', 'none').after('<img src="t-annonces/images/select.gif" alt="" class="select" />');
	
	  // On retourne l'objet jQuery
	return this;
}

jQuery.fn.centrer = function () {
	var iLeft = (jQuery(window).width() / 2) - (this.width() / 2);
	var iTop = (jQuery(window).height() / 2) - (this.height() / 2);
	
	var sPosition = this.css('position');
	if (sPosition != 'fixed') {
		iLeft += jQuery(document).scrollLeft();
		iTop += jQuery(document).scrollTop();
	}
	
	this.css({ position: sPosition, top: iTop + 'px', left: iLeft + 'px' });
	
	  // On retourne l'objet jQuery
	return this;
}

jQuery.fondNoir = function () {
	if (!jQuery("#blackScreen").length)
		jQuery("body").append('<div id="blackScreen"></div>');
	
	var iWidth = jQuery(document).width();
	var iHeight = jQuery(document).height();
	
	if (jQuery.browser.msie && parseInt(jQuery.browser.version) < 7)
		iWidth -= 22;
	
	jQuery("#blackScreen").hide().css({ position: 'absolute', top: 0, left: 0, 'z-index': 300, width: iWidth, height: iHeight, 'background-color': '#000', opacity: '.6', filter: 'alpha(opacity=60)' }).show();
	
	  // On retourne l'objet jQuery
	return this;
}