/******************************************************************************
*                                                                             *
* This Javascript iterates through the page causing certain links to pop up   *
* windows when clicked. I got much assistance in writing this from            *
* <alistapart.com> and <sitepoint.com>.                                       *
*                                                                             *
* Author:   Michael A. Smith                                                  *
* Modified: 12/20/05                                                          *
*                                                                             *
******************************************************************************/

/** Performs the popUp action */
function popupWin(link, attribs, rel) {

  // rel describes the relationship of the new window
  if (rel == "") rel = "_blank";

	window.open(link, rel, attribs);
} // end popupWin

/** Returns true if the href doesn't end in asp, html, htm, or xml */
function isSpecialFile(href) {
	href = cleanHashFromURL(href);
	href = cleanParamsFromURL(href);
  var fileExt = getFileExtension(href);

  if (fileExt == "") return false;  // If there's no file extension, bail
  return ((fileExt != "asp") &&
          (fileExt != "html") &&
          (fileExt != "htm") &&
          (fileExt != "xml"));
} // end isSpecialFile

/** Gets the file extension. Returns an empty string if unknown. */
function getFileExtension(href) {
  if (href.indexOf(".") < 0) return ""; // If there's no "." in the URL, bail

  var extArray = href.split(".");
  var lastIndex = [extArray.length - 1];
  var ext = extArray[lastIndex];

  // If there's a "/" in the extension, bail
  if (ext.indexOf("/") > -1) return "";

  return ext.split("?")[0]; // If there's a ? in the extension, truncate there
} // end getFileExtension


/** Gets the protocol (http, https, mailto, etc...) */
function getURLProtocol(href) {
  /* Gecko browsers return the actual href attribute. IE and Opera return the
     calculated complete URL from the protocol on. */
  var prot = href.split(":")[0];

  // if there's a slash or pound in the prot we assume it's http (for geckos)
  if (prot.indexOf("/") > -1) return "http";
  if (prot.indexOf("#") > -1) return "http";

  return prot;
} // end getURLProtocol

/** clean hashes from urls */
function cleanHashFromURL(url) {
  var index = url.indexOf("#");
  if (index < 0) return url;

  return url.substring(0, index);
} // end cleanHashFromURL

/** clean parameters from urls */
function cleanParamsFromURL(url) {
  var index = url.indexOf("?");
  if (index < 0) return url;

  return url.substring(0, index);
} // end cleanParamsFromURL


/** Returns false if the href can be determined to be an external link. */
function isInternal(href) {

  href = cleanHashFromURL(href); // if the URL has a # get rid of it

  // count as internal if there are no "/"s in the URL
  if (href.indexOf("/") < 0) return true;

  // split the URL into an array of strings across the "/" character
  var hrefSplit = href.split("/");

  // if gecko use the simple method of looking for "http" in the URL.
  if (!(document.all || window.opera))
    return (hrefSplit[0].indexOf("http") < 0);

  // if the browser is not gecko...

  // "url" is from the href attribute of the anchor tag
  var url = "";
  if (hrefSplit[2]) url = hrefSplit[2];

  // httpHost is the 2nd string from the window.location.href split across "/"
  var locHrefSplit = window.location.href.split("/");
  var httpHost = "";
  if (locHrefSplit[2]) httpHost = locHrefSplit[2];

  /* Count as internal if the url or the current site url doesn't exist
     (shouldn't ever happen) */
  if (!(url && httpHost)) return true;

  // otherwise check that the current site matches the url
  return (url == httpHost);
} // end isInternal


/** determine whether or not the popup should happen */
function isPopUp(rel, href){

  // if the href value is empty or null, don't pop up
  if (!href) return false;

  // if the URL protocol is not http or https, don't popup
  if (getURLProtocol(href).indexOf("http") < 0) return false;

  // if the rel attribute exists, pop up
  if (rel) return true;

  // if the link is to an image, pdf, or other special file, pop up
  if (isSpecialFile(href)) return true;

  // if the link is to an external file, pop up
  return (!isInternal(href));
} // isPopUp

/**
  This is the function you call when the page loads. It iterates through the
  page looking for anchor tags to which to attach.
*/
function popupWindows() {
  /*
    check that the user agent is modern enough to perform necessary
    operations.
  */
  if(!document.getElementsByTagName) return;

  // get an array of all anchor tags on the page
  var anchors = document.getElementsByTagName("a");
  // loop through all the anchor tags
  for (var i = 0; i < anchors.length; i++) {
    // current anchor
    var anchor = anchors[i];
    // href attribute
    var href = anchor.getAttribute("href");
    // rel attribute
    var rel = anchor.getAttribute("rel");

    // if we shouldn't pop up, go to the next step in the for loop
    if (!isPopUp(rel, href)) continue;

    // at this point we've determined that we should popup the link

    // default window attributes
    var winWidth =      650;
    var winHeight =     350;
    var winLocation =   "yes";
    var winMenubar =    "yes";
    var winStatus =     "yes";
    var winScrollbars = "yes";
    var winResizable =  "yes";
    var winToolbar =    "yes";

    // rel can change the attributes
		// @@@ status bar option not respected by firefox?
		switch (rel) {
			case "sidebar":
				//just in time breakout
				continue;
				break; // end case "sidebar"
			case "claim":
				winWidth =      801;
				winHeight =     700;
				winLocation =   "no";
				winMenubar =    "no";
				winStatus =     "no";
				// winScrollbars = "yes";
				// winResizable =  "yes";
				winToolbar =    "no";
				break; // end case "claim"
			case "policy":
				winWidth =      801;
				winHeight =     700;
				winLocation =   "no";
				winMenubar =    "no";
				winStatus =     "no";
				// winScrollbars = "yes";
				// winResizable =  "yes";
				winToolbar =    "no";
				break; // end case "claim"
			case "standard":
				winWidth =      801;
				winHeight =     700;
				// winLocation =   "yes";
				// winMenubar =    "yes";
				// winStatus =     "yes";
				// winScrollbars = "yes";
				// winResizable =  "yes";
				// winToolbar =    "yes";
				break; // end case "standard"
		} // end switch-case block

    // stick all the attributes together in a single string
    var winAttributes;
    winAttributes  =  "width=" +      winWidth;
    winAttributes += ",height=" +     winHeight;
    winAttributes += ",location=" +   winLocation;
    winAttributes += ",menubar=" +    winMenubar;
    winAttributes += ",status=" +     winStatus;
    winAttributes += ",scrollbars=" + winScrollbars;
    winAttributes += ",resizable=" +  winResizable;
    winAttributes += ",toolbar=" +    winToolbar;

    // stick the "onclick" attribute stuff together as a string
    var onclick = "popupWin('" + href + "','" + winAttributes + "', '";
    onclick    += rel + "');return false;";

    // attach the onclick attribute to the anchor tag
    anchor.setAttribute("onclick", onclick);

    // if not IE we're done
    if (!document.all) continue;

    /* IE is... "special" -- you have to attach a function reference to events
    for IE elements */
    anchor.onclick = function() {
      popupWin(this.href, winAttributes, this.rel);
      return false;
    }

  } // end for loop
} // end popupWindows
