$(function() {
	if(!pageData.frontPage) {
		$(".delete_portlet_btn").hide();
		$("#setDefaultLayout").hide();
		$(".portlet-header").css('cursor', 'default');
		return;
	}
	
	var slayout = {};
	updateLayoutCookie = function() {
		$(".column").each(function() {
			//console.log(this);
			var id = this.id;
			slayout[id] = [];
			$(this).children('div.portlet').each(function() {
				if(this.id) {
					slayout[id].push(this.id);
				}
			});
		});
		Cookie.set('slayout', JSON.encode(slayout), 31*24);
		updateRegionHeight();
	}
	
	//updateLayoutCookie();
		updateRegionHeight = function() {
			/**
			$('.region').each(function() {
				var h = $(this.parentNode).height();
				$(this).css('minHeight', h + 'px');
			});
			*/
			
			setTimeout(function() {
				$('.column').each(function() {
					$(this).css('height', '1%');
				});
				
				$('.column').each(function() {
					var h = $(this.parentNode.parentNode).height()-2;
					$(this).css('height', h + 'px');
				});
				
			}, 200);
			
		}
		
		
		$(".column").sortable({
			connectWith: ['.column'],
			handle: '.portlet-header',
			//revert: true,
			cursor: 'move',
			cursorAt: 'top',
			update: function(e) {
				updateLayoutCookie();
			},
			forceHelperSize: false,
			forcePlaceholderSize: true,
			//opacity: 0.5,
			tolerance: 'pointer'
		});

		//$(".column").css('border', 'dashed green 1px');
		/*
		$(".portlet").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
			.find(".portlet-header")
				.addClass("ui-widget-header ui-corner-all")
				.prepend('<span class="ui-icon ui-icon-plusthick"></span>')
				.end()
			.find(".portlet-content");
		*/
		
		$(".delete_portlet_btn").click(function() {
			//$(this).toggleClass("ui-icon-minusthick");
			$(this).parents(".portlet:first").remove();
			updateLayoutCookie();
			document.location.href=telegraf.siteUrl;
		});
		
		updateRegionHeight();
	});

