/*
Explorer methods

  var height;
  if (document.body && document.body.offsetHeight) height=document.body.offsetHeight;
  else if (document.body && document.body.clientHeight) height=document.body.clientHeight;
  else if (window.innerHeight) height= window.innerHeight;

*/

// this method get only one value parameter from search string in requested URI 
function getParameter(name) {
  var value=null;
  if (!name) return value;
  var search=window.location.search;
  if (!search) return value;
  if (search.charAt(0)=='?') search=search.substr(1);
  if (search.indexOf(name) < 0) return value;
  search=search.substr(search.indexOf(name) + name.length);
  if (search.charAt(0)!='=') return value;
  search=search.substr(1);
  if (search.indexOf('&') < 0) return search;
  return value=search.substring(0, search.indexOf('&'));
}

var about=getParameter('about');


/*
TODO, find a way to put this in frame
*/

function explorerInit() {

  if (!document.getElementById) return false; 
  var dir=document.getElementById("explorer");
  if (!dir) return false;
  
  dir.links=dir.getElementsByTagName('A');
  if(dir.links.length == 0) dir.links=dir.getElementsByTagName('a');
  var i=0;
  // set index of links
  while(dir.links[i]) {
    // focus on the second link (first supposed to be the root)
    if (i == 1) dir.links[i].focus();
    dir.links[i].index=i;
    dir.links[i].onkeydown=explorerKeyDown;
    dir.links[i].onmouseover=explorerMouseOver;
    i++;
  }
  if (!dir.childNodes) return false;
  // close all folders
  folders=dir.getElementsByTagName('li');
  for (var i=0; i<folders.length ; i++) {
    li=folders[i];
    nested=li.getElementsByTagName('ul');
    if (nested[0] && nested[0].style) {
      li.ul=nested[0];
      li.onclick=expand;
      nested[0].style.display="none";
    }
  }
}

/*
@see <http://www.quirksmode.org/js/events_order.html>
*/

function explorerLinkClick() {
  // in case of IE ()
  if (document.all ) event.cancelBubble = true;
}

function expand(e, button, action) {

 // the target element from which is event
  var el;
	if (!e) var e = window.event;
	if (e.target) el = e.target;
	else if (e.srcElement) el = e.srcElement;
  // Safari bug
	if (el && el.nodeType == 3) el = el.parentNode;
  var elTagName;
  if (el) elTagName=el.tagName.toLowerCase();
  // if we are on a link, without a desired call (button != null)
  // let link work
  if (elTagName == "a" && !button) return true;
  // maybe optimized
  if(!button || !button.style) {
    button=this;
  }
  // ul is supposed to have been affected by init
  var ul=button.ul;
  if (!ul) return false;
  // find an action to do
  if (!action) if (ul.style.display == "") action="close"
  else if (!action) action="open";
  // do action (hide/show)
  if (action == "open") ul.style.display="";
  else if (action == "close") ul.style.display="none";
  else ul.style.display=(ul.style.display == 'none')?'':'none';
  
  // change className
  if(button.className) button.className=action;
  // give focus to link
  var links=button.getElementsByTagName('A');
  if(links.length == 0) links=button.getElementsByTagName('a');
  // don't give focus when close (IE don't like it)
  if(action == "open" && links[0] && links[0].focus) links[0].focus();
  // in case of IE 
  if (document.all ) e.cancelBubble = true;
  if (e.stopPropagation) e.stopPropagation();
  // how to cancel bubble for Firefox ?
  return false;
}




function explorerMouseOver(e, o) {
  if (!o) o=this;
  o.focus();
}

function goLang(select) {
  var s=new String(window.location); 
  var lang=select.options[select.selectedIndex].text;
  var sharp="";
  var query="";
  if (s.indexOf('#') != -1) {
    sharp=s.substring(s.indexOf('#'));
    s=s.substring(0,s.indexOf('#')) ;
  }
  if (s.indexOf('?') != -1) {
    query=s.substring(s.indexOf('?'));
    s=s.substring(0,s.indexOf('?')) ;
  }
  if( s.lastIndexOf('.') - s.search(/_..\./) == 3) s=s.replace(/_..\./, '.');
  s=s.substring(0, s.lastIndexOf('.')) + '_'+lang + s.substring(s.lastIndexOf('.')); 
  window.location=s+query+sharp;
}

function getKey(e) {
  if (document.all) e = window.event;
  if(!e)return;
  if (e.keyCode) return e.keyCode;
  else if (e.which) return e.which;
  else return;
}

function explorerKeyDown(e, o) {
  if(!o) o=this;
  key=getKey(e);
  if (!document.getElementById) return false; 
  var dir=document.getElementById("explorer");
  if (!dir || !dir.links) return false;
  // previous
  if (key==38) {
    if (o.index == 0) return;
    previous=dir.links[o.index - 1];
    while (previous && previous.index > 0 && !isVisible(previous)) {
      previous=dir.links[previous.index - 1];
    }
    if (!previous || !isVisible(previous)) return;
    previous.focus();
    return false;
  }
  // next
  else if (key==40) {
    if (o.index == dir.links.length - 1) return;
    next=dir.links[o.index + 1];
    while (next && !isVisible(next) && next.index < dir.links.length - 2 ) {
      next=dir.links[next.index + 1];
    }
    if (!next || !isVisible(next)) return;
    next.focus();
    return false;
  }
  // open
  else if (key==39) {
    if (o.parentNode) expand(e, o.parentNode, "open"); 
  }
  // close
  else if (key==37) {
    if (o.parentNode) expand(e, o.parentNode, "close"); 
  }
  // page up
  else if(key==33) {
    diff=10;
    if (o.index - diff < 0) return;
    previous=dir.links[o.index - diff];
    while (previous && previous.index > 0 && !isVisible(previous)) {
      previous=dir.links[previous.index - 1];
    }
    if (!previous || !isVisible(previous)) return;
    previous.focus();
    return false;
  }
  // page down
  else if(key==34) {
    diff=10;
    if (o.index + diff > dir.links.length - 1) return;
    next=dir.links[o.index + diff];
    while (next && !isVisible(next) && next.index < dir.links.length - 2 ) {
      next=dir.links[next.index + 1];
    }
    if (!next || !isVisible(next)) return;
    next.focus();
    return false;
  }
  
}

function isVisible (o) {
  while (o && o.style) {
    if (o.style.display == "none") return false;
    o=o.parentNode;
  }
  return true;
}

/*
get properties of an object
for debug
*/
function props(o) {
   var result = ""
   var a=new Array();
   var i=0
   for (var prop in o) {
      a[i]= prop  + "\t"; // + " = " + o[prop]
      i++;
   }
   a.sort();
   return a;
}

