/*
 * MultiBar v1.01
 * by doron rosenberg <doron at netscape.com>
 *  
 * Fixed to work on mozilla 1.7 by Ho Yiu Yeung after
 * porting original xul sidebar to html
 *  
 * Copyright (c) 2003 Netscape Communications Corp.
 */

  var xmlDoc = document.implementation.createDocument("", "test", null);
  function windowResize() {
		var newHeight = (window.innerHeight-30) + 'px'
		document.getElementById("sidebarContent").style.height = newHeight;
  }
  function doOnload(){
    windowResize();
    createMenuItems();
  }

  cookieName = "sidebarChoice";
  
  function setCookie(aSidebarNum){
    document.cookie = "sidebarChoice="+aSidebarNum;
  }
  
  function getCookie(){
    if (document.cookie.length > 0){
      // we can have multiple cookies on a domain  
      begin = document.cookie.indexOf(cookieName+"=");
      if (begin != -1){
        begin += cookieName.length+1;
        end = document.cookie.indexOf(";", begin);
        if (end == -1) end = document.cookie.length;
        return document.cookie.substring(begin, end); 
      }     
    } else {
      return -1;
    }
  }

  function loadPage(){
    var picker = document.getElementById("sidebarPicker");
    var myUrl = picker.options[picker.selectedIndex].value;

    setCookie(); 
    document.getElementById("sidebarContent").src=myUrl;
  }
  
  function createMenuItems(){
    xmlDoc.addEventListener("load", documentLoaded, false);    
    xmlDoc.load("sidebar-contents.xml");
  }
  
  function documentLoaded(){
    windowResize();
    
    var oldChoice = getCookie();

    // load the first item if we get -1 back or null (ie no cookie set)
    if ((oldChoice == -1) || !oldChoice) oldChoice = 0;

    document.getElementById("sidebarPicker").selectedIndex = oldChoice;
    loadPage();
  }
 

