(function($){
     $.fn.extend({
          center: function (options) {
               var options =  $.extend({ // Default values
                    inside: window, // element, center into window
                    transition: 0, // millisecond, transition time
                    minX:0, // pixel, minimum left element value
                    minY:0, // pixel, minimum top element value
                    withScrolling: true, // booleen, take care of the scrollbar (scrollTop)
                    vertical: true, // booleen, center vertical
                    horizontal: true, // booleen, center horizontal
                    show: true
               }, options);
               return this.each(function() {
                    var props = {position:'absolute'};
                    
                    if (options.vertical) {
                         var top = ($(options.inside).height() - $(this).outerHeight()) / 2;
                         if (options.withScrolling) top += $(options.inside).scrollTop() || 0;
                         top = (top > options.minY ? top : options.minY);
                         $.extend(props, {top: top+'px'});
                    }
                    if (options.horizontal) {
                          var left = ($(options.inside).width() - $(this).outerWidth()) / 2;
                          if (options.withScrolling) left += $(options.inside).scrollLeft() || 0;
                          left = (left > options.minX ? left : options.minX);
                          $.extend(props, {left: left+'px'});
                    }
                    if (options.transition > 0) $(this).animate(props, options.transition);
                    else $(this).css(props);
                    
                    if (options.show)
                    	$(this).show();
                    
                  
                    
                    return $(this);
               });
          }
     });
})(jQuery);

(function($)
{
    $.fn.extend(
    		{
    			centerModal: function ()
    			{
    				return this.each(function()
    					{
    						var props = {position:'absolute'};
                   
    						var h_windows = $(window).height();
    						var h_scrcollTop = $(window).scrollTop();
    						var top = (h_windows - $(this).outerHeight()) / 2;
    						top = (top < 0 ) ? 0 : top;
    						top += h_scrcollTop;
    						$.extend(props, {top: top + 'px'});
                	   
    						var w_windows = $(window).width();
    						var w_scrcollLeft = $(window).scrollLeft();
    						var left = (w_windows - $(this).outerWidth()) / 2;
    						left = (left < 0 ) ? 0 : left;
    						left += w_scrcollLeft;
    						$.extend(props, {left: left + 'px'});

    						$(this).css(props);
    						return $(this);
    					}
    				);
    			}
    		}
    );
})(jQuery);

function JSandros(options)
{
	this.options = options;
	
	this.backgroundModalScreenId = options.backgroundModalScreenId;
	this.backgroundScreen = null;
	
	this.modalDiv = null;
	this.modalDivConteiner = null;
	this.modalConteinerId = options.modalConteinerId,
	this.modalDivInner = null;
	this.modalInnerId = options.modalInnerId,
	this.modalId = options.modalId;
	this.modalCloseButton = null;
	this.modalCloseButtonId = options.modalCloseButtonId;
	
	this.callback = null;
	
	this.init = function()
	{
		_createDiv(options.backgroundModalScreenId, 'body');
		this.backgroundScreen = $('#' + options.backgroundModalScreenId);
		
		this.modalDiv = $('#' + options.modalId);
		this.modalCloseButton = $('#' + options.modalCloseButtonId);
		this.modalDivConteiner = $('#' + options.modalConteinerId);
		this.modalInnerId = $('#' + options.modalInnerId);
		this.modalDivInner = $('#' + options.modalInnerId);
	};
	
	this.showBackgroundModalScreenId = function()
	{
		_adjustBackgroundModalHeight.call(this);
		this.backgroundScreen.show();
	};
	
	this.hideBackgroundModalScreenId = function()
	{
		this.backgroundScreen.hide();
	};
	
	this.showModal = function()
	{
		var obj = this;
		
		_modal_event.call(this);
		
		this.modalDiv.show();
		this.centerModal();
		this.showBackgroundModalScreenId();
	};
	
	function _adjustBackgroundModalHeight()
	{
		var h_max = 0;
		var h_windows = $(window).height(true);
		var h_body = $('body').outerHeight(true);
		var h_modal = this.modalDiv.outerHeight(true);
		if ( h_modal > h_body )
		{
			if ( h_modal > h_windows)
				h_max = h_modal;
			else
				h_max = h_windows;
		}
		else
		{
			if ( h_body > h_windows)
				h_max = h_body;
			else
				h_max = h_windows;
		}
			
		//h_max += $(window).scrollTop();
		
		if ( (h_windows + $(window).scrollTop()) > h_max )
			h_max = h_windows + $(window).scrollTop();
		
		this.backgroundScreen.css('height', h_max + 'px' );	
	}
	
	this.centerModal = function()
	{
		this.modalDiv.centerModal();
	};
	
	this.hideModal = function()
	{
		this.modalDiv.hide();
		_unbind_modal_event.call(this);
		this.hideBackgroundModalScreenId();
	};
	
	function _createDiv(elemId, append)
	{
		$(append).append('<div id="' + elemId + '"></div>');
	}
	
	function _modal_event()
	{
		var obj = this;
		this.modalCloseButton.click(function(){ obj.hideModal(); return false;} );
		//this.backgroundScreen.click( function(){ obj.hideModal(); } );
	}
	
	function _unbind_modal_event()
	{
		this.modalCloseButton.unbind('click');
	}
}

function GrafillJS(options)
{
	this.options = options;
	
	this.init = function()
	{
		this.JSandros = new JSandros(this.options);
		this.JSandros.init();
	};
	
	this.showModal = function(){ this.JSandros.showModal(); }
	
	this.hideModal  = function(){ this.JSandros.hideModal(); }
}

var $GrafillJS;
			
$(document).ready(
	function()
	{
		var callcenter_request = 
			'<div id="modal">' +
				'<div id="modalClose">' +
					'<div id="modalCloseButton"><a href="#">CHIUDI&nbsp;&nbsp; X</a></div>' +
				'</div>' +
				'<div id="modalContainer">' +
					'<div id="modalInner">' +
						'<div style="padding-top: 140px; padding-left: 8px; ">' +
							'<span style="font-size: 12px; color: #1f1d1d;"><b>Inserisci qui il tuo numero di telefono</b></span><br />' +
							'<input id="callcenter_send_request_input" type="text" value="" name="num" style="width: 203px; height: 36px; border: 0; padding: 0; margin: 0; margin-top: 5px; margin-bottom: 10px; padding-left: 10px; background: url(\'/files_callcenter/callcenter_input.png\');"/><br />' +
							'<span style="font-size: 12px; color: #1f1d1d;"><b>Inserisci qui il tuo Nome e Cognome (facoltativo)</b></span><br />' +
							'<input id="nome_cognome" type="text" value="" name="nome_cognome" style="width: 203px; height: 36px; border: 0; padding: 0; margin: 0; margin-top: 5px; margin-bottom: 10px; padding-left: 10px; background: url(\'/files_callcenter/callcenter_input.png\');"/><br />' +
						'</div>' +
						'<div style="background: url(\'/files_callcenter/sfondo_callcenter_bottom.png\') no-repeat 0 20px #ffffff; padding-top: 10px;  padding-left: 8px; height: 90px;">' +
							'<input id="callcenter_send_request" type="button" value="" name="num" style="width: 192px; height: 41px; padding: 0; margin: 0; border: 0px none; cursor: pointer; background: url(\'/files_callcenter/chiamami.png\');"/>' +
						'</div>' +
					'</div>'+
				'</div>' +
			'</div>';
		$('body').append(callcenter_request);
			
		var options = 
		{
			backgroundModalScreenId 		: 	'backgroundModal',
			modalId 						: 	'modal',
			modalConteinerId 				: 	'modalContainer',
			modalInnerId 					: 	'modalInner',
			modalCloseButtonId 				:	'modalCloseButton',
		}
						
		$GrafillJS = new GrafillJS(options);
		$GrafillJS.init();
		
		$('#callcenter_chiamata').click(
				function()
				{
					$GrafillJS.showModal();
					return false;
				}
			);
			
		$('#callcenter_send_request').click(
			function()
			{
				if ( $('#callcenter_send_request_input').val() != '' )
				{
					$.ajax({
						type: 'POST',
						url: '/callcenter_send_request.php',
						data: {num : $('#callcenter_send_request_input').val(), nome_cognome: $('#nome_cognome').val() },
						success: function(response){ if (response == 'success') { alert('Sarai contatto al pių presto da un nostro operatore'); $('#callcenter_send_request_input').val(''); $GrafillJS.hideModal(); } else alert('Siamo spiacenti, si č verificato un errore, riprovare fra qualche minuto.'); return false; },
						dataType: 'text'
					});
				}
				
				return false;
			}
		
		);
		
		if(window.location.hash == '#chiamami') 
			$GrafillJS.showModal();
	}
);
	
