// set the list selector
var setSelector = "#column1,#column2";
// set the cookie name
var setCookieName = "listOrder";
// set the cookie expiry time (days):
var setCookieExpiry = 7;

function getOrder() {
	// save custom order to cookie
	var pos = [];
	$(setSelector).each(function(){ pos.push($(this).sortable("toArray")); })
	$.cookie(setCookieName, pos.join("|"), { expires: setCookieExpiry, path: "/" });
}

function restoreOrder() {
	// fetch the cookie value (saved order)
	var cookie = $.cookie(setCookieName); 
	if (!cookie) return;
	//alert(cookie);
	
	
	var colms = cookie.split("|");
	
	var allitems = new Array();

	$(setSelector).each(function(){
		
		var list = $(this); 
		if (list == null) return

		// make array from saved order
		var IDs = colms.shift().split(",");
		//alert(IDs.length);		
		// fetch current order
		var items = list.sortable("toArray");
		// make array from current order
		for ( var v=0, len=IDs.length; v<len; v++){
			allitems.push($("div.ui-sortable").children("#" + IDs[v]));
			$("div.ui-sortable").children("#" + IDs[v]).remove();
		}
	})
	
	var colms = cookie.split("|");
	
	$(setSelector).each(function(){
	
		//var list = $(setSelector);
		var list = $(this); 
		if (list == null) return
		
		// make array from saved order
		var IDs = colms.shift().split(",");
		//alert(IDs);	
		// fetch current order
		var items = list.sortable("toArray");
		// make array from current order
		var rebuild = new Array();
		for ( var v=0, len=IDs.length; v<len; v++ ){
			list.append(allitems.shift());
		}
	})
}
