var setSelector = '#sidebar';
var setCookieName = 'sidebar';
var setCookieExpiry = 70;

function checkHeight()
{
	$.cookie('tweetHeight', $('#twitter').height(), { expires: setCookieExpiry, path: "/" });
}

// function that writes the list order to a cookie
function getOrder() {

	// save custom order to cookie
	$.cookie(setCookieName, $(setSelector).sortable("toArray"), { expires: setCookieExpiry, path: "/" });
}
 
// function that restores the list order from a cookie
function restoreOrder() {
	var list = $(setSelector);
	if (list == null) return
 
	// fetch the cookie value (saved order)
	var cookie = $.cookie(setCookieName);
	if (!cookie) return;
 
	// make array from saved order
	var IDs = cookie.split(",");

	// fetch current order
	var items = list.sortable("toArray");
	
	// make array from current order
	rebuild = {}
    for (v=0; v<items.length; v++ )
    { 
		rebuild[items[v]] = items[v];
    }

	for (var i = 0, n = IDs.length; i < n; i++)
	{
 		// item id from saved order
		var itemID = IDs[i];
		
		if(itemID != '' && itemID != 'ad')
		{
			if (itemID in rebuild) {
	
				// select item id from current order
				var item = rebuild[itemID];

				if($.cookie('div_' + itemID) == 'none')
				{
					$('#' + itemID).find(".portlet-content").toggle();
					$('#' + itemID).find(".ui-icon").toggleClass("ui-icon-minusthick");
				}
	 
				// select the item according to current order
				var child = $(setSelector).children("#" + item);
	 
				// select the item according to the saved order
				var savedOrd = $(setSelector).children("#" + itemID);
	 
				// remove all the items
				child.remove();
				
				// add the items in turn according to saved order
				// we need to filter here since the "ui-sortable"
				// class is applied to all ul elements and we
				// only want the very first!  You can modify this
				// to support multiple lists - not tested!
				try
				{
					$(setSelector).filter(":first").append(savedOrd);
				}
				catch(e) { }
			}
		}
	}
	return true;
}