// Except where noted all JS code is copyright TDZ Ståle Sæbøe © 2009
var tdzPopupwin="";
var tdzdefaultlists="";
var tdzdefaultcookielife=365;

if (!Array.prototype.indexOf)
{
  // indexOf implementation copied directly from:
  //   https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/indexOf
  //
  // This function and this function only on this website is subject to the following license:
  //   http://www.ibiblio.org/pub/Linux/LICENSES/mit.license
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

var tdzDisplayBlockArray = new Array(); // An array to contain the id of which get the display property set to block when the page is loaded.
var tdzDocumentId=""; // String to hold the identity of the document div selected.
var tdzItemId=""; // String to hold the identity of the list item that is currently active
var seldocdiv; // Reference to the div that holds the visible selected document if any



 
  function tdzInit()
  {
    tdzDisplayBlockArray=getCookie("displayblockarray", tdzdefaultlists).split(",");
    tdzRefreshDisplayBlockElements();
    seldocdiv = document.getElementById('documentselected');
  }
  
  function tdzToggleItem(elemid, itemtype, itemid)
  {
    if(itemtype=='document')
    {
      var linkitem = document.getElementById(itemid)
      tdzClearSelectedDoc();
      if(elemid==tdzDocumentId)
      {
        document.getElementById('documentswrap').style.display="block";
        document.getElementById('pagebody').style.display="block";
        linkitem.className = "linklistitem"
        tdzDocumentId="";
        tdzItemId="";
      }
      else
      {
        var cloneddoc = document.getElementById(elemid).cloneNode(true);
        cloneddoc.removeAttribute('id');
        linkitem.className = "linklistitemactive"
        if(tdzItemId!="" && tdzItemId!=null)
        {
          document.getElementById(tdzItemId).className="linklistitem";
        }
        
        document.getElementById('documentswrap').style.display="none";
        document.getElementById('pagebody').style.display="none";
        
        seldocdiv.className = "document";
        seldocdiv.appendChild(cloneddoc);
        tdzDocumentId=elemid;
        tdzItemId=itemid;
      }
    }
    if(itemtype=='link')
    {
      // Process link toggling here
    }
  }
  
  function tdzClearSelectedDoc()
  {
    
    seldocdiv.className = "empty";
    while(seldocdiv.firstChild)
    {
      seldocdiv.removeChild(seldocdiv.firstChild);
    };
  }
  
  function tdzToggleDisplay(elemid)
  {
    var tdzElement=document.getElementById(elemid);
    var tdzHeader=document.getElementById("h"+elemid);
    var tdzDisplay=tdzElement.style.display;
    var tdzIndex=tdzDisplayBlockArray.indexOf(elemid);
    var tdzImage;
    
    if(tdzDisplay=="none" || tdzDisplay=="")
    {
      tdzDisplay="block";
      tdzImage="images/minus.png";
      (tdzIndex==-1)?tdzDisplayBlockArray.push(elemid):tdzDisplayBlockArray.splice(tdzIndex,1,elemid);
    }
    else
    {
      tdzDisplay="none";
      tdzImage="images/plus.png";
      (tdzIndex!=-1)?tdzDisplayBlockArray.splice(tdzIndex,1):"";
    }
    
    setCookie("displayblockarray", tdzDisplayBlockArray.join(","), tdzdefaultcookielife)
    tdzElement.style.display=tdzDisplay;
    tdzHeader.style.backgroundImage="url("+tdzImage+")";
  }

  function tdzRefreshDisplayBlockElements()
  {
    var tdzElement;
    var index;
    for (index in tdzDisplayBlockArray)
    {
      tdzElement=document.getElementById(tdzDisplayBlockArray[index]);
      if(tdzElement!=null)
      {
        tdzElement.style.display="block";
        document.getElementById("h"+tdzDisplayBlockArray[index]).style.backgroundImage="url(images/minus.png)";
      }
    }
  }
  
  function setCookie(c_name,value,expiredays)
  {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
  }
  
  function getCookie(c_name, defaultvalue)
  {
    if (document.cookie.length>0)
    {
      c_start=document.cookie.indexOf(c_name + "=");
      if (c_start!=-1)
      { 
        c_start=c_start + c_name.length+1; 
        c_end=document.cookie.indexOf(";",c_start);
        if (c_end==-1) c_end=document.cookie.length;
        return unescape(document.cookie.substring(c_start,c_end));
      }
    }
    setCookie(c_name,defaultvalue,tdzdefaultcookielife);
    return defaultvalue;
  }
      function tdzPopup(strURL,strName,strType,strWidth,strHeight)
  {
    var strOptions="";

    if(strType=="flexible")
    {
      strWidth=strWidth;
      strHeight=strHeight;
      strOptions="toolbar=no,menubar=no,scrollbars=yes,resizable=yes,width="+strWidth+",height="+strHeight;
    }
    if(strType=="fixed") {
      strOptions="toolbar=no,menubar=no,scrollbars=no,resizable=no,width="+strWidth+",height="+strHeight
    }
    if (tdzPopupwin.location && !tdzPopupwin.closed) {
      tdzPopupwin.location.href=strURL;
      tdzPopupwin.focus(); }
    else {
      tdzPopupwin=window.open(strURL,strName,strOptions);
    }
  }