;(function($){
  $('<abbr>');
  
  /* plugin by Joel Birch */
  $.fn.checkEmail = function(emailField){
    return this.submit(function(){
      var email=$(this).find(emailField);
      var val=email.val();
      if (val!='' && val.match(/^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i) ) {
        return true;
      } else {
        var msg = (val=='') ? "The email address field is empty, please enter your email address."
          : "You have entered an invalid email address - please correct it before proceeding.";
        alert(msg);
        email.focus();
        return false;
      }
    });
  };
/*
  Sweet Titles (c) Creative Commons 2005
  http://creativecommons.org/licenses/by-sa/2.5/
  Author: Dustin Diaz | http://www.dustindiaz.com
  jQuery alterations: Joel Birch
*/
  $.st = { 
    xCord : 0,        // @Number: x pixel value of current cursor position
    yCord : 0,        // @Number: y pixel value of current cursor position
    tipElements : ['a','abbr','acronym'], // @Array: Allowable elements that can have the toolTip
    obj : Object,     // @Element: That of which you're hovering over
    $tip : Object,    // @Element: The actual toolTip itself in a jQuery object
    active : 0,       // @Number: 0: Not Active || 1: Active
    init : function() {
      this.$tip = $('<div>')
        .attr('id','toolTip')
        .appendTo("body")
        .css({
          'top':'0',
          'visibility':'hidden'});
      $(document).mousemove(this.updateXY);
      $(this.tipElements).each(function(i){
        $($.st.tipElements[i]+'[title]').each(function(){
          var $$ = $(this);
          $(this).data('tip',$$.attr('title')).hover($.st.tipOver,$.st.tipOut).removeAttr('title');
        });
      });
      $(window).unload(function() {
        $($.st.tipElements.join(','))
        .unbind('mouseover',$.st.tipOver)
        .unbind('mouseout',$.st.tipOut);
      });
    },
    updateXY : function(e) {
      $.st.xCord = e.pageX;
      $.st.yCord = e.pageY;
    },
    tipOut: function() {
      if ( window.tID ) clearTimeout(tID);
      if ( window.opacityID ) clearTimeout(opacityID);
      $.st.$tip.css({'visibility':'hidden'});
    },
    tipOver : function() {
      $.st.obj = this;
      tID = window.setTimeout("$.st.tipShow()",400);
    },
    tipShow : function() {		
        var scrX = Number(this.xCord),
        scrY = Number(this.yCord),
        tp = parseInt(scrY+15),
        lt = parseInt(scrX+10),
        anch = this.obj,
        $anch = $(anch),
        addy = '',
        access = '';
      if ( $anch.is('a') ) {
        addy = anch.href.replace('http://','');
        addy = (addy.length > 31) ? addy.substring(0,31)+"&#8230;" : addy;
        var access = ( anch.accessKey ) ? ' <span>['+anch.accessKey+']</span> ' : '';
      } else {
        addy = $anch.text();
      }
      var tip = $.st.$tip[0];
      $.st.$tip.html("<p>"+$anch.data('tip')+"<em>"+access+addy+"</em></p>");
      var left = ( $(document).width() - $(document).scrollLeft() < parseInt(tip.offsetWidth+lt) )
        ? parseInt(lt-(tip.offsetWidth+10))+'px'
        : lt+'px';
      var top = ( $(document).height()+$(document).scrollTop() < parseInt(tip.offsetHeight+tp) )
        ? parseInt(tp-(tip.offsetHeight+10))+'px'
        : tp+'px';
      $.st.$tip.css({
        'left':left,
        'top':top,
        'visibility':'visible',
        'opacity':'.1'
      });
      $.st.$tip.fadeTo(200,0.8);
    }
  };
  
  /* trigger site functionality before page load */
  $(function(){
    /* email address via AHAH */
    if ($("#contact").length){
      /* email validator */
      $("form").checkEmail("input.email");
      $("#content span.emailAddress").load("/assets/php/includes/infoAtLink.php");
    }
    
    /* initialise sweet tiles */
    $.st.init();
  });

})(jQuery);
