/* generated javascript */
var skin = 'monobook';
var stylepath = '/w/skins';

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

/* MediaWiki:Monobook.js */
/* Any JavaScript here will be loaded for users using the MonoBook skin */

// Chat: I think the following line is broken?
//ta['research_prompt'] = new Array ('c', 'Research');

// Mudmeet signup test
if (wgPageName == "User:Chat/test_signup")
{
  importScript("User:Chat/signup.js")
}

/************************************************************************/
/* Script utility functions.                                            */
/************************************************************************/

// Tests if an elements has a certain class.  Taken from Wikipedia.
var hasClass = (function () {
    var reCache = {};
    return function (element, className) {
        return (reCache[className] ? reCache[className] : (reCache[className] = new RegExp("(?:\\s|^)" + className + "(?:\\s|$)"))).test(element.className);
    };
})();
/************************************************************************/
/* End of script utility functions.                                     */
/************************************************************************/

/************************************************************************/
/* Multi-infobox functions.                                             */
/************************************************************************/

// This variable holds the selected tab index for each table.
var mib_tables = [];

// This function deals with actually selecting an infobox. It unfades the 
// relevant tab, and shows that infobox, while fading the other tabs and 
// hiding their infoboxes.
function mib_select_tab(table_index, tab_index)
{
  var tables;
  var table;
  var rows;
  var row;
  var cells;
  var old_tab;
  var old_box;
  var new_tab;
  var new_box;

  // We first need to revert the settings for the currently selected 
  // tab and box - go and get them, along with the intended new
  // tab and box.
  tables = document.getElementsByTagName("table");
  if (table_index >= tables.length)
  {
    // Something's gone horribly wrong if we get here...
    return;
  }

  table = tables[table_index];
  
  if (table.rows)
  {
    // Firefox
    rows = table.rows;
  }
  else
  {
    // IE
    rows = table.childNodes;
    // Why does the table have itself as a child node?  Who knows...
    rows = rows[0].childNodes;
  }
  
  if ((mib_tables[table_index] >= rows.length - 1) ||
      (tab_index >= rows.length - 1))
  {
    // Something's gone horribly wrong if we get here...
    return;
  }

  if (rows[0].cells)
  {
    // Firefox
    cells = rows[0].cells;
  }
  else
  {
    // IE
    cells = rows[0].childNodes;
  }
  if ((mib_tables[table_index] >= cells.length) ||
      (tab_index >= cells.length))
  {
    // Something's gone horribly wrong if we get here...
    return;
  }

  old_tab = cells[mib_tables[table_index]];
  new_tab = cells[tab_index];

  if (rows[mib_tables[table_index] + 1].cells)
  {
    // Firefox
    cells = rows[mib_tables[table_index] + 1].cells;
  }
  else
  {
    // IE
    cells = rows[mib_tables[table_index] + 1].childNodes;
  }

  if (cells.length == 0)
  {
    // Something's gone horribly wrong if we get here...
    return;
  }

  old_box = cells[0];

  if (rows[tab_index + 1].cells)
  {
    // Firefox
    cells = rows[tab_index + 1].cells;
  }
  else
  {
    // IE
    cells = rows[tab_index + 1].childNodes;
  }

  if (cells.length == 0)
  {
    // Something's gone horribly wrong if we get here...
    return;
  }

  new_box = cells[0];

  // Revert the old tab and box settings - the tab's colour is faded and
  // the box is made invisible.
  old_tab.setAttribute("style", "text-align:center;border:1px solid lightgrey;background-color:whitesmoke;cursor:pointer");
  old_box.setAttribute("style", "display:none");

  // Now set the style for the new tab and box.  The new tab is unfaded, and has its 
  // bottom border removed (makes it look like it's part of the box).  The new box is
  // unhidden; its top border is likewise removed to make it one with the appropriate
  // tab.
  new_tab.setAttribute("style", "text-align:center;border:1px solid lightgrey;border-bottom-width:0px;cursor:pointer");
  new_box.setAttribute("style", "border:1px solid lightgrey;border-top-width:0px");

  // Update the selected tab for this table.
  mib_tables[table_index] = tab_index;   
}

// This function deals with setting up the page.  It initializes the infoboxes and 
// tabs such that all are in their 'unselected' state - the tabs look faded, the infoboxes
// are hidden.  It then selects the first tab as default.
function mib_setup_page()
{
  var tables = document.getElementsByTagName("table");
  var i;
  var j;
  var rows;
  var cells;

  // Look for tables with class 'multiinfobox'
  for (i = 0; i < tables.length; i++) 
  {
    // Initialize the tab index for this table to that of the first tab.
    // We do this for all tables (whether multiinfobox or not) for ease
    // of reference later.
    mib_tables.push(0);

    if (hasClass(tables[i], "multiinfobox")) 
    {
      // Give this table a unique ID.
      tables[i].setAttribute( "id", "multiinfobox" + i );

      // Get the first row
      if (tables[i].rows)
      {
        // Firefox
        rows = tables[i].rows;
      }
      else
      {
        // IE
        rows = tables[i].childNodes;
        // Why does the table have itself as a child node?  Who knows...
        rows = rows[0].childNodes;      
      }
      
      if (rows.length == 0) 
      {
        // Someone created an empty multiinfobox table.  Weirdo.
        continue;
      }

      // The first row contains the tabs - we need to linkify them, and
      // associate them with the corresponding infoboxes.
      if (rows[0].cells)
      {
        // Firefox
        cells = rows[0].cells;
      }
      else
      {
        // IE
        cells = rows[0].childNodes;
      }

      for (j = 0; j < cells.length; j++)
      {
        // Set unique ID and script.
        cells[j].setAttribute("id", "multiinfobox-tab-" + i + "-" + j);
        cells[j].setAttribute("onclick", "mib_select_tab(" + i + ", " + j + ");");

        // Initialize all tabs in their 'unselected' mode.
        cells[j].setAttribute("style", "text-align:center;border:1px solid lightgrey;background-color:whitesmoke;cursor:pointer");
      }

      // The rest of the table comprises single-cell rows, each of which 
      // should contain an infobox.  We need to set IDs which can be used
      // to associate them with the relevant tabs.
      for (j = 1; j < rows.length; j++)
      {
        if (rows[j].cells)
        {
          // Firefox
          cells = rows[j].cells;
        }
        else
        {
          // IE
          cells = rows[j].childNodes;
        }
        if (cells.length == 0)
        {
          // Someone created a row without anything in it.  Weirdo.
          continue;
        }
        
        cells[0].setAttribute("id", "multiinfobox-box-" + i + "-" + (j - 1));

        // Initialize the box as hidden.
        cells[0].setAttribute("style", "display:none");
      }

      // Now select the first tab/box.
      mib_select_tab(i, 0);
    }
  }     
}

// Get the multiinfobox page set up
addOnloadHook(mib_setup_page);

/************************************************************************/
/* End of multi-infobox functions.                                      */
/************************************************************************/

/************************************************************************/
/* Collapsible tables (imported from wikipedia).                        */
/************************************************************************/

var autoCollapse = 2;
var collapseCaption = "hide";
var expandCaption = "show";

function collapseTable(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")) 
    {
      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";

      ButtonLink.style.color = Header.style.color;
      ButtonLink.setAttribute("id", "collapseButton" + tableIndex);
      ButtonLink.setAttribute("href", "javascript:collapseTable(" + tableIndex + ");");
      ButtonLink.appendChild(ButtonText);

      Button.appendChild(document.createTextNode("["));
      Button.appendChild(ButtonLink);
      Button.appendChild(document.createTextNode("]"));

      Header.insertBefore(Button, Header.childNodes[0]);
      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;
        }
      }
    }
  }
}

addOnloadHook(createCollapseButtons);

/************************************************************************/
/* End of collapsible table functions.                                  */
/************************************************************************/