$('.lhead').hover(
	function(){
		if ($(this).parent().siblings('.dragbox-content').css('display')=="none") {
			$(this).css('background-position', '-30px -15px');
		} else {
			$(this).css('background-position', '-15px -15px');
		}
	},
	function(){
		if ($(this).parent().siblings('.dragbox-content').css('display')=="none") {
			$(this).css('background-position', '-30px 0');
		} else {
			$(this).css('background-position', '-15px 0');
		}
	}
);

$(function(){
	$('.dragbox')
	.each(function(){
		$(this).find('.lhead').click(function(){
			$(this).parent().siblings('.dragbox-content').toggle();
			updateWidgetData();
			if ($(this).parent().siblings('.dragbox-content').css('display')=="none") {
				$(this).css('background', 'url(/img/icons3.gif) no-repeat -30px 0');
			} else {
				$(this).css('background', 'url(/img/icons3.gif) no-repeat -15px 0');
			}
		})
	});

	$('.column').sortable({
		connectWith: '.column',
		handle: '.bheadr',
		cursor: 'move',
		placeholder: 'placeholder',
		forcePlaceholderSize: true,
		opacity: 0.4,
		start: function(event, ui){
			//Firefox, Safari/Chrome fire click event after drag is complete, fix for that
			//if($.browser.mozilla || $.browser.safari) 
			//	$(ui.item).find('.dragbox-content').toggle();
		},
		stop: function(event, ui){
			ui.item.css({'top':'0','left':'0'}); //Opera fix
			// if(!$.browser.mozilla && !$.browser.safari)
				updateWidgetData();
		}
	})
	.disableSelection();
});

function updateWidgetData(){
	var sortorder='';
	$('.column').each(function(){
		$('.dragbox', this).each(function(i){
			var collapsed=0;
			if($(this).find('.dragbox-content').css('display')=="none") {
				collapsed=1;
			}
			sortorder+=$(this).attr('id')+','+collapsed+','+i+'&';
		});
	});
	/* alert(sortorder); */
	now=new Date();
	dauer=new Date(31536000000+now.getTime());
	document.cookie='fenster='+sortorder+'; expires='+dauer.toGMTString()+';';
}

