if(!window.Richfaces) window.Richfaces = {};
Richfaces.InplaceSelect = Class.create(Richfaces.InplaceInput, {
	initialize : function($super, listObj, clientId, temValueKeepId, valueKeepId, tabberId, attributes, events, classes, barParams, buttonId) {
		this.button = $(buttonId);
		this.comboList = listObj;
		$super(clientId, temValueKeepId, valueKeepId, tabberId, attributes, events, classes, barParams);
		this.clickOnBar = false;
		this.inplaceSelect = $(clientId);		
		this.inplaceSelect.component = this;
				
		this.attrCloseOnSelect = true; //will be corrected in a future version (http://jira.jboss.com/jira/browse/RF-2814)
		
		this.currentItemValue = this.value;
		this.button.style.top = Richfaces.getBorderWidth(this.tempValueKeeper, "tb") + "px";
		this["rich:destructor"] = "destroy";
	},
	
	destroy: function() {
		this.inplaceSelect.component = null;
	},
	
	
	initHandlers : function($super) {
		$super();
		this.tempValueKeeper.observe("click", function(e){this.tempKeeperClickHandler(e);}.bindAsEventListener(this));
		
		this.button.observe("mousedown", function(e){this.buttonClickHandler(e);}.bindAsEventListener(this));
		
		this.comboList.listParent.observe("mousedown", function(e){this.listMousedownHandler(e);}.bindAsEventListener(this));
		this.comboList.listParent.observe("mousemove", function(e){this.listMouseMoveHandler(e)}.bindAsEventListener(this));
		this.comboList.listParent.observe("click", function(e){this.listClickHandler(e);}.bindAsEventListener(this));
	},
	
	setInputWidth : function($super, textSize) {
		$super(textSize);
		this.button.show();
		var width = parseInt(this.tempValueKeeper.style.width);
		this.button.style.left = ( width - this.button.offsetWidth) + "px";
		return width;
	},
	
	switchingStatesHandler : function($super, e) {
		var el = (e.srcElement) ? e.srcElement : e.target;
		if ((el.id == this.inplaceInput.id) || (e.type == "focus")) {
			$super(e);
		} else if(this.tempValueKeeper.style.display != "none") {
			this.tempValueKeeper.focus();
		}
	},
	
	startEditableState : function($super, textSize) {
		this.inplaceInput.style.position = "relative";
		$super(textSize);
		this.button.show();
		if (this.attributes.openOnEdit) {
			this.comboList.showWithDelay();
		}
	},
	
	endEditableState : function($super) {
		this.inplaceInput.style.position = "";
		$super();
		this.button.hide();
	},
	
	tempKeeperClickHandler : function() {
		this.comboList.isList = false;
		this.comboList.showWithDelay();
	},
	
	buttonClickHandler : function(e) {
		this.tempKeeperClickHandler();
		this.tempValueKeeper.focus();
		this.button.isClicked = true;
		Event.stop(e);
	},
	
	tmpValueBlurHandler : function($super, event) {
		
		if (this.button.isClicked) {
			// click on arrow under IE 
			this.button.isClicked = false;
			return;
		}				
		
		if (this.clickOnScroll) {
			//click on scroll under IE 
			this.clickOnScroll = false;
			this.tempValueKeeper.focus();
			return;		
		}
		$super(event);
		if (!this.byTab) {
			if (!this.comboList.isList || this.clickOnBar) {
				if (this.comboList.activeItem) {
					this.comboList.doNormalItem(this.comboList.activeItem);
				}
				this.comboList.hideWithDelay();
			}
		
			if (!this.attributes.showControls) {
				this.save();
			} else {
				this.applyTmpValue();
			}
			this.comboList.isList = false;
		} else if(event.target != this.tabber) { 
			this.comboList.hideWithDelay();
		} else {
			this.byTab = false;
		}
	},
	
	listClickHandler : function(event) {
		this.tempValueKeeper.focus();
		this.comboList.hideWithDelay();
	},
	
	listMouseMoveHandler : function(event) {
		//changes item's decoration
		var item = this.comboList.getEventItem(event);
		if (item) {
			this.comboList.doActiveItem(this.comboList.getEventItem(event));			
		}
	},
	
	listMousedownHandler : function(e) {
		if (Prototype.Browser.IE) {
			if (!this.comboList.getEventItem(e)) {
				this.clickOnScroll = true;
			}
			this.comboList.isList = true;
		} else if (Prototype.Browser.Gecko) {
			if (this.comboList.getEventItem(e)) {
				this.comboList.isList = true;			
			}
		} else {
			this.comboList.isList = true;
		}
		
		if (this.attrCloseOnSelect) {
			//bug : http://jira.jboss.com/jira/browse/RF-2810, 
			//will be corrected in a future version (http://jira.jboss.com/jira/browse/RF-2814)
			if (!this.attributes.showControls) {
				this.comboList.isList = false;
			}
		}
	},
	
	tmpValueKeyDownHandler : function(event) {
		switch (event.keyCode) {
			case Event.KEY_RETURN :
				this.comboList.isList = false; 
				this.save();
				this.comboList.hideWithDelay();
				Event.stop(event);
				break;
			case Event.KEY_DOWN : 
				this.comboList.moveActiveItem(event);
				Event.stop(event);
				break;
			case Event.KEY_UP :
				this.comboList.moveActiveItem(event);
				Event.stop(event);
				break; 
			case Event.KEY_ESC : 
				this.comboList.resetSelection();
				this.comboList.hideWithDelay();
				this.cancel(event);
				break;
			case Event.KEY_TAB :
				this.save();
				this.byTab = true;
				break;
		}
	},
	
	save : function($super) {
		this.applyTmpValue();
		if (!this.comboList.isList || this.clickOnBar) {
			var unescapeText = this.tempValueKeeper.value;
			this.saveValue(this.currentItemValue, unescapeText);
		}
		
		if (this.attributes.showControls) {
			this.comboList.hide();
		}	
	},
	
	applyTmpValue : function() {
		if (this.comboList.activeItem) {
			var userLabel = this.comboList.activeItem.innerHTML.unescapeHTML();
			this.currentItemValue = this.comboList.activeItem.value;
			this.tempValueKeeper.value = userLabel;
			this.comboList.selectedItem = this.comboList.activeItem;
		}
	},
	
	deleteViewArtifacts : function () {
		var text = this.inplaceInput.childNodes[6];
		if (text) {
			this.inplaceInput.removeChild(text);
		}
	},
	
	getCurrentText : function() {
		return this.inplaceInput.childNodes[6];
	},
	
	getLabelItem : function(value) {
		var length = this.comboList.getItems().length;
		for (var i = 0; i < length; i++) {
			var item = this.comboList.getItems()[i];
			if (item.value == value) {
				return item;
			}
		}
	},
	
	cancel : function($super, e) {
		var item = this.getLabelItem(this.valueKeeper.value);
		this.comboList.resetSelection();
		
		if (item) {
			this.comboList.doSelectItem(item);
			$super(e, item.innerHTML.unescapeHTML());
		} else {
			$super(e, "");
		}
			
		this.comboList.hide();
	}
});
	

