// FilterLists.js
//
//  In every <ol> of the document, hides list items that don't substring match the criteria
//  In non-hidden <li>s, highlight the matches
//
//  To use,
//  add in the document's body   <input onkeyup = "filter(this.value)" />
//  add in the document's head   <script type="text/javascript" src="FilterLists.js"></script>
//  add in the document's css    span.highlighted {background-color: #00FFFF}
//
// Copyleft GPL v3, Minh Ha-Duong, CNRS, 2008, 2009.

function filter(criteria) {
  olists = document.getElementsByTagName("ol")
  for (j=0; j<olists.length; j++) {
    filterList(criteria, olists[j])
  }
}

function filterList(searchString, textContainerNode) {
 var tempinnerHTML = textContainerNode.innerHTML

  // Unlight all
 var regex = new RegExp('<span class="highlighted">([^>]*)?</span>',"ig")
 tempinnerHTML = tempinnerHTML.replace(regex,'$1')

 // Highlight matching spans
 regex = new RegExp(">([^<]*)?("+searchString+")([^>]*)?<","ig")
 textContainerNode.innerHTML = tempinnerHTML.replace(regex,'>$1<span class="highlighted">$2</span>$3<');

 // Hide nonmatching list items
 regexp = new RegExp(searchString,"i")
 var allitems = textContainerNode.getElementsByTagName("li")
 for (i=0 ; i<allitems.length; i++) {
   rawtext = allitems[i].innerHTML.replace(/<[^>]*>/g,"").replace(/\(Abstract\)/,"")
   allitems[i].style.display = (rawtext.match(regexp) ? 'block' : 'none')
 }
}

// To be called at page load

function initPage(){
  saveOutrefText();
  document.getElementById('Checkbox1').checked = false;   // We do not care that Zotero will not be able to scrape DOIs
  toggleOutref();
  reverse_lists();
  setupToC();
  document.navForm.filterBox.focus();
}

// Finds each ol element on the page, 
// and changes the “value” attribute of each li to the number it would have
// in a native reversed-order presentation.
// It’s not semantically perfect, but it degrades gracefully and will do
// until browsers support the HTML5 incarnation of ol.
//
// To use:
//  Include script in document's head
//  Call function at the end of the page, after all lists are loaded
//
// Source: http://rossshannon.com/2009/02/10/reverse-ordered-html-lists/

function reverse_lists() {
    var olists = document.getElementsByTagName('ol');
    for (var i = 0; i < olists.length; i++) {
        var items = olists[i].getElementsByTagName('li');
        for(var j = 0; j < items.length; j++) {
            items[j].setAttribute("value", items.length - j);
        }
    }
}


// Clicking on the section head toggle the visibility of the section's body
// Source: http://www.devx.com/tips/Tip/13638 and http://www.w3schools.com/dom/prop_element_nextsibling.asp

function toggleNext(me){
  x=me.nextSibling;
  while (x.nodeType!=1){x=x.nextSibling;} // skip empty text nodes
  if (	x.style.display=="none"){
	x.style.display="block";
	me.className = "pushed";
	}
  else {
	x.style.display="none";
	me.className = "raised";
	}
}


// Show only section headings

function fold() {
   var h2list = document.getElementsByTagName('h2');
   for (var i = 0; i < h2list.length; i++) {
     if (h2list[i].className != "raised") { toggleNext(h2list[i]) }
     };
}

// Save the text of the link into the "title" field
// To be called once at pageload

function saveOutrefText() {
   var alist = document.getElementsByTagName('a');
   for (var i = 0; i < alist.length; i++) {
       if (alist[i].className == "outref") { alist[i].title = alist[i].innerHTML };
       };
}

// Control the presence of all <a class="outref"> according to the state of Checkbox1
// Actually delete the text because .visibility="hidden" or .display="none"
// would leave the text remaining when cut and paste.

function toggleOutref() {
   var alist = document.getElementsByTagName('a');
   for (var i = 0; i < alist.length; i++) {
       if (alist[i].className == "outref") {
             if (  document.getElementById('Checkbox1').checked ) {
                   alist[i].style.display = 'inline';
                   alist[i].innerHTML = alist[i].title;
                   }
             else {
		   alist[i].style.display = 'none';
                   alist[i].innerHTML = '';
                   }
          }
       };
}

// Setup the table of content
// Meant to be called one time only, once the page has loaded.

function setupToC() {
   var ToC = '';
   var h2list = document.getElementsByTagName('h2');
   for (var i = 0; i < h2list.length; i++) {
     var a= h2list[i].firstChild.name;
     ToC += '<li><a href="#' + a + '">' + a + '</a></li> ';
     };
   document.getElementById('ToC').innerHTML = ToC
}
