/*********************************************************************************************************************/
/**************************************************************************************************** Hierarchy tree */
// =========================================================
// listItem object represents every element in Hierarchy tree
//
// fields:
// name - Name of element that is displayed in list
// value - value of element
// inheritors - array of elemnt's inheritors if any


// =========================================================
// listHierarchy object represents levels of Hierarchy tree
// it stores every level list in array levelLists

// first argument must be an Array of list items for initialization
// all rest is names of levelLists


function listItem(name, value, selected, inheritors) {
  this.name = name;
  this.value = value;
  this.selected = selected;
  this.inheritors = inheritors;
}

function listHierarchy() {
  var args=listHierarchy.arguments;

  this.allLevels 			= new Array();
  this.maxLevel 			= 1;

  this.init                 = listHierarchy_init;
  this.listLoad             = listHierarchy_listLoad;
  this.listSelectionChanged = listHierarchy_listSelectionChanged;
  this.initArray			= listHierarchy_initArray;
  this.AddInheritors		= listHierarchy_AddInheritors;
  this.ProcLevel			= listHierarchy_ProcLevel;

  this.elements = args[0];
  if (this.elements.length) {
  	if (!this.elements[0].name)
  		this.initArray(this.elements);
	}
  this.levelLists = Array(args.length-1);
  for (i=1; i< args.length; i++) {
    this.levelLists[i-1] = MM_findObj(args[i]);
  }
  this.init();
}

function listHierarchy_init(){
	listHierarchy_initialized = false;
	this.listLoad(0, this.elements);
	listHierarchy_initialized = true;
}

function listHierarchy_listLoad(level, elementsData) { // dataObject must be an Array of listItems
	var i=0, list;
	if (list=this.levelLists[level]) {

		if (list.type == 'select-one') {
			for (i = list.length; i > 0; i-- )
				list.options[i]=null;
			
		    if (elementsData && elementsData.length) {
				for (i=0; i<elementsData.length; i++ ) {
					list.options[i] = new Option(elementsData[i].name, elementsData[i].value, 0, elementsData[i].selected);
					elementsData[i].selected = false;
			    }
			}
		} else {
			list.selectedIndex = 0;
			for (i=0; i<elementsData.length; i++ ) {
			    if (elementsData[i].selected) {
					elementsData[i].selected = false;
					list.selectedIndex = i;
				}
		    }
		}
		
		if (listHierarchy_initialized && list.onchange) list.onchange();
		else this.listSelectionChanged(list);
	}
}

function listHierarchy_listSelectionChanged(list) {
	var i, level, elementsData;
	if (list) {
		elementsData = this.elements;

  		for (level=0; (level < this.levelLists.length) && (this.levelLists[level] != list);level++){
			i = this.levelLists[level].selectedIndex;
  			if (elementsData.length) 
 				elementsData = elementsData[i].inheritors;
  		}

  		if (level < this.levelLists.length) {
  			i = list.selectedIndex;
  			if (elementsData && elementsData.length) {
		    	this.listLoad(level+1,elementsData[i].inheritors);
		    } else 	this.listLoad(level+1, new Array());
		}
	}
}

function listHierarchy_AddInheritors(source, destination, start) {
	var k, j;
	if (!destination) {
		destination = new Array();
		destination[0] = new listItem('                        ','', 0);
	}
	for (k=start; k<source.length; k++) {
		flag = false;
		for (j=0; j<destination.length; j++)
			if (source[k].value == destination[j].value) {
				flag = true;
				break;
			}
		if (!flag)
			destination.push(source[k]);
	}
	return destination;
}

function listHierarchy_ProcLevel(level, element) {
	var i;
	var children = element.inheritors;
	for (i=1; i<children.length; i++) {
		this.allLevels[level].push(children[i]);
		if (children[i].inheritors) {
			if (this.maxLevel<(level+1)) {
				this.allLevels[level+1] = new Array();
				this.maxLevel++;
			}
			this.ProcLevel(level+1, children[i]);
			children[0].inheritors = this.AddInheritors(children[i].inheritors, children[0].inheritors, 1);
		}
	}
}


function listHierarchy_initArray(data) {
	var i, j, s;
	this.allLevels[1] = new Array();
	for (i=1; i<data.length; i++) {
		this.ProcLevel(1, data[i]);
	}

	common = data[0];
	for (j=1; j<= this.maxLevel; j++) {
		common.inheritors = this.AddInheritors(this.allLevels[j], common.inheritors, 0);
		common = common.inheritors[0];
	}
}

/*********************************************************************************************************************/
/**************************************************************************************************** Hierarchy tree */

function nav_toggle(id, prefix){
	var obj;
	var img = MM_findObj('img_'+id);
	if (obj = MM_findObj('nav_'+id)) {
		if (obj.style) {
			if (obj.style.visibility == 'visible') {
				obj.style.visibility = 'hidden';
				obj.style.display = 'none';
				if (img) img.src = prefix+'images/arrow-right.gif';
			} else {
				obj.style.visibility = 'visible';
				obj.style.display = 'block';
				if (img) img.src = prefix+'images/arrow-down.gif';
			}
		}
	}
}