﻿//acts as a dictionary for modal dialogs using unique identifier as the key
var modalDialogRegistry = function(){
	var dialogs = {};
	var add = function(key, reference){
		dialogs[key] = reference;
	};
	var find = function(key){
		return dialogs[key];
	};
	return {
		add: add,
		find: find
	};
}();

/*
	Requires a javascript object that defines
	
		REQUIRED
		id : [a guid that uniquely identifies this modal dialog helper instance]
		$modalDialog : [jQuery collection with single item referencing the div that will act as a modal dialog]
		errorUrl : [url of an error page]
		defaultTargetUrl : [url of resource that should be loaded intitially]
		
		OPTIONAL
		targetContentSelector : [jQuery selector to select only a portion of the loaded content for display]
*/
var createModalDialogHelper = function(config){
	var urlPrevious;
	var dialogLoadSuccessfully = false;
	var $modalDialog = config.$modalDialog;

	var dialogInitialize = function(){
		if(config.jQueryUiDialogOptions && config.jQueryUiDialogOptions.length > 0){
			$modalDialog.dialog(eval('({autoOpen: false, closeOnEscape: true, modal: true, ' + config.jQueryUiDialogOptions + '})'));
		} else {
			$modalDialog.dialog({ autoOpen: false, closeOnEscape: true, modal: true });
		}
	};
	
	var dialogShow = function(url){
		if (!url){
			url = config.defaultTargetUrl;
		}
		
		if((urlPrevious !== url) || !dialogLoadSuccessfully){
			dialogPurge();
			dialogLoad(url);
		}
		$modalDialog.dialog('open');
	};
	
	var dialogDisplayErrorMessage = function(){
		$modalDialog.html('<div class="errorText" style="text-align: center;">An error has occurred.<p />Please close this window and try again.<p /><a href="javascript:dialogHide();">Close</a></div>');
	};
	
	var dialogHide = function(){		
		$modalDialog.dialog('close');
	};
	
	var dialogPurge = function(){
		$modalDialog.html('');
	};
	
	var dialogLoad = function(url, data){
		if (!url){
			url = urlPrevious;
		}
	
		if (url){
			urlPrevious = url;
			
			//append unique id as querystring parameter
			data = appendModalDialogId(data, '&');
									
			//after loading content, apply the selector to select only a portion of that content for display
			if (config.targetContentSelector && config.targetContentSelector.length > 0){
				url = url + ' ' + config.targetContentSelector;
			}
			
			$modalDialog.load(url, data, dialogHandleResponse);
		} else {
			dialogHandleError();
		}
	};
	
	var dialogHandleResponse = function(responseText, textStatus, XMLHttpRequest){
		if(textStatus == "success"){
			dialogLoadSuccessfully = true;
		} else {
			dialogHandleError();
		}
	};
	
	var dialogHandleError = function(){
		if (config.errorUrl.length > 0) {
			//try to load error page
			$modalDialog.load(appendModalDialogId(config.errorUrl, '?'), 
				function(responseText, textStatus, XMLHttpRequest) {
					if(textStatus != "success"){
						dialogDisplayErrorMessage();
					}
				}
			);
			
		} else {
			dialogDisplayErrorMessage();
		}
	};
	
	var appendModalDialogId = function(original, delimiter){
		return original + delimiter + 'ModalDialogId=' + config.id;
	};
	
	return {
		id : config.id,
		$modalDialog : $modalDialog,
		init : dialogInitialize,
		load : dialogLoad,
		show : dialogShow,
		hide : dialogHide
	};
};

var createModalDialogHelperConfig = function(id, $modalDialog, defaultTargetUrl, errorUrl, targetContentSelector, jQueryUiDialogOptions) {
	return {
		id: id, 
		$modalDialog: $modalDialog, 
		defaultTargetUrl: defaultTargetUrl,
		errorUrl: errorUrl,
		targetContentSelector: targetContentSelector,
		jQueryUiDialogOptions: jQueryUiDialogOptions
	};
};
