$(function() {
	// MAKE THE CONTAINERS SORTABLE
	// ============================
	$('.widget-container').sortable(layout.options);
	layout.init();
	$('.widget-controls .widget-close').click( function(e){layout.close($(this),e);} );
	$('.widget-controls .widget-minimize').click(function(e){layout.minimize($(this),e);});
	$('.widget-controls .widget-maximize').click(function(e){layout.maximize($(this),e);});
	$('.widget-controls .widget-options').click(function(e){layout.config($(this),e);});
	$('.widget-uncloseable .widget-close').hide();
});

layout.options.cancel = ".widget-control";
layout.onchange = function(serialized) {
	//alert("Insert some ajax call to persist this: "+serialized);
}
layout.onclose = function($widget,id,e) {
	//alert("Insert ajax to persist: widget ["+id+"] was removed");
}
layout.onminimize = function($widget,id,e) {
	//alert("Insert ajax to persist: "+id+" was minimized");
}
layout.onmaximize = function($widget,id,e) {
	//alert("Insert ajax to persist: "+id+" was maximized");
}
layout.onconfig = function($widget,id,e) {
	var $optionsContent = $widget.find('.options-content');
	var $optionsContainer = $widget.find('.widget-options-content');
	// Move the options from the widget file up into the settings area
	if ($optionsContent.length>0) {
		$optionsContainer.empty()[0].appendChild($optionsContent.show()[0]);
	}
	$optionsContainer.slideDown('fast');
}
