$(document).ready(function(){
	$('a#check_all').unbind();
	$('a#check_all').bind('click',function(){
		$('input:checkbox').attr('checked','checked');
		return false;
	});
	
	$('a#uncheck_all').unbind();
	$('a#uncheck_all').bind('click',function(){
		$('input:checkbox').attr('checked','');
		return false;
	});
});


function store_widget_position()
{	  
		lcolValue = new Array();
		ccolValue = new Array();
		rcolValue = new Array();
		
	
		$('#lcol').children().each(function(){
			widget_id = $(this).attr('id').split('widget-');
			lcolValue.push(widget_id[1]);
		});
		$('#ccol').children().each(function(){
			widget_id = $(this).attr('id').split('widget-');
			ccolValue.push(widget_id[1]);
		});
		$('#rcol').children().each(function(){
			widget_id = $(this).attr('id').split('widget-');
			
			rcolValue.push(parseInt(widget_id[1]));
		});
		
		$.cookie("lcol",lcolValue.join('-'), { expires: 31, path: '/' });
		$.cookie("ccol",ccolValue.join('-'), { expires: 31, path: '/' });
		$.cookie("rcol",rcolValue.join('-'), { expires: 31, path: '/' });
		$.cookie("widgets",'true', { expires: 31, path: '/' });
}
		
$(function() {		
	//create widgets
	$(".widget-column").sortable({
			connectWith: '.widget-column',
			revert: 300,
			handle: 'h3',
			update: function() {
				store_widget_position();
			}
	});
	
	$(".widget").addClass("ui-widget ui-widget-content ui-helper-clearfix ui-corner-all")
		.find(".widget-header")
		.addClass("ui-widget-header ui-corner-all")
		.css('cursor','move')
		.end()
	$(".widget")
	
	$(".widget-header").hover(function() {
		$(this).parents(".widget").addClass("widget-hover");
	},
	function() {
		$(this).parents(".widget").removeClass("widget-hover");
	})
	
	$(".widget-header .ui-icon").click(function() {
		if(confirm('Are you sure you want to remove this widget?'))
		{
			//remove widget
			var widget = $(this).parents(".widget:first");
			widget.remove();
			
			//store widget position
			store_widget_position();
		}
	});
	
	//customise widgets
	$('#customise-widgets-link').click(function(e) {  
			e.preventDefault(); //cancel the default behavior
			
			//show customise widget panel
			$('#customise-widgets').show('blind', {}, 1000);
			
			//update link clicks to close the panel				
			$('#close-customise-widgets-link').click(function(e) {  
					e.preventDefault(); //cancel the default behavior
					$('#customise-widgets').hide(10);
				});
		});
	
	$(".item-control").addClass("item-control-expand");
	$(".item-control").parent().css('list-style','none');
	$(".item-control").attr('title','Show summary');
	$(".item-control").toggle(function() { $(this).attr('title','Hide summary'); }, function() { $(this).attr('title','Show summary'); });
	$(".item-control").click(function() {
		$(this).toggleClass("item-control-expand").toggleClass("item-control-collapse");
		$(this).parent().find(".item-desc").toggle(0);
		store_widget_position();
	});
});

