$(document).ready(
	function()
	{
		// Toggle Single Portlet
		$('a.toggle').click(function()
			{
				if(this.className == 'red-toggle'){
					this.className = 'toggle'
				}else{
					this.className = 'red-toggle'
				}
				$(this).parent('div').next('div').toggle();
				writeCookies();
				return false;
			}
		);
		// Invert All Portlets
		$('a#all_invert').click(function()
			{
				$('div.portlet_content').toggle();
				return false;
			}
		);

		// Expand All Portlets
		$('a#all_expand').click(function()
			{
				$('div.portlet_content:hidden').show();
				return false;
			}
		);

		// Collapse All Portlets
		$('a#all_collapse').click(function()
			{
				$('div.portlet_content:visible').hide();
				return false;
			}
		);

		// Open All Portlets
		$('a#all_open').click(function()
			{
				$('div.portlet:hidden').show();
				$('a#all_open:visible').hide();
				$('a#all_close:hidden').show();
				return false;
			}
		);

		// Close All Portlets
		$('a#all_close').click(function()
			{
				$('div.portlet:visible').hide();
				$('a#all_close:visible').hide();
				$('a#all_open:hidden').show();
				return false;
			}
		);

		// Controls Drag + Drop
		$("#columns td").sortable({
			accept: 'portlet',
			helperclass: 'sort_placeholder',
			opacity: 0.7,
			tolerance: 'intersect',
			 	stop: function(event, ui) {
			       var order = $(this).sortable('serialize');
			       setCookie('sortable', order);
			 	}
		});
		var c = getCookie('sortable');
		if (c) {
		    $.each(c.split('&'), function () {
		        var id = this.replace('[]=', '-');
		        $('#' + id).appendTo('#columns td');
		    });
		}
		
		var i = 1;
		while(hiddenBlock = getCookie('hidden_div['+i+']')){
			if(hiddenBlock.indexOf('portlet_content_') != -1){
				document.getElementById(hiddenBlock).style.display = "none";
				document.getElementById('toggle_'+hiddenBlock).className = 'red-toggle';
			}
			i++;
		}
		
	}
);

function writeCookies(){
	var divArr = document.getElementsByTagName('div');
	var j = 1;
	for(i=1; i<divArr.length; i++){
		if(divArr[i].style.display == "none"){
			setCookie('hidden_div['+j+']', divArr[i].id);
			j++;
		}
	}
}

function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}