/*  ****************************************************************************
    popup.js
    Christopher E. Simpson
    ERAC      UEG    *3517
    September 2005

    popup.js allows for any link to be declared a 'popup' link simply
    by setting its class to 'popup'.
    
    Of course, class may contain any number of values, space seperated.
    
    Set the href of the <a> element to the url just as if it were a
    'regular' link.
    
    Optionally, you may define the following attributes for the popup
    window (default values are also listed):
    
    name            newWindow
    width           300
    height          400
    left            150
    top             100
    menubar         no
    resizable       yes
    scrollbars      yes
    status          no
    toolbar         no
    
    This is done by using the popup class and appending a string beginning
    with '?' and defining each of the desired variable / value pair as
    _attributeName=value
    where attributeName is on of the abovelisted optional window attributes.
    The attribute / value pairs should be seperated by ampersands (&).
    (The syntax is similar to adding GET variables to the end of a url).
    e.g.
    <a href="http://www.enterprise.com" class="popup?_name=home&_width=250&_height=600&_status=yes">E.com</a>
    
    ...would, if the user-agent allows popups, open a new window named "home" with a 
    width of "250" and a height of "600" with a statusbar. The content of that
    window would be the webpage at http://www.enterprise.com.
    
    If the user-agent does not allow popups, the <a> element will be given
    an attribute 'target' equal to '_blank', which should open a new window
    (with no controls over attributes) with the new document loaded in it.
    
    If the user-agent or the doctype does not support target, and the user-agent
    does not support popups then the <a> element will simply act like any old link.
    
    Same goes for if JS is not enabled.    
    ********************************************************************************/

var popup = function()
{
  if(!document.createTextNode || !document.getElementsByTagName){ return; }
  
  for(var i=0; i<document.getElementsByTagName("a").length; i++)
  {
    var a = document.getElementsByTagName("a")[i];
    if(a.className.match('popup'))
    {
      a.onclick = function()
      {
        var url = this.href;
        
        p = new Array();
        p["_size"]        = "0";
        p["_name"]        = "newwindow";
        p["_width"]       = "300";
        p["_height"]      = "400";
        p["_left"]        = "150";
        p["_top"]         = "100";
        p["_menubar"]     = "no";
        p["_resizable"]   = "yes";
        p["_scrollbars"]  = "yes";
        p["_status"]      = "no";
        p["_toolbar"]     = "no";
        p["_location"]     = "no";

        var pString = this.className.replace(/(\S*\s*)popup\?(\S*)(\s?.*)/, "$2");

        var pTokens = pString.split("&");
        for (var i = 0; i < pTokens.length; i++)
        {
          var pName = pTokens[i].replace(/(.*)=.*/, "$1");
          var pValue = pTokens[i].replace(/.*=(.*)/, "$1");
          p[pName] = pValue;
        }
        if (parseInt(p["_size"]) >0)
        {
          if (window.innerWidth && typeof(window.innerWidth) == 'number') 
          {
            w = window.innerWidth;
            h = window.innerHeight;
          }
          else if (document.documentElement && (document.documentElement.clientWidth != 0 && document.documentElement.clientHeight != 0)) 
          {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
          }
          else if (document.body.clientHeight && document.body.clientWidth)
          {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
          }
          else
          {
            w = 800;
            h = 600;
          }
          p["_height"]      =  h * p["_size"] / 100;
          p["_width"]       =  w * p["_size"] / 100;
          p["_menubar"]     = "yes";
          p["_resizable"]   = "yes";
          p["_scrollbars"]  = "yes";
          p["_status"]      = "yes";
          p["_toolbar"]     = "yes";
          p["_location"]     = "yes";
        }
        
        if(window.open)
        {          
          var attString  = 'width='+p["_width"];
              attString += ', height='+p["_height"];
              attString += ', left='+p["_left"];
              attString += ', top='+p["_top"];
              attString += ', menubar='+p["_menubar"];
              attString += ', resizable='+p["_resizable"];
              attString += ', scrollbars='+p["_scrollbars"];
              attString += ', status='+p["_status"];
              attString += ', toolbar='+p["_toolbar"];
              attString += ', location='+p["_location"];
              
          var newWin = window.open(url, p["_name"], attString);
          newWin.focus();
          return false;
        }
        else
        {
          this.target = "_blank";
        }
      }    
    }
  }
}
addEvent(window, 'load', popup);