var isPopup =
{
  init:function()
  {
    if(!document.getElementById || !document.createTextNode){ return; }
    if(!document.getElementsByTagName('BODY')[0].className.match('popup')){ return; }

    isPopup.makeSureIBelongHere();
    isPopup.makeSureImOnTop();
    isPopup.closeWinLink();
    isPopup.changeParent();
    
    if(document.getElementById("FAQ")) { isPopup.linkHighlight(); }
    
  },

  makeSureIBelongHere:function()
  {
    if(window.history.length == 1 && window.opener == null)
    {
      window.location.href=window.location.protocol+"//"+window.location.host;
    }  
  },

  makeSureImOnTop:function() { window.self.focus(); },

  closeWinLink:function()
  {
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++)
    {
      if(anchors[i].className.match('closeWin'))
      {
        var a = anchors[i];
        a.onclick = function(){ window.self.close(); }
        a.style.display = "block";
      }
    }
  },

  changeParent:function()
  {
    var anchors = document.getElementsByTagName('a');
    for(var i=0; i<anchors.length; i++)
    {
      if(anchors[i].className.match('parentHrefChanger'))
      {
        var a = anchors[i];
        a.onclick = function()
        {
          window.opener.location.href = this.href;
          window.self.close();
          return false;
        }
      }
      if(anchors[i].className.match('parentFormValueChanger'))
      {
        var a = anchors[i];
        a.onclick = function()
        {
          var query = this.href.split('?')[1].split('&');
          for(var i = 0; i<query.length; i++)
          {
            var token = query[i].split('=');
            var elem = window.opener.document.getElementsByName(token[0])[0];
            if(elem && (elem.tagName == "INPUT" || elem.tagName == "SELECT"))
            {
              var val = token[1].replace(/\+/g, ' ');
              isPopup.setValue(elem,val);
            }
          }
          window.self.close();
          return false;
        }
      }
    }
  },

  setValue : function(elem, val)
  {
    if(elem.tagName == 'SELECT')
    {
      for(var i = 0; i<elem.options.length; i++)
      {
        if(elem.options[i].value == val)
        {
          elem.options[i].selected = 'selected';
          break;
        }
      }
    }
    else if(elem.tagName == 'INPUT' && (elem.type == 'text' || elem.type == 'hidden')) { elem.value = val; }
  },

  handlePorts : function()
  {
    var portsList = document.getElementById('ports');
	if (!portsList) {return false;}
    var ports = portsList.getElementsByTagName('li');

	if (ports.length<3){return;}
	
    var mod = ports.length%3;
    if(mod == 0){ var colLength = ports.length/3; }
    else if(mod == 1){ var colLength = Math.ceil(ports.length/3); }
    else{ var colLength = Math.floor(ports.length/3); }

    var col1 = document.createElement('li');
    var Ul1 = document.createElement('ul');
        col1.appendChild(Ul1);
    var col2 = document.createElement('li');
    var Ul2 = document.createElement('ul');
        col2.appendChild(Ul2);
    var col3 = document.createElement('li');
    var Ul3 = document.createElement('ul');
        col3.appendChild(Ul3);

    for(var i=0; i<colLength; i++){ Ul1.appendChild(ports[i].cloneNode(true)); }
    for(i; i<colLength*2; i++){ Ul2.appendChild(ports[i].cloneNode(true)); }
    for(i; i<ports.length; i++){ Ul3.appendChild(ports[i].cloneNode(true)); }

    while(portsList.hasChildNodes()){ portsList.removeChild(portsList.firstChild);}
    
    portsList.appendChild(col1);
    portsList.appendChild(col2);
    portsList.appendChild(col3);
  },
  linkHighlight:function()
  {
    var FAQ = document.getElementById('FAQ');
    var scroller = document.getElementById('scroller');
    
    FAQ.className = scroller.className = 'javascript';
    FAQ.getElementsByTagName('LI')[0].className = 'selected';
        
    var links = FAQ.getElementsByTagName('A');
    var firstLink = FAQ.getElementsByTagName('A')[0];
    
    var anchors = scroller.getElementsByTagName('A');
    //alert(anchors.length);
    firstLink.className = 'selected';
    
    for(x=0; x<links.length; x++)
    {
      links[x].onclick = function()
      {
       for(q=0;q<links.length;q++)  links[q].className = "";      
       this.className = "selected";
       
         var str = this.href;
         var start = str.lastIndexOf('#')+1;
         var end = str.length;
         
         linkHREF = str.slice(start,end);
         
         //alert(linkHREF);
         
         var matchingAnchor = document.getElementById(linkHREF);
         //alert(matchingAnchor.parentNode.nodeName);
         
         for(j=0;j<anchors.length;j++){
           
           if(anchors[j].parentNode.nodeName.match(/DIV/gi)){
             var parentDiv = anchors[j].parentNode;
             parentDiv.className = 'hidden';
           }
           
         }        
         matchingAnchor.parentNode.className = 'visible';
       
       return false; 
      }
    }
  }
};
addEvent(window, 'load', isPopup.init);
