/**
* Includes
*/
includeJS.Path = './js/';
includeJS('prelude/quickDOM.js');
includeJS('treeMenu.js');
includeJS('prelude/debugMsg.js');

/**
* Script maestro
*/

window.onload = function () {
  if ( window.includeJS.IsLoading ) { window.includeJS.OnEmpty = main; }
  else main();
}


function main () {
	debugMsg();
    activarMenu( document.getElementById('menu') ); debugMsg('activarMenu');
	//treeMenu(); debugMsg('treeMenu');
}



/**
* Agrega la clase "active" a cada LI y enlace cuyo href sea esta página
* TO DO.. hacerlo con los mejores, no solo uno.
*/
function activarMenu(node, newClass) {
  if (!node) node = document.getElementsByTagName('BODY')[0];
  if (!newClass) newClass = 'active';

  var bestNode = [];
  
  var thisPage = document.location.href;
  var ifNode = function (n) { 
  	return n.nodeName == 'A' && thisPage.indexOf(n.href)!=-1; 
  };
  var perform = function (n) {
    if (bestNode.length == 0 || bestNode[0][1]> txtDistance(thisPage, n.href) )
	{ 
	  bestNode = [[n, txtDistance(thisPage, n.href) ]]; 
	}
  };
  var txtDistance = function(a, b) { return Math.abs( a.length - b.length ) };

  DOMIterator(ifNode, perform, node);
  
  bestNode.map( function(item) {
  	var n = item[0];
    var addClass = function (n,c) { n.className = (n.className?n.className+' ':'') + c; };
    addClass(n, newClass);
    if (n.parentNode.nodeName == 'LI') addClass(n.parentNode, newClass);
  } );
}


// Agrega Nedstat
function agregarNedstat(nedstatCode, nodeId, visible) {
  includeJS.OnEmpty = function () {
    var dw = document.write;
    document.write = function (e) { document.globalNedtstat = e; }
    nedstatbasic(nedstatCode, 0);
    document.write = dw;
    var span = document.createElement('span');
    span.innerHTML = document.globalNedtstat;
    if (!visible) {
      span.style.position = 'absolute';
      span.style.height = '1px';
      span.style.textIndent = '-1000px';
      span.style.overflow = 'hidden';
    }
    document.getElementById(nodeId).appendChild(span);
  };
  includeJS( "http://m1.nedstatbasic.net/basic.js");
}