/**
*
* egDropDownListBox Class
*/
function egDropDownListBox(id, posX, posY, width, height, container, name, value, selectionType) {
	 this.objName = 'dropDownListBox_'+id;
	 this.id = id;
	 if(name.length > 0) {
	   this.name = name;
	 }
	 else {
	   this.name = null;
	 }
	 this.selectedItem = 0;
	 this.preSelectedItem = value;
	 this.selectionType = selectionType; /* 'VALUE', 'INDEX', 'LABEL'  */
	 this.x = posX;
	 this.y = posY;
	 this.width = 0;
	 this.height = 0;

	 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.oldBoxName = null;
	 this.itemsList = new Array();

	 this.addListItem = egDropDownListBox_addListItem;
	 this.removeListItem = egDropDownListBox_removeListItem;	 
	 this.showList = egDropDownListBox_showList;
	 this.hideList = egDropDownListBox_hideList;
	 this.setPressed = egDropDownListBox_setPressed;
	 this.setUnPressed = egDropDownListBox_setUnPressed;
	 this.setActive = egDropDownListBox_setActive;
	 this.setInactive = egDropDownListBox_setInactive;
	 this.selectListItem = egDropDownListBox_selectListItem;

	 this.getValue = egDropDownListBox_getValue;
	 this.setValue = egDropDownListBox_setValue;

	 this.onChange = egDropDownListBox_onChangeHandler;

   this.maxHeight = 200;

   if(height > 0){
	   this.maxHeight = height;
   }

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

	 divRoot = document.getElementById(container);

	 divRoot.innerHTML += '<div id="'+this.id+'" class="egDropDownListBoxFrame" style="z-index: 200;">'
		+'	<div class="egDropDownBoxFrame">'
		+'		<div id="'+this.id+'_box" class="egDropDownTextBox" onmouseover="javascript:' + this.objName + '.setActive(document.getElementById(\''+this.id+'_button\'));" onmouseout="javascript:' + this.objName + '.setInactive(document.getElementById(\''+this.id+'_button\'));" onmousedown="javascript:' + this.objName + '.setPressed(document.getElementById(\''+this.id+'_button\'));" onclick="javascript:cancelBubble(arguments[0]);" onmouseup="javascript:' + this.objName + '.setUnPressed(document.getElementById(\''+this.id+'_button\'));">'
		+'				<div style="position: absolute;"></div>'
		+'		</div>'
		+'		<div id="'+this.id+'_button" class="egDropDownButton" onmouseover="javascript:' + this.objName + '.setActive(this);" onmouseout="javascript:' + this.objName + '.setInactive(this);" onmousedown="javascript:' + this.objName + '.setPressed(this);" onclick="javascript:cancelBubble(arguments[0]);" onmouseup="javascript:' + this.objName + '.setUnPressed(this);"></div>'
		+'	</div>'
		+'</div>';

	 if(this.name) {
	   divRoot.innerHTML += '<input id="'+this.id+'_value" name="'+this.name+'" type="hidden" value="" />';
	 }

	 divTop = document.getElementById('easy-GUI_topLayer');
	 divTop.innerHTML += '<div id="'+this.id+'_list" class="egDropDownList" style="display: none; overflow: auto; max-height: ' + this.maxHeight + 'px;"></div>';

	 div = document.getElementById(this.id);
	 div.style.width = width+'px';
	 div.style.left = posX+'px';
	 div.style.top  = posY+'px';
	 div = document.getElementById(this.id+'_box');
	 div.style.width = (width-17)+'px';
	 div = document.getElementById(this.id+'_button');
	 div.style.right = '1px';
	 div = document.getElementById(this.id+'_list');
	 div.style.width = width+'px';
}

function egDropDownListBox_getValue() {
	if(this.selectedItem != null) {
		if(this.selectedItem < this.itemsList.length) {
		  return this.itemsList[this.selectedItem];
		}
	}
	return -1;
}

function egDropDownListBox_setValue(value){
	for(i = 0; i < this.itemsList.length; ++i) {
		if(this.itemsList[i] == value) {
			this.selectedItem = i;
			break;
		}
	}

	boxName = this.id+'_list_item_'+this.selectedItem+'_box';
	if(this.oldBoxName) {
		document.getElementById(this.oldBoxName).style.display = 'none';
	}
	document.getElementById(boxName).style.display = 'block';
	this.oldBoxName = boxName;
}

function egDropDownListBox_addListItem(title, value, action) {
 		var itemIndex = this.itemsList.length;
 		this.itemsList[itemIndex] = value;
		divBox = document.getElementById(this.id+'_box');
		divList = document.getElementById(this.id+'_list');
 	  display = 'none';

		if(this.selectionType == 'INDEX' && parseInt(this.preSelectedItem) === itemIndex) {
			this.selectedItem = itemIndex;
		  display = 'block';
		  this.oldBoxName = this.id+'_list_item_'+itemIndex+'_box';
      if(this.name) {
			 document.getElementById(this.id+'_value').value = value;
      }
		}

		if(this.selectionType == 'VALUE' && this.preSelectedItem == value) {
			this.selectedItem = itemIndex;
		  display = 'block';
		  this.oldBoxName = this.id+'_list_item_'+itemIndex+'_box';
      if(this.name) {
			 document.getElementById(this.id+'_value').value = value;
      }
		}

		if(this.selectionType == 'LABEL' && this.preSelectedItem == title) {
			this.selectedItem = itemIndex;
		  display = 'block';
		  this.oldBoxName = this.id+'_list_item_'+itemIndex+'_box';
      if(this.name) {
			 document.getElementById(this.id+'_value').value = value;
      }
		}

		divBox.innerHTML += '<div id="'+this.id+'_list_item_'+itemIndex+'_box" style="display: '+ display +'; position: absolute;">'+title+'&nbsp;</div>';
		divList.innerHTML += '<div id="'+this.id+'_list_item_'+itemIndex+'" class="egDropDownListItem" onmouseover="javascript:' + this.objName + '.setActive(this);" onmouseout="javascript:' + this.objName + '.setInactive(this);" onmouseup="javascript:' + this.objName + '.selectListItem(this);' + action + ';">'+title+'&nbsp;</div>';
}

function egDropDownListBox_removeListItem(value) {
	for(i = 0; i < this.itemsList.length; ++i) {
		if(this.itemsList[i] == value) {
			itemIndex = i;
			document.getElementById(this.id+'_list_item_'+itemIndex+'_box').style.display = 'none';
			document.getElementById(this.id+'_list_item_'+itemIndex).style.display = 'none';
			break;
		}
	}
}


function egDropDownListBox_showList(){
	egHideDropDownListBox();
	egDropDownListBoxObject = this;
	var parentDiv = document.getElementById(this.id);
	var div = document.getElementById(this.id+'_list');
	leftValue = egGetAbsoluteLeft(this.id);
	topValue = egGetAbsoluteTop(this.id);
  div.style.left = (leftValue+1)+'px';
  div.style.top = (topValue+parentDiv.offsetHeight+1)+'px';
	div.style.display = 'block';

	if(div.offsetHeight > this.maxHeight){
		div.style.height = this.maxHeight+'px';
	}
}

function egDropDownListBox_hideList(){
	document.getElementById(this.id+'_list').style.display = 'none';
}

function egDropDownListBox_setPressed(item){
  var pos = 0;

	if( (pos = item.className.indexOf('Pressed')) < 0 ) {
		item.className = item.className + 'Pressed';
	}
	this.showList();
}

function egDropDownListBox_setUnPressed(item){
	var pos = 0;

	if( (pos = item.className.indexOf('Pressed')) > 0 ) {
		item.className = item.className.substring(0, pos);
	}
}

function egDropDownListBox_setActive(item){
	var pos = 0;

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

function egDropDownListBox_setInactive(item){
	var pos = 0;

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

function egDropDownListBox_selectListItem(item){
	boxName = item.id + '_box';
	if(this.oldBoxName) {
		document.getElementById(this.oldBoxName).style.display = 'none';
	}
	document.getElementById(boxName).style.display = 'block';
	this.oldBoxName = boxName;

	var itemMark = this.id+'_list_item_';

	if( (pos = item.id.indexOf(itemMark)) >= 0 ) {
		this.selectedItem = item.id.substring(itemMark.length);
	}
  if(this.name) {
  	document.getElementById(this.id+'_value').value = this.getValue();
  }
	this.hideList();
	egDropDownListBox_onChange(this.id+'_value');
}

function egDropDownListBox_onChangeHandler() {
}

function egDropDownListBox_onChange(id) {
	var append = '_value';
	egObjectsArray['dropDownListBox_'+id.substr(0, id.length-(append.length))].onChange();
}