if(!window.Richfaces) window.Richfaces = {};
Richfaces.InplaceSelectList = Class.create(Richfaces.ComboBoxList, {
	initialize : function($super, listId, parentListId, selectFirstOnUpdate, classes, width, height, itemsText, onlistcall, fieldId, shadowId, showDelay, hideDelay, value) {
		$super(listId, parentListId, selectFirstOnUpdate, classes, width, height, itemsText, onlistcall, fieldId, shadowId, showDelay, hideDelay);
		this.wrappingItems(value);
	},
	
	setPosition : function($super, fieldTop, fieldLeft, fieldHeight) {
		var field = this.fieldElem;
		
		field.show();
		var height = field.offsetHeight;				
		var top = height;
		var docHeight = Richfaces.getDocumentHeight();
		var comBottom = fieldTop + height;
		var listHeight = parseInt(this.list.style.height) + Richfaces.getBorderWidth(this.list.parentNode, "tb");
		
		if (parseInt(listHeight) > (docHeight - comBottom)) {
			if (fieldTop > (docHeight - comBottom)) {
				//opens up
				top = 0 - parseInt(listHeight);
			}
		}
		
		this.listParent.style.top = top + "px";
		this.listParent.style.left = 0 + "px";
		
		if (this.iframe) {
			this.iframe.style.top = top + "px";
			this.iframe.style.left = 0 + "px";
		}
	},
	
	show : function($super) {
		$super();
	},
	
	resetState : function() {
		this.activeItem = null;
	},
	
	getEventItem : function(event) {
		var item = Event.findElement(event, "span");
		if (item) {
			if (this.listParent.parentNode.id == item.id) {
				return;
			}
		}	
		return item;
	},
	
	setWidth : function(width) {
		var positionElem = this.listParent.childNodes[1];
		var correction = parseInt(width) - Richfaces.getBorderWidth(positionElem.firstChild, "lr") - Richfaces.getPaddingWidth(positionElem.firstChild, "lr") + "px";
		this.list.style.width = correction;
		if (this.iframe) {
			this.iframe.style.width = correction;			
		}
	},
	
	hide : function() {
		this.resetState();
		if (this.iframe) {
			this.iframe.hide();			
		}
		var component = this.listParent.parentNode;
		this.listParent.hide();
	},
	
	wrappingItems : function(value) {
		var len = this.getItems().length;
		for (var i = 0; i < len; i++) {
			var it = this.getItems()[i];
			it.value = this.itemsText[i][1];
			if (it.value == value) {
				this.doSelectItem(it);
			}
		}
	},
	
	resetSelection: function() {
		if (this.activeItem) {
			this.doNormalItem(this.activeItem);
		}
		if (this.selectedItem) {
			this.doNormalItem(this.selectedItem);
		}
	}
});
