/***********************/
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus		= 0;
var idPopUP			="#PopupDiv";
var idPopUPBg		="#PopupBackground";
var idPopUPContent	="#PopContent";
var idStartPop		="#ShowPopUpDiv";
var idStopPop		="#PopupDivClose";
var idStopPop2		="#PopupDivClose2";
var	urlAjax			="ajax.php";
var url				='ajax.php';


var boUpdatedPage	=false;

//loading popup with jQuery magic!
function loadPopup(){
	//loads popup only if it is disabled
	if(popupStatus==0){
		boUpdatedPage=false;
		$(idPopUPBg).css({
			"opacity": "0.7"
		});
		$(idPopUPBg).fadeIn("slow");
		$(idPopUP).fadeIn("slow");
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){
	//disables popup only if it is enabled
	if(popupStatus==1){
		$(idPopUPBg).fadeOut("slow");
		$(idPopUP).fadeOut("slow");
		popupStatus = 0;

	}
	$(idPopUPContent).html("");
	if(boUpdatedPage)
	{
		location.reload();
	}
}

//centering popup
function centerPopup(){
	//request data for centering
	var windowWidth		= document.documentElement.clientWidth;
	var windowHeight	= document.documentElement.clientHeight;
	var popupHeight		= $(idPopUP).height();
	//var popupWidth		= $(idPopUP).width();
	var popupWidth		= 800;

	//var popupOffset		= document.body.scrollTop;	
	var popupOffset		= document.documentElement.scrollTop;		
	//alert(document.body.scrollTop);
	//centering
	$(idPopUP).css({
		"position": "absolute",
		"top": 200 + popupOffset,
		"left": windowWidth/2-popupWidth/2
	});
	//windowHeight/2-popupHeight/2
	//only need force for IE6
	
	$(idPopUPBg).css({
		"height": windowHeight
	});
}
// manual start
function ShowPopUp(parameters)
{
	
	GetNewPopUpContent(parameters);
	//centering with css
	centerPopup();
	//load popup
	loadPopup();
	return false;
}
function GetNewPopUpContent(parameters)
{

	$.ajax({
		type:		"GET",
		url:		url,
		data:		parameters,
		cache:		false,
		dataType:	"html",
		success: function(html){

			$(idPopUPContent).html(html);
		}
	});
}

// autostart
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
	
	//Click the button event!
	$(idStartPop).click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		return false;
	});
				
	//CLOSING POPUP
	//Click the x event!
	$(idStopPop).click(function(){
		disablePopup();
	});
	$(idStopPop2).click(function(){
		disablePopup();
	});
	//Click out event!
	$(idPopUPBg).click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
			return false;	
		}
	});

});



function PostFormInPopup(form,div,linkparam,submit)
{
	boUpdatedPage=true;
	PostForm(form,div,linkparam,submit);
}
