﻿/*! dlgs.js - FINRA dialogs (FDlgs) module */
var FDlgs = (function ($) {
	// Elts to be exposed publicly are enclosed in this obj
	var _public = {};

	// Private member vars
	var OpenModalDefaults = {
			url: '',
			name: '',
			title: '',
			query_str: '',
			width: 500,
			height: 500,
			result_map: {},
			result_effect: '',
			ok_label: 'OK',
			cancel_label: 'Cancel',
			success_fxn: function() {},
			cancel_fxn: function() {}
	};

	_public.OpenModal = function (options) {
		var dlg;
		var x_padding = 50;
		var y_padding = 120;
		var ieb = ($.browser.msie == true);

		// Merge the spec'd options into the defaults, making all option values exactly that... optional
		var args = $.extend(true, {}, OpenModalDefaults, options);
		
		// Construct the "buttons" object we'll use below for the jQuery dialog
		// The order in which we add buttons determines the position of the buttons on the dialog
		var btns = {};
		if (args.cancel_label.length > 0) {
			btns[args.cancel_label] = function() {		// The "Cancel" button click event-handler fxn
				$(this).dialog('close');
				$(this).dialog('destroy');
				args.cancel_fxn.call();
			};
		}
		btns[args.ok_label] = function() {				// The "OK" button click event-handler fxn
			if ($.isFunction(dlg.Validate)) {
				if (!dlg.Validate()) return;
			}

			// Results can be (optionally) passed from dlg form fields to parent page form fields
			// by spec'ing an array or map of jQuery selectors for the from/to fields
			$.each(args.result_map, function(from, to) {
				$(to).val($(dlg.document.body).find(from).val());  // set val of "to" field to val of "from" field
				$(to).effect(args.result_effect, {}, 400);
			});

			$(this).dialog('close');
			$(this).dialog('destroy');
			args.success_fxn.call();
		};

		// Based on the browser, we need to adjust the dimensions that we'll use for the dialog
		args.width = ieb ? args.width : args.width + 50;
		args.height = ieb ? args.height : args.height + 10;

		// To be safe, we remove any previous temp iframe that might still be in the DOM
		$("#tmp-dlg-ifr").remove();
		
		// Construct the temp iframe tag we'll use to create the jQuery dialog
		var ifr_html = "<iframe id='tmp-dlg-ifr' title='" + args.title + "' " +
							"src='" + args.url + args.name + ".aspx" + args.query_str + "' " +
							"frameborder='0' scrolling='no' />"

		// Open the jQuery modal dialog
		$(ifr_html).dialog({
			autoOpen: true,
			resizable: false,
			width: args.width,
			height: args.height,
			modal: true,
			buttons: btns,
			close: function() {
				$(this).dialog('destroy');
			},
			open: function() {
			}
		}).width(args.width-x_padding).height(args.height-y_padding);

		// "dlg" points to the DOM of the page in the temp iframe
		dlg = $("#tmp-dlg-ifr")[0].contentWindow;
	};

	// This return stmt exposes all "public members" of this module to the global namespace
	return _public;
	
}(jQuery));		// We pass in a ref to jQuery to optimize calls to it from within this module

