/**
*
* egTreeNode Class
*/
function egTreeNode(id, parentNodeId, treeListId, title, value, iconOff, iconOn, container, action) {
	 this.objName = 'treeList_' + treeListId;
	 this.id = id;
	 /*if(parentNodeId) {
	 		this.id = parentNodeId + '_' + id;
	 }*/
	 this.x = 0;
	 this.y = 0;
	 this.width = 0;
	 this.height = 0;

	 this.value = value;
	 
	 this.approxWidth = ((title.length)*8) + 50;

	 this.addNode = egTreeNode_addNode;
	 this.getHTML = egTreeNode_getHTML;

	 this.insideHTML = '';

	 this.headerHTML = '<div style="width: ' + this.approxWidth + 'px;">'
		+ '<div id="' + this.id + '" class="egTreeListItem" onmouseover="javascript:cancelBubble(arguments[0]);' + this.objName + '.activateItem(this);" onmouseout="javascript:cancelBubble(arguments[0]);' + this.objName + '.deactivateItem(this);" onclick="javascript:cancelBubble(arguments[0]);' + this.objName + '.selectItem(this, ' + this.value + ', \'' + container + '\');' + action + ';">'
		+ ' <div id="' + this.id + '_selector" class="egTreeListSelector"></div>'
		+ ' <div id="' + this.id + '_icon_off" class="egTreeListIcon" style="background: url(' + iconOff + ') no-repeat;"></div>'
		+ ' <div id="' + this.id + '_icon_on" class="egTreeListIcon" style="background: url(' + iconOn + ') no-repeat; display: none;"></div>'
		+ ' <div id="' + this.id + '_text" class="egTreeListText">' + title + '</div>'
		+ '</div>';
	 this.footerHTML = '</div>';
}

function egTreeNode_addNode(treeNode){
	 if(this.insideHTML == '') {
			this.insideHTML += '<div id="' + this.id + '_sub" class="egTreeListSubList">';
	 }
	 this.insideHTML += treeNode.getHTML();
}

function egTreeNode_getHTML() {
	 if(this.insideHTML != '') {
	 		this.insideHTML += '</div>';
	 }
	 return this.headerHTML + this.insideHTML + this.footerHTML;
}

/**
*
* egTreeList Class
*/
function egTreeList(id, posX, posY, width, height, container, name) {
	 this.objName = 'treeList_'+id;
	 this.id = id;
	 this.x = posX;
	 this.y = posY;
	 this.width = width;
	 this.height = height;
	 
	 this.selectedItem = null;
	 this.selectedRoot = null;
	 this.selectedItemContainer = null;
	 
	 this.getHeight = egWindow_getHeight;
	 this.getWidth  = egWindow_getWidth;

	 this.show = egWindow_show;
	 this.hide = egWindow_hide;
	 this.moveTo = egWindow_moveTo;
	 this.displayAt = egWindow_displayAt;

	 this.addRoot = egTreeList_addRoot;
	 this.setValue = egTreeList_setValue;
	 
	 this.setActive = egTreeList_setActive;
	 this.setInactive = egTreeList_setInactive;
	 this.setSelected = egTreeList_setSelected;
	 this.setUnselected = egTreeList_setUnselected;
	 this.showSubList = egTreeList_showSubList;
	 this.hideSubList = egTreeList_hideSubList;
	 this.activateItem = egTreeList_activateItem;
	 this.deactivateItem = egTreeList_deactivateItem;
	 this.deselectItem = egTreeList_deselectItem;
	 this.selectItem = egTreeList_selectItem;

	 this.rootNode = null;
	
	 this.value = null;

   if (!container) {
      container = 'easy-GUI_mainLayer';
   }
	 divRoot = document.getElementById(container);

	 divRoot.innerHTML += '<div id="' + this.id + '" class="egTreeListFrame"></div>';
	 divRoot.innerHTML += '<input id="' + this.id + '_value" name="' + name + '" type="hidden" value="0" />';
	 
	 div = document.getElementById(this.id);
	 div.style.width  = width+'px';
	 div.style.height  = height+'px';
	 div.style.left = posX+'px';
	 div.style.top  = posY+'px';
}

function egTreeList_setValue(value){
	this.value = value;
	document.getElementById(this.id+'_value').value = value;
}

function egTreeList_addRoot(treeNode){
	 if(treeNode) {
		 var div = document.getElementById(this.id);
		 div.innerHTML += treeNode.getHTML();
		 this.rootNode = treeNode;
	 }
}

function egTreeList_setActive(item){
	var pos = 0;
	if( (pos = item.className.indexOf('Hover')) < 0 ) {
		item.className = item.className + 'Hover';
	}
}

function egTreeList_setInactive(item){
	var pos = 0;
	if( (pos = item.className.indexOf('Hover')) > 0 ) {
		item.className = item.className.substring(0, pos);
	}
}

function egTreeList_setSelected(item){
  var pos = 0;
	if( (pos = item.className.indexOf('Selected')) < 0 ) {
		item.className = item.className + 'Selected';
	}
}

function egTreeList_setUnselected(item){
	var pos = 0;
	if( (pos = item.className.indexOf('Selected')) > 0 ) {
		item.className = item.className.substring(0, pos);
	}
}

function egTreeList_showSubList(item) {
	var selectorItem = document.getElementById(item.id+'_selector');
	var subListItem = document.getElementById(item.id+'_sub');

	if(selectorItem) {
		this.setSelected(selectorItem);
	}
	if(subListItem) {
	  subListItem.style.display = 'block';
	}
	this.selectedRoot = item;
}

function egTreeList_hideSubList(item) {
	var selectorItem = document.getElementById(item.id+'_selector');
	var subListItem = document.getElementById(item.id+'_sub');

	if(selectorItem) {
		this.setUnselected(selectorItem);
	}
	if(subListItem) {
		subListItem.style.display = 'none';
	}
}

function egTreeList_activateItem(item){
	var textItem = document.getElementById(item.id+'_text');

	if(textItem) {
 		this.setActive(textItem);
	}
}

function egTreeList_deactivateItem(item){
	var textItem = document.getElementById(item.id+'_text');

	if(textItem) {
 		this.setInactive(textItem);
	}
}

function egTreeList_deselectItem(item){
	var textItem = document.getElementById(item.id+'_text');
 	var iconOnItem = document.getElementById(item.id+'_icon_on');
 	var iconOffItem = document.getElementById(item.id+'_icon_off');

	if(textItem) {
 		this.setUnselected(textItem);
	}
	if(iconOnItem && iconOffItem) {
 		iconOnItem.style.display = 'none';
 		iconOffItem.style.display = 'block';
	}
}

function egTreeList_selectItem(item, value, container){
	var textItem = document.getElementById(item.id+'_text');
 	var iconOnItem = document.getElementById(item.id+'_icon_on');
 	var iconOffItem = document.getElementById(item.id+'_icon_off');
	var subList = document.getElementById(item.id+'_sub');


	if(this.selectedItemContainer) {
	  if(document.getElementById(this.selectedItemContainer)) {
	  	document.getElementById(this.selectedItemContainer).style.display = 'none';
	  }
	}

	if(container) {
	  if(document.getElementById(container)) {
	  	document.getElementById(container).style.display = 'block';
	  }
	}

	this.selectedItemContainer = container;

	this.setValue(value);

	if(this.selectedItem) {
		this.deactivateItem(this.selectedItem);
		this.deselectItem(this.selectedItem);
	}

	/* hide items sublists */
	if(this.selectedRoot && (item.id.indexOf(this.selectedRoot.id) < 0)) {
		var selectedRoot = this.selectedRoot;
		while(selectedRoot) {
		  var parentItem = selectedRoot.parentNode.parentNode;
			if(parentItem && (pos = parentItem.id.indexOf('_sub'))>=0) {
				parentItem = document.getElementById(parentItem.id.substring(0, pos));
			}
			if(parentItem && (item.id.indexOf(parentItem.id) >= 0)) {
			  parentItem = null;
			}
			this.hideSubList(selectedRoot);
			selectedRoot = parentItem;
		}
  }

	if(textItem) {
 		this.setInactive(textItem);
		this.setSelected(textItem);
	 	this.setActive(textItem);
		iconOnItem.style.display = 'block';
		iconOffItem.style.display = 'none';
	}
	if(subList) {
		this.showSubList(item);
	}
	else {
		var selectorItem = document.getElementById(item.id+'_selector');
		selectorItem.className = 'egTreeListSelectorNone';
	}
	this.selectedItem = item;
}
