jQuery(document).ready(function() {
	// create and display the dialog on click
	jQuery('.cuwindow').click(function() {
		var href = jQuery(this).attr('href');
		if(href.indexOf('#') == -1)
		{
			href = addModalParameter(href);
			// ajax link
			var now = new Date();
			var ticks = now.getTime();
			var dialogID = 'dialog' + ticks;
			jQuery('body').append('<div id="' + dialogID + '"></div>');
			jQuery('#' + dialogID).load(href, function() {
				var pageTitle = jQuery('#' + dialogID + ' h1').hide().text();
				jQuery('#' + dialogID).dialog({
					width: 450,
					modal: true,
					title: pageTitle,
					buttons: {"Close": function() { jQuery(this).dialog("close"); }}
				});
			});
		}
		else
		{
			// hidden div
			var pageTitle = jQuery(href + ' h1').hide().text();
			jQuery(href).dialog({
				width: 450,
				modal: true,
				title: pageTitle,
				buttons: {"Close": function() { jQuery(this).dialog("close"); }}
			});
		}
		return false;
	});
});

function addModalParameter(href)
{
	// add modal=1 (do this with JS so non-JS users will get the full version of the page
	var ret = href;
	if (ret.indexOf('?') > 0) {
		ret=ret.concat('&');
	} else {
		ret=ret.concat('?');
	}
	ret=ret.concat('modal=1');
	return ret;
}
