/**
*
* egPropertyList Class
*/

function egPropertyList(id, posX, posY, width, height, container) {
	 this.objName = 'propertyList_'+id;
	 this.id = id;
	 this.x = posX;
	 this.y = posY;
	 this.tabs = 0;

	 this.tabsContainers = new Array();
	 this.selectedItem = null;
	 this.selectedItemIndex = null;

	 this.width = width;
	 this.height = height;

	 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.selectListItem = egPropertyList_selectListItem;
	 this.setSelectedItem = egPropertyList_setSelectedItem;
	 this.setUnselectedItem = egPropertyList_setUnselectedItem;
	 this.addListItem = egPropertyList_addListItem;
	 this.removeListItem = egPropertyList_removeListItem;
	 this.getValue = egProperyList_getValue;
   this.unselectAll = egPropertyList_unselectAll;
   this.selectFirstItem = egPropertyList_selectFirstItem;
   this.selectLastItem = egPropertyList_selectLastItem;

   if (!container) {
      container = 'easy-GUI_mainLayer';
   }
	 this.container = container;

	 divRoot = document.getElementById(container);

	 divRoot.innerHTML += '<div id="'+this.id+'" class="egPropertyListFrame"></div>';

	 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 egPropertyList_addListItem(title, container) {
	var div = document.getElementById(this.id);

	if(this.selectedItem) {
    this.selectedItem.className = 'egPropertyListItem';
		if(this.tabsContainers[this.selectedItemIndex]) {
			document.getElementById(this.tabsContainers[this.selectedItemIndex]).style.display = 'none';
		}
	}

	div.innerHTML += '<div id="'+this.id+'_list_item_'+this.tabs+'" class="egPropertyListItem" onclick="javascript:' + this.objName + '.selectListItem(this, ' + this.tabs + ');">'+title+'</div>';

	this.tabsContainers[this.tabs] = container;

	this.tabs++;
}

function egPropertyList_removeListItem(index) {
	var childItem = document.getElementById(this.id+'_list_item_'+index);
	var div = document.getElementById(this.id);
	div.removeChild(childItem);

	var childContainer = document.getElementById(this.tabsContainers[index]);
	var container = document.getElementById(this.container);
	container.removeChild(childContainer);

  this.tabsContainers[index] = null;
  this.selectFirstItem();
}

function egProperyList_getValue() {
	return this.selectedItemIndex;
}

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

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

function egPropertyList_selectFirstItem() {
	for(i = 0; i < this.tabs; ++i) {
		if(this.tabsContainers[i]) {
			var item = document.getElementById(this.id+'_list_item_'+i);
			this.selectListItem(item, i);
			break;
		}
	}
}

function egPropertyList_selectLastItem() {
  var selection = 0;
	for(i = 0; i < this.tabs; ++i) {
		if(this.tabsContainers[i]) {
			selection = i;
		}
	}
	var item = document.getElementById(this.id+'_list_item_'+selection);
	this.selectListItem(item, selection);
}

function egPropertyList_unselectAll(){
	if(this.selectedItem) {
		this.setUnselectedItem(this.selectedItem);
		if(this.tabsContainers[this.selectedItemIndex]) {
			document.getElementById(this.tabsContainers[this.selectedItemIndex]).style.display = 'none';
		}
	}
	this.selectedItem = null;
	this.selectedItemIndex = 0;
}

function egPropertyList_selectListItem(item, index){
	if(this.selectedItem) {
		this.setUnselectedItem(this.selectedItem);
		if(this.tabsContainers[this.selectedItemIndex]) {
			document.getElementById(this.tabsContainers[this.selectedItemIndex]).style.display = 'none';
		}
	}
	if(this.tabsContainers[index]) {
		document.getElementById(this.tabsContainers[index]).style.display = 'block';
	}

	this.setSelectedItem(item);
	this.selectedItem = item;
	this.selectedItemIndex = index;
}