(function($){
	$.fn.aPopup = function(options) {

		var speed = 200;
		var defaults = {
			msg: "",
			url: "",
			img: "",
			post: "",
			width: ($(window).width() / 2)+"px",
			onComplete : function(){}
		};
		var options = $.extend(defaults, options);


		// attacchiamo il metedo al resize
		$(window).bind("resize", function(){
			center();
		});

		// centriamo la popup
		function center() {
			var wHeight = $(window).height();
			var wWidth = $(window).width();
			var eHeight = $('#popup').height();
			var eWidth = $('#popup').width();


			$('#popup').css({
				'position' : 'absolute',
				'top'	:	$(window).scrollTop() + ((wHeight / 2) - (eHeight / 2)),
				'left'	:	(wWidth / 2) - (eWidth / 2)
			});
			$('#pop_close').css({
				'position' : 'absolute',
				'top'	:	-28, // ($('#popup').offset().top+20),
				'left'	:	eWidth-17 // ($('#popup').offset().left + eWidth - 200 )
			});

		}

		// visualizziamo
		function show() {

			setTimeout(function () {

				// settiamo l'altezza e la larghezza della maschera
				var mHeight = $(document).height();
				var mWidth = $(window).width();
				$('#mask').css({'height':mHeight});

				// settiamo la popup al centro
				center();

				// visualizziamo
				$('#mask').fadeIn(speed, function(){
					$('#mask').fadeTo(speed,0.75, function() {
						$('#popup').fadeIn(speed, function() {
							defaults.onComplete();
						});
					});
				});

			}, 1);
		}

		// distruggiamo la popup creata
		function destroy() {

			// eliminiamo il pulsante
			$('#pop_close').fadeOut(200, function(){$('#pop_close').remove();});

			// eliminiamo la popup
			$('#popup').fadeOut(200, function(){$('#popup').remove();});

			// eliminiamo la maschera
			$('#mask').fadeOut(200, function(){$('#mask').remove();});

		}

		function popup() {

			// variabili
			var s = "";
			var is_type = "";
			var btClosePosLeft = $(document).width()-220;

			// vediamo cosa abbiamo passato
			if (defaults.msg != "") {
				is_type = "msg";
			} else {
				if (defaults.img != "") {
					is_type = "img";
				} else {
					if (defaults.url != "") {
						is_type = "url";
					}
				}
			}

			// se non abbiamo passato nulla si esce
			if (is_type == "") return false;



			// correzione ie6 (grrrrr!)
			var ua = $.browser;

			// creiamo gli elementi da aggiungere
			s = "<div id='mask' style='position:absolute; left:0; top:0; z-index:9000;'></div>";
			var close = "<div id='pop_close'>&nbsp;</div>";
			switch (is_type) {
				case "url":
					s += "<div id='popup' style='position:fixed;z-index:10000;'>"+close+"</div>";
				break;
				case "img":
					s += "<div id='popup' style='padding:10px; background:#ffffff;display:none;z-index:10000;'>"+close+"<img src='" + defaults.img + "' /></div>";
				break;
				case "msg":
					s += "<div id='popup' style='width:" + defaults.width + "; padding:10px; background:#ffffff;display:none;z-index:10000;'>" + close + defaults.msg + "</div>";
				break;
			}
			$('body').append(s);

			// eventi
			$('#mask').click(function () {
				destroy();
			});

			$('#pop_close').click(function () {
				destroy();
			});

			// assegniamo
			switch (is_type) {
				case "url":
					$.post(defaults.url, defaults.post, function(data) {
						//$('#popup').empty().html(data);
						$('#popup').append(data);
						show();
					});
				break;
				case "img":
				case "msg":
					show();
				break;
			}

		}

		return this.each(function() {
			popup();

			// rendiamo pubblica una funzione per essere richiamata
			// anche dall'esterno
			$.fn.close_popup = function() {
				destroy();
			}
		});
	};
})(jQuery);

(function($) {
})(jQuery);


