/* cross-browser (tested with ie5, mozilla 1 & opera 5) keypress detection */
function get_keycode(evt) {
         
  // if taken from nanotree.
	if (window.event) {
		evt = window.event;
	}

  // IE
    code = document.layers ? evt.which
           : document.all ? event.keyCode // event.keyCode!=evt.keyCode!
           : evt.keyCode;

  if (code==0) 
    code=evt.which; // for NS
  return code;
}

    function RefreshClick (name) {
      window.location="/php/php.exe/mathtree/index.php?"+name;
    }

    function open_url(name) {
	window.open(name);
    }

    function combine_str(a,b,c) {
        return (a+b+c);
    }

    function open_combined_url(a,b,c) {
        open_url(a+b+c);
    }

    function test_key(event,a,b,c) {
	code=event.keyCode; // for IE
	if (code==0) code=event.which; // for NS
	if (code==13) 
		open_url(a+b+c)    
    }

var lastnode=null;
var listnodes = null;
var list_index=1;
var lastnodetype=''; // determines if node is a link, input or text;

// up, left, down, right, keypress codes
//ijkl
//var keys = new Array(105,106,107,108);
//num arrows
//var keys = new Array(56,52,50,54);
//wasd
 var press2 = new Array(119,97,115,100); 
 var press = new Array(47,45,42,43);

// keydown codes
  //  var keys2=new Array(87,65,83,68);
  var keys= new Array(38,37,40,39);

  // keyset 1 = keydown, otherwise press
function checkup(keyset,n) {
  if (keyset==1) return (n==keys[0]);
  return ((n==press[0]) || (n==press2[0]))
}

function checkdn(keyset,n) {
  if (keyset==1) return (n==keys[2]);
  return ((n==press[2]) || (n==press2[2]))
}

function checkl(keyset,n) {
  if (keyset==1) return (n==keys[1]);
  return ((n==press[1]) || (n==press2[1]))
}

function checkr(keyset,n) {
  if (keyset==1) return (n==keys[3]);
  return ((n==press[3]) || (n==press2[3]))
}



function is_exp(n) {
  if (n==null) return false;
  return ((n.className=='exp') || (n.className=='exp_active'));
}

function is_col(n) {
  if (n==null) return false;
  return ((n.className=='col') || (n.className=='col_active'));
}

function is_basic(n) {
  if (n==null) return false;
  return ((n.className=='basic') || (n.className=='basic_active'));
}

/* returns i>=0 if true */
function is_active(node) {
  if (node.className==null) return false
  return node.className.indexOf('_active');
}

function toggle_class(node) {
  if ((node==null) || (node.className==null)) return;
  str=node.className;
  result="";
  i = str.indexOf('_active');
  if (i>0)
    result= str.substr(0,i);
  else
    result= str+"_active";
  node.className=result;
  return node;
}

function is_list_node(n) {
  if (n==null) return false;
  if (n.className==null) return false;
  if ( (is_exp(n)) || 
       (is_col(n)) ||
       (is_basic(n)) )
   return true; else return false;
}


function get_href(n) {
  alist=n.attributes;
  if (alist!=null) {
    hr = alist.getNamedItem('href');
    if (hr!=null) return hr.nodeValue;
  }
  if (n.childNodes.length==0) return '';
  for (var i=0; i<n.childNodes.length; i++) {
    s = get_href(n.childNodes[i]);
    if (s!='') return s;
  }
  return '';
}

function get_link(n) {
  if (n==null) return null;
  //  alert(n.nodeName);
  if (n.nodeName=='A') return n;
  if (n.childNodes.length==0) return null;
  for (var i=0; i<n.childNodes.length; i++) {
    s = get_link(n.childNodes[i]);
    if (s!=null) return s;
  }
  return null;
}

function set_lastnode(n) {
  if (is_active(lastnode)>=0)
    toggle_class(lastnode);
  lastnode=n;
  if (!(is_active(lastnode)>=0))
    toggle_class(lastnode);
}

function next_list_node() {
  tempIndex = list_index;
  while (tempIndex<listnodes.length-1) {
    tempIndex++;
    var x = listnodes[tempIndex];
    if (is_list_node(x)) {
      list_index=tempIndex;
      return;
    }
  }
}

function prev_list_node() {
  tempIndex = list_index;
  while (tempIndex>0) {
    tempIndex--;
    var x = listnodes[tempIndex];
    if (is_list_node(x)) {
      list_index=tempIndex;
      return;
    }
  }
}

function getsub (li) {
  for (var c = 0; c < li.childNodes.length; c++)
    if ( (li.childNodes[c].className == 'sub') || (li.childNodes[c].className == 'subexp') ) 
      return li.childNodes[c];
}

function find_listnode_recursive (li) {
  if (is_list_node(li)) return li; 
  if (li.childNodes.length==0) return null;
  result=null;
  for (var c = 0; c < li.childNodes.length; c++) {
    result=find_listnode_recursive(li.childNodes[c]);
    if (result!=null) return result;
  }
  return null;
}

function next_child_listnode(li) {
  var result=null;
  for (var i=0; i<li.childNodes.length; i++) {
    result=find_listnode_recursive(li.childNodes[i]);
    if (result!=null) return result;
  }
  return null;  
}

function next_actual_sibling_listnode(li) {
  if (li==null) return null;
  var temp=li;
  while (1) { 
    var n = temp.nextSibling;
    if (n==null) {
      n=parent_listnode(temp);
      return next_actual_sibling_listnode(n);
    }
    if (is_list_node(n)) return n;
    temp=n;
  }
}

function next_sibling_listnode(li) {
if (li==null) return null; 
 var result=null;
  var temp=li;
  if (is_col(temp)) return next_child_listnode(temp);
  while (1) { 
    var n = temp.nextSibling;
    if (n==null) {
      n=parent_listnode(temp);
      return next_actual_sibling_listnode(n);
    }
    if (is_list_node(n)) return n;
    temp=n;
  }
}

function last_sibling_listnode(li) {
  if (li==null) return null;
  var temp=li;
  var last=null;
  while(1) {
    var n = temp.nextSibling;
    if (is_list_node(temp)) 
      last = temp;
    if (n==null) {
      if (is_col(last)) return last_sibling_listnode(next_child_listnode(last));
      else return last;
    }
    temp = n;
  }
}

function prev_sibling_listnode(li) { 
  if (li==null) return null;
  var temp=li;
  var n = null;
  while (1) { 
    n = temp.previousSibling;
    if (n==null) {
      return parent_listnode(li);
    }
    if (is_list_node(n)) {
      if (is_col(n)) 
        return last_sibling_listnode(next_child_listnode(n));
      else
        return n;
    }
    temp=n;
  }
}


function parent_listnode(li) {
  n=li;
  while (1) {
    n=n.parentNode;
    if (n==null) return null;
    if (is_list_node(n)) return n;
  }
}

function getVisibleParents(id) {
  var n = document.getElementById(id);
  while(1) {
    expand(n);
    n = parent_listnode(n);
    if (n==null) return;
  }
}

function onClickHandler (evt) {
if (lastnode==null) 
{
listnodes = document.getElementsByTagName('li');
lastnode=listnodes[1];
temp=listnodes[1];
}


  var target = evt ? evt.target : event.srcElement;
  if (!is_list_node(target)) return;
  toggle(target);
  set_lastnode(target);
}

function expand(node) {
    if (!is_exp(node)) return;
    if (node.className=='exp_active') 
      node.className='col_active';
    else 
        node.className='col';
    getsub(node).className='subexp';
}

function collapse(node) {
  if (!is_col(node)) return;
  
if (node.className=='col_active')
    node.className='exp_active'
  else 
    node.className='exp';

  getsub(node).className='sub';

}

function toggle(target) {
  if (!is_list_node(target)) return;
    if (is_col(target)) {
      target.className='exp';
      getsub(target).className='sub';
    }
    else if (is_exp(target)) {
      target.className='col';
      getsub(target).className='subexp';
    }
 
}

function expandAll(node) {
    if (node.className=='exp') {
        node.className='col';
        getsub(node).className='subexp';
    }
    var i;
    if (node.childNodes!=null) 
//    if (node.hasChildNodes()) 
        for ( i = 0; i<node.childNodes.length; i++)
            expandAll(node.childNodes[i]);
}

function collapseAll(node) {
    if  (node.className=='col') {
        node.className='exp';
        getsub(node).className='sub';
    }
    var i;        
    if (node.childNodes!=null) 
// for opera   if (node.hasChildNodes()) 
        for ( i = 0; i<node.childNodes.length; i++)
            collapseAll(node.childNodes[i]);
}

  function keytest (evt) {
 var c = get_keycode(evt);

if (lastnode==null) 
{
listnodes = document.getElementsByTagName('li');
lastnode=listnodes[1];
temp=listnodes[1];
}
//window.alert(c);
if (checkup(1,c)) { // i 
  temp=prev_sibling_listnode(lastnode);
}
else if (checkdn(1,c)) { // k
  temp=next_sibling_listnode(lastnode);
}
else if (checkr(1,c)) { // l
  expand(lastnode);
      temp=next_child_listnode(lastnode);
  if (temp==null) {
    a = get_link(lastnode)
    if (a!=null) a.focus(); 
    }
}
else if (checkl(1,c)) { // j
  if (is_col(lastnode)) 
    collapse(lastnode);
  else {
    temp=parent_listnode(lastnode);  
    collapse(temp);
  }

if (temp==null)
  lastnode.focus(); // forces focus to correct div (try mozilla typesearch)
}
else return;
if (temp!=null) set_lastnode(temp);
   // alert('pressed ' + String.fromCharCode(c) + '(' + c + ')');
    return true;
  };


  function presstest (evt) {
 var c = get_keycode(evt);

if (lastnode==null) 
{
listnodes = document.getElementsByTagName('li');
lastnode=listnodes[1];
temp=listnodes[1];
}
//window.alert(c);
if (checkup(0,c)) { // i 
  temp=prev_sibling_listnode(lastnode);
}
else if (checkdn(0,c)) { // k
  temp=next_sibling_listnode(lastnode);
}
else if (checkr(0,c)) { // l
  expand(lastnode);
      temp=next_child_listnode(lastnode);
  if (temp==null) {
    a = get_link(lastnode)
    if (a!=null) a.focus(); 
    }
}
else if (checkl(0,c)) { // j
  if (is_col(lastnode)) 
    collapse(lastnode);
  else {
    temp=parent_listnode(lastnode);  
    collapse(temp);
  }
if (temp==null)
  lastnode.focus(); // forces focus to correct div (try mozilla typesearch)
}
else return;
  if (temp!=null) set_lastnode(temp);
   // alert('pressed ' + String.fromCharCode(c) + '(' + c + ')');
    return true;
  };


document.onclick = onClickHandler;
  document.onkeypress = presstest;
  document.onkeyup = keytest;


