(function() {
	
	$.removePortlet = function(name) {
		var portlet = $('#shop-portlet-' + name);
		var content = portlet.find('.shop-portlet-content');
		
		$.ajax({url: '/portlet/' + name + '/remove', 
				dataType: 'json', 
				success: function(json) {
					$.removeUpdaterEvents();
					if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
					if (json.content) {
						content.html(json.content);
						$.evalScripts(json.content);
					}
					
					if (portlet.is('.shop-portlet-required')) {
						portlet.addClass('shop-portlet-sticky');
					} else {
						portlet.remove();
						$.refreshPortletSortable();
					}
					
					$.updateEvents();
				}
			}
		);
	};
	
	$.addPortlet = function(name) {
		$.ajax({url: '/portlet/' + name + '/add', 
				dataType: 'json', 
				success: function(json) {
					$.removeUpdaterEvents();
					
					var portlet = $('#shop-portlet-' + name);
					if (!portlet.length) {
						$('#shop-portlets').prepend('<div id="shop-portlet-' + name + '" class="shop-portlet"><div class="shop-portlet-header"><h5>' + json.title + '</h5><div class="shop-portlet-icon shop-portlet-icon-close"></div><div class="shop-portlet-icon shop-portlet-icon-sticky"></div><div class="shop-portlet-icon shop-portlet-icon-maximize"></div><div class="shop-portlet-icon shop-portlet-icon-bigger"></div><div class="shop-portlet-icon shop-portlet-icon-smaller"></div><div class="shop-portlet-icon shop-portlet-icon-minimize"></div></div><div class="shop-portlet-content"></div></div>');
						portlet = $('#shop-portlet-' + name);
						$.refreshPortletSortable();
					}
					
					var content = portlet.find('.shop-portlet-content');
					
					if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
					if (json.content) {
						content.html(json.content);
						$.evalScripts(json.content);
					}
					
					portlet.removeClass('shop-portlet-sticky');
					$.updateEvents();
				}
			}
		);
	};
	
	$.attachUpdater(function() {
		$('#shop-portlets>.shop-portlet').each(function() {
			var self = $(this);
			if (self.find('.shop-portlet-content > .shop-portlet-content-max').size()) {
				if (self.is('.shop-portlet-nomax')) self.removeClass('shop-portlet-nomax');
			} else {
				if (!self.is('.shop-portlet-nomax')) self.addClass('shop-portlet-nomax');
			}
		});
		
		$('#shop-portlets div.shop-portlet-icon').bind('mousedown.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
		});
		
		$('#shop-portlets div.shop-portlet-icon-sticky').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			
			var portlet = $(this).parent().parent();
			var content = portlet.children('.shop-portlet-content');
			var name = portlet.attr('id').split('-')[2];
			
			$.ajax({url: '/portlet/' + name + '/add', 
					data: {}, 
					dataType: 'json', 
					error: function(req, errorType) {
					},
					success: function(json) {
						$.removeUpdaterEvents();
						if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
						if (json.content) {
							content.html(json.content);
							$.evalScripts(json.content);
						}
						
						portlet.removeClass('shop-portlet-sticky');
						$.updateEvents();
					}
				}
			);
		});
		
		$('#shop-portlets div.shop-portlet-icon-close').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			$.removePortlet($(this).parent().parent().attr('id').split('-')[2]);
		});
		
		$('#shop-portlets div.shop-portlet-icon-smaller, #shop-portlets div.shop-portlet-icon-bigger, #shop-portlets div.shop-portlet-icon-minimize, #shop-portlets div.shop-portlet-icon-maximize').bind('click.update', function(event) {
			event.preventDefault();
			event.stopPropagation();
			
			var self = $(this);
			var portlet = self.parent().parent();
			var content = portlet.children('.shop-portlet-content');
			var name = portlet.attr('id').split('-')[2];
			
			if (self.is('.shop-portlet-icon-maximize') || self.is('.shop-portlet-icon-bigger') && !portlet.is('.shop-portlet-min')) {
				if (portlet.is('.shop-portlet-min')) {
					$.ajax({url: '/portlet/' + name + '/update', 
							data: {state: 2}, 
							dataType: 'json', 
							error: function(req, errorType) {
							},
							success: function(json) {
								$.removeUpdaterEvents();
								if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
								if (json.content) {
									content.html(json.content);
									$.evalScripts(json.content);
								}
								$.updateEvents();
								portlet.removeClass('shop-portlet-min');
								portlet.addClass('shop-portlet-max');
							}
						}
					);
				} else {
					$.ajax({url: '/portlet/' + name + '/update', 
							data: {state: 2}, 
							dataType: 'json', 
							error: function(req, errorType) {
							},
							success: function(json) {
								$.removeUpdaterEvents();
								if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
								if (json.content) {
									content.html(json.content);
									$.evalScripts(json.content);
								}
								$.updateEvents();
								portlet.removeClass('shop-portlet-min');
								portlet.addClass('shop-portlet-max');
							}
						}
					);
				}
			} else if (self.is('.shop-portlet-icon-minimize') || self.is('.shop-portlet-icon-smaller') && !portlet.is('.shop-portlet-max')) {
				$.ajax({url: '/portlet/' + name + '/update', 
						data: {state: 1}, 
						dataType: 'json', 
						error: function(req, errorType) {
						},
						success: function(json) {
							$.removeUpdaterEvents();
							if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
							if (json.content) {
								content.html(json.content);
								$.evalScripts(json.content);
							}
							$.updateEvents();
							portlet.removeClass('shop-portlet-max');
							portlet.addClass('shop-portlet-min');
						}
					}
				);
			} else {
				if (portlet.is('.shop-portlet-min')) {
					$.ajax({url: '/portlet/' + name + '/update', 
							data: {state: 0}, 
							dataType: 'json', 
							error: function(req, errorType) {
							},
							success: function(json) {
								$.removeUpdaterEvents();
								if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
								if (json.content) {
									content.html(json.content);
									$.evalScripts(json.content);
								}
								$.updateEvents();
								portlet.removeClass('shop-portlet-min');
								portlet.removeClass('shop-portlet-max');
							}
						}
					);
				} else {
					$.ajax({url: '/portlet/' + name + '/update', 
							data: {state: 0}, 
							dataType: 'json', 
							error: function(req, errorType) {
							},
							success: function(json) {
								$.removeUpdaterEvents();
								if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
								if (json.content) {
									content.html(json.content);
									$.evalScripts(json.content);
								}
								$.updateEvents();
								portlet.removeClass('shop-portlet-min');
								portlet.removeClass('shop-portlet-max');
							}
						}
					);
				}
			}
		});
		
		$('form.shop-form-portlet').bind('submit.update', function(event) {
			var self = $(this);
			
			event.preventDefault();
			event.stopPropagation();
			
			var portlet = self.parents('.shop-portlet');
			$.ajax({url: self.attr('action'), 
					data: self.serialize(), 
					success: function(json) {
						$.removeUpdaterEvents();
						if (json.title) portlet.find('.shop-portlet-header h5').text(json.title);
						if (json.content) {
							portlet.find('.shop-portlet-content').html(json.content);
							$.evalScripts(json.content);
						}
						$.updateEvents();
					}
				}
			);
			
			return false;
		});
	});
	
	$.destroyPortletSortable = function() {
		$('#shop-portlets.shop-portlets-sortable').sortable('destroy');
	};
	
	$.refreshPortletSortable = function() {
		$('#shop-portlets.shop-portlets-sortable').sortable('refresh');
	};
	
	$.createPortletSortable = function() {
		$('#shop-portlets.shop-portlets-sortable').sortable({
			handle: 'div.shop-portlet-header',
			scroll: true,
			axis: 'y',
			update: function(event, ui) {
				$.ajax({url: '/portlet/sort',
						data: $(this).sortable('serialize').replace(/shop\-portlet\[\]/g, 'portlets'),
						dataType: 'json', 
						error: function(req, errorType) {
						},
						success: function(json) {
							$.removeUpdaterEvents();
							for (name in json) {
								var portlet = $('#shop-portlet-' + name);
								if (portlet) {
									if (json[name].title) portlet.find('.shop-portlet-header h5').text(json[name].title);
									if (json[name].content) {
										portlet.removeClass('shop-portlet-sticky');
										portlet.find('.shop-portlet-content').html(json[name].content);
										$.evalScripts(json[name].content);
									}
								}
							}
							
							$.updateEvents();
						}
					}
				);
			}
		});
	};
	
	$.createPortletSortable();
})();

