(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 addItem = function(){
		var sortable = $(this).parents('.ui-sortable');
		var options = '<span class="options"><a class="up">up</a><a class="down">down</a></span>';
		var tpl = '<dl class="sort"><dt>{name}' + options + '</dt><dd>{desc}</dd></dl>';
		var html = tpl.replace(/{name}/g, 'Dynamic name :D').replace(/{desc}/g, 'Description');
	
		sortable.append(html).sortable('refresh').find('a.up, a.down').bind('click', moveUpDown);
		updateUpDown(sortable);
	};
	*/
	
	var removeItem = function(){
		//var link = $(this),dl = link.parents('dl'),prev = dl.prev('dl'),next = dl.next('dl');
		var link = $(this), dl = link.parents('dl');
		
	    $(dl).hide("blind", { direction: "vertical" },500);

	    //$(dl).remove;
	
		updateUpDown(dl.parent());
		
		var this_id = "#check_" + dl.attr("id");
		$(this_id).attr('checked', false);
	};
	
	/*
	var emptyTrashCan = function(item){
		item.remove();
	};
	*/
	
	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 {
			updateUpDown(ui.element[0]);
			if(ui.sender)
				updateUpDown(ui.sender[0]);
		}
	};
	
	$(document).ready(function(){
	
	    var els = ['#col1', '#col2', '#col3'];
		var $els = $(els.toString());

		// NAV BAR CMS CONTROLS
  
		 // Add onclick handler to each checkbox 
		 //$("#check_nav_1").click(function()
		 //	{
		 //  	if ($("#check_nav_1").is(":checked")) $("#Nav_1").show("fast");
		 //  	else $("#Nav_1").hide("fast");
		 //	});
		 
		 $("#check_nav_2").click(function()
			{
		  	if ($("#check_nav_2").is(":checked")) $("#Nav_2").show("fast");
		  	else $("#Nav_2").hide("fast");
			});
		 $("#check_nav_3").click(function()
			{
		  	if ($("#check_nav_3").is(":checked")) $("#Nav_3").show("fast");
		  	else $("#Nav_3").hide("fast");
			});
		 $("#check_nav_4").click(function()
			{
		  	if ($("#check_nav_4").is(":checked")) $("#Nav_4").show("fast");
		  	else $("#Nav_4").hide("fast");
			});
		 $("#check_nav_5").click(function()
			{
		  	if ($("#check_nav_5").is(":checked")) $("#Nav_5").show("fast");
		  	else $("#Nav_5").hide("fast");
			});
		 $("#check_nav_6").click(function()
			{
		  	if ($("#check_nav_6").is(":checked")) $("#Nav_6").show("fast");
		  	else $("#Nav_6").hide("fast");
			});
		 $("#check_nav_7").click(function()
			{
		  	if ($("#check_nav_7").is(":checked")) $("#Nav_7").show("fast");
		  	else $("#Nav_7").hide("fast");
			});
      
		// CMS blocks
		//check_id_calc
		$("#check_id_1").click(function()
			{
		  	if ($("#check_id_1").is(":checked")) $("#id_1").show("blind", { direction: "vertical" },500);
		  	else $("#id_1").hide("blind", { direction: "vertical" },500);
			});
		
		//market snapshots
		$("#check_id_2").click(function()
			{
		  	if ($("#check_id_2").is(":checked")) $("#id_2").show("blind", { direction: "vertical" },500);
		  	else $("#id_2").hide("blind", { direction: "vertical" },500);
			});
		//client area
		$("#check_id_3").click(function()
			{
		  	if ($("#check_id_3").is(":checked")) $("#id_3").show("blind", { direction: "vertical" },500);
		  	else $("#id_3").hide("blind", { direction: "vertical" },500);
			});
		//markets
		$("#check_id_4").click(function()
			{
		  	if ($("#check_id_4").is(":checked")) $("#id_4").show("blind", { direction: "vertical" },500);
		  	else $("#id_4").hide("blind", { direction: "vertical" },500);
			});
		//get in touch
		$("#check_id_5").click(function()
			{
		  	if ($("#check_id_5").is(":checked")) $("#id_5").show("blind", { direction: "vertical" },500);
		  	else $("#id_5").hide("blind", { direction: "vertical" },500);
			});
		//news
		$("#check_id_6").click(function()
			{
		  	if ($("#check_id_6").is(":checked")) $("#id_6").show("blind", { direction: "vertical" },500);
		  	else $("#id_6").hide("blind", { direction: "vertical" },500);
			});
		$("#check_id_7").click(function()
			{
		  	if ($("#check_id_7").is(":checked")) $("#id_7").show("blind", { direction: "vertical" },500);
		  	else $("#id_7").hide("blind", { direction: "vertical" },500);
			});
		$("#check_id_8").click(function()
			{
		  	if ($("#check_id_8").is(":checked")) $("#id_8").show("blind", { direction: "vertical" },500);
		  	else $("#id_8").hide("blind", { direction: "vertical" },500);
			});
		$("#check_id_9").click(function()
			{
		  	if ($("#check_id_9").is(":checked")) $("#id_9").show("blind", { direction: "vertical" },500);
		  	else $("#id_9").hide("blind", { direction: "vertical" },500);
			});
		$("#check_id_10").click(function()
			{
		  	if ($("#check_id_10").is(":checked")) $("#id_10").show("blind", { direction: "vertical" },500);
		  	else $("#id_10").hide("blind", { direction: "vertical" },500);
			});
		
		//create tool bar
		//$('dt', $els).append('<a class="movable"></a><a class="remove"></a>');
		
		// bind remove link to remove function
		$('a.remove').bind('click', removeItem);
		
		$els.each(function(){updateUpDown(this);});
		
		$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) {
				ui.helper.css("width", ui.item.width());
			},
			change: sortableChange,
			update: sortableUpdate
		});
	});
	
	$(window).bind('load',function(){
		setTimeout(function(){
			$('#overlay').fadeOut(function(){
				$('body').css('overflow', 'auto');
			});
		},10);
	});
})(jQuery);

	// preload images for rollovers
	(function($) {
	  var cache = [];
	  // Arguments are image paths relative to the current page.
	  $.preLoadImages = function() {
	    var args_len = arguments.length;
	    for (var i = args_len; i--;) {
	      var cacheImage = document.createElement('img');
	      cacheImage.src = arguments[i];
	      cache.push(cacheImage);
	    }
	  }
	})(jQuery)

function serial_cms ()
	{
	$('#col1_serialized').val($('#col1').sortable('serialize'));
	$('#col2_serialized').val($('#col2').sortable('serialize'));
	$('#col3_serialized').val($('#col3').sortable('serialize'));
	//return true;
	}