    var expInt, colInt, expElem, colElem, expAmnt, colAmnt, expMaxHeight;

    document.write("<style> .widget_content { display: none; }</style>");

    /**
     * Function used to expand widgets as the user enters the page.
     * Do NOT call this function on onLoad, this script does it itself.
     * To force widgets to open, set before the script:
     *   widgetPlatform_forceLoad as an array of widget IDs
     *   widgetPlatform_overrideUser as a boolean
     * If overrideUser is set to true, the widgets specified in forceLoad
     * will load.
     * If overrideUser is set to false, the widgets specified in forceLoad
     * will load, except if the user has closed them in the cookie, and a
     * widget will be opened if they are in the cookie regardless of whether
     * they are specified in forceLoad or not.
     */
    function preExpand() {

      if (typeof widgetPlatform_forceLoad == 'undefined' ) widgetPlatform_forceLoad = null;
      if (typeof widgetPlatform_overrideUser == 'undefined' ) widgetPlatform_overrideUser = true;
      
      var begin,end, vars;
      vars = new Array();
      begin=document.cookie.indexOf("widgets");
      if (begin != -1) {
        begin += 8;
        end=begin;
        while (document.cookie.charAt(end) != ";" && end<=document.cookie.length)
          end++;
          
        vars=document.cookie.substring(begin,end).split(",");
      }
   
      var widgetsExist = false;
      for (i = 0; i < vars.length; i++) {
        var findstr = vars[i];
        if (findstr.charAt(0) == "!") {
          findstr = findstr.substring(1);
        }
        if (document.getElementById(findstr)) {
          widgetsExist = true;
          break;
        }
      }
      
      if (!widgetPlatform_overrideUser) {
        for (i = 0; i < widgetPlatform_forceLoad.length; i++) {
          var find = widgetPlatform_forceLoad[i];
          
          if (contains(vars, find)) {
            widgetPlatform_forceLoad.splice(i, 1);
            i--;
          }
        }
        
        widgetPlatform_forceLoad = widgetPlatform_forceLoad.concat(vars);
      }
      
      for (i = 0; i < widgetPlatform_forceLoad.length; i++) {
        if (document.getElementById(widgetPlatform_forceLoad[i])) {
          if (widgetPlatform_forceLoad[i].charAt(0) != "!") {
              doExpand(widgetPlatform_forceLoad[i], false, false);
          }
        }
      }
    }
    
    /**
     * Removes duplicates in the array 'a'
     * @author Johan Känngård, http://dev.kanngard.net
     */
    function unique(a) {
        tmp = new Array(0);
        for(i=0;i<a.length;i++){
            if(!contains(tmp, a[i])){
                tmp.length+=1;
                tmp[tmp.length-1]=a[i];
            }
        }
        return tmp;
    }
    
    /**
     * Returns true if 's' is contained in the array 'a'
     * @author Johan Känngård, http://dev.kanngard.net
     */
    function contains(a, e) {
        for(j=0;j<a.length;j++) {
          s = a[j];
          if(s.charAt(0) == "!")
            s = s.substring(1);
          if(s==e)
            return true;
        }
        return false;
    }
    
    /**
     * Expands a widget with animation. If it is already expanded, it will
     * collapse.
     * @param wid The widget ID to expand or collapse.
     */
    function expand(wid) {
      doExpand(wid, true, true);
    }
    
    /**
     * Forces open a widget regardless if the user clicked the heading or
     * not.
     * @param wid The widget ID to force open.
     */
    function openWidget(wid) {
      var elems = document.getElementById(wid).getElementsByTagName("div");
      if (elems[0].style.display != 'block') {
        doExpand(wid, true, false);
      }
    }
    
    /**
     * Forces closing a widget regardless if the user clicked the heading or
     * not.
     * @param wid The widget ID to force closing.
     */
    function closeWidget(wid) {
      var elems = document.getElementById(wid).getElementsByTagName("div");
      if (elems[0].style.display == 'block') {
        doExpand(wid, true, true);
      }
    }
    
    /**
     * The primary expand function. Expands or collapses a widget with
     * optional animation.
     * @param wid The widget ID to expand or collapse
     * @param animate If true will animate the widget expansion
     */
    function doExpand(wid, animate, remember) {
      var elems = document.getElementById(wid).getElementsByTagName("div");
      
      if (elems[0].style.display == 'block' && elems[0] != colElem) {
        if(colElem) {
          clearInterval(colInt);
          colElem.style.display = "none";
        }
        colElem = elems[0];
        if (colElem == expElem) {
          clearInterval(expInt);
          expElem = null;
        }
        if (animate) {
          colAmnt = (parseInt(colElem.scrollHeight) / 5);
          colInt = setInterval("animateCollapse()", 50);
        } else {
          colElem.style.display = "none";
          colElem = null;
          
        }
        document.getElementById(wid).getElementsByTagName("span")[2].style.backgroundImage = "url('http://www-test.waikato.ac.nz/enrol/images/expand.gif')";
        if (remember) 
          updateCookie(wid, false);
      } else {
        if(expElem) {
          clearInterval(expInt);
          if (parseInt(expElem.style.height) < expElem.scrollHeight) {
            expElem.style.height = (expElem.scrollHeight+5) + "px";
          }
        }
        if (elems[0] != colElem) {
        elems[0].style.height = '1px';
        }
        elems[0].style.display = 'block';
        expElem = elems[0];
        if (colElem == expElem) {
          clearInterval(colInt);
          colElem = null;
        }
        expAmnt = (parseInt(expElem.scrollHeight) / 5);
        if (expAmnt < 8) {
          expAmnt = 8;
        }
        
        document.getElementById(wid).getElementsByTagName("span")[2].style.backgroundImage = "url('http://www.waikato.ac.nz/enrol/images/collapse.gif')";
        if (animate) {
          expMaxHeight = expElem.scrollHeight;
          expInt = setInterval("animateExpand()", 50);
        } else {
          expElem.style.height = expElem.scrollHeight + "px";
          colElem = null;
        }
        if (remember)
          updateCookie(wid, true);
      }
    }
    
    /**
     * Updates the cookie information. If the cookie doesn't exist, it creates it.
     * @param wid The widget to modify cookie information for
     * @param add If set to true, will add the wid to the cookie as is
     *            If set to false, will add the wid with an ! prefixed as this is
     *             widget notation that the widget was closed.
     */
    function updateCookie(wid, add) {
      var begin,end;
      begin=document.cookie.indexOf("widgets");
      if (begin != -1) {
        begin += 8;
        end=begin;
        while (document.cookie.charAt(end) != ";" && end<=document.cookie.length)
          end++;
          
        var vars=document.cookie.substring(begin,end).split(",");
        var widAt = -1;
        for (i = 0; i < vars.length; i++) {
          if (vars[i] == wid || vars[i] == "!" + wid) {
            widAt = i;
          }
        }
        
        if (!add) {
          wid = "!" + wid;
        }
        
        if (widAt < 0) {
          vars.push(wid);
        } else {
          vars.splice(widAt, 1);
          vars.push(wid);
        }
        
        var built = vars.join(",");
        document.cookie = "widgets=" + built;
        
      } else {
        if (!add) {
            wid = "!" + wid;
        }
        document.cookie = "widgets=" + wid;
      }
    }
    
    /**
     * Animation handler for the expansion.
     */
    function animateExpand() {
      if (parseInt(expElem.style.height) < expMaxHeight && parseInt(expElem.style.height)+expAmnt < expMaxHeight) {
        expElem.style.height = (parseInt(expElem.style.height)+expAmnt) + "px";
      } else {
        expElem.style.height = expMaxHeight + "px";
        clearInterval(expInt);
        expElem = null;
      }
    
    }
    
    /**
     * Animation handler for the collapse.
     */
    function animateCollapse() {
    
      if (parseInt(colElem.style.height) > 1 && parseInt(colElem.style.height)-colAmnt > 1) {
        colElem.style.height = (parseInt(colElem.style.height)-colAmnt) + "px";
      } else {
        colElem.style.display = "none";
        clearInterval(colInt);
        colElem = null;
      }
    }
    
    /**
     * addEvent function from http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
     */
    function widget_addEvent( obj, type, fn )
    {
     if (obj.addEventListener)
      obj.addEventListener( type, fn, false );
     else if (obj.attachEvent)
     {
      obj["e"+type+fn] = fn;
      obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
      obj.attachEvent( "on"+type, obj[type+fn] );
     }
    }

    if (typeof widgetPlatform_autoLoad == 'undefined' ) widgetPlatform_autoLoad = true;
    if (document.getElementById && document.createTextNode && widgetPlatform_autoLoad) {
        widget_addEvent(window, 'load', preExpand);
    }
    
