var agt = navigator.userAgent.toLowerCase();
var is_ie = ((agt.indexOf('msie') != -1) && (agt.indexOf('opera') == -1));

function clearNode(node){
	while (node.firstChild){
		node.removeChild(node.firstChild);
	}
	return node;
}
function setClass(element, className){
	if (element && element.className.indexOf(className) == -1){
		element.className += ' ' + className;
	}
}
function removeClass(element, className){
	if (element){
		element.className = element.className.replace(className, '');
	}
}

function AutoComplete(data, input, select){
	this.data = data;
	this.input = input;
	this.select = select;
	this._init();
}
AutoComplete.prototype = {
	_init: function(){
        var _this = this;
		this.input.onkeypress = function(e){return _this.inputOnKey(e)};
		this.input.onkeydown = function(e){return _this.inputOnKey(e)};
		this.input.onkeyup = function(e){return _this.inputOnKey(e)};
        EventFactory.create('keyup', function(e){_this.onKeyUp(e); _this.onaction(e);}, this.input, true);
		EventFactory.create('blur', function(e){_this.onBlur(e)}, this.input, true);
		EventFactory.create('keyup', function(e){_this.documentKeyUp(e)}, document, true);
		this.select.onchange = function(e){
			_this.onselect();
			_this.doSelect(e);
		};
	},
	onaction: function(e){},
    onselect: function(){
        this.input.value = this.select.getSelectedText() || this.input.value;
    },
	documentKeyUp: function(e){
		if (e.keyCode == 27){
			this.hide();
		}
	},
	inputOnKey: function(e){
		var evt = e || window.event;
		if (evt.keyCode == 13 || evt.keyCode == 40 || evt.keyCode == 38 || evt.keyCode == 27){
			return false;
		}
		return true;
	},
	onKeyUp: function(e){
		var value = this.trim(this.input.value);
		if (!value){
            this.hide();
            return;
		}
		switch (e.keyCode){
			case 27:
				return;
			case 38:
				this.moveUp();
				return;
			case 40:
				this.moveDown();
				return;
			case 13:
				this.onselect(e);
				this.doSelect(e);
				return;
		}
        var html = this.data.match(new RegExp('<o [^>]*>' + value + '[^<]*</o>', 'ig'));
        if (!html){
            this.hide();
            return;
        }
		this._fillSelect(html);
		if (html.length == 1 && html[0].match(new RegExp('<o [^>]*>' + value + '</o>', 'ig'))){
			this.onselect();
			this.doSelect(e);
		}
	},
	moveUp: function(){
		this.select.moveUp();
	},
	moveDown: function(){
		this.select.moveDown();
	},
	onBlur: function(e){
		var _this = this;
		window.setTimeout(function(){_this.hide()}, 100);
	},
	hide: function(){
		this.select.display(false);
	},
	show: function(){
		this.select.display(true);
	},
	doSelect: function(){
		this.hide();
	},
	_fillSelect: function(html){
		this.select.fill(html.join(''));
		this.show();
	},
	trim: function (str){
		return str.replace(/^[\s\xA0]+/, '').replace(/[\s\xA0]+$/, '');
	}
}

if (typeof(Function.prototype.apply) == 'undefined')
{
	Function.prototype.apply = function(context)
	{
		var name = '__apply' + new Date().getTime() + Math.floor(Math.random() * 10000000) + '__';

		context = context || window;
		context[name] = this;

		var args = [];

		if (arguments[1]) {
			var len = arguments[1].length;
			args = new Array(len);
			for (var i = 0; i < len; i++) {
				args[i] = 'arguments[1][' + i + ']';
			}
		}

		var ret = eval('context[name](' + args.join(',') + ')');
		delete context[name];

		return ret;
	}
}
