var popup_sample_status = 0;

function get_sample_UrlVars(vurl) {
	if ( vurl ) {
		var map = {};
		var parts = vurl.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
			map[key] = value;
		});
		return map;
	}
}

function load_popup_sample() { //loads popup only if it is disabled
	if ( popup_sample_status == 0 ) {
		$("#popup_sample_backdrop").css({
			"opacity": "0.7"
		});
		$("#popup_sample_backdrop").show();
		$("#popup_sample").show();
		popup_sample_status = 1;
	}
}

//disabling popup with jQuery magic!
function disable_popup_sample() { //disables popup only if it is enabled
	if ( popup_sample_status == 1 ) {
		document.getElementById('popup_sample_contents').innerHTML = '';
		$("#popup_sample_backdrop").hide();
		$("#popup_sample").hide();
		popup_sample_status = 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() {
	$('.sample_link').css("visibility","visible");
	$('.sample_link').live('click', function(e) {
		$("#popup_sample").width(325);
		$("#popup_sample").height(180);
		e.preventDefault();
		var popup_sample_title = this.title;
		var popup_sample_path = this.href;
		var popup_sample_focus = '';
		aURL_Vars = get_sample_UrlVars(popup_sample_path);
		if ( aURL_Vars['popup_sample_focus'] ) popup_sample_focus = aURL_Vars['popup_sample_focus'];
		$("#popup_sample_title").html(popup_sample_title);
		//$("#popup_sample_contents").attr("src","themes/"+popup_sample_path+"/pop_up.php?area="+popup_sample_title);
		while(popup_sample_title.indexOf(' ')!=-1) popup_sample_title = popup_sample_title.replace(' ','|');
		$("#popup_sample_contents").load(popup_sample_path,{ cache: false });
		load_popup_sample();
		/* Can't get this to work yet - needs to set focus on field in pop up
		if ( popupFocus ) {
			$("#popup_contents").popupFocus.focus();
		}
		*/
		$("#popup_sample").centerInClient();
	});
	$("#popup_sample_close").click(function(){
		disable_popup_sample();
	});
	$("#popup_sample_backdrop").click(function(){
		disable_popup_sample();
	});
	$('#popup_sample_cancel').live('click', function() {
		disable_popup_sample();
	});
	//	Pressing escape
	$(document).keypress(function(e){
		if(e.keyCode==27 && popup_sample_status==1){
			disable_popup_sample();
		}
	});
});

