// inspired by http://blixt.org/articles/tabbed-navigation-using-css

// CSS helper functions
CSS = {
    // Adds a class to an element.
    AddClass: function (e, c) {
        if (!e.className.match(new RegExp("\\b" + c + "\\b", "i")))
            e.className += (e.className ? " " : "") + c;
    },

    // Removes a class from an element.
    RemoveClass: function (e, c) {
        e.className = e.className.replace(new RegExp(" \\b" + c + "\\b|\\b" + c + "\\b ?", "gi"), "");
    }
};

// Functions for handling tabs.
Tabs = {
    // Changes to the tab with the specified ID.
    GoTo: function (contentId, skipReplace) {
        // This variable will be true if a tab for the specified
        // content ID was found.
        var foundTab = false;

        
        // Get the TOC element.
        if(contentId=="Archive" || contentId=="Subscribe" || contentId=="Change" || contentId=="FAQ")
        {
         var toc = document.getElementById("toc");
         var toplevel=true;
        }
        else
        {
         var toc = document.getElementById("toc2");
         var toplevel=false;
        }

        if (toc) {
            var lis = toc.getElementsByTagName("li");


            for (var j = 0; j < lis.length; j++) {
                var li = lis[j];

                // Give the current tab link the class "current" and
                // remove the class from any other TOC links.
                var anchors = li.getElementsByTagName("a");

               

                for (var k = 0; k < anchors.length; k++) { 
                    if (anchors[k].hash == "#" + contentId) {
                        CSS.AddClass(li,"current");
                        
                        foundTab = true;
                        break;
                    } else {
                        CSS.RemoveClass(li,"current");
                    }
                }
            }
        }

        // Show the content with the specified ID.
        var divsToHide = [];
        var divs = document.getElementsByTagName("div");
        for (var i = 0; i < divs.length; i++) {
            var div = divs[i];

            if(toplevel)
            {
             if (div.className.match(/\bcontent\b/i))
             {
                if (div.id == "_" + contentId)
                    div.style.display = "block";
                else
                    divsToHide.push(div);
             }
            }
            else
            {
             if (div.className.match(/\bcontent2\b/i))
             {
                if (div.id == "_" + contentId)
                    div.style.display = "block";
                else
                    divsToHide.push(div);
             }

            }
        }

        // Hide the other content boxes.
        for (var i = 0; i < divsToHide.length; i++)
            divsToHide[i].style.display = "none";

        // Change the address bar.
        if (!skipReplace) window.location.replace("#" + contentId);
    },

    OnClickHandler: function (e) {
        // Stop the event (to stop it from scrolling or
        // making an entry in the history).
        if (!e) e = window.event;
        if (e.preventDefault) e.preventDefault(); else e.returnValue = false;

        // Get the name of the anchor of the link that was clicked.
        Tabs.GoTo(this.hash.substring(1),true);
    },




Init: function ()
{
 if(document.getElementsByTagName)
 {
  var anchors=document.getElementsByTagName("a");

  for(var i=0;i<anchors.length;i++)
  {
   var a=anchors[i];
   if(a.hash && a.hash.length>1 && a.hash.slice(0,1)=='#') {a.onclick=Tabs.OnClickHandler;}
  }


  var contentId;
  var contentId2;
  var contentgoto=true;
  if(window.location.hash) 
  {
   contentId=window.location.hash.substring(1);
   contentgoto=false;
  }

  var divs=document.getElementsByTagName("div");
  for(var i=0;i<divs.length;i++) 
  {
   var div=divs[i];
   if(div.className.match(/\bcontent\b/i))
   {
    if(!contentId) contentId=div.id;
    div.id="_"+div.id;
   }

   if(div.className.match(/\bcontent2\b/i))
   {
    if(!contentId2) contentId2=div.id;
    div.id="_"+div.id;
   }
  }

  if(contentId) Tabs.GoTo(contentId,contentgoto);
  if(contentId2) Tabs.GoTo(contentId2,true);
 }




 var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
 var monthsf = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];
 var modes = ['Date','Thread','Subject','Author','Download'];
 var modeh = ["/date.html","/thread.html","/subject.html","/author.html",".txt.gz"];

 var currentDate = new Date()

 var nowyear=currentDate.getFullYear();
 var nowmonth=currentDate.getMonth();


 for(var modei=0;modei<5;modei++)
 {
  var mode=modes[modei];

  var output='<table class="tclass">';

  for(var year=2002;year<=nowyear;year++)
  {
   if(year & 1) var rowclass="todd";
   else         var rowclass="teven";

   output+="<tr class='"+rowclass+"'><td class='tyear'>"+year+"</td>";
  
   for(var month=0;month<12;month++)
   {
    if((year==2002 && month<5) || (year==nowyear && month>nowmonth)) output+="<td></td>";
    else
    {
     var href="http://lists.ibiblio.org/pipermail/pbs/"+year+"-"+monthsf[month]+modeh[modei];
     output+="<td><a href='"+href+"'>"+months[month]+"</a></td>";
    }
   }

   output+="</tr>";
  }

  output+='</table>';
  
  document.getElementById('_'+mode).innerHTML = output;
 }

}
};


// Hook up the OnLoad event to the tab initialization function.
window.onload = Tabs.Init;

