var xmlHttp
var eID
var pNode
var sortedBy

if (document.ELEMENT_NODE == null) {
  document.ELEMENT_NODE = 1;
  document.TEXT_NODE = 3;
}

function sortBy(listID)
{    
      if (listID == sortedBy && !pNode) pNode = true;
      else pNode = false;
      alt = false;
      var arr = new Array();
      var list = document.getElementById("sortBy");
      var sort = list.getElementsByTagName(listID);
      var i = sort.length-1;

      do
      {
            var listItem = sort.item(i);
            arr.push(listItem);
            listItem.parentNode.className = "";           
      }
      while(i--)
      if(listID == "PLAYERS") arr.sort(sortNumber);
      else arr.sort(sortAlhpa);
       
      var len = arr.length-1;
      i = arr.length-1;
      do
      {
            //list.insertBefore(arr[len-i].parentElement,list.firstChild);
            list.appendChild(arr[len-i].parentNode);
            if (alt) {arr[len-i].parentNode.className = "alt"; alt = false;}
            else {arr[len-i].parentNode.className = ""; alt = true;}
      }
      while(i--)
      
      arr = null;
      sortedBy = listID;


}
function sortNumber(a, b)
{
      var aN, bN;
      aN = parseInt(a.textContent);
      bN = parseInt(b.textContent);
      if(pNode) return aN - bN;
      else return bN - aN;
}

function sortAlhpa(a, b)
{
      var aA, bA;
      aA = a.textContent.toUpperCase();
      bA = b.textContent.toUpperCase();
      if(pNode)
        return ( aA == bA ? 0 : (aA < bA ? 1 : -1));  
      else 
        return ( aA == bA ? 0 : (aA < bA ? -1 : 1));     
}

function showDiv(ID) { 
    eID = ID;   
    if(!document.getElementById(ID).shown) {
    document.getElementById(ID).style.display = "block";
    document.getElementById(ID).shown = true;
    document.getElementById('blip').innerHTML = "[-]";
    xmlHttp=GetXmlHttpObject();
    if (xmlHttp==null)
      {
      document.getElementById(ID).innerHTML = "<a href=\"http://www.tribesnext.com/inc/list.php\">Your browser does not support AJAX.</a>";
      return;
      }        
    xmlHttp.onreadystatechange=stateChanged;
    xmlHttp.open("GET","../inc/list.php",true);
    xmlHttp.send(null);

    } else {     document.getElementById(ID).shown = false;
    document.getElementById(ID).style.display = "none";
    document.getElementById('blip').innerHTML = "[+]"; }
}
function stateChanged() 
{ 
if (xmlHttp.readyState==4)
   { 
   document.getElementById(eID).innerHTML=xmlHttp.responseText;

   document.getElementById('sortByName').onclick = function() { sortBy('NAME') };
   document.getElementById('sortByPlayers').onclick = function() { sortBy('PLAYERS') };
   document.getElementById('sortByMap').onclick = function() { sortBy('MAP') };
   document.getElementById('sortByMod').onclick = function() { sortBy('MOD') };
   document.getElementById('sortByType').onclick = function() { sortBy('TYPE') };
   document.getElementById('sortByIPA').onclick = function() { sortBy('IPA') };

   }
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

var wideLode
wideLode = false;
function sitWide()
{
    if (document.getElementById("content").offsetWidth != 550 && document.getElementById("content").offsetWidth != 750) {wideLode = true;}
    wideLode = !wideLode;
    var exdate=new Date();
    if (wideLode) { 
	document.getElementById("content").style.width = "85%";
	document.getElementById("wideLegs").innerHTML = "go slim";

	exdate.setDate(exdate.getDate()+365);
	document.cookie="widelode=" + wideLode + ";expires=" + exdate.toGMTString();
    } else { 
	document.getElementById("content").style.width = "750px";
	document.getElementById("wideLegs").innerHTML = "go wide";
	document.cookie="widelode=;expires=" + exdate.toGMTString();
    }
}
