dom=document.getElementById?1:0;
ns4=(document.layers&&!document.getElementById)?1:0;
window.Tree=[];

function loadImage(arg){
  var im=new Image();
  im.src=arg;
}

function undefined(value) {
 return typeof(value) == 'undefined';
}

function MakoTree(name, itemsFromConfig){
	this.name=name;
  window.Tree[this.name]=this;
	this.properties=itemsFromConfig[0];
  this.left=this.properties["location"][0];
  this.top=this.properties["location"][1];
  if (this.properties["plus_minus_images_visible"]) {
    loadImage(this.properties["plus_minus_images"]["minus"]);
    loadImage(this.properties["plus_minus_images"]["plus"]);
    loadImage(this.properties["plus_minus_images"]["spacer"]);
  }
	if (this.properties["folder_images_visible"]) {
    loadImage(this.properties["folder_images"]["close"]);
    loadImage(this.properties["folder_images"]["open"]);
    loadImage(this.properties["folder_images"]["doc"]);
  }

  this.Items=[];
	this.root=new TreeItem(null, null, {},  0);
	this.root.tree=this;
  this.maxWidth=0;
  this.maxHeight=0;


	this.itemStyle=function(level) {
	  if (undefined(this.properties["level_styles"]) || undefined(this.properties["level_styles"][level]))
      return this.properties["default_style"]
    else
      return this.properties["level_styles"][level];
  }

	this.getItemByID=function(id) {
    for (i=0;i<this.Items.length;i++)
    {
      //alert (this.Items[i].itemID)
      if (this.Items[i].itemID==id)
        return i;
    }
    return null;
  }

	this.addItem=function(item){
		var parentItem=item.parentItem;
		this.Items[this.Items.length]=item;
		item.number=this.Items.length-1;
		if (parentItem == null) this.root.children[this.root.children.length]=item;
		else {
      parentItem.children[parentItem.children.length]=item;
    }
		return item;
	}


	this.redraw=function(){
		this.drawTop=this.properties["location"][1];
		this.maxHeight=0;
    this.maxWidth=0;
		for (var i=0;i < this.root.children.length;i++)
      this.root.children[i].redraw();
	}

	this.updateImages=function(item, ff){
	 if (item.tree.properties["plus_minus_images_visible"]) {
		 var plusminus_image;
     if (item.expanded) plusminus_image=this.properties["plus_minus_images"]["minus"];
     else plusminus_image=this.properties["plus_minus_images"]["plus"];

     var img_plusminus;
		 if (ns4) img_plusminus=item.itemDiv.document.images[item.id()+"img_plusminus"];
		 else if (dom) img_plusminus=document.getElementById(item.id()+"img_plusminus");
		 else img_plusminus=document.all[item.id()+"img_plusminus"];
		 if (!undefined(img_plusminus) && img_plusminus && img_plusminus.src!=plusminus_image) img_plusminus.src=plusminus_image;
   }
	 if (item.tree.properties["folder_images_visible"]) {
		 var folder_image;
     if (item.hasChildren()) {
       if (item.expanded) folder_image=this.properties["folder_images"]["open"];
       else folder_image=this.properties["folder_images"]["close"];
     } else {
         folder_image=this.properties["folder_images"]["doc"];
     }
     var img_folder;
		 if (ns4) img_folder=item.itemDiv.document.images[item.id()+"img_folder"];
		 else if (dom) img_folder=document.getElementById(item.id()+"img_folder");
		 else img_folder=document.all[item.id()+"img_folder"];
		 if (!undefined(img_folder) && img_folder && img_folder.src!=folder_image){
       img_folder.src=folder_image;
     }
   }
	}


	this.expandItem=function(index){
		var item=this.Items[index];
		var pItem=item.parentItem ? item.parentItem : null;
		if (!undefined(item) && item.hasChildren()){
			item.expanded=!item.expanded;
			this.updateImages(item);
			if (!item.expanded)
				item.hideChildren();
			else {
				if (this.properties["single_branch_open"])
					for (var i=0;i < this.Items.length;i++){
						this.Items[i].show(false);
						if(this.Items[i] != item && this.Items[i].parentItem == pItem) {
							this.Items[i].expanded=false;
							this.updateImages(this.Items[i]);
						}
					}
			}
      this.redraw();
		}
	}

	this.rollOver=function(index, over){
		var item=this.Items[index];
		if (!undefined(item)){
      if (ns4) img=item.itemDiv.document.images[item.id()+"_img"];
		  else if (dom) img=document.getElementById(item.id()+"_img");
		  else img=document.all[item.id()+"_img"];
		  if (over=='over') img.src=item.img_over;
		  else img.src=item.img;
		}
	}
  
  this.selectItem=function(index){
		var item=this.Items[index];
		var img;
		
		if(!undefined(item)) {
      if (ns4) img=item.itemDiv.document.images[item.id()+"_img"];
		  else if (dom) img=document.getElementById(item.id()+"_img");
		  else img=document.all[item.id()+"_img"];
      item.img=item.img_sel?item.img_sel:item.img_over;
      img.src=item.img
    }
	}
	this.readItems=function(itemsFromConfig, level){
    //var parent=null;
    var level;
		function readSingleItem(item_from_config , tree, level, parent){
			if (undefined(item_from_config)) return;
			var treeitem=new TreeItem(tree, parent, item_from_config, level);
			var item=tree.addItem(treeitem);
			if (item_from_config.id) item.itemID=item_from_config.id;
			if (!undefined(item_from_config.subitems)) {
  			for (var i=0;i<item_from_config.subitems.length;i++)
  			  readSingleItem(item_from_config.subitems[i], tree, level+1, item);
			}
		}
		for (var i=1;i<itemsFromConfig.length;i++){
      readSingleItem(itemsFromConfig[i], this, 0, null);  //level 0
		}
	}

	this.collapseAll=function(){
		for (var i=0;i < this.Items.length;i++){
			if (this.Items[i].parentItem != this.root) this.Items[i].show(false);
			this.Items[i].expanded=false;
			this.updateImages(this.Items[i]);
		}
		this.redraw();
	}

	this.expandAll=function(){
		for (var i=0;i < this.Items.length;i++){
			this.Items[i].expanded=true;
			this.updateImages(this.Items[i]);
		}
		this.redraw();
	}

  //build and redraw whole tree
	this.readItems(itemsFromConfig);
	//alert (this.Items[8].text + ' : ' + this.Items[8].children.length)
	for (var i=0;i < this.Items.length;i++) {
    this.Items[i].makeDiv();
		if (ns4) this.Items[i].itemDiv=document.layers[this.Items[i].id()+"div"];
		else if (dom)    this.Items[i].itemDiv=document.getElementById(this.Items[i].id()+"div");
		else this.Items[i].itemDiv=document.all[this.Items[i].id()+"div"];
	}
	this.redraw();
}




function TreeItem(tree, parentItem , item, level){
	this.number=-1;
  this.tree=tree;
  this.parentItem=parentItem;
  this.expanded=false;
	this.level=level;
	this.text=undefined(item.contenttxt)?null:item.contenttxt;
	this.img=undefined(item.contentimg)?null:item.contentimg;
	if (tree && tree.properties["size"]) this.size=tree.properties["size"];
	if (!undefined(item.size)) this.size=item.size;
  this.url=undefined(item.url)?'':item.url;
  this.target=undefined(item.target)?'':item.target;
  if (undefined(item.contentimg_over)) this.img_over=null;
  else {
    this.img_over=item.contentimg_over;
    loadImage(item.contentimg_over);
  }
  if (undefined(item.contentimg_sel)) this.img_sel=null;
  else {
    this.img_sel=item.contentimg_sel;
    loadImage(item.contentimg_sel);
  }
	this.children=[];
	
	this.hasChildren=function(){
    return this.children.length > 0;
  }

	this.makeDiv=function(){
	 if (ns4) {
     document.write ('<layer id="'+this.id()+'div" z-index="'+(this.number+50)+'" visibility="hidden">'+this.getHTMLContent()+'</layer>');
   } else {
     document.write ('<div id="'+this.id()+'div" style="width:1px; height:1px;position:absolute;visibility:hidden;'+';z-index:'+(this.number+50)+';">'+this.getHTMLContent()+'</div>');
   }
	}
	this.getHeight=function() {
    return this.size[1];
    /*if (ns4) return this.itemDiv.clip.height
    else return this.itemDiv.offsetHeight;*/
  }
  


	this.getWidth=function() {
    return this.size[0];
    //if (ns4) return this.itemDiv.clip.width
    //else return this.itemDiv.offsetWidth;
  }


	this.id=function() {
    return 'id_'+this.tree.name+this.number;
  }

	this.getHTMLContent=function(){
		function folderCell(item){
		  var imageFile;
		  if (!item.hasChildren()) imageFile="doc";
		  else if (item.expanded) imageFile="open";
		  else imageFile="close";

      var img=item.tree.properties["folder_images"][imageFile]; 
		  var width=item.tree.properties["folder_images_size"][0];
      var height=item.tree.properties["folder_images_size"][1];

			
			var img_tag = '<img id="'+item.id()+'img_folder" name="'+item.id()+'img_folder" src="' + img + '" width='+width+' height='+height+' border=0>';

			if (item.hasChildren())
  			img_tag = '<a href="javascript:ExpandItem(\''+item.tree.name+'\','+item.number+')">'+img_tag+'</a>';
			return '<td width="'+width+'">'+img_tag+"</td>\n";
		}

		function plusminusCell(item){
		  if (item.expanded) var img=item.tree.properties["plus_minus_images"]["expanded"];
		  else var img=item.tree.properties["plus_minus_images"]["plus"];
			var width=item.tree.properties["plus_minus_images_size"][0];
      var height=item.tree.properties["plus_minus_images_size"][1];
      
			return '<td width="'+width+'"><a href="javascript:ExpandItem(\''+item.tree.name+'\','+item.number+')"><img name=\''+item.id()+'img_plusminus\' id=\''+item.id()+'img_plusminus\' src="' + img + '" width="'+width+'" height="'+height+'" border=0></a></td>\n';
		}

		function SpacerCell(item, width){
			var image_file=item.tree.properties["plus_minus_images"]["spacer"];
			return "<td width=\""+width+"\"><img src=\"" + image_file + "\" width="+width+" height=1 border=0></td>\n";
		}


		var html_code='';
		html_code += '<table cellpadding='+this.tree.properties["cellpadding"]+' cellspacing='+this.tree.properties["cellspacing"]+' border=0><tbody><tr>';

		var indent=this.tree.properties["indents"][this.level];
		if (indent > 0) html_code += SpacerCell(this, indent);
		
    if(this.tree.properties["plus_minus_images_visible"]) {
      if (this.hasChildren()) html_code += plusminusCell(this);
      else html_code += SpacerCell(this, this.tree.properties["plus_minus_images_size"][2]);
    }
		if(this.tree.properties["folder_images_visible"])
      html_code += folderCell(this);

    var style=this.tree.itemStyle(this.level);
	  html_code += '<td nowrap=1><a onfocus="blur();" class="'+style+'" href="';
		if(this.url == ""){
      if (this.hasChildren()) html_code += 'javascript:ExpandItem(\''+this.tree.name+'\','+this.number+')';
			else html_code += 'javascript:void(0)';
		}
		else 
			html_code += this.url+'" target="'+this.target+'" onclick="ExpandItem(\''+this.tree.name+'\','+this.number+')';


    var img_size='';
    if (this.size) img_size=' width=' + this.size[0] + ' height=' + this.size[1];

    var htmlcontent='';
    if (this.img)
      htmlcontent='<img src="'+this.img + '"' + img_size + ' border=0 id="' + this.id()+'_img" name="' + this.id()+'_img" onmouseover="RollOver(\''+this.tree.name+'\','+this.number+',\'over\')" onmouseout="RollOver(\''+this.tree.name+'\','+this.number+',\'out\')">';
    else htmlcontent=this.text;
    
	  html_code += '">'+htmlcontent+'</a></td></tr></tbody></table>';

    //alert (html_code)
    return html_code;
	}



	this.moveTo=function(x, y) {
    if (ns4)
      this.itemDiv.moveTo(x,y);
    else {
      this.itemDiv.style.left=x+'px';
      this.itemDiv.style.top=y+'px';
    }
  }
	this.show=function(arg) {
    if (ns4) this.itemDiv.visibility=arg ? 'show': 'hide';
    else this.itemDiv.style.visibility=arg ? 'visible': 'hidden';
    this.visible=arg;
  }

	this.hideChildren=function() {
    this.show(false);
    for (var i=0;i < this.children.length;i++) {
      //alert (this.children[i].level)
      this.children[i].hideChildren();
    }
  }
	this.redraw=function() {
    this.moveTo(this.tree.left, this.tree.drawTop);
    this.show(true);
    if (this.tree.left+this.getWidth()> this.tree.maxWidth) this.tree.maxWidth=this.tree.left+this.getWidth();
    this.tree.drawTop += this.getHeight();
    if (this.tree.drawTop > this.tree.maxHeight)
      this.tree.maxHeight=this.tree.drawTop;
    if (this.expanded && this.hasChildren())
      for (var i=0;i < this.children.length;i++)
        this.children[i].redraw();
  }
}

function OnLoad(){
	if (ns4){
		window.initialWidth=window.innerWidth;
		window.initialHeight=window.innerHeight;
		window.onresize=resizeHandler;
	}
}
window.onload=new OnLoad();
function resizeHandler() {
	if (window.reloading) return;
	window.initialWidth=window.innerWidth;
  window.initialHeight=window.innerHeight;
	if (window.innerWidth != window.initialWidth || window.innerHeight != window.initialHeight) {
    window.reloading=1;
    document.location.reload();
  }
}

function ExpandItem(name, index) {
  window.Tree[name].expandItem(index)
}

function RollOver(name, index, over) {
  window.Tree[name].rollOver(index, over)
}

