// Laball Portlet v 0.1 by Dariusz Małachowski - version development

jQuery.labPortlet = {
	_init: function() {
		this.initEdit();
		this.initDelete();
		this.initZoom();
	},
	
	initEdit: function() {
		$('.pb-edit').click(function() {
			jQuery.labPortlet.ajaxEditPortlet(this);
			jQuery.labPortlet.editPortlet(this);
			return false;
		});
	},
	
	initDelete: function() {
		$('.pb-delete').click(function() {
			jQuery.labPortlet.removePortlet(this);
			return false;
		});
	},
	
	initZoom: function() {
		$('.pb-zoom').click(function() {
			jQuery.labPortlet.hideContent(this);
			return false;
		});
	},
	
	ajaxEditPortlet: function(obj) {
		var editContainer = jQuery.labPortlet.getEdit(obj);
		var contentContainer = jQuery.labPortlet.getContent(obj);
		var portlet_id = this.getParent(obj).attr('id');
		$.laballRemote.proceedCall('edit_'+portlet_id,{
			url: "/portlet"+$(obj).attr('href'),
			cache: true,
			data: "portletId="+portlet_id,
			type: "POST",
			error:  function(response) {
				editContainer.append('<div class="error">'+msg['error']+'</div>');
			},
		  	success: function(response) {
				$(editContainer).html(response['html']);
				var optionsForm = {
						success: function(response_form) {
							var error_container = editContainer.find('.error-container');
							if(response_form['html']['error']) {
								error_container.show();
								jQuery.labPortlet.displayErrors(response_form['html'], error_container);
							} else {
								editContainer.hide();
								error_container.hide();
								contentContainer.html(response_form['html']);
							}
							
						}
				};
				$(editContainer).find(".edit-portlet-form-close").click(function() {
					editContainer.hide();
					return false;
				});
				$(editContainer).find("form").ajaxForm(optionsForm);
				return false;
			}
		});
	},
	
	displayErrors: function(response, error_container) {
		var content_error = "";
		content_error += '<div class="error"><b>'+response['error_msg']+"</b>";
		content_error += "<ul>";
		$.each(response['error_fields'], function(key,msg){
			content_error += "<li>"+key+": "+msg+"</li>";
		});
		content_error += '</ul></div>';
		error_container.html(content_error);
	},
	
	editPortlet: function(obj) {
		var editContainer = jQuery.labPortlet.getEdit(obj);
		if (editContainer.is(":hidden")) {
			editContainer.fadeIn("slow");
	    } else {
	    	editContainer.hide();
	    }
	},
	
	removePortlet: function(obj) {
		var parentContainer = jQuery.labPortlet.getParent(obj); 
		parentContainer.fadeOut("slow",function() {
			parentContainer.remove();
		});
	},
	
	hideContent: function(obj) {
		var contentContainer = jQuery.labPortlet.getContent(obj);
		if (contentContainer.is(":hidden")) {
			contentContainer.slideDown("slow");
			
	    } else {
	    	contentContainer.slideUp("slow");
	    	$('.pb-zoom').css('background','/img/portlet/arrow_rihgt.png');
	    }
	},
	
	getContent: function(obj) {
		return $(obj).parents(".portlet-container").find('.portlet-content');
	},
	
	getEdit: function(obj) {
		return $(obj).parents(".portlet-container").find('.portlet-edit');
	},
	
	getParent: function(obj) {
		return $(obj).parents(".portlet-container");
	}
	
}		

labPortlet = function() {
	jQuery.labPortlet._init();
}
