$(document).ready(function(){

  // Fix all TOP links
  $("a[href^=#]").click(function(event){ event.preventDefault(); document.location.hash = $(this).attr('href'); });
  
  // Update all images with "hover" states
  $("img[data-hover]").each(function(){
    // Add a data-blur to the image
    $(this).attr('data-blur', $(this).attr('src'));
    // Add a hover event to the image
    $(this).hover(
      function(){
        $(this).attr('src',$(this).attr('data-hover'));
        },
      function(){
        $(this).attr('src',$(this).attr('data-blur'));
        }
      );
    });
    
  // If there is a messagewrapper on the page, clone/move it and add click-events
  if ($("#messagewrapper").length) {
    // Pull the existing messages from the HTML
    var messages = $("#messagewrapper").html();
    // Check to make sure there are messages present
    if (messages.length) {
      // And if so create/initialize the message wrapper
      // with the messages collected above
      create_message_wrapper(messages);
    }
  }
  
  // Register the flash banner if it's on this page
  $("#FlashID").ready(function(){ swfobject.registerObject("FlashID"); });
  
});

// --------------------------------- //
// -- COMMON FUNCTIONS ------------- //
// --------------------------------- //

function create_message_wrapper(messages)
{
  var header_object = $("#content-container h1:first");
  if (!header_object.length) { header_object = $("#content-container span:first"); }
  if ($("#messagewrapper").length) { $("#messagewrapper").remove(); }
  header_object.after("<ul id=\"messagewrapper\">"+messages+"</ul>\r\n");
  $("#messagewrapper").css('display', 'block').css('cursor', 'crosshair');
  // Add click-events to all status messages
  $("#messagewrapper li").live("click", function(event){
        event.preventDefault();
        $(this).animate({opacity:0.01,marginTop:"-10px"},300,"linear",function(){
          if ($(this).is("#messagewrapper li:only-child")) {
            $("#messagewrapper").animate({height:"1px"},300).remove();
            }
          else {
            $(this).remove();
            }
          });
        });
}
function new_status_message(colour, message)
{
  // First check to see if the messagewrapper is created
  if (!$("#messagewrapper").length)
  {
    // Create the wrapper if it doesn't exist
    create_message_wrapper('');
  }
  // Now add a message to the wrapper
  $("#messagewrapper").append("<li class=\""+colour+"\">"+message+"</li>");
}
