Difference between revisions of "MediaWiki:Common.js"

From LIMSWiki
Jump to navigationJump to search
(Updated collapsible table code.)
(Testing out new collapsible code.)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
/* Any JavaScript here will be loaded for all users on every page load. */
/*
Table sorting script  by Joost de Valk, check it out at http://www.joostdevalk.nl/code/sortable-table/.
Based on a script from http://www.kryogenix.org/code/browser/sorttable/.
Distributed under the MIT license: http://www.kryogenix.org/code/browser/licence.html .
Copyright (c) 1997-2007 Stuart Langridge, Joost de Valk.
Version 1.5.7
*/
/* You can change these values */
var image_path = "http://www.joostdevalk.nl/code/sortable-table/";
var image_up = "arrow-up.gif";
var image_down = "arrow-down.gif";
var image_none = "arrow-none.gif";
var europeandate = false;
var alternate_row_colors = true;
/* Don't change anything below this unless you know what you're doing */
window.killEvt = function( evt ) {
evt = evt || window.event || window.Event; // W3C, IE, Netscape
if ( typeof ( evt.preventDefault ) != 'undefined' ) {
evt.preventDefault(); // Don't follow the link
evt.stopPropagation();
} else {
evt.cancelBubble = true; // IE
}
return false; // Don't follow the link (IE)
}
addEvent(window, "load", sortables_init);
var SORT_COLUMN_INDEX;
var thead = false;
function sortables_init() {
// Find all tables with class sortable and make them sortable
if (!document.getElementsByTagName) return;
tbls = document.getElementsByTagName("table");
for (ti=0;ti<tbls.length;ti++) {
thisTbl = tbls[ti];
if (((' '+thisTbl.className+' ').indexOf("sortable") != -1) && (thisTbl.id)) {
ts_makeSortable(thisTbl);
}
}
}
function ts_makeSortable(t) {
if (t.rows && t.rows.length > 0) {
if (t.tHead && t.tHead.rows.length > 0) {
var firstRow = t.tHead.rows[t.tHead.rows.length-1];
thead = true;
} else {
var firstRow = t.rows[0];
}
}
if (!firstRow) return;
// We have a first row: assume it's the header, and make its contents clickable links
for (var i=0;i<firstRow.cells.length;i++) {
var cell = firstRow.cells[i];
var txt = ts_getInnerText(cell);
if (cell.className != "unsortable" && cell.className.indexOf("unsortable") == -1) {
cell.innerHTML = '+txt+''<a href="#" class="sortheader" onclick="ts_resortTable(this, '+i+');return false;"><span class="sortarrow">&nbsp;&nbsp;<img src="'+ image_path + image_none + '" alt="&darr;"/></span></a>';
}
}
if (alternate_row_colors) {
alternate(t);
}
}
function ts_getInnerText(el) {
if (typeof el == "string") return el;
if (typeof el == "undefined") { return el };
if (el.innerText) return el.innerText; //Not needed but it is faster
var str = "";
var cs = el.childNodes;
var l = cs.length;
for (var i = 0; i < l; i++) {
switch (cs[i].nodeType) {
case 1: //ELEMENT_NODE
str += ts_getInnerText(cs[i]);
break;
case 3: //TEXT_NODE
str += cs[i].nodeValue;
break;
}
}
return str;
}
function ts_resortTable(lnk, clid) {
var span;
for (var ci=0;ci<lnk.childNodes.length;ci++) {
if (lnk.childNodes[ci].tagName && lnk.childNodes[ci].tagName.toLowerCase() == 'span') span = lnk.childNodes[ci];
}
var spantext = ts_getInnerText(span);
var td = lnk.parentNode;
var column = clid || td.cellIndex;
var t = getParent(td,'TABLE');
// Work out a type for the column
if (t.rows.length <= 1) return;
var itm = "";
var i = 0;
while (itm == "" && i < t.tBodies[0].rows.length) {
var itm = ts_getInnerText(t.tBodies[0].rows[i].cells[column]);
itm = trim(itm);
if (itm.substr(0,4) == "<!--" || itm.length == 0) {
itm = "";
}
i++;
}
if (itm == "") return;
sortfn = ts_sort_caseinsensitive;
if (itm.match(/^\d\d[\/\.-][a-zA-z][a-zA-Z][a-zA-Z][\/\.-]\d\d\d\d$/)) sortfn = ts_sort_date;
if (itm.match(/^\d\d[\/\.-]\d\d[\/\.-]\d\d\d{2}?$/)) sortfn = ts_sort_date;
if (itm.match(/^-?[£$€Û¢´]\d/)) sortfn = ts_sort_numeric;
if (itm.match(/^-?(\d+[,\.]?)+(E[-+][\d]+)?%?$/)) sortfn = ts_sort_numeric;
SORT_COLUMN_INDEX = column;
var firstRow = new Array();
var newRows = new Array();
for (k=0;k<t.tBodies.length;k++) {
for (i=0;i<t.tBodies[k].rows[0].length;i++) {
firstRow[i] = t.tBodies[k].rows[0][i];
}
}
for (k=0;k<t.tBodies.length;k++) {
if (!thead) {
// Skip the first row
for (j=1;j<t.tBodies[k].rows.length;j++) {
newRows[j-1] = t.tBodies[k].rows[j];
}
} else {
// Do NOT skip the first row
for (j=0;j<t.tBodies[k].rows.length;j++) {
newRows[j] = t.tBodies[k].rows[j];
}
}
}
newRows.sort(sortfn);
if (span.getAttribute("sortdir") == 'down') {
ARROW = '&nbsp;&nbsp;<img src="'+ image_path + image_down + '" alt="&darr;"/>';
newRows.reverse();
span.setAttribute('sortdir','up');
} else {
ARROW = '&nbsp;&nbsp;<img src="'+ image_path + image_up + '" alt="&uarr;"/>';
span.setAttribute('sortdir','down');
}
    // We appendChild rows that already exist to the tbody, so it moves them rather than creating new ones
    // don't do sortbottom rows
    for (i=0; i<newRows.length; i++) {
if (!newRows[i].className || (newRows[i].className && (newRows[i].className.indexOf('sortbottom') == -1))) {
t.tBodies[0].appendChild(newRows[i]);
}
}
    // do sortbottom rows only
    for (i=0; i<newRows.length; i++) {
if (newRows[i].className && (newRows[i].className.indexOf('sortbottom') != -1))
t.tBodies[0].appendChild(newRows[i]);
}
// Delete any other arrows there may be showing
var allspans = document.getElementsByTagName("span");
for (var ci=0;ci<allspans.length;ci++) {
if (allspans[ci].className == 'sortarrow') {
if (getParent(allspans[ci],"table") == getParent(lnk,"table")) { // in the same table as us?
allspans[ci].innerHTML = '&nbsp;&nbsp;<img src="'+ image_path + image_none + '" alt="&darr;"/>';
}
}
}
span.innerHTML = ARROW;
alternate(t);
}
function getParent(el, pTagName) {
if (el == null) {
return null;
} else if (el.nodeType == 1 && el.tagName.toLowerCase() == pTagName.toLowerCase()) {
return el;
} else {
return getParent(el.parentNode, pTagName);
}
}
function sort_date(date) {
// y2k notes: two digit years less than 50 are treated as 20XX, greater than 50 are treated as 19XX
dt = "00000000";
if (date.length == 11) {
mtstr = date.substr(3,3);
mtstr = mtstr.toLowerCase();
switch(mtstr) {
case "jan": var mt = "01"; break;
case "feb": var mt = "02"; break;
case "mar": var mt = "03"; break;
case "apr": var mt = "04"; break;
case "may": var mt = "05"; break;
case "jun": var mt = "06"; break;
case "jul": var mt = "07"; break;
case "aug": var mt = "08"; break;
case "sep": var mt = "09"; break;
case "oct": var mt = "10"; break;
case "nov": var mt = "11"; break;
case "dec": var mt = "12"; break;
// default: var mt = "00";
}
dt = date.substr(7,4)+mt+date.substr(0,2);
return dt;
} else if (date.length == 10) {
if (europeandate == false) {
dt = date.substr(6,4)+date.substr(0,2)+date.substr(3,2);
return dt;
} else {
dt = date.substr(6,4)+date.substr(3,2)+date.substr(0,2);
return dt;
}
} else if (date.length == 8) {
yr = date.substr(6,2);
if (parseInt(yr) < 50) {
yr = '20'+yr;
} else {
yr = '19'+yr;
}
if (europeandate == true) {
dt = yr+date.substr(3,2)+date.substr(0,2);
return dt;
} else {
dt = yr+date.substr(0,2)+date.substr(3,2);
return dt;
}
}
return dt;
}
function ts_sort_date(a,b) {
dt1 = sort_date(ts_getInnerText(a.cells[SORT_COLUMN_INDEX]));
dt2 = sort_date(ts_getInnerText(b.cells[SORT_COLUMN_INDEX]));
if (dt1==dt2) {
return 0;
}
if (dt1<dt2) {
return -1;
}
return 1;
}
function ts_sort_numeric(a,b) {
var aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
aa = clean_num(aa);
var bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
bb = clean_num(bb);
return compare_numeric(aa,bb);
}
function compare_numeric(a,b) {
var a = parseFloat(a);
a = (isNaN(a) ? 0 : a);
var b = parseFloat(b);
b = (isNaN(b) ? 0 : b);
return a - b;
}
function ts_sort_caseinsensitive(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]).toLowerCase();
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]).toLowerCase();
if (aa==bb) {
return 0;
}
if (aa<bb) {
return -1;
}
return 1;
}
function ts_sort_default(a,b) {
aa = ts_getInnerText(a.cells[SORT_COLUMN_INDEX]);
bb = ts_getInnerText(b.cells[SORT_COLUMN_INDEX]);
if (aa==bb) {
return 0;
}
if (aa<bb) {
return -1;
}
return 1;
}
function addEvent(elm, evType, fn, useCapture)
// addEvent and removeEvent
// cross-browser event handling for IE5+, NS6 and Mozilla
// By Scott Andrew
{
if (elm.addEventListener){
elm.addEventListener(evType, fn, useCapture);
return true;
} else if (elm.attachEvent){
var r = elm.attachEvent("on"+evType, fn);
return r;
} else {
alert("Handler could not be removed");
}
}
function clean_num(str) {
str = str.replace(new RegExp(/[^-?0-9.]/g),"");
return str;
}
function trim(s) {
return s.replace(/^\s+|\s+$/g, "");
}
function alternate(table) {
// Take object table and get all it's tbodies.
var tableBodies = table.getElementsByTagName("tbody");
// Loop through these tbodies
for (var i = 0; i < tableBodies.length; i++) {
// Take the tbody, and get all it's rows
var tableRows = tableBodies[i].getElementsByTagName("tr");
// Loop through these rows
// Start at 1 because we want to leave the heading row untouched
for (var j = 0; j < tableRows.length; j++) {
// Check if j is even, and apply classes for both possible results
if ( (j % 2) == 0  ) {
if ( !(tableRows[j].className.indexOf('odd') == -1) ) {
tableRows[j].className = tableRows[j].className.replace('odd', 'even');
} else {
if ( tableRows[j].className.indexOf('even') == -1 ) {
tableRows[j].className += " even";
}
}
} else {
if ( !(tableRows[j].className.indexOf('even') == -1) ) {
tableRows[j].className = tableRows[j].className.replace('even', 'odd');
} else {
if ( tableRows[j].className.indexOf('odd') == -1 ) {
tableRows[j].className += " odd";
}
}
}
}
}
}


/* Return whether a particular class is used**************************************
/* Return whether a particular class is used**************************************
Line 347: Line 12:
  })();
  })();


/** Collapsible tables code *****************************************************
/** Collapsible tables *********************************************************
  *  Description: Allows tables to be collapsed, showing only the header
*
  * Author: User:Bigfoot Lover @ BionicWiki.com
  *  Description: Allows tables to be collapsed, showing only the header. See
  *  Added: 24 September 2007
  *               [[Wikipedia:NavFrame]].
  *  Maintainers: [[User:R. Koot]]
  */
  */
 
   
/* Add a hook to make buttons, where need be, on every pageload */
var autoCollapse = 2;
addOnloadHook( createTableButtons );
var collapseCaption = "hide";
 
var expandCaption = "show";
/* Define global variables:
* autoShrink is the number of tables that must exist on the page for usage of "class=collapsible autocollapse"
window.collapseTable = function( tableIndex ){
* minimizeSymbol can be either a symbol such as a minus sign or a word such as hide or disappear
    var Button = document.getElementById( "collapseButton" + tableIndex );
  * maximizeSymbol can be either a symbol such as a plus sign or a word such as show or appear */
 
var autoShrink = 2;
var minimizeSymbol = "hide";
var maximizeSymbol = "show";
 
/* Define functions that do-the-work */
/* Function toggleTableView() toggles a specified table's view from minimized to maximized, or vice versa */
function toggleTableView( tableIndex, tableShrink )
{
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
     var Table = document.getElementById( "collapsibleTable" + tableIndex );
    /* If there is no collapsible tables on the page, no need to do any shrinking */
     if (!Table) {
     if ( !Table || !Button ) {
      return false;
    }
 
    var Button = document.getElementById( "collapseButton" + tableIndex );
    /* If no collapsible buttons, no need to do any shrinking */
    if (!Button ) {
         return false;
         return false;
     }
     }
 
    /* Grab the rows of the specified table */
     var Rows = Table.rows;
     var Rows = Table.getElementsByTagName( "tr" );  
 
     if ( Button.firstChild.data == collapseCaption ) {
    /* Do the hiding/unhiding */
         for ( var i = 1; i < Rows.length; i++ ) {
     if ( Button.firstChild.data == minimizeSymbol || tableShrink == 1 ) {
             Rows[i].style.display = "none";
         /* if the button is set to minimize its contents,
        * then loop through the rows and mark them hidden */
        var count = 1;
        while (Rows.length > count) {
          if (Rows[count].parentNode.parentNode.id == ("collapsibleTable" + tableIndex))
          {
             Rows[count].style.display = "none";
          }
          count++;
         }
         }
        /* After marking, change the table to show the maximize symbol */
         Button.firstChild.data = expandCaption;
         Button.firstChild.data = maximizeSymbol;
     } else {
     } else {
         /* if the button is set to maximize its contents,
         for ( var i = 1; i < Rows.length; i++ ) {
        * then loop through the rows and mark them visible */   
            Rows[i].style.display = Rows[0].style.display;
        var count = 1;
        while (Rows.length > count) {
          if(Rows[count].parentNode.parentNode.id == ("collapsibleTable" + tableIndex))
          {
              Rows[count].style.display = Rows[0].style.display;
          }
          count++;
         }
         }
        /* After marking, change the table to show the minimize symbol */
         Button.firstChild.data = collapseCaption;
         Button.firstChild.data = minimizeSymbol;
     }
     }
}
}
 
   
/* Funtion createTableButtons() creates the plus or minus symbol and alignment text
function createCollapseButtons(){
  * to be applied on collapsible tables */
function createTableButtons()
{
    /* Define local variables */
     var tableIndex = 0;
     var tableIndex = 0;
     var NavBoxes = new Object();
     var NavigationBoxes = new Object();
     var Tables = document.getElementsByTagName( "table" );
     var Tables = document.getElementsByTagName( "table" );
 
     /* Use two count variables to handle cases where continue is used */
     for ( var i = 0; i < Tables.length; i++ ) {
    var loopcount = 0;
         if ( hasClass( Tables[i], "collapsible" ) ) {
    var count = 0;  
    while (Tables.length > loopcount) {
             /* only add button and increment count if there is a header row to work with */
        /* For all collapsible table on the page, this code goes through
             var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
        * them and makes a button for each one individually */
        count = loopcount;
        loopcount++;
         if ( usesClass( Tables[count], "collapsible" ) ) {
 
             /* Proceed only if a header row and header exist */
             var HeaderRow = Tables[count].getElementsByTagName( "tr" )[0];
             if (!HeaderRow) continue;
             if (!HeaderRow) continue;
             var Header = HeaderRow.getElementsByTagName( "th" )[0];
             var Header = HeaderRow.getElementsByTagName( "th" )[0];
             if (!Header) continue;
             if (!Header) continue;
 
             /* Log where you are in the looping */
             NavigationBoxes[ tableIndex ] = Tables[i];
            NavBoxes[ tableIndex ] = Tables[count];
             Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
            /* Set the identifier of the table being edited in this iteration */
             var Button     = document.createElement( "span" );
             Tables[count].setAttribute( "id", "collapsibleTable" + tableIndex );
 
            /* Create the button assuming it is a minimized table
            * to do the initial creation. */
             var Button = document.createElement( "span" );
             var ButtonLink = document.createElement( "a" );
             var ButtonLink = document.createElement( "a" );
             var ButtonText = document.createTextNode( maximizeSymbol );
             var ButtonText = document.createTextNode( collapseCaption );
 
            /* Define where the button floats and its font and size.
             Button.className = "collapseButton"; //Styles are declared in Common.css
            * The width should be set to the max character count of
            * the mininizeSymbol and maximizeSymbol + 2. ie. min and max
            * are 3 letters each + 2 = 5em */
             Button.style.styleFloat = "right";
            Button.style.cssFloat = "right";
            Button.style.fontWeight = "normal";
            Button.style.textAlign = "right";
            Button.style.width = "5em";
 
            /* Set the link color and identifier */
             ButtonLink.style.color = Header.style.color;
             ButtonLink.style.color = Header.style.color;
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
             ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
 
             ButtonLink.setAttribute( "href", "#" );
            /* Set the destination of the button */
            addHandler( ButtonLink,  "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
             ButtonLink.setAttribute( "href", "javascript:toggleTableView(" + tableIndex + ");" );
             ButtonLink.appendChild( ButtonText );
             ButtonLink.appendChild( ButtonText );
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( document.createTextNode( "[" ) );
             Button.appendChild( ButtonLink );
             Button.appendChild( ButtonLink );
             Button.appendChild( document.createTextNode( "]" ) );
             Button.appendChild( document.createTextNode( "]" ) );
 
            /* Load the next header and table for the next iteration */
             Header.insertBefore( Button, Header.firstChild );
             Header.insertBefore( Button, Header.childNodes[0] );
             tableIndex++;
             tableIndex++;
         }
         }
     }
     }
 
     /* Earlier in this function, assumed all tables are minimized to do the initial creation.
     for ( var i = 0; i < tableIndex; i++ ) {
    * Now, loop again through the tables and set those to maximized that need setting,
         if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
    * and set the rows of those that don't as hidden */
            collapseTable( i );
    loopcount = 0;
         }  
    while (tableIndex > loopcount) {
         else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
        /* If autocollapse or collapse is not invalid, maximize; otherwise, minimize */
            var element = NavigationBoxes[i];
         if (((autoShrink > tableIndex) && usesClass( NavBoxes[loopcount], "autocollapse" )) || !usesClass( NavBoxes[loopcount], "collapsed" ) ) {
            while (element = element.parentNode) {
          toggleTableView( loopcount, 0 );
                if ( hasClass( element, "outercollapse" ) ) {
         }
                    collapseTable ( i );
         else {
                    break;
          toggleTableView( loopcount, 1 );
                }
            }
         }
         }
        loopcount++;
     }
     }
}
}
 
$( createCollapseButtons );
/** Dynamic Navigation Bars (experimental) *************************************
/** Dynamic Navigation Bars (experimental) *************************************
  *
  *
Line 499: Line 107:
  *  Maintainers: UNMAINTAINED
  *  Maintainers: UNMAINTAINED
  */
  */
 
// set up the words in your language
// set up the words in your language
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
 
// shows and hides content and picture (if available) of navigation bars
// shows and hides content and picture (if available) of navigation bars
// Parameters:
// Parameters:
//    indexNavigationBar: the index of navigation bar to be toggled
//    indexNavigationBar: the index of navigation bar to be toggled
function toggleNavigationBar(indexNavigationBar){
window.toggleNavigationBar = function(indexNavigationBar){
     var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
     var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
     var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
     var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
     if (!NavFrame || !NavToggle) {
     if (!NavFrame || !NavToggle) {
         return false;
         return false;
     }
     }
 
     // if shown now
     // if shown now
     if (NavToggle.firstChild.data == NavigationBarHide) {
     if (NavToggle.firstChild.data == NavigationBarHide) {
Line 523: Line 131:
         }
         }
     NavToggle.firstChild.data = NavigationBarShow;
     NavToggle.firstChild.data = NavigationBarShow;
 
     // if hidden now
     // if hidden now
     } else if (NavToggle.firstChild.data == NavigationBarShow) {
     } else if (NavToggle.firstChild.data == NavigationBarShow) {
Line 534: Line 142:
     }
     }
}
}
 
// adds show/hide-button to navigation bars
// adds show/hide-button to navigation bars
function createNavigationBarToggleButton(){
function createNavigationBarToggleButton(){
Line 543: Line 151:
         // if found a navigation bar
         // if found a navigation bar
         if (hasClass(NavFrame, "NavFrame")) {
         if (hasClass(NavFrame, "NavFrame")) {
 
             indexNavigationBar++;
             indexNavigationBar++;
             var NavToggle = document.createElement("a");
             var NavToggle = document.createElement("a");
Line 549: Line 157:
             NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
             NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
             NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
             NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
 
             var isCollapsed = hasClass( NavFrame, "collapsed" );
             var isCollapsed = hasClass( NavFrame, "collapsed" );
             /*
             /*
Line 574: Line 182:
             var NavToggleText = document.createTextNode(isCollapsed ? NavigationBarShow : NavigationBarHide);
             var NavToggleText = document.createTextNode(isCollapsed ? NavigationBarShow : NavigationBarHide);
             NavToggle.appendChild(NavToggleText);
             NavToggle.appendChild(NavToggleText);
 
             // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
             // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
             for(var j=0; j < NavFrame.childNodes.length; j++) {
             for(var j=0; j < NavFrame.childNodes.length; j++) {
Line 586: Line 194:
     }
     }
}
}
 
$( createNavigationBarToggleButton );
$( createNavigationBarToggleButton );

Revision as of 19:36, 3 February 2012

/* Any JavaScript here will be loaded for all users on every page load. */

/* Return whether a particular class is used**************************************
 * Description: Uses regular expressions and caching for better performance.
 */
 
 var usesClass = (function () {
     var reCache = {};
     return function (element, className) {
         return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
     };
 })();

/** Collapsible tables *********************************************************
 *
 *  Description: Allows tables to be collapsed, showing only the header. See
 *               [[Wikipedia:NavFrame]].
 *  Maintainers: [[User:R. Koot]]
 */
 
var autoCollapse = 2;
var collapseCaption = "hide";
var expandCaption = "show";
 
window.collapseTable = function( tableIndex ){
    var Button = document.getElementById( "collapseButton" + tableIndex );
    var Table = document.getElementById( "collapsibleTable" + tableIndex );
 
    if ( !Table || !Button ) {
        return false;
    }
 
    var Rows = Table.rows;
 
    if ( Button.firstChild.data == collapseCaption ) {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = "none";
        }
        Button.firstChild.data = expandCaption;
    } else {
        for ( var i = 1; i < Rows.length; i++ ) {
            Rows[i].style.display = Rows[0].style.display;
        }
        Button.firstChild.data = collapseCaption;
    }
}
 
function createCollapseButtons(){
    var tableIndex = 0;
    var NavigationBoxes = new Object();
    var Tables = document.getElementsByTagName( "table" );
 
    for ( var i = 0; i < Tables.length; i++ ) {
        if ( hasClass( Tables[i], "collapsible" ) ) {
 
            /* only add button and increment count if there is a header row to work with */
            var HeaderRow = Tables[i].getElementsByTagName( "tr" )[0];
            if (!HeaderRow) continue;
            var Header = HeaderRow.getElementsByTagName( "th" )[0];
            if (!Header) continue;
 
            NavigationBoxes[ tableIndex ] = Tables[i];
            Tables[i].setAttribute( "id", "collapsibleTable" + tableIndex );
 
            var Button     = document.createElement( "span" );
            var ButtonLink = document.createElement( "a" );
            var ButtonText = document.createTextNode( collapseCaption );
 
            Button.className = "collapseButton";  //Styles are declared in Common.css
 
            ButtonLink.style.color = Header.style.color;
            ButtonLink.setAttribute( "id", "collapseButton" + tableIndex );
            ButtonLink.setAttribute( "href", "#" );
            addHandler( ButtonLink,  "click", new Function( "evt", "collapseTable(" + tableIndex + " ); return killEvt( evt );") );
            ButtonLink.appendChild( ButtonText );
 
            Button.appendChild( document.createTextNode( "[" ) );
            Button.appendChild( ButtonLink );
            Button.appendChild( document.createTextNode( "]" ) );
 
            Header.insertBefore( Button, Header.firstChild );
            tableIndex++;
        }
    }
 
    for ( var i = 0;  i < tableIndex; i++ ) {
        if ( hasClass( NavigationBoxes[i], "collapsed" ) || ( tableIndex >= autoCollapse && hasClass( NavigationBoxes[i], "autocollapse" ) ) ) {
            collapseTable( i );
        } 
        else if ( hasClass( NavigationBoxes[i], "innercollapse" ) ) {
            var element = NavigationBoxes[i];
            while (element = element.parentNode) {
                if ( hasClass( element, "outercollapse" ) ) {
                    collapseTable ( i );
                    break;
                }
            }
        }
    }
}
 
$( createCollapseButtons );
 
/** Dynamic Navigation Bars (experimental) *************************************
 *
 *  Description: See [[Wikipedia:NavFrame]].
 *  Maintainers: UNMAINTAINED
 */
 
// set up the words in your language
var NavigationBarHide = '[' + collapseCaption + ']';
var NavigationBarShow = '[' + expandCaption + ']';
 
// shows and hides content and picture (if available) of navigation bars
// Parameters:
//     indexNavigationBar: the index of navigation bar to be toggled
window.toggleNavigationBar = function(indexNavigationBar){
    var NavToggle = document.getElementById("NavToggle" + indexNavigationBar);
    var NavFrame = document.getElementById("NavFrame" + indexNavigationBar);
 
    if (!NavFrame || !NavToggle) {
        return false;
    }
 
    // if shown now
    if (NavToggle.firstChild.data == NavigationBarHide) {
        for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
            if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
                NavChild.style.display = 'none';
            }
        }
    NavToggle.firstChild.data = NavigationBarShow;
 
    // if hidden now
    } else if (NavToggle.firstChild.data == NavigationBarShow) {
        for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
            if (hasClass(NavChild, 'NavContent') || hasClass(NavChild, 'NavPic')) {
                NavChild.style.display = 'block';
            }
        }
        NavToggle.firstChild.data = NavigationBarHide;
    }
}
 
// adds show/hide-button to navigation bars
function createNavigationBarToggleButton(){
    var indexNavigationBar = 0;
    // iterate over all < div >-elements 
    var divs = document.getElementsByTagName("div");
    for (var i = 0; NavFrame = divs[i]; i++) {
        // if found a navigation bar
        if (hasClass(NavFrame, "NavFrame")) {
 
            indexNavigationBar++;
            var NavToggle = document.createElement("a");
            NavToggle.className = 'NavToggle';
            NavToggle.setAttribute('id', 'NavToggle' + indexNavigationBar);
            NavToggle.setAttribute('href', 'javascript:toggleNavigationBar(' + indexNavigationBar + ');');
 
            var isCollapsed = hasClass( NavFrame, "collapsed" );
            /*
             * Check if any children are already hidden.  This loop is here for backwards compatibility:
             * the old way of making NavFrames start out collapsed was to manually add style="display:none"
             * to all the NavPic/NavContent elements.  Since this was bad for accessibility (no way to make
             * the content visible without JavaScript support), the new recommended way is to add the class
             * "collapsed" to the NavFrame itself, just like with collapsible tables.
             */
            for (var NavChild = NavFrame.firstChild; NavChild != null && !isCollapsed; NavChild = NavChild.nextSibling) {
                if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
                    if ( NavChild.style.display == 'none' ) {
                        isCollapsed = true;
                    }
                }
            }
            if (isCollapsed) {
                for (var NavChild = NavFrame.firstChild; NavChild != null; NavChild = NavChild.nextSibling) {
                    if ( hasClass( NavChild, 'NavPic' ) || hasClass( NavChild, 'NavContent' ) ) {
                        NavChild.style.display = 'none';
                    }
                }
            }
            var NavToggleText = document.createTextNode(isCollapsed ? NavigationBarShow : NavigationBarHide);
            NavToggle.appendChild(NavToggleText);
 
            // Find the NavHead and attach the toggle link (Must be this complicated because Moz's firstChild handling is borked)
            for(var j=0; j < NavFrame.childNodes.length; j++) {
                if (hasClass(NavFrame.childNodes[j], "NavHead")) {
                    NavToggle.style.color = NavFrame.childNodes[j].style.color;
                    NavFrame.childNodes[j].appendChild(NavToggle);
                }
            }
            NavFrame.setAttribute('id', 'NavFrame' + indexNavigationBar);
        }
    }
}
 
$( createNavigationBarToggleButton );