;(function($) {
	serializeObjectForPost = function(data, name) {
		if(!typeof data == 'object') return '';
		//pass empty array as a simple string
		var ret = name+"=";
		for(index in data)
		{
			var elName = name+'['+index+']';
			var elString = "";
			if(typeof data[index] == 'object') {
				elString = serializeObjectForPost(data[index],elName);
			} else if (typeof data[index] != 'function' && typeof data[index] != 'object' && typeof data[index] != 'undefined') {
				elString = elName+'='+data[index];
			}
			if(elString != '') {
				if(ret != '') {
					ret += '&';
				}
				ret += elString;
			}
		}
		return ret;
	};
	jQuery.fn.extend({
		configurablePortlet: function(override) {
			var settings = {
					
			};
			$(settings).extend(override);
		
			$(this).each(function(){
				var me = this;
				me.type = $(me).attr('id');
				me.onloadfunctionname = me.type + "_onload";
				this.loadSettings = function() {
					me.oldContent = $(this).children('.portlet-content').children().clone().get();
						
					$.post('/scripts/portlets.php', {type: me.type, settings : 'true'}, function(data){
						$(me).find('.portlet-content').html(data);
						$(me).find('form input.cancel-button').click(function(e){
							e.stopPropagation();
							me.restore();
							return false;
						});
						$(me).find('form').submit(function(e){
							if(!this.validate || this.validate())
							{
								var data = $(this).serializeArray();
								me.saveSettings(data);
							}
							return false;
						});
					}, 'html');
					$(me).find('.portlet-settings').hide();
					//$('.portlet-column').sortable('disable');
				};
				this.saveSettings = function(data) {
					$.post('/scripts/portlets.php', data, function(data){
						$(me).find('.portlet-content').html(data);
						$(document).ready(function(){
							// show settings button 
							$(me).find('.portlet-settings').show();
							
							// or re-declare the click function, if the settings button is in the portlet itself
//							$(me).find('.portlet-settings').show().click(function() {
//								me.loadSettings();
//							});
							
							// call portlet specific function, if exists
							if (window[me.onloadfunctionname]) window[me.onloadfunctionname]();
							
						});
					}, 'html');
					//$('.portlet-column').sortable('enable');
				};
				this.restore = function() {
					if(me.oldContent) {
						$(me).children('.portlet-content').empty();
						$(me).children('.portlet-content').append($(this.oldContent));

						// show settings button 
						$(me).find('.portlet-settings').show();

						// or re-declare the click function, if the settings button is in the portlet itself
//						$(me).find('.portlet-settings').show().click(function() {
//							me.loadSettings();
//						});

						// call portlet specific function, if exists
						if (window[me.onloadfunctionname]) window[me.onloadfunctionname]();
					}
					//$('.portlet-column').sortable('enable');
				}
				$(this)
//					.addClass('ui-widget ui-widget-content ui-helper-clearfix ui-corner-all')
					.find(".portlet-header")
				 	.prepend('<span class="ui-icon ui-icon-settings portlet-settings">settings</span>');
				$(this).find('.portlet-settings').click(function() {
					me.loadSettings();
				});
				//$(this).find('.portlet-settings').hover(function() {
				//	me.loadSettings();
				//});
			});
		}, 
		sortablePortlet: function(override) {
			var settings = {
				name: 'home',
				type: 'home',
				data: 'position',
				elements: $('.portlet-draggable'),
				target: '/scripts/portlets.php'
			};
			$(settings).extend(override);
			//make the portlet-column sortable
			$('.portlet-column').sortable({
				placeholder: 'ui-state-highlight',
				connectWith: ['.portlet-column-droppable'],
				delay: 100,
				distance: 30,
				tolerance: 'pointer',
				handle: ".portlet-header", //drag using the portlet-header
				items: $('.portlet-draggable'), //only allow portlet-draggable to be moved
				stop: function() {
					var data = new Array();
					$('.portlet-column').each(function(i){
						data[i] = new Array();
						$(this).find('.portlet').each(function(j){
							data[i][j] = $(this).attr('id');
						});
					});
					senddata = "type="+settings.type+"&"+serializeObjectForPost(data, settings.data);
					$.post('/scripts/portlets.php', senddata);
				}
			});
//			$(this)
//				.addClass('ui-widget ui-widget-content ui-helper-clearfix ui-corner-all')
//				.find(".portlet-header")
//				.prepend('<span class="ui-icon ui-icon-plusthick portlet-minimize"> minimize </span>');
			$(this)
				.find(".portlet-draggable")
				.find(".portlet-header")
				.append("<span class='portlet-sleep'>sleep</span>");
		},
		hidablePortlet: function(override) {
			var settings = {};
			$(settings).extend(override);

			$(this).each(function(){
				$(this)
					.find(".portlet-header")
					.append("<span class='portlet-hide'> verwijder me</span>");
				
				var me = this;
				me.hidePortlet = function() {
					me.type = $(me).attr('id');
					senddata = "type=home&hide=true&name="+me.type;
					$.post('/scripts/portlets.php', senddata);
					$(me).remove();
				};
	
				$(this).find('.portlet-hide').click(function() {
					me.hidePortlet();
				});
			});
		}
	});
})(jQuery);

$(function(){
//	$(".portlet-header").hide();
	$('.portlet-column').sortablePortlet();
	$('.portlet-configurable').configurablePortlet();
	$('.portlet-hidable').hidablePortlet();
});
