
// Setup up a 'layout' namespace
var layout = (function() {
	return {
		onchange: function(serialized) { }
		,
		onclose: function($widget,id,e){}
		,
		onminimize: function($widget,id,e) { }
		,
		onmaximize: function($widget,id,e) { }
		,
		onconfig: function($widget,id,e) { }
		,
		// Serialize all containers and their widgets using element ID's
		serialize: function(container,ui) {
			var containers = ui.options.connectWith || [container];
			var serialized = [];
			if (containers) {
				// Loop through all container selectors in the 'connectWith' array
				$.each(containers,function(i,containerSelector){
					// Loop through each of the containers that matches each selector
					$(containerSelector).each(function() {
						var container = this;
						var containerChildren = [];
						// Loop through each of the sortable items in the container
						$(ui.options.items,container).each(function(){
							serialized.push(container.id+"="+this.id);
						});
					});
				});
			}
			return serialized.join("&");
		}
		,
		// Default action when minimize is clicked
		minimize: function(o,e) {
			var $o = $(o).toggle();
			var $widget = $o.parents('.widget');
			$widget.find('.widget-maximize').show().end().find('.widget-body').hide();
			layout.onminimize($widget,$widget[0].id,e);
		}
		,
		// Default action when maximize is clicked
		maximize: function(o,e) {
			var $o = $(o).toggle();
			var $widget = $o.parents('.widget');
			$widget.find('.widget-minimize').show().end().find('.widget-body').show();
			layout.onmaximize($widget,$widget[0].id,e);
		}
		,
		// Default action when config is clicked
		config: function(o,e) {
			var $widget = $(o).parents('.widget');
			layout.onconfig($widget,$widget[0].id,e);
		}
		,
		confirmclose: function($widget,id,e) {
			return confirm("Are you sure you want to remove this widget?");
		}
		,
		close: function(o,e) {
			var $widget = $(o).parents('.widget');
			if (layout.confirmclose($widget,$widget[0].id,e)) {
				$widget.fadeOut(layout.options.fadeOutDuration,function(){$widget.remove()});
				layout.onclose($widget,$widget[0].id,e);
			}
		}
		,
		init: function() {
			$('.widget-minimized .widget-minimize,.widget-minimized .widget-body').hide(); // Hide the minimize icon if the widget is already minimized
			$('.widget-maximize').hide(); // Hide all the maximize controls by default
			$('.widget-minimized .widget-maximize').show(); // Show the maximize icon if the widget is minimized
		}
		,
		// This is a default "options" object that will be passed to the sortable() call. It can be modified by the user
		options: {
			appendTo:'body'
			,cancel:'.widget-control'
			,connectWith:['.widget-container']
			,cursor:'move'
			,delay:0
			,distance:0
			,dropOnEmpty:true
			,handle:'.widget-handle'
			,items:'> .widget'
			,opacity:.5
			,placeholder:'layout-dropzone'
			,revert:100
			,zIndex:500
			,start: function(e,ui) { ui.helper.css("width", ui.item.width());	}
			,sort: function(e,ui) {}
			,change: function(e,ui) {}
			,stop: function(e,ui) { layout.onchange(layout.serialize(this,ui)); }
			,beforeStop: function(e,ui) {}
			,update: function(e,ui) {}
			,receive: function(e,ui) {}
			,remove: function(e,ui) {}
			,over: function(e,ui) { $(this).addClass('widget-container-hover'); }
			,out: function(e,ui) { $(this).removeClass('widget-container-hover'); }
			,activate: function(e,ui) { $(this).addClass('widget-container-active'); }
			,deactivate: function(e,ui) { $(this).removeClass('widget-container-active'); }
			
			// Options that are specific to layout (not Sortable)
			,fadeOutDuration:200
		}
	}
})();

