var _treeCurrentId=null;
function treeHandler(e) {
    e = e?e:window.event;
    var o = e[window.event?'srcElement':'target'];
    var oLi;


/*
 some magic: in IE; clickable area belonged to the parent block; when A display:block mode 
so there was no way to know which optin the user wanted to click.
use ie elementFromPoint function to simulate user clicking 40px to the right and use that as the
element clicked
*/
if( o.tagName=="UL" || o.tagName == 'DIV'  && document.elementFromPoint ) 
  o =  document.elementFromPoint( window.event.clientX+40,window.event.clientY );

 
    if (o.tagName == 'A')   oLi = o.parentNode;
    else if (o.tagName == 'LI')  oLi = o;
    else return;

     var oFc = _firstChild( oLi );

        if (oLi.className.indexOf('collapse') != -1) {
             oLi.className = oLi.className.replace('collapsed', 'open');
            oFc.className = oFc.className.replace('collapsed', 'open');
             }    
    else       if (oLi.className.indexOf('open') != -1) {
             oLi.className = oLi.className.replace('open', 'collapsed');
            oFc.className = oFc.className.replace('open', 'collapsed');
           
    }    
   var bLoadOnDemand = ( oLi.className.indexOf('loadOnDemand') != -1 );


    var loc = location.href;
    if( location.hash ) loc = loc.replace(location.hash,'');
     
    oFc.blur();
   if ( oFc.href.indexOf( loc )!= -1 && oFc.hash==''  || oFc.href.indexOf('void')>-1 || bLoadOnDemand  ){
       // stop bubbling 
        if ( e.preventDefault ) e.preventDefault(); //Netscape, Gecko
         else e.returnValue = false; //IE
   }         


  if( bLoadOnDemand )    setTimeout(function(){treeLoadXml( oFc.href ,oLi)},10);

}

function treeCollapseAll( sTreeId ){
  treeActivate( sTreeId ,'collapsed') ;
}

function treeExpandAll( sTreeId ){
  treeActivate( sTreeId ,'open') ;
}

function treeActivate( treeId ,sDefaultStyle,bLoadOnDemand) {
   var oTree;
   if( typeof( treeId ) == 'string' ) 
    oTree = document.getElementById(treeId);
   else
      oTree = treeId;
   
   bLoadOnDemand=bLoadOnDemand||false;

    sDefaultStyle=sDefaultStyle||'open';
    sDefaultStyle=sDefaultStyle.toLowerCase();
     
    sDefaultStyle = 'open:collapsed'.indexOf(sDefaultStyle) == -1 ?'collapsed':sDefaultStyle;
    if( ! bLoadOnDemand )
    oTree.onclick = function(event){ treeHandler(event)};
    
    var oUls = oTree.getElementsByTagName("UL");
    var l = oUls.length;
    for (var i = 1; i < l; i++) {
        var oParent = oUls[i].parentNode; //LI item
       
   if( oParent.className.indexOf('open') != -1 || oParent.className.indexOf('collapsed') != -1  )
          oParent.className = oParent.className.replace(oParent.className, sDefaultStyle);
   else
          oParent.className += ' ' +  sDefaultStyle;

   oFc = _firstChild(oParent);

   if( oFc.className.indexOf('open') != -1 || oFc.className.indexOf('collapsed') != -1  )
          oFc.className = oFc.className.replace(oFc.className, sDefaultStyle);
   else
          oFc.className += ' ' +  sDefaultStyle;
     
        var oNs = _nextSibling(oParent);
        
        if( oNs && oNs.tagName=='LI' ) 
         oUls[i].className = 'section';
        else   
            oParent.className +=  (i==1?'First':'Last');  /*you got no section you must be ither the first or last expander*/
       
        var oLc = _lastChild(oUls[i]);         
       
        if( oLc ) oLc.className='last';
        
    }
       oLc = _lastChild(oUls[0]);         
       var oFc = _firstChild(oUls[0]);         
       if( oLc && oLc != oFc) oLc.className='last';
}


var ELEMENT_NODE = 1;
function _nextSibling(o){
 do
 {
    o = o.nextSibling;
   } while( o && o.nodeType != ELEMENT_NODE  ) 
     return o;
}

function _previousSibling(o){
 do
 {
    o = o.previousSibling;
   } while( o && o.nodeType != ELEMENT_NODE  ) 
  return o;
}

function _lastChild(o){
  o = o.lastChild;
   while( o && o.nodeType != ELEMENT_NODE ){
     o = o.previousSibling;
   }
   
    
 return o;
} 

function _firstChild(o){
  o = o.firstChild;
   while( o && o.nodeType != ELEMENT_NODE ){
     o = o.nextSibling;
   }
 return o;
} 

function showTitle(e){
 o = (e?e:window.event)[window.event?'srcElement':'target']; //
if(o.tagName=='A' && ! o.title ) o.title=o.innerHTML.replace(/<[^>]+>/g,"");

 }


