window.onload=SetStyleSheet();
//window.onload=init();

function init(){
  SetStyleSheet();
  changeLinks();
}

// LeafTurner starts here. Modified from Eric Meyer's script function MaKo.

function GetElementsWithClassName(elementName,className) {
  var i;
  var allElements = document.getElementsByTagName(elementName);
  var elemColl = new Array();
  for (i = 0; i < allElements.length; i++) {
    if (allElements[i].className == className) {
      elemColl[elemColl.length] = allElements[i];
    }
  }
  return elemColl;
}

function initialLeaf() {
  var n;
  var imgColl = GetElementsWithClassName('img','turner');
  var olColl = GetElementsWithClassName('ol','leaf');
  var divColl = GetElementsWithClassName('div','leaf');
  for (n = 0; n < imgColl.length; n++) {
    imgColl[n].src = '../i/arrow-plus.gif';
  }
  for (n = 0; n < divColl.length; n++) {
    divColl[n].style.display = 'none';
  }
  for (n = 0; n < olColl.length; n++) {
    olColl[n].style.display = 'none';
  }
}

function leafTurn(leafName) {
  var myLeaf = document.getElementById(leafName);
  var myArrow = document.getElementById(leafName+'Turn');
  if (myLeaf.style.display == 'none') {
    myLeaf.style.display = 'block';
    myArrow.src = '../i/arrow-minus.gif';
  } else {
    myLeaf.style.display = 'none';
    myArrow.src = '../i/arrow-plus.gif';
  }
}


// Styleswitcher starts here

function SetStyleSheet(newtitle){
  var i;
  var savedtitle = '';
  var links = document.getElementsByTagName("link");
  if (!newtitle){
    newtitle = savedtitle = ReadCookie("panel-style");
  }
  if (!newtitle){
    newtitle = GetPreferredStyleSheet();
  }
  var activesheet = null;
  for (i = 0; i < links.length; i++){
    var a = links[i];
    var rel = a.getAttribute('rel');
    var title = a.getAttribute('title');
    if (rel.indexOf("style") != -1 && title){
      a.disabled = true;
      if (title == newtitle){
        activesheet = a;
      }
    }
  }
  if (activesheet){
    activesheet.disabled = false;
  }
  if (newtitle != savedtitle){
    CreateCookie("panel-style", newtitle, 365);
  }
}

function GetActiveStyleSheet(){
  var i;
  var links = document.getElementsByTagName("link");
  for(i = 0; i < links.length; i++){
    var a = links[i];
    var rel = a.getAttribute('rel');
    var title = a.getAttribute('title');
    if (rel.indexOf("style") != -1 && title && !a.disabled){
      return title;
    }
  }
  return null;
}

function GetPreferredStyleSheet(){
  var i, a;
  var links = document.getElementsByTagName("link");
  for (i = 0; i <  links.length; i++){
    a = links[i];
    var rel = a.getAttribute('rel');
    var title = a.getAttribute('title');
    if (rel.indexOf("style") != -1 && rel.indexOf("alt") == -1 && title){
      return title;
    }
  }
  return null;
}

function CreateCookie(name,value,days){
  var cookie = name + "=" + value + ";";
  if (days){
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    cookie += " expires=" + date.toGMTString() + ";";
  }
  cookie += " path=/";
  document.cookie = cookie;
}

function ReadCookie(name){
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i = 0; i < ca.length; i++){
    var c = ca[i];
    while (c.charAt(0) == ' '){
      c = c.substring(1, c.length);
    }
    if (c.indexOf(nameEQ) == 0){
      return c.substring(nameEQ.length,c.length);
    }
  }
  return null;
}



// if the browser supports sidebar.addPanel and is not Firefox, call the function changeLinks onload
// Firefox directly support rel=sidebar so it doesn't need the script

if((window.sidebar) && (window.sidebar.addPanel) && navigator.vendor != 'Firefox' ){
  window.onload=changeLinks;
}

// add event handlers to links with rel=sidebar

function changeLinks() {
  var links,i;
// grab all links, loop over them;
  links = document.getElementsByTagName('a');
  for(i=0; i < links.length; i++) {
// check which link has a rel attribute, with value 'sidebar' and send this one to addTab
//    if(links[i].getAttribute('rel')!='') {
    if(links[i].getAttribute('rel') && links[i].getAttribute('rel').match(/\bsidebar\b/)) {
// Note: the simple function below doesn't provide for handling relative URLs, and addPanel only works with absolute URLs...
//      links[i].onclick=function(){return(addTab(this.title,this.href,''));};
// Hence the hack below to make the URL absolute if it isn't already.
      links[i].onclick=function(){
        var abshref = this.href;
        if(!this.href.match(/^(https?|file):/)) {
          abshref = location.protocol + '\/\/' + location.pathname.replace(/\/[^\/]*$/,'\/') + abshref;
          }
        return(addTab(this.title,abshref,''));
      };
    }
  }
}


function addTab(tabName,tabURL,tabCURL) {
  if ((window.sidebar) && (window.sidebar.addPanel) && navigator.vendor != 'Firefox' ){
    try {
      window.sidebar.addPanel(tabName,tabURL,tabCURL);
      }
    catch (e) {
      alert('An error occurred when trying to add this tab to your '+
            'Sidebar. Maybe you have hit a known bug '+
            '(see Bugzilla #97016) in some Netscape6/Mozilla versions. '+
            'You should be able to click the add tab link again now '+
            'and it should work properly, else press F9 twice.');
      }
    return false;
  } else {
    return true;
  } 
}

