function serializeOrder() {
  var output = [];
  
  
  $(".sortable li").each(function(i) {
      output[i] = "\"" +
	$(this).attr('position') + "," +
	$(this).parents("li").attr("position") +
	"\"";
    });

  $("#pageOrder").val("[" + output + "]");

}

$(document).ready(function(){

    // jQuery UI Nested Sortables

    $("ul.sortable").nestedSortable({
      forcePlaceholderSize: true,
	  handle: 'div',
	  items: 'li',
	  opacity: .6,
	  placeholder: 'placeholder',
	  tabSize: 25,
	  tolerance: 'pointer',
	  toleranceElement: '> div',
	  stop: serializeOrder
      });

    $('#tabs').tabs({ cookie: { expires: 1 } });
    $('#tabs ul').css('margin-left','0');


    // Error and alert text
    $('.cl_errorText').addClass('ui-state-error').addClass('ui-corner-all');
    
    // Accordion
    $(".accordion").accordion({ header: "h3" });

    // Dialog
    $('#dialog').dialog({
      autoOpen: false,
	  width: 600,
	  buttons: {
	  "Ok": function() {
	    $(this).dialog("close");
	  },
	    "Cancel": function() {
	      $(this).dialog("close");
	    }
	}
      });

    // Dialog Link
    $('#dialog_link').click(function(){
	$('#dialog').dialog('open');
	return false;
      });

    // Datepicker
    $('#datepicker').datepicker({
	inline: true
	  });

    // Slider
    $('#slider').slider({
      range: true,
	  values: [17, 67]
	  });

    // Progressbar
    $('#progressbar').progressbar({
      value: 20
	  });

    $('table.padded th')
      .addClass('ui-state-default');
    
    $('.button, a.controls, .children li a, ul.subs li a')
      .addClass('ui-state-default')
      .addClass('ui-corner-all');

    //hover states on the static widgets
    $('#dialog_link, ul#icons li, .button, ul.subs li a').hover(
				 function() { $(this).addClass('ui-state-hover'); },
				 function() { $(this).removeClass('ui-state-hover'); }
				 );
    $('table.topNode a').hover(
			       function() { $(this).parent().addClass('hover')},
			       function() { $(this).parent().removeClass('hover')}
			       );
    
    // Error and alert text
    $('.cl_errorText').addClass('ui-state-error').addClass('ui-corner-all');
    $('.cl_infoText').addClass('ui-state-highlight').addClass('ui-corner-all');

	
    $("a.ajaxLoader").live("click", function(event) {
	event.preventDefault();
	var ajaxUrl = $(this).attr("href");

	

      });

    
    // Simple accordions    
    $(".aHead").live("click", function() {
	var aHead = $(this);
	var aBody = $(this).siblings(".aBody");

	
	aHead.closest("ul").find(".aHeadCurrent")
	  .addClass("aHead")
	  .removeClass("aHeadCurrent")
	  .siblings(".aBody")
	  .slideUp("fast");

	aBody.slideDown("fast");
      
	
	aHead.addClass("aHeadCurrent").removeClass("aHead");

	return(false);
      });
    
    $(".aHeadCurrent").live("click", function() {
	$(this)
	  .addClass("aHead")
	  .removeClass("aHeadCurrent")
	  .siblings(".aBody")
	  .slideUp("fast");
	return(false);
      });

    // Open accordion of it's set as the current
    $(".aHeadCurrent").each(function() {
	$(this)
	  .siblings(".aBody")
	  .show();
      });

    
    $(".bigCheck").each(function(i) {
	if ($("input",this).attr("checked")) {
	  $(this).closest("tr").addClass("selected");
	}
	else {
	  $(this).closest("tr").removeClass("selected");
	  $(".checkImage",this).show();
	}
      });

    $(".hostingPlan-wrap label").live("click", function() {
	return false;  // Fixme
      });
    
    $(".hostingPlan-wrap tr").live("click", function() {
	$("input",this).attr("checked","checked");
	$(".bigCheck").each(function() {
	    if ($("input",this).attr("checked")) {
	      $(".checkImage",this).fadeOut(5, function() {
		  $(this).closest("tr").addClass("selected");
		  $(this).fadeIn(300);
		});
	    }
	    else {
	      $(this).closest("tr").removeClass("selected");
	    }
	  });
      });

    flowplayer("a.flowPlayer", "flowplayer-3.1.5.swf");

    // Table Accordion

    $(".expander").live("click", function() {
	
	$(".expanded")
	  .parent()
	  .next(".expandable")
	  .find("td")
	  .removeClass("expandedCell");
	
	$(".expanded")
	  .removeClass("expanded")
	  .addClass("expander")
	  .parent()
	  .next(".expandable")
	  .find(".expandableContent")
	  .slideUp("fast");
	
	$(this)
	  .removeClass("expander")
	  .addClass("expanded")
	  .parent()
	  .next(".expandable")
	  .find(".expandableContent")
	  .slideDown("fast");

	$(this).parent().next(".expandable").find("td").addClass("expandedCell");

      });

    $(".expanded").live("click", function() {
	$(this)
	  .removeClass("expanded")
	  .addClass("expander")
	  .parent()
	  .next(".expandable")
	  .find(".expandableContent")
	  .slideUp("fast");
	$(this).parent().next(".expandable").find("td").removeClass("expandedCell");
      });
    
    // AJAX login

    $("a.loginBt").click(function() {
	var $topLogin = $("div.topLogin");
	var $loginBt = $(this);

	if ( $topLogin.is(":visible") ) {
	  $topLogin.slideUp();
	  $loginBt.removeClass("topLoginClose");
	}
	else {
	  $topLogin.load($(this).attr("href") + " #loginForm", function() {
	      $("label", $topLogin).each(function() {
		  $(this).prev("input").before(this);
		});
	      $loginBt.addClass("topLoginClose");
	      $topLogin.slideDown(function() {
		  $("input#cl_account").focus();
		});
	  });
	}
	
	return false;
      });
    
  });

