﻿$(function() {
    // Style Switcher
    $('#switcher').switcher();
    $('#switcher').show('slow');
    
	// Loading Dialog
	$('#loading-dialog').dialog({
		dialogClass: 'noclose',
		autoOpen: false,
		bgiframe: true,
		closeOnEscape: false,
		resizable: false,
		draggable: false,
		modal: true,
		width: 247,
		open: function() {
			// jQueryUI ignores low Height option during initialization. 
			// We call it here as a workaround
			$('#loading-dialog').dialog('option', 'height', 77);
		}
	});
	
	// Message Dialog
	$('#message-dialog').dialog({
		autoOpen: false,
		bgiframe: true,
		resizable: false,
		width: 400,
		buttons: {		    
			OK: function() {
			    $(this).dialog('close');
			}
		}
	});
});

// Makes an AJAX call, complete with progress dialog and timeout
jQuery.masterAjax = function(p) {
    if (typeof p.timeout == 'undefined') jQuery.extend(p, { timeout: 10000 });
    
    jQuery.extend(p, { loading: function() { jQuery('#loading-dialog').dialog('open'); } });
    jQuery.extend(p, { complete: function() { jQuery('#loading-dialog').dialog('close'); } });
    
    return jQuery.pageAjax(p);
};

// Shows a Message Dialog
jQuery.masterDialog = function(p) {
	// Invalid params?
	if (typeof p.message != 'string') return;

	// Call BeforeOpen() function
	if (typeof p.beforeOpen == 'function') p.beforeOpen();
	
	// Bind DialogClose() event
	jQuery('#message-dialog').unbind('dialogclose');
	if (typeof p.afterClose == 'function') jQuery('#message-dialog').bind('dialogclose', p.afterClose);
	
	// Set options
	if (typeof p.modal == 'boolean') jQuery('#message-dialog').dialog('option', 'modal', p.modal);
	if (typeof p.title == 'string') jQuery('#message-dialog').dialog('option', 'title', p.title);
	if (typeof p.buttons == 'object') jQuery('#message-dialog').dialog('option', 'buttons', p.buttons);
	
	// Show dialog
	jQuery('#message-dialog').html(p.message).dialog('open');
};