var popupStatus = 0;

function getUrlVars(vurl) {
	if ( vurl ) {
		var map = {};
		var parts = vurl.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			map[key] = value;
		});
		return map;
	}
}

function loadPopup(){
	if(popupStatus==0){
		$("#popup_modal_backdrop").css({
			"opacity": "0.7"
		});
		$("#popup_modal_backdrop").show();
		$("#popup_modal").show();	
		popupStatus = 1;
	}
}

//disabling popup with jQuery magic!
function disablePopup(){ //disables popup only if it is enabled
	if(popupStatus==1){
		document.getElementById('popup_modal_contents').innerHTML = '';
		$("#popup_modal_backdrop").hide();
		$("#popup_modal").hide();
		popupStatus = 0;
	}
}

$.fn.centerInClient = function(options) {
	/// <summary>Centers the selected items in the browser window. Takes into account scroll position.
	/// Ideally the selected set should only match a single element.
	/// </summary>    
	/// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
	/// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
	///  and attached to the body element to ensure proper absolute positioning. 
	/// Be aware that this may cause ID hierachy for CSS styles to be affected.
	/// </param>
	/// <returns type="jQuery" />
	var opt = { forceAbsolute: true,
				container: window,    // selector of element to center in
				completeHandler: null
			  };
	$.extend(opt, options);
   
	return this.each(function(i) {
		var el = $(this);
		var jWin = $(opt.container);
		var isWin = opt.container == window;
		
		// force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.appendTo("body");
            else
                el.appendTo(jWin.get(0));
        }
		
		// force to the top of document to ENSURE that 
		// document absolute positioning is available
		/*
		if (opt.forceAbsolute) {
			if (isWin)
				el.remove().appendTo("body");
			else
				el.remove().appendTo(jWin.get(0));
		}
		*/
		
		// have to make absolute
		el.css("position", "absolute");

		// height is off a bit so fudge it
		var heightFudge = isWin ? 2.0 : 1.8;

		var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
		var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;

		el.css("left", x + jWin.scrollLeft());
		el.css("top", y + jWin.scrollTop());

		// if specified make callback and pass element
		if (opt.completeHandler)
			opt.completeHandler(this);
	});
}

$(document).ready(function(){
	// Open the admin login dialog box
	$(".modal_popup").click(function(ev) {
		ev.preventDefault();
		var popupTitle = this.title;
		var popupPath = this.href;
		var popupRel = this.rel;
		var popupFocus = '';
		aURL_Vars = getUrlVars(popupPath);
		if ( aURL_Vars['popfocus'] ) popupFocus = aURL_Vars['popfocus'];
		$("#popup_modal_title").html(popupTitle);
		while(popupTitle.indexOf(' ')!=-1) popupTitle = popupTitle.replace(' ','|');
		if ( popupRel == "iframe" ) {
			$("#popup_modal_contents").html("<iframe src=\"" + popupPath + "\" width=\"600\" height=\"400\" frameborder=\"0\"></iframe>");
		} else {
			$("#popup_modal_contents").load(popupPath,{ cache: false });
		}
		loadPopup();
		$("#popup_modal").centerInClient();
	});
	
	$("#popup_modal_close").click(function(){
		disablePopup();
	});
	$("#popup_modal_backdrop").click(function(){
		disablePopup();
	});
	$('#popup_modal_cancel').live('click', function() {
		disablePopup();
	});
	//	Pressing escape
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});
});
