$(document).ready(function() {
  // Apply the panelLoader function to all the relevant links on the page
  // We use the live command to automatically bind the event to all future links 
  // as they are loaded from the panelLoader 
  $('.panelLoader').live("click" ,panelLoader);
});

function upDateMenu(section,sub_section){
	var section, sub_section;
	$('#nav .panelLoader, #nav ul').removeClass('on');
  $('#nav .panelLoader[rel="'+section+'"]:visible, #nav .panelLoader[rel="'+section+'"] ~ ul, #nav li ul .panelLoader[rel="'+sub_section+'"]').addClass('on');
}

// PanelLoader function to 
function panelLoader() {
  $('body').css('cursor', 'wait');
  
  // Unload old panel data
  panel_unloader();

  // Fetch Data for the panelScroller
  $.get(vrootdir+'/panel.php', {panel: $(this).attr('rel')}, panelScroller); 
  return false;
}

function panelSubmitLoader(event) {

  // Disable the default submit browser submit action
  // This means we don't have to return false to prevent a HTTP submit 
  event.preventDefault();

  // Check the result of the previous triggered method for this event if there was one
  if( (event.result) || (event.result==undefined) ) {

    $('body').css('cursor', 'wait');
    
    // Unload old panel data
    panel_unloader();
  
    // POST Data and send response through the panelScroller
    var formData = $(this).serialize();
    $.post(vrootdir+'/panel.php', formData, panelScroller);
  }
}

function panelScroller(data) {
  $('.panel').addClass('scrollout');
  $(data).addClass('scrollin').appendTo('.panel_scroller');
    
  var newHeight = $('.scrollin').height();
  var oldHeight = $('.scrollout').height();
    
  // Scroll down the footer if required
  if(newHeight > oldHeight) {
    $('.scrollin').height(oldHeight).show().animate({height:newHeight}, 'slow');
  } else {
   $('.scrollin').show();
  }
  
  $('.scrollout').animate({marginLeft: -1020, height:newHeight}, 'slow', function() {
    $('.scrollin').removeClass('scrollin');
    $('.scrollout').remove();
    
    // Load New panel data
    panel_loader();
    $('body').css('cursor', 'auto');
  });
}

// Add empty functions that can be overridden later
function panel_loader() {}
function panel_unloader() {}
