/**
 * SelectAjaxControl (c)2007
 * A. Karasev 
 * 
 */

if(typeof window.SelectAjaxControl == 'undefined'){

	function _updateSelect(ajaxSelect, newValue, newOptions){
		var control = $(ajaxSelect.control);

		if(newOptions != null){		
			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				jslog.debug(_format("Set new optins: " + ajaxSelect.control));
			}
			
			var sandBox = control.up("div");
			Element.remove(control);
			sandBox.innerHTML = _cloneSelect(control, newValue, newOptions, ajaxSelect.options);			
			AjaxEventProxy.addAjaxEvent(ajaxSelect.control, "change");
			
			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				jslog.debug(_format("Set new optins. Done: " + ajaxSelect.control));
			}
		} else if(newValue != null && newValue != $F(control)){
			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				jslog.debug(_format("Set new value: " + ajaxSelect.control));
			}
			
			var option = $A(control.options).find(function(opt){
				return opt.value == newValue;
			});
			
			if(option != null){
				option.selected = true;
			}
		}
	}	
	
	var attributes = $A(["id", "name", "class", "size"/*, "disabled", "readonly"*/]);	
	
	function _cloneSelect(control, newValue, newOptions, ajaxOptions){
		var buff = [];
		
		buff.push("<select");
		
		attributes.each(function(attr){
			var val = Element.readAttribute(control, attr);
			if(val != null && val != ""){
				buff.push(" ", attr, "=\"", val, "\"");
			}
		});
	
		buff.push(">");
		
		if(ajaxOptions != null && ajaxOptions["emptyOption"] != null){
			_createOption(buff, ajaxOptions["emptyOption"].value, ajaxOptions["emptyOption"].text, ajaxOptions["emptyOption"].value==newValue);
		}
		
		for(i=0;i<newOptions.length;i++){
			var opt = newOptions[i];
			
			if(ajaxOptions != null && ajaxOptions["exclude"] != null && ajaxOptions["exclude"].detect(function(ex){return ex == opt.id;})){
				continue;
			}	
					
			_createOption(buff, opt.id, opt.name, opt.id==newValue);
		}
		
		if(ajaxOptions != null && ajaxOptions["otherOption"] != null){
			_createOption(buff, ajaxOptions["otherOption"].value, ajaxOptions["otherOption"].text, ajaxOptions["otherOption"].value==newValue);
		}
				
		buff.push("</select>");
		
		return buff.join('');		
	}

	function _createOption(buff, value, text, selected){
		buff.push("<option value=\"", value, "\" ", (selected?"selected>":">"), abbrv(text, 90), "</option>");
	}

	
	function abbrv(text, maxLength) {
		if (maxLength > 4 && text.length > maxLength) {
			text = text.substring(0, maxLength - 3) + "...";
		}
		return text;
	}

	SelectAjaxControl = Class.create();
	
	Object.extend(SelectAjaxControl.prototype, AjaxControl.prototype);
	Object.extend(SelectAjaxControl.prototype, {
		skipOnChange: false,
		
		initControl:function(){
			AjaxEventProxy.registerListener(this.control, "change", this, this.onSelect);
			AjaxEventProxy.addAjaxEvent(this.control, "change");
		},		
		
		getQueryString:function(){
			var queryString;
			if(this.options != null && this.options["initValue"] != null){
				queryString = this.control + "=" + this.options["initValue"];
				this.options["initValue"] = null;
			} else {
				queryString = this.control + "=" + $F(this.control);
			}
			
			if(this.options != null && this.options["requiredIds"] != null){
				queryString += "&" + this.control + ".requiredIds=" + this.options["requiredIds"];
			}
			
			return queryString;
		},
		
		onSelect:function(subj, msg){
			if(this.skipOnChange){
				return;
			}

			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				_setStartTime(new Date());
				jslog.debug(_format("SelectAjaxControl.onSelect: " + this.control));
			}
			
			if(this.onSelectTimeout != null){
				window.clearTimeout(this.onSelectTimeout);
				this.onSelectTimeout = null;	
			}	
					
			var tmp = this;
			this.onSelectTimeout = window.setTimeout(function(){
				tmp.onSelectTimeout = null;
				tmp.sendMessage();
			}, 150);
		},
		
		onAjaxResponse:function(subj, message, data){
			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				jslog.debug(_format("SelectAjaxControl.onAjaxResponse: " + this.control));
			}
			
			var oldValue = $F(this.control);
			var newValue = message.body[this.control];
			var newOptions = message.body[this.control + "_list"];

			if(newValue != null || newOptions != null){
				this.skipOnChange = true;
				
				try{
					_updateSelect(this, newValue!=null?newValue:oldValue, newOptions);
					Event.fire(this.control, "change");
					if(newOptions != null){
						var control = $(this.control);
						control.disabled = control.readonly = (newOptions.length == 0) && (this.options == null || this.options["otherOption"] == null);
					}								
				}catch(err){
					alert("Error on page!!");
				}
				
				this.skipOnChange = false;
			}
						
			if(typeof window.jslog != 'undefined' && jsLog_enabled){
				jslog.debug(_format("SelectAjaxControl.onAjaxResponse. Done: " + this.control));
			}
		}
	});
}