/***********************************************************
  externalLinks.js | Version 1
  Coded by Rob Ballou (rob@contrastsweb.com)
  Last updated: 2.7.02
  ----------------------------------------------------------
  This code changes the way that external links behave on
  any website that uses this code.
************************************************************/

function ExLinks(){
  this.safeURLs = new Array();
  this.options = new Object();
  this.options.externalTarget = 0;
  this.options.externalTargetWin = "";
  this.options.selfSafe = true;
  this.options.selfSafeInclusion = true;
}

new ExLinks();

/***********************************************************
  Public Methods
************************************************************/
/***********************************************************
  addSafeURL()
  ----------------------------------------------------------
  This method adds any number of URLs to the "safe" URL
  list (a list of URLs that the code ignores and does not
  change the behavior of).

  Example:
    var xLinks = new ExLinks();
    xLinks.addSafeURL("www.contrastsweb.com");

  Receives:
    Any number of strings that represent URLs that are to be
    considered safe. When checking for these URLs in a
    link's href attribute, the code checks only after the
    protocol notation (e.g. after the "http://"). If you
    want to make a certain folder of a site "safe", add the
    URL from the beginning (e.x. "www.w3.org/MarkUp/").
************************************************************/
ExLinks.prototype.addSafeURL = function (){
  for(var i=0; i<arguments.length; i++){
    if(typeof arguments[i] == "string")
      this.safeURLs.push(arguments[i]);
    }
}

/***********************************************************
  setExternalTarget
  ----------------------------------------------------------
  This method sets the targeting behavior of external links.

  Example:
    var xLinks = new ExLinks();
    xLinks.setExternalTarget("blank");

  Receives:
    The desired target for external links. This target may
    be represented as a string:
      "same"   opens the links in the current window
      "blank"  opens links in a new window each time

    Any other strings are treated as the name for the
    target window. You may also pass a number as the target:
      0        opens the links in the current window
      1        opens links in a new window each time
      2        opens links in a targeted window. If you want
               to use targeted windows, I would suggest
               sending the window name to this method instead
               of the number 2.
************************************************************/
ExLinks.prototype.setExternalTarget = function (target){
  if(typeof target == "number")
    this.options.externalTarget = target;
  else if(typeof target == "string"){
    if(target == "same")
      this.setExternalTarget(0);
    else if(target == "blank")
      this.setExternalTarget(1);
    else {
      this.setExternalTarget(2);
      this.setExternalTargetWin(target);
    }
  }
}

/***********************************************************
  setExternalTargetWin()
  ----------------------------------------------------------
  This method sets the targeted window for external links.
  This is used when you want all external links to open in
  specific window.

  Receives:
    A string specifiying the external links window.
************************************************************/
ExLinks.prototype.setExternalTargetWin = function (winName){
  if(typeof winName == "string")
    this.options.externalTargetWin = winName;
}

/***********************************************************
  setSelfSafe()
  ----------------------------------------------------------
  This method tells the link checker to ignor links from
  the site in the browser's address bar. By default, this
  option is already on.

  Receives:
    "true" (or a positive number) if you want the link
    checker to ignore the current web page's address.
    "false" (or zero or a negative number) if you want it 
    to be treated as an external link.
************************************************************/
ExLinks.prototype.setSelfSafe = function (safe){
  if(typeof safe == "boolean")
    this.options.selfSafe = safe;
  else if(typeof safe == "number"){
    if(safe > 0)
      this.setSelfSafe(true);
    else if(safe <= 0)
      this.setSelfSafe(false);
  }
}

/***********************************************************
  setSelfSafeInclusion()
  ----------------------------------------------------------
  Sets the inclusion option for the "self safe" feature.
  Self safe is on by default. It looks at the current
  windows URL and ignores everything from that domain (e.g.
  if the current address is "http://contrastsweb.com/links/",
  then the link checker will ignore everything on
  "http://contastsweb.com/").

  But if you want it to treat only the current address as the
  exact URL to treat as "safe" (in the example below it would
  treat everything other than "http://contrastsweb.com/links/"
  as external), then use this method to set the option to 
  false.

  Receives:
************************************************************/
ExLinks.prototype.setSelfSafeInclusion = function (inc){
  if(typeof safe == "boolean")
    this.options.selfSafeInclusion = safe;
  else if(typeof safe == "number"){
    if(safe > 0)
      this.setSelfSafeInclusion(true);
    else if(safe <= 0)
      this.setSelfSafeInclusion(false);
  }
}

/***********************************************************
  updateLinks()
  ----------------------------------------------------------
  This is the link checker. It checks all the links on the
  page and changes the link's target as necessary.
************************************************************/
ExLinks.prototype.updateLinks = function (){
  if(document.links){
    // If we count the current URL as "safe"
    // make sure we add it to the list
    if(this.options.selfSafe)
      this.addSelfAsSafe();

    // Update all the links to have the correct
    // target
    for(var i=0; i<document.links.length; i++){
      // Parse HREF
      var link = document.links[i].href;
      link = link.split(/.*:\/\//);
      link = link[0];
      // Check if it is "safe"
      var safeLink = false;
      for(var n=0; n<this.safeURLs.length; n++){
        if(link.indexOf(this.safeURLs[n]) == 0)
          safeLink = true;
      }
      // Set target
      if(!safeLink){
        if(this.options.externalTarget == 0) // this window
          document.links[i].target = "_self";
        else if(this.options.externalTarget == 1) // blank window
          document.links[i].target = "_blank";
        else // targeted window
          document.links[i].target = this.options.externalTargetWin;
      }
    }
  }
}

/***********************************************************
  updateSettings()
  ----------------------------------------------------------
  This method checks the state of a checkboxto see how it 
  should treat external links. For more info, look on the 
  website.

  Receives:
    The name of the checkbox
    The second argument, assume, is optional. If you pass
    "true", it will treat this to mean that if the checkbox
    is checked, to open external links in a new window (which
    is the default), but if you pass "false" it treats it
    in the opposite manner (if the checkbox is NOT checked,
    the link checker will open external links in a new
    window).
************************************************************/
ExLinks.prototype.updateSettings = function (input, assume){
  if(document.getElementById){
    var thisInput = document.getElementById(input);
    if(!thisInput) return false;
    if(thisInput.type == "checkbox"){
      if((assume) && (thisInput.checked))
        this.setExternalTarget(1);
      else if(((assume == "") || (assume == null)) && (thisInput.checked))
        this.setExternalTarget(1);
      else
        this.setExternalTarget(0);
    }
    this.updateLinks();
  }
}

/***********************************************************
  Private Methods
************************************************************/
ExLinks.prototype.addSelfAsSafe = function (){
  var winLoc = ""+ window.location +"";
  winLoc = winLoc.split(/.*:\/\//);
  winLoc = winLoc[0];
  if(this.options.selfSafeInclusion){
    winLoc = winLoc.split('/');
    this.addSafeURL(winLoc[0]);
  }
  else
    this.addSafeURL(winLoc);
}