/**
 * Requires: jquery.js, blockUI.js, json.js
 */
var blockUIDelegate;
$(document).ready(function(){

	if(!window.parent.blockUI || window.parent == window){
		$.blockUI.defaults.message = $('<div><div style="display:none;">Please Wait</div></div>').children();
	}
});

function blockUIConfigure(){
	blockUIDelegate = {
		okButton: $('<button style="float:right;">OK</button>'),
		cancelButton: $('<button style="float:right;">Cancel</button>'),
		displayDiv: $('<div><div style="display:none;"></div></div>').children()
	}
}

/**
 * Calls the blockUI delegate on every parent window, until it reaches a parent window that does
 * not have the BlockUIDelegate.js included in the page. Once that page is hit, the previous window will
 * process the options and show the message in a blocked UI.
 * options = {message: messageContents,
 *			  css: cssObject}
 *	 MessageContents - is an HTML representation of the message that should be displayed in the
 *					   blockUI.
 *	 cssObject - is the css object as defined by the blockUI.
 *	 
 * 	 FYI: all but the message contents from the original blockUI documents are carried over as
 * 	 	  is in the object. So it is possible to use any of the other objects like the cssObject
 *		  that were previously defined in the blockUI documents(http://www.malsup.com/jquery/block/#options).
 */
function blockUI(options, onOK, onCancel){
	var innerArguments = arguments;
	if(window.parent.blockUI && window.parent != window){
		if(options && !(options instanceof String || typeof(options) == 'string')){
			if(options.message && !(options.message instanceof String || typeof(options.message) == 'string')){
				var parent = options.message.parentNode;
				var message = options.message;
				var display = options.message.style.display;
				options.message.style.display = '';
				options.message = $('<div></div>').append(options.message).html();
				parent.appendChild(message);
				message.style.display = display;
			}
			var css;
			if(options.css){
				css = options.css
			}
			options.css = new Object();
			for(p in $.blockUI.defaults.css)
				options.css[p] = $.blockUI.defaults.css[p];
			if(css){
				for(p in css)
					options.css[p] = css[p];
			}
			options = $.toJSON(options);
		}
		var newOK;
		if(onOK){
			newOK = function(param){
				if(!innerArguments[3] && (param instanceof String || typeof(param) == 'string')){
					onOK($.parseJSON(param));
				}else{
					onOK(param);
				}
			};
		}
		window.parent.blockUI(options, newOK, onCancel, true);
	}else{
		// parse the blockUI options
		if(options && (options instanceof String || typeof(options) == 'string')){
			var options = $.parseJSON(options);
		}
			
		var newOK;
		if(onOK){
			newOK = function(param){
				if(innerArguments[3] && !(param instanceof String || typeof(param) == 'string')){
					onOK($.toJSON(param));
				}else{
					onOK(param);
				}
			};
		}
		blockUIThisFrame(options, newOK, onCancel);
	}
}

function blockUIThisFrame(options, onOK, onCancel){

	if(!blockUIDelegate){
		blockUIConfigure();
	}
	
	// parse the blockUI options
	if(options){
		// and insert the innerHTML in the innerHTML div.
		if(options.message){
			blockUIDelegate.displayDiv.empty();
			blockUIDelegate.displayDiv.append('<div id="innerHTML"></div>').children().append(options.message);
			if(options.message.style){
				options.message.style.display = '';
			}
			options.message = blockUIDelegate.displayDiv;
		}
	}
			
	// If there is a cancel callback, add the cancel button to the UI
	if(onCancel){
		blockUIDelegate.displayDiv.append(blockUIDelegate.cancelButton);
		blockUIDelegate.cancelButton.click(function(){
			onCancel();
		});
	}
	
	// If there is an ok callback, add the ok button to the UI
	if(onOK){
		blockUIDelegate.displayDiv.append(blockUIDelegate.okButton);
		blockUIDelegate.okButton.click(function(){
			var param = new Object();
			$('#innerHTML').find('input,textarea').each(function(){
				param[this.name] = this.value;	
			}).end().find('select').each(function(){
				param[this.name] = this.selectedIndex;
			});
			onOK(param);
		});
	}
	
	// Display the block UI.
	$.blockUI(options);
}

/**
 * Calls the unblockUI delegate on every parent window, until it reaches a parent window that does
 * not have the BlockUIDelegate.js included in the page. Once that page is hit, the previous window will
 * unblock the UI.
 */
function unblockUI(){
	if(window.parent.blockUI && window.parent != window){
		$.unblockUI();
		window.parent.unblockUI();
	}else{
		$.unblockUI();
	}
}
