function DialogLayoutController(dialog, parameters) {
	
	this.dialog = dialog;
	this.parameters = parameters;
	
	this.init = function() {
		
		extractParameters(parameters);
		
		dialog.addClass("dialog-layout");
		
		addHeaderButtons();
		
		dialog.css("width", dialogWidth);
		
		dialog.css("height", dialogHeight);
		
		dialog.css("z-index", 2000);
		
		var modalCover = null;
		if(modal) {
			modalCover = jQuery("body").find(dialog.attr("id") + "_modal_cover");
			if(modalCover.size() <= 0) {
				modalCover = jQuery("<div class=\"modal-cover\" id=\"" + dialog.attr("id") + "_modal_cover" +  "\"></div>")
					.appendTo("body");
			}
			modalCover.css("position", "absolute");
			modalCover.css("left", 0);
			modalCover.css("top", 0);
			modalCover.css("width", "100%");
			modalCover.css("height", "100%");
			modalCover.css("z-index", 1500);
			modalCover.css("background-color", "#000000");
			
		}
		
		if(!autoOpen) {
			dialog.hide();
			if(modal) {
				modalCover.hide();
			}
		}
		
		if(dialogDraggable) {
			if(jQuery(dialog.find("div.dialog-header")).size() == 0) {
				throw "Cannot set draggable for dialog without header";
			}
			dialog.draggable({
				handle: dialog.find("div.dialog-header"),
				cursor: "move"
			});
		}
		
		if(dialogResizable) {
			dialog.resizable({
				resize: function(event, ui) {
					refresh();
				}
			});
		}
		
		refresh();
	};
	
	this.refresh = function() {
		refresh();
	};
	
	this.open = function() {
		if(modal) {
			jQuery("div#" + dialog.attr("id") + "_modal_cover").show();
		}
		dialog.show();
		refresh();
		var winWidth = jQuery(window).width();
		var winHeight = jQuery(window).height();
		var left = (winWidth - dialog.width())/2;
		var top = (winHeight - dialog.height())/2;
		dialog.css("left", left);
		dialog.css("top", top);
	};
	
	this.close = function() {
		dialog.hide();
		if(modal) {
			jQuery("div#" + dialog.attr("id") + "_modal_cover").hide();
		}
		if( onClose != null ) {
			onClose.call();
		}
	};
	
	this.setTitle = function(title) {
		jQuery(dialog).find("td.dialog-title").html(title);
	}
	
	function refresh() {
		
		var cdw = dialogWidth; 
		if(dialogWidth == "auto"){
			cdw = dialog.find("div.west-dialog-panel").width();
			cdw += dialog.find("div.content-dialog-panel").width();
			cdw += dialog.find("div.east-dialog-panel").width();
		}
		dialog.css("width", cdw);
		
		var cdh = dialogHeight; 
		if(dialogHeight == "auto"){
			cdh = dialog.find("div.dialog-header").height();
			cdh += dialog.find("div.north-dialog-panel").height();
			var contentHeight = dialog.find("div.content-dialog-panel").height();
			var westHeight = dialog.find("div.west-dialog-panel").height();
			var eastHeight = dialog.find("div.east-dialog-panel").height();
			if((contentHeight > westHeight) && (contentHeight > eastHeight)) {
				cdh += contentHeight;
			} else {
				if(westHeight > eastHeight) {
					cdh += westHeight;
				} else {
					cdh += eastHeight;
				}
			}
			cdh += dialog.find("div.south-dialog-panel").height();
		}
		dialog.css("height", cdh);
		
		var rootWidth = dialog.width();
		var rootHeight = dialog.height();
		
		var headerHeight = refreshHeader(rootWidth);
		var northPanelHeight = refreshNorthPanel(rootWidth, headerHeight);
		var southPanelHeight = refreshSouthPanel(rootWidth, rootHeight);
		var westPanelWidth = refreshWestPanel(rootHeight, headerHeight, northPanelHeight, 
				southPanelHeight);
		var eastPanelWidth = refreshEastPanel(rootWidth, rootHeight, headerHeight, 
				northPanelHeight, southPanelHeight);
		refreshContentPanel(rootWidth, rootHeight, westPanelWidth, eastPanelWidth, 
				headerHeight, northPanelHeight, southPanelHeight);
		
		dialog.resizable( "option", "minHeight", headerHeight + northPanelHeight + southPanelHeight);
	}
	
	function addHeaderButtons() {
		var header = dialog.find("div.dialog-header");
		if(jQuery(header).size() == 1 ) {
			var headerContent = "<table width=\"100%\"><tr>";
			headerContent += "<td class=\"dialog-title\">";
			headerContent += header.html();
			headerContent += "</td>";
			if(showClose) {
				
			}
			headerContent += "</tr></table>"
			header.html(headerContent);
		}
	}
	
	function refreshHeader(rootWidth) {
		var header = dialog.find("div.dialog-header");
		var headerHeight = 0;
		if(jQuery(header).size() == 1 ) {
			header.addClass("dialog-layout");
			header.css("left", 0);
			header.css("top", 0);
			header.css("width", rootWidth);
			headerHeight = header.height();
		} else if(jQuery(header).size() > 1) {
			throw "Only one header is allowed for dialog";
		}
		return headerHeight;
	}
	
	function refreshNorthPanel(rootWidth, headerHeight) {
		var northPanel = dialog.find("div.north-dialog-panel");
		var northPanelHeight = 0;
		if(jQuery(northPanel).size() == 1 ) {
			northPanel.addClass("dialog-layout");
			northPanel.css("left", 0);
			northPanel.css("top", headerHeight);
			northPanel.css("width", rootWidth);
			northPanelHeight = northPanel.height();
		} else if(jQuery(northPanel).size() > 1) {
			throw "Only one north pannel is allowed for dialog";
		}
		return northPanelHeight;
	}
	
	function refreshSouthPanel(rootWidth, rootHeight) {
		var southPanel = dialog.find("div.south-dialog-panel");
		var southPanelHeight = 0;
		if(jQuery(southPanel).size() == 1 ) {
			southPanel.addClass("dialog-layout");
			southPanelHeight = southPanel.height();
			southPanel.css("left", 0);
			southPanel.css("top", rootHeight - southPanelHeight);
			southPanel.css("width", rootWidth);
		} else if(jQuery(southPanel).size() > 1) {
			throw "Only one south panel is allowed for dialog";
		}
		return southPanelHeight;
	}
	
	function refreshWestPanel(rootHeight, headerHeight, northPanelHeight, southPanelHeight) {
		var westPanel = dialog.find("div.west-dialog-panel");
		var westPanelWidth = 0;
		if(jQuery(westPanel).size() == 1 ) {
			westPanel.addClass("dialog-layout");
			westPanel.css("left", 0);
			westPanel.css("top", headerHeight + northPanelHeight);
			westPanel.css("height", rootHeight - headerHeight - northPanelHeight - southPanelHeight);
			westPanelWidth = westPanel.width();
		} else if(jQuery(westPanel).size() > 1 ) {
			throw "Only one west panel is allowed for dialog";
		}
		return westPanelWidth;
	}
	
	function refreshEastPanel(rootWidth, rootHeight, headerHeight, northPanelHeight, southPanelHeight) {
		var eastPanel = dialog.find("div.east-dialog-panel");
		var eastPanelWidth = 0;
		if(jQuery(eastPanel).size() == 1 ) {
			eastPanel.addClass("dialog-layout");
			eastPanelWidth = eastPanel.width();
			eastPanel.css("left", rootWidth - eastPanelWidth);
			eastPanel.css("top", headerHeight + northPanelHeight);
			eastPanel.css("height", rootHeight - headerHeight - northPanelHeight - southPanelHeight);
		} else if(jQuery(eastPanel).size() > 1 ) {
			throw "Only one east panel is allowed for dialog";
		}
		return eastPanelWidth;
	}
	
	function refreshContentPanel(rootWidth, rootHeight, westPanelWidth, eastPanelWidth, headerHeight, northPanelHeight, southPanelHeight) {
		var contentPanel = dialog.find("div.content-dialog-panel");
		if(jQuery(contentPanel).size() == 1 ) {
			contentPanel.addClass("dialog-layout");
			contentPanel.css("left", westPanelWidth);
			contentPanel.css("top", headerHeight + northPanelHeight);
			if(dialogWidth != "auto") {
				contentPanel.css("width", rootWidth - westPanelWidth - eastPanelWidth);
			}
			if(dialogHeight != "auto") {
				contentPanel.css("height", rootHeight - headerHeight - northPanelHeight - southPanelHeight);
			}
		} else if(jQuery(contentPanel).size() > 1 ) {
			throw "Only one content panel is allowed for dialog";
		} else {
			throw "Dialog should contains at least one content panel";
		}
	}
	
	var dialogWidth = "auto";
	var dialogHeight = "auto";
	var dialogDraggable = false;
	var dialogResizable = false;
	var autoOpen = true;
	var modal = false;
	var onClose = null;
	var showClose = true;
	var closeIcon = null;
	
	function extractParameters(parameters) {
		dialogWidth = extractParameter(parameters, "width", "auto");
		dialogHeight = extractParameter(parameters, "height", "auto");
		dialogDraggable = extractParameter(parameters, "draggable", false);
		dialogResizable = extractParameter(parameters, "resizable", false);
		autoOpen = extractParameter(parameters, "autoOpen", true);
		modal = extractParameter(parameters, "modal", false);
		onClose = extractParameter(parameters, "onClose", null);
		showClose = extractParameter(parameters, "showClose", true);
		closeIcon = extractParameter(parameters, "closeIcon", null);
	}
	
	function extractParameter(parameters, parameterName, defaultValue) {
		if(typeof(parameters)!="undefined" && typeof(parameters[parameterName]) != "undefined") {
			return parameters[parameterName];
		} else {
			return defaultValue;
		}
	}
}

var dialogMap = new Object();

jQuery.fn.initDialogLayout = function(parameters) {
	var controller = new DialogLayoutController(jQuery(this), parameters);
	dialogMap[jQuery(this).attr("id")] = controller;
	controller.init();
};

jQuery.fn.open = function() {
	var controller = dialogMap[jQuery(this).attr("id")];
	controller.open();
	controller.refresh();
};

jQuery.fn.setTitle = function(title) {
	var controller = dialogMap[jQuery(this).attr("id")];
	controller.setTitle(title);
};

jQuery.fn.close = function() {
	var controller = dialogMap[jQuery(this).attr("id")];
	controller.close();
};

