var $currentPopup = $(null);

$.fn.smoothPopup = function(options) {	
	//settings
	var settings = $.extend({}, jQuery.fn.smoothPopup.defaults, options);
		
	//generate a randomId
	var generateRandomPopupId = function() {
		var text = "popup-";
		var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
		for( var i = 0; i < 8; i++) text += possible.charAt(Math.floor(Math.random() * possible.length));

		return text;
	}
	
	id = $(this).attr('data-popup') != undefined ? $(this).attr('data-popup') : generateRandomPopupId();
	
	if ($('#'+id).length == 0) $('body').append($(document.createElement('div')).attr('id',id));
	$id = $('#'+id);
	
	//popup window centering
	var centerPopup = function() {
		if (settings.height != 0) $id.find('.popup-contact').css({'height' : settings.height});
		if (settings.width  != 0) $id.find('.popup-contact').css({'width' : settings.width});

		$id.find('.popup-contact').css({ 
			'left': $(window).width()/2 - $id.find('.popup-contact').width()/2,
			'top':  $(window).height()/2 - $id.find('.popup-contact').height()/2
		});
		
		$(window).resize();

		$().css({'height' : $(window).width()});
	}
	
	//hide the popup
	var hidePopup = function() {
		//if (settings.fadeOutCallback != null && !settings.fadeOutCallback()) return;
		$('.background-popup').fadeOut('slow'); 
		$id.find('.popup-contact').fadeOut('slow');
		
		$id.remove();
	}
	
	var showPopup = function() {
		
		$currentPopup = $id;
		
		//callback function
		var _callback = function() {
			//shows and center the popup after the load
			$id.find('.popup-contact').fadeIn('slow')
			   .css({ 
				'left': $(window).width()/2 - $id.find('.popup-contact').width()/2,
				'top':  $(window).height()/2 - $id.find('.popup-contact').height()/2
			});

			if (settings.fadeInCallback != null) settings.fadeInCallback();
		}
		
		$('.background-popup')
			.css({'opacity':'0.7', 'filter':'alpha(opacity=70)', '-moz-opacity':'70%'})
			.fadeIn('slow');
			
		if (settings.useAjax) 
			$id.find('.popup-contact-area').load(settings.ajaxUrl, function() { _callback(); });
		else {
		 	$id.find('.popup-contact-area').html(settings.dataDiv.html());
			_callback();
		}
		
	}
	
	if (settings.useAjax) settings.ajaxUrl = $(this).attr('href') + '.popup';
	else settings.dataDiv = $('#'+$(this).attr('data-source-id'));
	
	//create popup
	var $backgroundPopup = $(document.createElement('div')).addClass('background-popup');
	var $popupContact = $(document.createElement('div')).addClass('popup-contact');
	var $popupContactClose = $(document.createElement('div')).addClass('popup-contact-close');
	var $popupContactArea = $(document.createElement('div')).addClass('popup-contact-area');
	
	//insert elements
	$id.html('').append($popupContact).find('.popup-contact').append($popupContactClose).append($popupContactArea);
	
	if ($('.background-popup').length == 0) $('body').append($backgroundPopup);
	
	//close on background
	if (settings.closeOnBackgroundClick) $('.background-popup').click(hidePopup)
	
	//close on x click
	if (settings.showClose)
		$id.find('.popup-contact-close').html('<strong>X</strong>').click(hidePopup);
	
	//show the close button 
	if (settings.showCloseButton) {
		var button = $(document.createElement('span')).addClass('generic-button').click(hidePopup).html('Chiudi');
		$id.find('.popup-contact-area').after(button);
	}
	
	$(window).resize(function(e) { 				
		$id.find('.popup-contact').css({ 
			'left': $(window).width()/2 - $id.find('.popup-contact').width()/2,
			'top':  $(window).height()/2 - $id.find('.popup-contact').height()/2
		});		
	});
	
	showPopup();
	centerPopup();
	
	
}

//close the current popup
function closePopup() {
	$('.background-popup').fadeOut('slow'); 
	$currentPopup.find('.popup-contact').fadeOut('slow');
	$currentPopup.remove();
}

//default options
$.fn.smoothPopup.defaults = {
	ajaxUrl: "",
	dataDiv: $(null),
	useAjax: false,
	showClose: true,
	showCloseButton: false,
	closeOnBackgroundClick: true,
	fadeInCallback: null,
	fadeOutCallback: null,
	width: 0,
	height: 0,
}
