// Author:   http://theMechanism.com/
// Version:  13 Oct 2009
// -----------------------------------------------

confirmLoad = function()
{
  $("#viewport").addClass("is-loaded");
}

$(document).ready(
  function()
  {
    confirmLoad();
    attachAnchorBehaviour();
  }
);

// ----------------------------------------------

attachAnchorBehaviour = function()
{
  $("a[href$='.pdf'], a[href$='.doc'], a[href$='.xls'], a[href$='.ppt'], a[href$='.zip']").each(function(index)
  {
    var hrefParts = this.href.split(".");
    var fileType = hrefParts[hrefParts.length -1].toUpperCase();

    var titleValue = "Click to download " + fileType;

    this.title = titleValue;

  }).click(function(event)
  {
      openWindow(this.href);
        return false;
  });

  $("a[rel]").each(function(index)
  {
      switch(this.rel)
      {
          case 'external':
              this.title = "Link launches in new window";
              break;
      }
  }).click(function(event)
  {
      switch(this.rel)
    {
        case 'nofollow': case 'external':
            openWindow(this.href);
        return false;
            break;

        case 'within-page':
            scrollToSelector(this.href);
        return false;
            break;
    }
  });
}

// ----------------------------------------------
scrollToSelector = function(href)
{
    var modifiedHref = href.substring(href.indexOf("#")+1);
    var targetOffset = $("#" + modifiedHref).offset().top;

    $("html,body").animate(
    {
      scrollTop: targetOffset
    },1000);
}

openWindow = function(href)
{
  var attributes = "scrollbars=yes,toolbar=yes,menubar=yes,status=yes,directories=no,location=yes,resizable=yes";
  var windowOrTab = window.open(href,"",attributes);

  windowOrTab.focus();
}
