jQuery(document).ready(function(){
	jQuery('#user-register').click(function(){
		var page = getPageSize();
		jQuery('body').append('<div id="faden" style="display: none;"></div>');
		jQuery('#faden').css({
								position: 'absolute',
								top: 0,
								left: 0,
								width: jQuery(document).width() + 'px',
								height: jQuery(document).height() + 'px',
								opacity: 0,
								backgroundColor: '#000'
							});
							
		jQuery('#overlay_window').css({
								top: parseInt(page[3]/2) - parseInt(jQuery('#overlay_window').height()/2) + parseInt(jQuery(document).scrollTop()) + 'px',
								left: parseInt(page[2]/2) - parseInt(jQuery('#overlay_window').width()/2) + parseInt(jQuery(document).scrollLeft()) + 'px'
							  });
		
		jQuery('#overlay_window').slideUp();
							  
		jQuery('#faden').show().animate({opacity:0.7}, 500, function(){
			jQuery('#overlay_window').slideDown(500);
		});				

		return false;
	});
	
	jQuery('#faden').live('click', function(){
		closeOverlay();	
	});
	
	jQuery('#overlay_close').click(function(){
		closeOverlay();
		return false;
	});
	
	jQuery('#overlay_submit').click(function(){
		var noValid = false;
		jQuery('#overlay_shadow_body input[type=text]').each(function(){
			if (jQuery.trim(jQuery(this).val()) == ''){
				noValid = true;				
			}	
		});

		if (noValid){
			alert('Все поля обязательны для заполенения!');
			return false;
		}
		
		jQuery('#overlay_shadow_body input').each(function(){
			jQuery(this).attr('disabled', 'disabled');				
		});
		
		jQuery.post('/mail.php',
					{
						email: jQuery('#email').val(), 
						name: jQuery('#name').val(),
						company: jQuery('#company').val(),
						city: jQuery('#city').val(),
						phone: jQuery('#phone').val()
					},
					function(){
						closeOverlay();		
						/*if (jQuery('#download').attr('href')){
							window.location = jQuery('#download').attr('href');	
						}*/
					}
					);	
		return false;
	});
});

function closeOverlay(){
	jQuery('#overlay_window').slideUp(500, function(){
		jQuery('#faden').animate({opacity:0}, 500, function(){
			jQuery('#faden').remove();
		});	
	});		
}

function  getPageSize(){
	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else if (document.documentElement && document.documentElement.scrollHeight > document.documentElement.offsetHeight){ // Explorer 6 strict mode
		xScroll = document.documentElement.scrollWidth;
		yScroll = document.documentElement.scrollHeight;
	} else { // Explorer Mac...would also work in Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) { // all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}

	return [pageWidth,pageHeight,windowWidth,windowHeight];
}