// JavaScript Document

// requirements;
// The jcpopup.css file is required

// parms:
//	'refreshAfterClose' if set to true will reload the current page when the window is closed.  Default is false.
//  'loadFrom' The url of a php script file that will generate html to prepopulate the pop-up window.
//	'afterLoad' The name of a javascript function to execute after the page html loads.

// example use:
//	$('#popupContact').jcPopup({
//		refreshAfterClose: true,
//	 	loadFrom: 'ajaxProfileMaint.php'
//	});
jQuery.fn.jcPopup = function( o ){
	
	var refreshAfterClose = false;
	var loadFrom = '';
	var afterLoad = '';
	var doingReload = false;
		
	//SETTING UP OUR POPUP
	//0 means disabled; 1 means enabled;
	var popupStatus = 0;
	
	// populate the passed parms...
	if( o != undefined ){
		for(key in o){
			eval(key + ' = \'' + o[key] + '\'');
		}
	}
	
	// the name of the popup div...
	var name = $(this).attr('id');
	
	// construction...

	// add class for styling...
	$(this).addClass('jcpopup');
//	
//	// use ajax to populate the window...
//	if(loadFrom != ''){
//		$(this).load(loadFrom, function(){
//			$(this).prepend('<a class="popupClose" title="Close.">x</a>');
//			$(".popupClose, .popupCloseLink").click(function(){
//				disablePopup();
//			});
//			if(afterLoad != ''){
//				eval(afterLoad);
//			}
//		});
//	}
	
	// add the close link...
	$(this).prepend('<a class="popupClose" title="Close.">x</a>');

	
	//LOADING POPUP
	//Click the button event!
	$("#" + name + "Trigger").click(function(){
		//centering with css
		centerPopup();
		//load popup
		loadPopup();
		return false;
	});
	
	$(window).bind('resize', function(){
		if(popupStatus == 1) {
			centerPopup();
		}
	});
	
	//CLOSING POPUP
	//Click the x event!
	$(".popupClose, .popupCloseLink").click(function(){
		disablePopup();
	});
	//Click out event!
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==13) { return; }
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
	
	if(processFlag == 'true'){
		loadPopup( processFlag );
	}
	////////////////////// functions /////////////////////////
	
	//loading popup with jQuery magic!
	function loadPopup( processFlag ){
		
		//loads popup only if it is disabled
		if(popupStatus==0){
			// use ajax to populate the window...
			if(loadFrom != ''){
				$('#' + name).load(loadFrom, {processFlag: processFlag},function(){
					$('#' + name).prepend('<a class="popupClose" title="Close.">x</a>');
					$(".popupClose, .popupCloseLink").click(function(){
						disablePopup();
					});
					$("#backgroundPopup").css({
						"opacity": "0.7"
					});
					$("#backgroundPopup").fadeIn("slow");
					$("#" + name).fadeIn("slow");
					if(afterLoad != ''){
						eval(afterLoad);
					}
					centerPopup();
				});
			}
			
			popupStatus = 1;
		}
	}
	
	//disabling popup with jQuery magic!
	function disablePopup(){
		//disables popup only if it is enabled
		if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$("#" + name).fadeOut("slow");
			popupStatus = 0;
			if(refreshAfterClose == 'true'){
				location.reload();
			}
		}
	}
	
	//centering popup
	function centerPopup(){
		//request data for centering
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#" + name).height();
		var popupWidth = $("#" + name).width();
		//centering
		$("#" + name).css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		//only need force for IE6
		
		$("#backgroundPopup").css({
			"height": windowHeight
		});
		
	}

}
