/**-------------------- the following may need editing per template-------------------------*/

var height_of_menu = 20;
var width_of_drop_down_menu = 150;
var delay_time_in = 200;
var delay_time_out = 300;

/**-------------------- the previous may need editing per template -------------------------*/

var current_menu_drop_downs_open = new Array();
var mouse = new Mouse_Tracker();

/** ----------------------------- get rid of this stuff ---------------------------------- */
//function show_mouse_data(){
//   document.getElementById('mouse_on').innerHTML = ' ['+mouse+']';
//   setTimeout("show_mouse_data()",1000);   
//}
//show_mouse_data();
//document.write('<div id="other" style="color: #ffffff;">start</div>');
/** ----------------------------- get rid of this stuff ---------------------------------- */

function update_visible(){
   /** make array of should be visibles */
   var on_array = mouse.get_the_on();
   var will_be_visible = new Array();
   for(var i = 0; i<on_array.length;i++){
      id = on_array[i];
      if(id.substr(0,4) == 'sub_') id = id.substr(4);
      should_be_visible = menu_root.find_leaf(id).get_lineage();
      will_be_visible = will_be_visible.concat(should_be_visible);
   }
   will_be_visible = unique_ma(will_be_visible);
   //document.getElementById('visible').innerHTML = ' ['+will_be_visible+']';
   
   //hide
   //current_menu_drop_downs_open = unique_ma(current_menu_drop_downs_open);
   //min_length = current_menu_drop_downs_open.length;
   //if(min_length > will_be_visible.length) min_length = will_be_visible.length;
   //var start_at = 0;
   //if(will_be_visible+'' != ''+current_menu_drop_downs_open) document.getElementById('other').innerHTML = will_be_visible +' != '+ current_menu_drop_downs_open;
   //for(var i=0; i<min_length; i++){
   //   if(will_be_visible[i] != current_menu_drop_downs_open[i]){
   //      start_at = i;
   //      i = min_length;
   //   }
      //else document.getElementById('other').innerHTML = ' ['+will_be_visible[i] +' != '+ current_menu_drop_downs_open[i]+']';
   //}
   for(var i=0;i<current_menu_drop_downs_open.length;i++){
      hide = true;
      id = current_menu_drop_downs_open[i];
      for(var j=0;j<will_be_visible.length;j++){
         if(will_be_visible[j] == id) hide = false;
      }
      if(hide) hide_sub_menu(id);
   }
   // and make visible acoordingly
   for(var i=0;i<will_be_visible.length;i++){
      show = true;
      id = will_be_visible[i];
      for(var j=0;j<current_menu_drop_downs_open.length;j++){
         if(current_menu_drop_downs_open[j] == id) hide = false;
      }
      if(show)show_sub_menu(id);
   }
   current_menu_drop_downs_open = will_be_visible;
}







function menu_mouseover(obj, id){
   setTimeout("main_menu_in('"+ id +"')", delay_time_in);
}
function menu_mouseout(obj, id){
   setTimeout("main_menu_out('"+ id +"')", delay_time_out);
}
function main_menu_in(id){
   mouse.push(id);
   update_visible();
}
function main_menu_out(id){
   mouse.pop_spec(id);
   update_visible();
}
function show_sub_menu(id){
   pos_obj = document.getElementById(id);
   obj = document.getElementById('sub_'+id);
   if(obj){
      var left = get_left_value(pos_obj);
      var top = get_top_value(pos_obj); 
   
      //current_menu_pos = id;
      //current_menu_drop_downs_open[current_num_of_drop_downs_open] = id;
      //current_num_of_drop_downs_open++;
      
      depth = menu_root.find_leaf(id).get_depth();
      if(depth > 1){
         top -= height_of_menu/2;
         left += width_of_drop_down_menu - height_of_menu/2;
      }
      obj.style.left = left;
      obj.style.top = top + height_of_menu;
      obj.style.visibility = 'visible';
   }
   else{
   }
}
function hide_sub_menu(id){
   obj = document.getElementById('sub_'+id);
   if(obj){
      obj.style.visibility = 'hidden';
   }
}














function get_left_value(obj){
	var left = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			left += obj.offsetLeft
			obj = obj.offsetParent;
		}
	}
	else if (obj.x)
		left += obj.x;
	return left;
}
function get_top_value(obj){
	var top = 0;
	if (obj.offsetParent){
		while (obj.offsetParent){
			top += obj.offsetTop
			obj = obj.offsetParent;
		}
	}
	else if (obj.y)
		top += obj.y;
	return top;
}

/**------------------------------- tree functions -----------------------------------*/
function Leaf(id, title, parent, depth){
   this.id = id;
   this.title = title;
   this.num_children = 0;
   this.children = new Array();
   this.parent = parent;
   this.depth = depth;
   
   // initialize class functions
   this.get_id = get_id;
   this.get_title = get_title;
   this.add_child = add_child;
   this.get_children = get_children;
   this.toString = toString;
   this.menu_string = menu_string;
   this.menu_drop_down_string = menu_drop_down_string;
   this.find_leaf = find_leaf;
   this.get_depth = get_depth;
   this.get_parent_id = get_parent_id;
   this.in_lineage = in_lineage;
   this.get_lineage = get_lineage;
}
function get_id(){
   return this.id;
}
function get_title(){
   return this.title;
}

function get_children(){
   return this.children;
}
function get_parent_id(){
   return this.parent.get_id();
}
function get_depth(){
   return this.depth;
}
function add_child(child){
   this.children[this.num_children] = child;
   this.num_children++;
}
function toString(){
   var string = this.get_id();
   if(this.num_children != 0) string += '(';
   for(var i=0;i<this.num_children;i++){
      string += this.children[i].toString();
      if(this.num_children != i+1) string += ', ';
   }
   if(this.num_children != 0) string += ')';
   return string;
}
function menu_string(){
   var string = '<div id="menu"><ul>';
   for(var i=0; i<this.num_children; i++){
      if(i==0) first_q = 'class="first"';
      else first_q = '';
      if(this.children[i].num_children > 0) arrow_string = ' &#187;';
      else arrow_string = '';
      string += '<li id="'+this.children[i].get_id()+'" ' + first_q + ' onMouseOver="menu_mouseover(this, \'' + this.children[i].get_id() + '\')" onMouseOut="menu_mouseout(this,\'' + this.children[i].get_id() + '\');"><a href="index.php?id='+minus+this.children[i].get_id()+'&goog='+(this.children[i].get_title()).replace(' ', '+')+'">' + this.children[i].get_title() + arrow_string + '</a></li>';
   }
   string += '</ul></div>';
   for(var i=0; i<this.num_children; i++){
      string += this.children[i].menu_drop_down_string();
   }
   //myAlert(string);
   return string; 
}
function menu_drop_down_string(){
   if(this.num_children>0){
      var string = '<div id="sub_' + this.get_id() + '" class="sub_menus" onMouseOver="menu_mouseover(this, \'sub_' + this.get_id() + '\')" onMouseOut="menu_mouseout(this,\'sub_' + this.get_id() + '\');"><ul>';
      for(var i=0; i<this.num_children; i++){
         if(i==0) first_q = 'class="first"';
         else first_q = '';
         if(this.children[i].num_children > 0) arrow_string = ' &#187;';
         else arrow_string = '';
         link_text = this.children[i].get_title();
         if(link_text.length > 20) link_text = link_text.substr(0,18)+'...';
         string += '<li id="'+this.children[i].get_id()+'" ' + first_q + ' onMouseOver="menu_mouseover(this, \'' + this.children[i].get_id() + '\')" onMouseOut="menu_mouseout(this,\'' + this.children[i].get_id() + '\');"><a href="index.php?id='+minus+this.children[i].get_id()+'&goog='+(this.children[i].get_title()).replace(' ', '+')+'">' + link_text + arrow_string + '</a></li>';
      }
      string += '</ul></div>';
      for(var i=0; i<this.num_children; i++){
         string += this.children[i].menu_drop_down_string();
      }
   }
   else string = '';
   return string;
}

function find_leaf(leaf_id){
   if(leaf_id == this.get_id())
      return this;
   for(var i=0; i<this.num_children; i++){
      if(this.children[i].find_leaf(leaf_id)) return this.children[i].find_leaf(leaf_id);
   }
   return null;
}
function in_lineage(req){
   if(this.get_id() == 'root') return false;
   if(req == this.get_id()) return true;
   return parent.in_lineage(req);
}
function get_lineage(){
   var lineage_array = new Array();
   leaf = this;
   while(leaf.get_id() != 'root'){
      lineage_array[lineage_array.length] = leaf.get_id();
      leaf = leaf.parent;
   }
   return lineage_array;
}

/**----------------------------- mouse_tracker class ---------------------------------*/
function Mouse_Tracker(){
   this.on_array = new Array();
   this.on_array_length = 0;
   
   // initialize class functions
   this.push = push;
   this.pop_spec = pop_spec;
   this.toString = toString_mt;
   this.contains = contains;
   this.get_the_on = get_the_on;
}
function push(id){
   this.on_array[this.on_array_length] = id;
   this.on_array_length++;
   return true;
}
function contains(id){
   var found = false;
   for(var i=0; i<this.on_array_length; i++){
      if(this.on_array[i] == id){
         found = true;
      }
   }
   return found;
}
function pop_spec(id){
   if(this.contains(id)){
      var found = false;
      for(var i=0; i<this.on_array_length-1; i++){
         if(this.on_array[i] == id) found = true;
         if(found){
            this.on_array[i] = this.on_array[i+1];
         }
      }
      this.on_array_length--;
      this.on_array.length--;
   }
}
function toString_mt(){
   var string = '';
   for(var i=0; i<this.on_array_length; i++){
      if(i != 0){
         string += ', ';
      }
      string += this.on_array[i];
   }
   return string;
}
function get_the_on(){
   return this.on_array;
}
/** ------------------------- My Array ----------------------------- */

function unique_ma(array){
   var unique_array = new Array();
   var not_unique_array = array.sort();
   for(var i=0;i<array.length;i++){
      if(unique_array.length<1 || unique_array[unique_array.length-1] != not_unique_array[i]){
         unique_array.push(not_unique_array[i]);
      }
   }
   return unique_array;
}
/** ------------------------- My Alert ----------------------------- */
function myAlert(string){
   a_window=window.open('','name','height=300,width=400');
	var temp = a_window.document;
	temp.write(string);
   temp.close();
}


