/*Create un-scrapable mailto links

  instead of <a href="mailto:name@domain.com">name@domain.com</a>

      use <a href="name(at)domain.com" class="email">name(at)domain.com</a>

*/

jQuery.fn.mailto = function() {

        return this.each(function(){

                var email = $(this).html().replace(/\s*\(.+\)\s*/, "@");

                $(this).before('<a href="mailto:' + email + '" rel="nofollow" title="Email ' + email + '">' + email + '</a>').remove();

        });

};



//Suround character string (pat) with <sup>...</sup>

jQuery.fn.superscript = function(pat) {

  function innerHighlight(node, pat) {

    var skip = 0;

    if (node.nodeType == 3) {

      var pos = node.data.toUpperCase().indexOf(pat);

      if (pos >= 0) {

        var spannode = document.createElement('sup');

        var middlebit = node.splitText(pos);

        var endbit = middlebit.splitText(pat.length);

        var middleclone = middlebit.cloneNode(true);

        spannode.appendChild(middleclone);

        middlebit.parentNode.replaceChild(spannode, middlebit);

        skip = 1;

      }

    } else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {

      for (var i = 0; i < node.childNodes.length; ++i) {

        i += innerHighlight(node.childNodes[i], pat);

      }

    }

    return skip;

  }

  return this.each(function() {

    innerHighlight(this, pat.toUpperCase());

  });

};



$(document).ready(function(){

  //Fix Mailto Anchors

  $('.email').mailto();



  //Add Animation to Navigation links

  $('.navlink, .sidemenu li a').click( function(ev){

      //prevent the default action of the event, this will stop the href in the anchor being followed

      //before the animation has started, you can also use return false;

      ev.preventDefault();

      //store a referene to the anchor tag

      var $self=$(this);

      //get the "main" div  and animate

      $('#main').hide( "slow", function(){

          //now get the anchor href and redirect the browser

          document.location = $self.attr('href');

      });

  });



  //Suround all &reg; characters with <sup>...</sup>

  $('body').superscript(String.fromCharCode(174));



});



// Group of functions that handle the drop down navigation menus

var curdiv = "";

var menu_timeout;

function onmenu(btnobj, menudiv, e) {

  if (menu_timeout) clearTimeout(menu_timeout);

  if ((curdiv != menudiv) && (curdiv != "")) {

    hidemenu(curdiv);

  }

  var divobj = document.getElementById(menudiv);

  divobj.style.top = (findPosY(btnobj)+30)+'px';

  divobj.style.left = findPosX(btnobj)+'px';

  divobj.className = divobj.className.replace("hidden", "shown");

  curdiv=menudiv;

}

function hidemenu(divid) {

  if ((divid=="") && (curdiv != "")) {

    divid = curdiv;

  }

  if (divid != '') {

    var divobj = document.getElementById(divid);

    divobj.className = divobj.className.replace("shown", "hidden");

  }

}

function mouseOut(menudiv) {

  divid = document.getElementById(menudiv).id;

  menu_timeout = setTimeout("hidemenu('"+divid+"')", 250);

}

function ondiv(divobj, e) {

  if (menu_timeout) clearTimeout(menu_timeout);

}



function findPosX(obj)

{

  var curleft = 0;

  if(obj.offsetParent)

      while(1)

      {

        curleft += obj.offsetLeft;

        if(!obj.offsetParent)

          break;

        obj = obj.offsetParent;

      }

  else if(obj.x)

      curleft += obj.x;

  return curleft;

}



function findPosY(obj)

{

  var curtop = 0;

  if(obj.offsetParent)

      while(1)

      {

        curtop += obj.offsetTop;

        if(!obj.offsetParent)

          break;

        obj = obj.offsetParent;

      }

  else if(obj.y)

      curtop += obj.y;

  return curtop;

}

// End of Navigation menu functions