var popupStatus = 0;	// 0 = means disabled | 1 = means enabled;
var popupImages = new Array("null.gif","alert.gif","error.gif","help.gif"); // 0 = no image | 1 = alert | 2 = error | 3 = help

//*********************************************
// Show pop-up
//*********************************************
function showPopup(w, h, img, title, content, align, buttonOk, buttonCancel, functionOk, functionCancel){
	if(popupStatus==0){
		// Load Content
		loadContent(img, title, content, align, buttonOk, buttonCancel, functionOk, functionCancel);
		
		// Display popup
		displayPopup();
		
		// Modify measures
		modifyMeasures(w, h);
		
		// Centering with css
		centeringPopup();	
		
		// Status pop-up enabled
		popupStatus = 1;
	}
}

//*********************************************
// Loading content pop-up
//*********************************************
function loadContent(img, title, content, align, buttonOk, buttonCancel, functionOk, functionCancel){
	
	if(title != null && title != '') {
		$("#popupTitle").css("display","block");
		$("#popupTitle").html(title);	
	}else {
		$("#popupTitle").css("display","none");
		$("#popupTitle").html('');	
	}

	if(img != null && img != 0) {
		$("#popupImage").css("display","block");
		$("#popupImage").html("<img src='./plugins/popup/"+popupImages[img]+"'>");
	}else {
		$("#popupImage").css("display","none");
		$("#popupImage").html('');		
	}

	if(align != null && align != '') {
		$("#popupContent").css("text-align",align);
	}
	$("#popupContent").html(content);	
	
	var buttons = '';
	if(buttonOk != null && buttonOk != '') {
		if(functionOk != null && functionOk != '') {
			buttons += '<input type="button" name="'+buttonOk+'" value="'+buttonOk+'" class="button_Ok" onClick="'+functionOk+'">';
		}else {
			buttons += '<input type="button" name="'+buttonOk+'" value="'+buttonOk+'" class="button_Ok">';
		}		
	}
	if(buttonCancel != null && buttonCancel != '') {
		if(functionCancel != null && functionCancel != '') {
			buttons += '<input type="button" name="'+buttonCancel+'" value="'+buttonCancel+'" class="button_Cancel" onClick="'+functionCancel+'">';
		}else {
			buttons += '<input type="button" name="'+buttonCancel+'" value="'+buttonCancel+'" class="button_Cancel">';
		}		
	}	
	
	if((buttonOk != null && buttonOk != '') || (buttonCancel != null && buttonCancel != '')) {
		$("#popupButtons").css("display","block");
		$("#popupButtons").html(buttons);
	}else {
		$("#popupButtons").css("display","none");
		$("#popupButtons").html('');		
	}
}

//*********************************************
// Display pop-up (only if it is disabled)
//*********************************************
function displayPopup(){
	$("#popupBackground").css({"opacity": "0.7"});
	$("#popupBackground").fadeIn("slow");
	$("#popup").fadeIn("slow");
}

//*********************************************
// Modify measures pop-up
//*********************************************
function modifyMeasures(w, h) {
	$("#popup").css("width", w);
	$("#popup").css("height",h);
}

//*********************************************
// Centering pop-up
//*********************************************
function centeringPopup(){
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#popup").height();
	var popupWidth = $("#popup").width();

	$("#popup").css({"position": "absolute","top": windowHeight/2-popupHeight/2,"left": windowWidth/2-popupWidth/2});
	$("#popupBackground").css({"height": windowHeight});	//only need force for IE6	
}

//*********************************************
// Hide pop-up (only if it is enabled)
//*********************************************
function hidePopup(){	
	if(popupStatus==1){
		$("#popupBackground").fadeOut("slow");
		$("#popup").fadeOut("slow");
		popupStatus = 0;
	}
}

//*********************************************
// Controlling events
//*********************************************
$(document).ready(function(){

	// Click the button event!
	$("#button").click(function(){
			showPopup(w, h, img, title, content, align, buttonOk, buttonCancel, functionOk, functionCancel);
	});

	// Click the image close event!
	$("#popupClose").click(function(){
			hidePopup();
	});
	
	// Click out event!
	$("#popupBackground").click(function(){
			hidePopup();
	});
	
	// Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			hidePopup();
		}
	});

});