
$.fn.pause = function(duration) {
  $(this).animate( { dummy: 1 }, duration );
  return this;
};

$.tablesorter.addParser({
  id: "date",
  is: function(s) {
    return /^\d{2} \w{3} \d{4}$/.test(s);
  },
  format: function(s) {
    var p = new Date(s).getTime();
    return p;
  },
  type: "numeric"
});

$.tablesorter.addParser({
  id: "datetime",
  is: function(s) {
    return /^\d{2} \w{3} \d{4} \d{2}:\d{2} \(\w{3}\)$/.test(s);
  },
  format: function(s) {
    var p = new Date(s).getTime();
    return p;
  },
  type: "numeric"
});

$(document).ready(function() { 
  // initialise any redirect buttons that simply change page
  $("button.redirect").click(function() {
    if( $(this).data("url") ) {
      window.location.href = $(this).data("url");
    }
  });

  // initialise confirmboxes for hyperlinks
  $("a.confirmbox").click(function(e) {
    if( !confirm("Are you sure?") ) {
      return e.preventDefault();
    }
  });

  // initialise any sortable tables
  $("table.sortable").tablesorter();

  // initialise any tooltips
  $(".tipside [title]").tooltip({position: "center right", offset: [0, 4], opacity: 0.9}); 
  $(".tiptop [title]").tooltip({position: "top left", offset: [-4, 610], opacity: 0.9}); 

  // tell any fadeable items to fade after 5 seconds
  $("div.fadeout").pause(5000).fadeTo("slow", 0);

  $("#help_content").dialog( {
    modal: true,
    width: 500,
    height: 500,
    autoOpen: false,
    position: "fixed",
    open: function(e, ui) { $(this).scrollTop(0); }
  } );

  $(".help_button").click(function() {
    $.post("help.php", { name: $(this).data("name"), script: $(this).data("script") }, function(data) {
      $("#help_content").dialog("option", "title", data.title); 
      $("#help_content").html(data.content).dialog("open"); 
    }, "json");
  });

  // print links
  $(".print_page").click(function(e) {
    window.print();
    e.preventDefault();
  });
});


