/*
  Hello anyone reading this!
  I understand that I have included some javascript that controls animations for the admin
  toolbar. I feel that I should mention that every time someone tries to load an admin page
  while not logged in, it gets logged and I get a nice email with all of your details. If you
  are just here to learn though, go ahead and read away!
*/

/* When the DOM has finished rendering */
$(document).ready(function() { 
  
  /* This inits the SHJS highlighting subsystem. Don't really have to do much more than this. */
  sh_highlightDocument();
  
  /* When the top bar with the plus is clicked the first time, ajax the new open toolbar in */
  $("#plus").click(function () {
      update_toolbar("/ajax/toolbar/open");
  });
  
  /* When the ajax stuff comes in, the onClick events are all messed up. So when it completes, we need to reattach the functions */
  $("#toolbar").ajaxComplete( function () {
    $("#plus").click(function () { /* if the user clicked the plus, the toolbar must be closed */
      update_toolbar("/ajax/toolbar/open"); /* which means we should open it */
    });
    $("#minus").click(function () { /* alternatively, if they clicked the minus then the toolbar is currently open */
      update_toolbar("/ajax/toolbar/closed"); /* so its time to close */
    });
  });
  
});

/* this is to update the top */
function update_toolbar(url) {
  $.ajax({ /* Since we are doing some weird stuff, it is just easier to go for the raw ajax function instead of the nicely abstracted ones */
      url: url, 
      cache: false,
      beforeSend: function(request) { /* before it sends, make sure that the toolbar is faded out so it looks good ;) */
        $("#toolbar").fadeOut("fast");
      },
      success: function(html) {
        $("#toolbar").empty(); /* clear out the div */
        $("#toolbar").append(html); /* put the new data in */
        $("#toolbar").fadeIn("fast"); /* and show it again */
      },
  });
}
