﻿//0 = disabled; 1 = enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup(file,param)
{
	if(popupStatus==0){		
		//carga el modulo seleccionado
		$.ajax({
			async: false,
			type: 'POST',
			url : '/inc/aj/' + file,
			data: param,
			success: function(data)
			{
				$('#dvPopup').html(data);
			}
		});
	
		centerPopup();
		$('#dvPopupBg').css({
			'opacity': '0.4'
		});
		$('#dvPopupBg').fadeIn('slow');
		$('#dvPopup').fadeIn('slow');

		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup()
{
	if(popupStatus==1){
		$('#dvPopupBg').fadeOut('slow');
		$('#dvPopup').fadeOut('slow');
		$('#dvPopup').html('');
		popupStatus = 0;
	}
}

//centering popup
function centerPopup()
{
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $('#dvPopup').height();
	var popupWidth = $('#dvPopup').width();
	var bodyScrollTop = $('html, body').scrollTop();
	
	//centering
	$('#dvPopup').css({
		'position': 'absolute',
		//'top': windowHeight/2-popupHeight/2+bodyScrollTop,
		'top': 100+bodyScrollTop,
		'left': windowWidth/2-popupWidth/2
	});
	
	//only need force for IE6
	$('#dvPopupBg').css({
		'height': windowHeight
	});
}

$(document).ready(function(){
	//Click out event!
	$('#dvPopupBg').click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});