if(!ch){
	var ch = {};
}
if(!ch.exmachina){
	ch.exmachina = {};
}
if(!ch.exmachina.bravofly){
	ch.exmachina.bravofly = {};
}
ch.exmachina.bravofly.RichField = function(/* object */ctorArgs){
	var _timestamp = (new Date()).getTime(),
		autocompleteURL = ctorArgs.autocompleteURL,
		resultsLimit = ctorArgs.resultsLimit || 20,
		type = ctorArgs.type,
		self = this
	;
	this.domNode = document.createElement("div");
	this.pNode = $(ctorArgs.pNode);
	this.pGroup = $(ctorArgs.pGroup);
	this.searchLanguage = ctorArgs.searchLanguage || this.searchLanguage;
	
	this.domNode.innerHTML = this.template.replace(/{\$FieldId}/g, _timestamp).
		replace("{$Searching}", ctorArgs.searching || "Searching...")
	;
	// ricava fieldNode prima di togliere il commento da queste due righe
	this.pNode.appendChild(this.domNode);
	this.fieldNode = $("RichField" + _timestamp);
	this.listNode = $("ta_list" + _timestamp);
	this.indicatorNode = $("ta_indicator" + _timestamp);
	this.closerNode = $("Clean" + _timestamp);
	// removing node ids
	this.fieldNode.writeAttribute("id", null);	
	//this.listNode.writeAttribute("id", null);	
	this.indicatorNode.writeAttribute("id", null);	
	this.closerNode.writeAttribute("id", null);	
	
	
	this.dirty = ctorArgs.dirty || this.dirty;
	this.promptText = ctorArgs.defaultValue || ctorArgs.promptText || "";
	
	this.indicatorNode.style.position = "absolute";
	this.onComplete = ctorArgs.onComplete || this.onComplete;
	this.onClose = ctorArgs.onClose || this.onClose;
	this.onClean = ctorArgs.onClean || this.onClean;
	this.onFreeze = ctorArgs.onFreeze || this.onFreeze;
	this.onUnfreeze = ctorArgs.onUnfreeze || this.onUnfreeze;
    this.countryCode = ctorArgs.countryCode || "";
    this.continentCode = ctorArgs.continentCode || "";
	// RIMUOVERE GLI ID!


	/* Autocompleter */
    !Prototype.Browser.IE && this.bindEvents();
    this.autocompleter = new Ajax.Autocompleter(this.fieldNode, this.listNode, autocompleteURL, {
    	paramName: "key", 
    	frequency: .01, 
    	minChars:3, 
    	parameters: "type=dep&language=" + self.searchLanguage + "&limit=" + 
            resultsLimit + "&countryCode=" + self.countryCode + "&continentCode=" + self.continentCode,
    	updateElement: function(cItem){
    		if(!self.isFreezed()){
				self.storedId = cItem.id;
    			self.fieldNode.value = cItem.innerHTML.stripTags();
	    		self.freeze();
	    		self.onComplete();
                self.fieldNode.writeAttribute("data", cItem.id);
    		}
    	},
    	fields: ['routeFilter'], 
    	indicator: self.indicatorNode
    });
	if(!this.dirty){
		this.fieldNode.value = this.promptText;
	}
    Prototype.Browser.IE && this.bindEvents();
};

ch.exmachina.bravofly.RichField.prototype = {
	// ****************************************************************************************************
	// members
	template: '<input type="text" value="" class="ta_rich_field" id="RichField{$FieldId}" />' +
			'<a id="Clean{$FieldId}" ' +
			'class="ta_cleanAirport_hidden" href="#"><img src="/images/pricefinder/ta_x.gif" /></a>' +
			'<div class="aeroList" id="ta_list{$FieldId}"></div>' +
			'<div id="ta_indicator{$FieldId}" ' +
			'style="display:none" id="ta_indicator{$FieldId}" class="ta_indicator">{$Searching}</div>',
	searchLanguage: "EN",
	resultsLimit: 20,
	dirty: false,
	promptText: "",
	storedId: "",
    countryCode: "",
    continentCode: "",
	// domNodes
	domNode: null,
	fieldNode: null,
	indicatorNode: null,
	closerNode: null,
	// parent node
	pNode: null,
	// status
	disabled: false,
	// parent group ref
	pGroup: null,
    autocompleter: null,

	// ****************************************************************************************************
	// methods
	getValue: function(){
		return this.fieldNode.value;
	},
	setValue: function(/** String */ value){
		this.fieldNode.value = value;
	},
	getStoredId: function(){
		return this.storedId;
	},
	// hooks
	onClean: function(){
	},
	onComplete: function(){
	},
	onClose: function(){
	},
	onBlur: function(){
	},
	onFocus: function(){
	},
	onFreeze: function(){
	},
	onUnfreeze: function(){
	},
	// event handlers
	closeHandler: function(evt){
		this.onClose();
        Event.stop(evt);
	},
	focusHandler: function(){
		if(this.disabled){ return; }
		Element.clonePosition(this.indicatorNode, this.fieldNode,{
			setLeft: true,
			setTop: true,
			setHeight: false,
			setWidth: false,
			offsetTop: this.fieldNode.offsetHeight
		});
		if(!this.dirty){
			this.fieldNode.value = "";
		}
		this.onFocus();
	},
	blurHandler: function(){
		if(this.disabled){ return; }
		this.dirty = this.getValue() !== "";
//		!this.dirty && (this.fieldNode.value = this.promptText);
		if(!this.dirty){
            this.fieldNode.value = this.promptText;
        }else{
            this.autocompleter.active && this.autocompleter.selectEntry();
        }
		this.onBlur();
	},
	keyupHandler: function(){
		this.closerNode.className = this.fieldNode.value.length ? "ta_cleanAirport" : "ta_cleanAirport_hidden";
	},
	// methods
	bindEvents: function(){
		Event.observe(this.fieldNode, 'blur', this.blurHandler.bindAsEventListener(this), false);
		Event.observe(this.fieldNode, 'focus', this.focusHandler.bindAsEventListener(this));
		Event.observe(this.fieldNode, 'keyup', this.keyupHandler.bindAsEventListener(this), false);
		Event.observe(this.closerNode, 'click', this.closeHandler.bindAsEventListener(this), false);
	},
	unbindEvents: function(){
		// todo
	},
	freeze: function(){
		var fnode = this.fieldNode;
		fnode.addClassName("fixed");
		this.disable();
		this.closerNode.className = "ta_cleanAirport";
		this.onFreeze();
	},
	unfreeze: function(){
		var fnode = this.fieldNode;
		fnode.removeClassName("fixed");
		this.enable();
		this.onUnfreeze();
//		this.closerNode.className = "ta_cleanAirport_hidden";
	},
	isFreezed: function(){
		return this.disabled;
	},
	disable: function(){
		this.fieldNode.disabled = true;
		this.disabled = true;
	},
	enable: function(){
		this.fieldNode.disabled = false;
		this.disabled = false;
	},
	destroy: function(){
		this.unbindEvents();
		this.pNode.removeChild(this.domNode);
	},
	clean: function(){
		this.fieldNode.value = this.promptText;
		this.dirty = false;
		this.closerNode.className = "ta_cleanAirport_hidden";
		this.unfreeze();
		this.onClean();
	},
	toString: function(){
		return "[ch.exmachina.bravofly.RichField]";
	}
};
