/** 
	Manages Sitecore dialogs.
*/

x = function ($) {

//DialogController
$.DialogController = {
	dialogs : {},
	defaultOptions : {
		height:400,
		width:400
	},
	add : function(name, options) {
		//$.extend(options, $.DialogController.defaultOptions);
		var lcName = name.toLowerCase();
		$.DialogController.dialogs[lcName] = options;
	},
	show : function(name) {
		var lcName = name.toLowerCase();
		var dialogInfo = $.DialogController.dialogs[lcName];
		if(dialogInfo === undefined) {
			alert("Cannot show dialog '" + name + "', dialog does not exist");
		}
		
		$md = $("#modal-dialog");
		$("#modal-dialog .title").text("");
		$("#modal-dialog .contents").children().remove();
		$("#modal-dialog .contents").addClass("loading");
		
		var url = encodeURI(name) + "?nocache=" + (new Date()).getTime();
		$.get( url, function(data) {
			$("#modal-dialog .contents").removeClass("loading");
			$("#modal-dialog .contents").append(data);
			$("#dialog-controller").trigger("showDialog");
			$("#modal-dialog .contents :checkbox").click(function() {
				var pack = function(o) {
					return function() {
						if($(o).attr("checked")) {
							$(o).removeAttr("checked");
						}
						else {
							$(o).attr("checked", "checked");
						}
					}
				}
				setTimeout(pack(this), 20);
			});
		});
        $md.animate({
			marginTop: (dialogInfo.height / -2) + "px", 
			marginLeft: (dialogInfo.width / -2) + "px",
			height: dialogInfo.height,
			width: dialogInfo.width
		}, 200);
		$("#dialog-controller")
			.fadeIn("fast")
			.data("name", name);
			
	},
	dismiss : function() {
		var name = $("#dialog-controller").data("name");
		$("#dialog-controller")
			.fadeOut("fast")
			.data("name", null)
			.trigger("dismissDialog", [name]);
			
		$("#modal-dialog .contents").children().remove();
	},
	isShowing : function(testName) {
		var name = $("#dialog-controller").data("name");
		if(name == null || name === undefined) {
			return false;
		}
		return (name.toLowerCase() == testName.toLowerCase());
	}
}

//Wireup
$(function() {
	//Bind To Links
	$("a[href^='/Appendix/Dialogs/']").click(function() {
		var name = $(this).attr("href");
		$.DialogController.show(name);
		return false;
	});

	//Bind to Overlay
	$("#dialog-controller").click($.DialogController.dismiss);
	
	$("#modal-dialog").click(function() {
		return false;
	});
	$("#modal-dialog > .close").click($.DialogController.dismiss);
	
	//Autoload a Dialog
	var getParameterByName = function ( name )
	{
	  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS );
	  var results = regex.exec( window.location.href );
	  if( results == null )
		return "";
	  else
		return decodeURIComponent(results[1].replace(/\+/g, " "));
	};
	var dialogToOpen = getParameterByName("dialog");
	if(dialogToOpen != "") {
		$.DialogController.show("/Appendix/Dialogs/" + dialogToOpen);
	}
});

}(jQuery);
