/*
	PureDOM explorer 3.0
	written by Christian Heilmann (http://icant.co.uk)
	Please refer to the pde homepage for updates: http://www.onlinetools.org/tools/puredom/
	Free for non-commercial use. Changes welcome, but no distribution without 
	the consent of the author.
*/
pde={
  childSelected:true,
	// CSS classes
	pdeClass:'pde',
	hideClass:'hide',
	showClass:'show',
	parentClass:'parent',
	currentClass:'current',
	// images added to the parent links
	openImage:'/gene/images/bg_pde_expand.gif',
	closedImage:'/gene/images/bg_pde_collapse.gif',
	openMessage:'close section',
	closedMessage:'open section',
	// boolean to make the parent link collapse the section or not 
	linkParent:false,
	init:function(){
		pde.createClone();
		if(!document.getElementById || !document.createTextNode){return;}
		var uls=document.getElementsByTagName('ul');
		for(var i=0;i<uls.length;i++){
		  var inneruls,parentLI;
			if(!pde.cssjs('check',uls[i],pde.pdeClass)){continue;}
      pde.gCustom(uls[i]);
			var inneruls=uls[i].getElementsByTagName('ul');
			for(var j=0;j<inneruls.length;j++){
				parentLI=inneruls[j].parentNode;
				if(parentLI.getElementsByTagName('strong')[0]){
          //alert('name: ' + parentLI.getElementsByTagName('strong')[0].nodeName);
					continue;
				}
				pde.cssjs('add',parentLI,pde.parentClass);
				parentLI.insertBefore(pde.clone.cloneNode(true),parentLI.firstChild);
				pde.cssjs('add',inneruls[j],pde.hideClass);
				pde.addEvent(parentLI.getElementsByTagName('a')[0],'click',pde.showhide,false);
				parentLI.getElementsByTagName('a')[0].onclick=function(){return false;} // Safari hack
				if(pde.linkParent){
					pde.addEvent(parentLI.getElementsByTagName('a')[1],'click',pde.showhide,false);
					parentLI.getElementsByTagName('a')[1].onclick=function(){return false;} // Safari hack
				}
			}
		}
	},
	showhide:function(e){
		var image,message;
		var elm=pde.getTarget(e);
		var ul=elm.parentNode.getElementsByTagName('ul')[0];
		var img=elm.parentNode.getElementsByTagName('img')[0];
		if(pde.cssjs('check',ul,pde.hideClass)){
			message=pde.openMessage;
			image=pde.openImage;
			pde.cssjs('remove',elm.parentNode.getElementsByTagName('ul')[0],pde.hideClass);
			pde.cssjs('add',elm.parentNode.getElementsByTagName('ul')[0],pde.showClass);
		} else {
			message=pde.closedMessage;
			image=pde.closedImage;
			pde.cssjs('remove',elm.parentNode.getElementsByTagName('ul')[0],pde.showClass);
			pde.cssjs('add',elm.parentNode.getElementsByTagName('ul')[0],pde.hideClass);
		}
		img.setAttribute('src',image);
		img.setAttribute('alt',message);
		img.setAttribute('title',message);
		pde.cancelClick(e);
	},
	createClone:function(){
		pde.clone=document.createElement('a');
		pde.clone.setAttribute('href','#');
		pde.clone.appendChild(document.createElement('img'));
		pde.clone.getElementsByTagName('img')[0].src=pde.closedImage;
		pde.clone.getElementsByTagName('img')[0].alt=pde.closedMessage;
		pde.clone.getElementsByTagName('img')[0].title=pde.closedMessage;
	},
/* helper methods */
	getTarget:function(e){
		var target = window.event ? window.event.srcElement : e ? e.target : null;
		if (!target){return false;}
		if (target.nodeName.toLowerCase() != 'a'){target = target.parentNode;}
		return target;
	},
	cancelClick:function(e){
		if (window.event){
			window.event.cancelBubble = true;
			window.event.returnValue = false;
			return;
		}
		if (e){
			e.stopPropagation();
			e.preventDefault();
		}
	},
	addEvent: function(elm, evType, fn, useCapture){
		if (elm.addEventListener) 
		{
			elm.addEventListener(evType, fn, useCapture);
			return true;
		} else if (elm.attachEvent) {
			var r = elm.attachEvent('on' + evType, fn);
			return r;
		} else {
			elm['on' + evType] = fn;
		}
	},
	cssjs:function(a,o,c1,c2){
		switch (a){
			case 'swap':
				o.className=!pde.cssjs('check',o,c1)?o.className.replace(c2,c1):o.className.replace(c1,c2);
			break;
			case 'add':
				if(!pde.cssjs('check',o,c1)){o.className+=o.className?' '+c1:c1;}
			break;
			case 'remove':
				var rep=o.className.match(' '+c1)?' '+c1:c1;
				o.className=o.className.replace(rep,'');
			break;
			case 'check':
				return new RegExp("(^|\s)" + c1 + "(\s|$)").test(o.className)
			break;
		}
	},
  gCustom:function(enode){
    var pass=1, parentLI, found=false;
    while (!found && pass<=2) {
      //alert(pass);
      if (pass==1){
        // set classes
        var cA,topLINodes, childULs, childLINodes, childFound;
        topLINodes = enode.childNodes;
        for(var x=0;x<topLINodes.length;x++){
          if(topLINodes[x].nodeName=='LI'){
            //alert('Name:' + topLINodes[x].nodeName + ' / ' + topLINodes[x].innerHTML + '\n' + topLINodes[x].getElementsByTagName('a').length);
            if(topLINodes[x].getElementsByTagName('a')[0]){
              cA=topLINodes[x].getElementsByTagName('a')[0];
              //if(cA.getAttribute('href').indexOf(pde.currentPage)>=0){
              if(pde.isThisPage(cA.getAttribute('href'), pass)){
                pde.childSelected=false;
                found=true;
                if(topLINodes[x].getElementsByTagName('ul')[0]){
                  //alert(topLINodes[x].nodeType + ' : ' + topLINodes[x].nodeName + ' /wc/ ' + topLINodes[x].innerHTML);
    					    pde.cssjs('add',topLINodes[x],'pdeatwc');
                } else {
                  //alert(topLINodes[x].nodeType + ' : ' + topLINodes[x].nodeName + ' /nc/ ' + topLINodes[x].innerHTML);
    					    pde.cssjs('add',topLINodes[x],'pdeatnc');
                }
                // add strong tag
                strongTag=document.createElement('strong');
                cA.appendChild(strongTag);
              }
            }
          }
        }
      }
      if(pde.childSelected){
        childFound=false;
        for(x=0;x<topLINodes.length;x++){
          if(childFound) break;
          if(topLINodes[x].nodeName=='LI'){
            if(topLINodes[x].getElementsByTagName('ul')[0]){
              //alert('Name: ' + topLINodes[x].getElementsByTagName('ul')[0].nodeName + ' /at/ ' + topLINodes[x].getElementsByTagName('ul')[0].innerHTML);
              childLINodes=topLINodes[x].getElementsByTagName('ul');
              childULs=topLINodes[x].getElementsByTagName('ul');
              if(childULs[0]){
                for(var y=0;y<childULs.length;y++){
                  childLINodes=childULs[y].getElementsByTagName('li');
                  for(var z=0;z<childLINodes.length;z++){
                    if(childLINodes[z].getElementsByTagName('a')[0]){
                      cA=childLINodes[z].getElementsByTagName('a')[0];
                      //alert(childLINodes[z].nodeType + ' : ' + childLINodes[z].nodeName + ' /at/ ' + childLINodes[z].innerHTML);
                      if(pde.isThisPage(cA.getAttribute('href'), pass)){
                        // add strong tag
                        strongTag=document.createElement('strong');
                        cA.appendChild(strongTag);
          					    pde.cssjs('add',childLINodes[z],'pdeat');
          					    pde.cssjs('add',childLINodes[z].parentNode.parentNode,'pdeinarrow');
                        found=true;
                        childFound=true;
                        break;
                      }
                    }
                  }
                  if(childFound) break;
                }
                if(childFound) break;
              }
            }
          }
        }
  		}
      pass++;
    }
  },
  isThisPage:function(linkURL, pass){
    linkURL=linkURL.toLowerCase();
    if (pass==1) return pde.isPage(linkURL);
    else return pde.isFolder(linkURL);
  },
  isPage:function(linkURL){
    thisPage=location.href;
    thisPage=thisPage.toLowerCase();
    if(thisPage.indexOf('http://')>=0 || thisPage.indexOf('https://')){
      //alert(thisPage + "\n" + url);
      var linkNoIndexStr='', linkIndexStr='';
      hostIndex=thisPage.indexOf("/", 12);
      path=thisPage.substring(hostIndex, thisPage.length);
      if (linkURL.indexOf("index.html") > -1) { linkNoIndexStr=stripFilename(linkURL); }
      if (linkURL.indexOf(".html") == -1) { linkIndexStr=addFilename(linkURL,'index.html'); }
      //alert(linkURL + '\n' + thisPage);
      if(linkURL==thisPage || linkURL==path) { return true;}
      else if (linkNoIndexStr==path || linkNoIndexStr==thisPage || linkIndexStr==path || linkIndexStr==thisPage) { return true;}
      else {return false;}
    }else if(thisPage.indexOf('file://')>=0){
      reversedURL=reverseStr(thisPage);
      thisFile=reverseStr(reversedURL.substring(0,reversedURL.indexOf('/')));
      reversedLink=reverseStr(linkURL);
      linkFile=reverseStr(reversedLink.substring(0,reversedLink.indexOf('/')));
      if(thisFile==linkFile) { return true; }
      else {return false;}
    }
  },
  isFolder:function(linkURL){
    thisPage=location.href;
    thisPage=thisPage.toLowerCase();
    if(thisPage.indexOf('http://')>=0 || thisPage.indexOf('https://')){
      var linkFolder=linkURL;
      hostIndex=thisPage.indexOf("/", 12);
      path=thisPage.substring(hostIndex, thisPage.length);
      if (thisPage.indexOf(".html") > -1){ thisPage=stripFilename(thisPage); }
      if (path.indexOf(".html") > -1){ path=stripFilename(path); }
      if (linkURL.indexOf(".html") >-1){ linkFolder=stripFilename(linkURL); }
      //alert(thisPage + '\n' + path + '\n' + linkURL + '\n' + linkFolder);
      if(linkFolder==thisPage || linkFolder==path) { return true; }
      else {return false;}
      return false;
    }else if(thisPage.indexOf('file://')>=0){
      return false;
    }
  }
}
pde.addEvent(window, 'load', pde.init, false);



function reverseStr(str) {
  var outp='';
  for (i = 0; i < str.length; i++) {
    outp = str.charAt(i) + outp;
  }
  return outp;
}

function stripFilename(str) {
  RStr = reverseStr(str);
  sfnB = RStr.indexOf("/");
  return reverseStr(RStr.substring(sfnB, RStr.length));
}

function addFilename(prefix, filename) {
  RStr = reverseStr(prefix);
  if (RStr.indexOf("/") == 0)
    return prefix + filename;
  else
    return prefix + "/" + filename;
}

function dumpObject (object) {
  var str = '';
  for (e in object) {
    str += e + '=' + object[e] + '\n';
  }
  return str;
}
