


$(function() {

	// open and close dialogs
	$('a[rel^=dialog]').click(function() {

		// zoek de dialog obv een div met een class 'dialog' en alles wat na 'dialog[' komt 
		// in het rel attribuut van de anchor.
		//
		var target = $(this).attr('rel'); 
		target = target.substring(7, target.length - 1);
		var dialog = $('div.dialog.' + target);
		
		// als de href is ingevuld ga er dan vanuit dat de dialog nog opgehaalt moet worden
		var action = $(this).attr('href'); 
		if (action != null && action.length > 1) {
		    var innerHtmlId = ' #container-' + target;
		    var action = action + innerHtmlId;
			dialog.load(action, function() {
    		    addCloseFunction(dialog);
            });
            
            
		} 
		
		$('body').append(dialog);

		var top = ($(window).height() - dialog.height()) / 2;
		var left = ($(window).width() - dialog.width()) / 2;
		dialog.css('top', top);
		dialog.css('left', left);
    	
    	dialog.show('fast');
    		
        addCloseFunction(dialog);
  		
		return false;
	});
});

function addCloseFunction(dialog) {
    var hideTrigger = function() {
		dialog.hide('fast');
		$('div.overlay').remove();
		$('body').css('overflow', 'visible');

		return false;
	}
    
	$('<a />').addClass('close').appendTo(dialog).click(hideTrigger);
	$('button[type=reset]', dialog).click(hideTrigger);
}
