/*
	DCMS portlet
	Use $('.portlet-column').portlets() ;
	This will transform all div inside the column (class="portlet-column") into portlets
	The title attribute of the div will be the title of the portlet
	If you use the customizable options, when the user click on the customize icon, the callback in the data('customize') of the div is called
	( $(div).data('customize').call()) 
	
	Depends on :
	- jquery.metadata.js
*/

(function($) {

$.widget("dcms.portlet", {
	_init:function() {
		var opt = $(this.element).metadata() ;
		if (typeof opt.portlet != 'undefined') {
			$.extend(this.options, opt.portlet) ;
		}
		
		var self = this;
		
		var portlet = (this.portlet = $('<div></div>'))
			.addClass('portlet')
			.addClass(self._getData('addPortletClass'))
			.addClass('ui-widget') ;
		
		var portlet_header = $('<div></div>')
			.addClass('ui-widget-header')
			.addClass('ui-corner-top')
			.addClass('portlet-header')
			.html(self._getData('title'))
			.appendTo(portlet) ;
			
		if (self._getData('minimizable')) {
			var minimize_icon = (this.minimize_icon = $('<span></span>'))
				.addClass('ui-icon')
				.addClass('ui-icon-minusthick')
				.addClass('ui-corner-all')
				.addClass('minimize')
				.data('enabled', true)
				.attr('title', 'Réduire')
				.click(function() {
					if (self.minimize_icon.data('enabled')) {
						var self_ = self ;
						$(this).toggleClass('ui-icon-minusthick')
							.toggleClass('ui-icon-plusthick') ;
						self.element.toggle('blind', {}, 500, function() {
							self_._trigger('minimize') ;
						}) ;
					}
				})
				.clickable()
				.appendTo(portlet_header) ;
		}
		
		if (self._getData('customizable')) {
			var customizable_icon = (this.customizable_icon = $('<span></span>'))
				.addClass('ui-icon')
				.addClass('ui-icon-gear')
				.addClass('ui-corner-all')
				.addClass('customizable')
				.attr('title', 'Configurer')
				.data('enabled', true)
				.click(function() {
					if (self.customizable_icon.data('enabled')) {
						self._trigger('customize') ;
					}
				})
				.clickable()
				.appendTo(portlet_header) ;
		}
		
		self.element.removeData('title') ;
			
		self.element.addClass('portlet-content')
			.addClass('ui-widget-content')
			.addClass('ui-corner-bottom')
			.appendTo(portlet) ;
			
		portlet.appendTo(self._getData('column')) ;
	},
	destroy:function() {
	},
	disable_button:function(button) {
		if (button == 'customize' && typeof this.customizable_icon != 'undefined') {
			this.customizable_icon.addClass('ui-state-disabled').data('enabled', false) ;
		} else if (button == 'minimize' && typeof this.minimize_icon != 'undefined') {
			this.minimize_icon.addClass('ui-state-disabled').data('enabled', false) ;
		}
	},
	enable_button:function(button) {
		if (button == 'customize' && typeof this.customizable_icon != 'undefined') {
			this.customizable_icon.removeClass('ui-state-disabled').data('enabled', true) ;
		} else if (button == 'minimize' && typeof this.minimize_icon != 'undefined') {
			this.minimize_icon.removeClass('ui-state-disabled').data('enabled', true) ;
		}
	},
	toggle_button:function(button) {
		if (button == 'customize' && typeof this.customizable_icon != 'undefined') {
			this.customizable_icon.toggleClass('ui-state-disabled').data('enabled', !this.customizable_icon.data('enabled')) ;
		} else if (button == 'minimize' && typeof this.minimize_icon != 'undefined') {
			this.minimize_icon.toggleClass('ui-state-disabled').data('enabled', !this.minimize_icon.data('enabled')) ;
		}
	},
	hide:function() {
		if (this.element.is(':visible')) {
			this.portlet.hide('blind', {}, 500) ;
		}
	},
	show:function() {
		if (this.element.is(':hidden')) {
			this.portlet.show('blind', {}, 500) ;
		}
	},
	toggle:function() {
		this.portlet.toggle('blind', {}, 500) ;
	}
}) ;

//$.dcms.ajaxDialog.portlet = "";

$.extend($.dcms.portlet, {
	defaults: {
		minimizable:true,
		customizable:false,
		column:$('body'),
		addPortletClass:'',
		title:'',
		showing:true
	},
});

})(jQuery);
(function($) {

$.widget("dcms.portlets", {
	_init:function() {
		var self = this;
		self.element.each(function() { // For each column
			var column_ = $(this) ;
			var my_self = self ;
			$(this).children('div').each(function() {
				var opt = {
					minimizable:my_self._getData('minimizable'), 
					customizable:my_self._getData('customizable') , 
					column:column_,
					addPortletClass:my_self._getData('addPortletClass')
				} ;
				$(this).portlet(opt) ;
			}) ;
		}) ;
		
		self.element.sortable(self.options) ;
	},
	destroy:function() {
	}
}) ;

//$.dcms.ajaxDialog.portlets = "";

$.extend($.dcms.portlets, {
	defaults: {
		title:'',
		minimizable:true,
		customizable:false,
		addPortletClass:'',
		connectWith: ['.portlet-column'],
		handle:'.portlet-header'
	},
});

})(jQuery);
