/*-------------------------------------------------------------------------------
	JQuery Pop Up (for symfony backend)
	By Giulio Andreini
-------------------------------------------------------------------------------*/
$.fn.popUp = function(options){

	/* Setup the options */  
	var defaults = {  
		width: 300,
		corner: "round"
	};  
	var options = $.extend(defaults, options); 
	// Create it like: $("#popUpPortlet").popUp({width: 600, corner: "suqare"});
	
	// Loop su gli elementi invocati
	$(this).each(function(){
		// Assegno elemento this
		var $this = $(this);
		var $overlay;
		
		// Nascondo tutti gli elementi di input con attributo hidden
		$(this).find("input").each(function(){
			if( $(this).attr("type") == "hidden") $(this).hide();
		});
		
		// Creazione del bg scuro - overlay
		createOverlay = function() {
		    
		    // Creo un elemento di bg nero per lo sfondo
			$this.before("<div id='popUpBg'></b>");
			
			var ie6 = !window.XMLHttpRequest;
			$overlay = $("#popUpBg");
			
			var overlayOpacity= 0.7;			// 1 is opaque, 0 is completely transparent (change the color in the CSS file)
			var overlayFadeDuration= 1;		// Duration of the overlay fade-in and fade-out animations (in milliseconds)
		
			// Overlay;
			compatibleOverlay = ie6 || ($overlay.currentStyle && ($overlay.currentStyle.position != "fixed"));
			// if (compatibleOverlay) $overlay.style.position = "absolute";
			if (compatibleOverlay) $overlay.css({"position":"absolute"});
			$overlay.css("opacity", overlayOpacity);
			$overlay.css({"display":"none", "margin":"0", "padding":"0", "position":"absolute", "background-color":"#000000", "width":"100%", "height":$(document).height()+"px","top":"0","left":"0", "z-index":"1001"});
			
			// $overlay.fadeIn(overlayFadeDuration);
			$overlay.fadeIn(overlayFadeDuration, function(){showPopUp();}); // Chiamata alla funzione shoePopUp alla fine del fade
			// $overlay.show(function(){showPopUp();}); // Chiamata alla funzione shoePopUp alla fine del fade
			
			// Click event sull overlay - nasconde tutto
			$overlay.click(function () { 
		    	removePopUpElements();
		    });
		} // createOverlay
		    
		// Mostra la pop up
		showPopUp = function() {
			// Set the width
			$this.css({"width":defaults.width + "px"});
			
			// Page height & width
			var pageHeight = $(window).height();
			var pageWidth = $(window).width();
			
			// Set vertical positioning
			$this.vCenter();     
			
			// Set orizontal positioning
			$this.css({"margin-left": "-" + $this.width()/2 + "px"});
			
			// Z-indexing
			$this.css({"z-index":"1002"});
			
			// Animazione comparsa
			 $this.animate( { "top": "-=20px" }, { queue:false, duration:300 } )
         	 	.animate( { "opacity": "toggle"}, 200 );
         	
         	// Focus handling
        	// Ciclo tutti i campi di input ad esclusione del submit e cancel
        	$this.find("input:not(#popUpFormSubmit, #popUpFormCancel)").each(function(){
        		// Focus fires
        		$(this).focus(function () {
        			$(this).css({"border":"1px solid #cccccc", "background-color":"#edf5fe"});
			    });
			    // Blur fires
        		$(this).blur(function () {
			    	$(this).css({"border":"1px solid #cccccc", "background-color":"#f3f3f3"});
			    });
        	});
        	// Pop Up Windowd Round corners
			if(defaults.corner == "round") $this.bg(14);
        
        	// Assegno il focus al primo campo di input
        	$this.find("input:first").focus();
        	
        	// Assegno la funzione di chiusura al pulsante cancel
        	$this.find("input#popUpFormCancel").click(function () {
        		removePopUpElements();
        		return false;
        	});
        	
        	
        } // showPopUp
		
		// Resize input fields
		resizeInputs = function() {
			$this.find("input:not(#popUpFormSubmit, #popUpFormCancel)").css({"width": (defaults.width - 14) + "px"});
			$this.find("textarea").css({"width": (defaults.width - 14) + "px"});
		}
		
		// Mostra la pop up
		startPopUp = function() {
		    createOverlay();
		    resizeInputs();
		}
		
		// Gestione dello scrolling della pagina
		// Si riposiziona verticalmente l'oggetto
		$(window).scroll(function () { 
			$this.vCenter();
	    });

		
		// Remove degli elementi dopo un bel fade out
		removePopUpElements = function() {
			/*
			$this.fadeOut(200, function() {
   				 $this.remove();
   				 $overlay.fadeOut(200, function() {$overlay.remove();});
			});
			*/
			
			// Animazione comparsa
			$this.animate( { "top": "-=20px" }, { queue:false, duration:300 } )
         		.animate( { "opacity": "toggle"}, 200, function() {
   				 $this.remove();
   				 $overlay.fadeOut(200, function() {$overlay.remove();});
			});
		}
		
		// Chiamata iniziale
		startPopUp();
	});
};

