
(function($) {
	var createHelper = function(event, $item) {
		return $item.clone().css({
			 width: $item.width() + 'px'
		}).addClass('ui_sortable_helper');
	};

	var setupHelper = function(event, ui) {
		var  ipl = parseInt(ui.item.css('padding-left')) || 0
			,ipr = parseInt(ui.item.css('padding-right')) || 0
			,ibr = parseInt(ui.item.css('border-right-width')) || 0
			,ibl = parseInt(ui.item.css('border-left-width')) || 0
			,isum = ipl+ipr+ibl+ibr
		;

		if (ui.sender) {
			var width = ui.element.width() - isum;
			if (ui.item.attr('id') == 'panel_widgetCalendar' && ui.element.attr('id') == 'place_centerColumn')
				width = 404;
			if (ui.item.attr('id') == 'panel_widgetNewsletter' && ui.element.attr('id') == 'place_centerColumn')
				width = 189;

			ui.helper.animate(
				 { 'width': width + 'px' }
				,{ duration: 'fast' }
			);
		}
	}


	var savePanelsPositions = function(event, ui) {
		var cookie = '';
		$(ui.options.connectWith.toString()).each(function(idx, place) {
			cookie += '#' + place.id + ':';

			$(ui.options.items, place).each(function(idx, panel) {
				cookie += '#' + panel.id + ',';
			});
			if (cookie.substr(cookie.length-1, cookie.length) == ',') {
				cookie = cookie.substr(0, cookie.length-1) + '/';
			}
			else {
				cookie += '/';
			}
		});
		cookie = cookie.substr(0, cookie.length-1);

		$.cookie($("body").attr('class')+'_panels_positions', cookie, { expires: 365, path: '/' });
	}

	var restorePanelsPositions = function() {
		var places = $.cookie($("body").attr('class')+'_panels_positions');
		if (places === null) {
			return false;
		}

		places = places.split('/');
		$.each(places, function(idx, place) {
			var placeAndPanels = place.split(':');
			var placeId = placeAndPanels[0];
			var panelsIds = placeAndPanels[1].split(',').reverse();

			$.each(panelsIds, function(idx, panel) {
				if (panel != "") {
					$(panel).prependTo(placeId);
				}
			});
		});
	};


	$(document).ready(function() {
		var  dropPlaces = ['.leftColumn, .centerColumn, .rightColumn']
			,$dropPlaces = $(dropPlaces.toString())
			,saveCookieDelayTimer = null
		;

		$dropPlaces.sortable({
			 //items:  '.widgetNewsletter, .widgetCalendar, .widgetProjectsMenu, .widgetNews'
			 items:  '.ui_sortable_panel'
			,handle: '.widgetHeader'
			,connectWith: dropPlaces
			,tolerance: 'pointer'
			,cursor: 'move'
			,opacity: 0.8
			,revert: true
			,placeholder: 'ui_sortable_placeholder'
			,forcePlaceholderSize: true

			,helper: createHelper

			,start: function(event, ui) {
				if (saveCookieDelayTimer) {
					clearTimeout(saveCookieDelayTimer);
				}
			}
			,change: function(event, ui) {
				setupHelper(event, ui);
			}
			,update: function(event, ui) {
				saveCookieDelayTimer = setTimeout(function () {
					savePanelsPositions(event, ui);
				}, 1500);
			}
		}); // sortable() end


		restorePanelsPositions();
	}); // ready() end
})(jQuery);


var getCookieString = function(parentId) {
	var  cookie = '#' + parentId + ':'
		,children = []
	;

	$('#'+parentId).children().each(function(idx, child) {
		children.push('#'+child.id);
	});

	return cookie + children.toString();
}

var decodeCookieString = function(cookieString) {
	var  elements = cookieString.split(':')
		,parentId = elements[0]
		,children = elements[1].split(',')
		,obj = {}
	;

	obj[parentId] = children;
	return obj;
}
