/**
 * IE && FireBug compatibility to prevent errors in IE
 * debug; should be removed in production
 */
if (typeof console==='undefined')
	console  = {
		log : function () {},
		debug: function () {},
		trace:  function () {},
		dir:   function () {}
	};
	
(function($){
	var updateUpDown = function(sortable){
		$('dl:not(.ui-sortable-helper)', sortable)
			.removeClass('first').removeClass('last')
			.find('.up, .down').removeClass('disabled').end()
			.filter(':first').addClass('first').find('.up').addClass('disabled').end().end()
			.filter(':last').addClass('last').find('.down').addClass('disabled').end().end();
	};
	/**
	var moveUpDown = function(){
		var link = $(this),
			dl = link.parents('dl'),
			prev = dl.prev('dl'),
			next = dl.next('dl');
	
		if(link.is('.up') && prev.length > 0)
			dl.insertBefore(prev);
	
		if(link.is('.down') && next.length > 0)
			dl.insertAfter(next);
	
		updateUpDown(dl.parent());
	};
	*/

	
	var sortableChange = function(e, ui){
		if(ui.sender){
			var w = ui.element.width();
			ui.placeholder.width(w);
			
			ui.helper.css("width",ui.element.children().width());

		}
	};
	
	var sortableUpdate = function(e, ui){
		if(ui.element[0].id == 'trashcan'){
			emptyTrashCan(ui.item);
		} else {
			window.setTimeout ( function () { // timout to prevent weird timing problems in IE. Of cause IE
				updateUpDown(ui.element[0]);
				if(ui.sender) {
					updateUpDown(ui.sender[0]);
				}
			}, 1);
		}
	};
	
	var closeThisOne =  function () {
		 $(arguments[0].target).parents('DL').children('DD').slideToggle('fast');
		 var el = $(arguments[0].target);
		 if (el.text() == '-')
		 	el.text('+');
		else
			el.text('-');
	};
	
	var deleteThisOne = function () {
	 var target = arguments[0].target
	 $(target).parents('DL').slideToggle('fast', function () {
		 ($(target).parents('DL')).appendTo('#header');
	 	 
	 	 $(target).parents('DL').toggle();
	 });

	 
	}
	
	$(document).ready(function(){
		var els = ['#header', '#contentleft', '#contentright', '#contentmiddle'];
		var $els = $(els.toString());
		$('dt', $els).append('<span class="options"><a class="close" title="Fold in">-</a><a class="delete" title="Close">x</a></span>');
		
//		$('a.up, a.down').bind('click', moveUpDown);
		$('a.close').bind('click', closeThisOne);
		$('a.delete').bind('click', deleteThisOne);
//		$('#contentmiddle').corner('10px');
//		$('#contentleft').corner('10px');
//		$('#contentright').corner('10px');
	$('DD').corner ('round bottom 10px')
		$els.each(function(){
			updateUpDown(this);
		});

		$('#clickheader').bind('click', function () {
			$('#header').slideToggle('fast');
			var b = $('#clickheader span');
			if (b.text() =='+') 
				b.text('-');
			else
				b.text('+');
			
		}
		);
		
		
		$els.sortable({
			
			items: '> dl',
			handle: 'dt',
			cursor: 'move',
			//cursorAt: { top: 2, left: 2 },
			opacity: 0.8,
			helper: 'clone',
			appendTo: 'body',
			//placeholder: 'clone',
			placeholder: 'placeholder',
			connectWith: els,
			
			start: function(e,ui) {
				var w = ui.element[0].clientWidth;
				ui.placeholder.css('height', 50);
				ui.helper.css("width", w);
				
				if (e.target.parentNode.parentNode.id == 'header') {
					ui.helper.css("width", 200);
				}
				
			},
			change: sortableChange,
			update: sortableUpdate
		});
	});
	
	$(window).bind('load',function(){
		setTimeout(function(){
			$('#overlay').fadeOut(function(){
				$('body').css('overflow', 'auto');
			});
		}, 750);
	});
})(jQuery);
