var cWindow_Select = {
 	init: function(top, left, input, default_key, callback)
 	{
 		if (this.inited) return false;
 		this.inited = true;

 		var _this = this;

 		this.callback = callback;
 		this.dom_dropdown.css({'top': top + 'px', 'left': left + 'px'});
 		this.dom_input = input
 			.val(input.attr('title'))
 			.attr('autocomplete','off')
 			.focus(function() {
 				var input = $(this);
//                if (input.val() == input.attr('title')) {
                    input.val('');
                //}
                _this.open(true);
            })
            .blur(function () {
            	var input = $(this);
                if (input.val().length == 0) {
                    input.val(input.attr('title'));
                }
            })
            .keypress(function(e) {
                if ($.browser.opera && e.keyCode == 40) {
              		_this.hightlightCursor(-1);
                }
            })
            .keyup(function(e) {
                if (e.keyCode == 13) {
                	if (_this.current_option != undefined) {
						var option = $(_this.dom_options.get(_this.current_option));
						_this.choice(option.attr('rel'), option.html());
                	}
                    _this.close();
                    return false;
                } else if (e.keyCode == 38) { //cursor up
                	_this.hightlightCursor(1);
                } else if (e.keyCode == 40 && !$.browser.opera) { //cursor down
                	_this.hightlightCursor(-1);
                } else if (this.value.length != 0) {
                    _this.shapeDropdown(this.value, e.keyCode);
                } else if (this.value.length == 0) {
                    _this.open(true);
                }
            });

 		if (typeof(this.data_options[default_key]) != 'undefined') {
 			this.dom_input.val(this.data_options[default_key]);
 		}

        this.dom_options = this.dom_dropdown.find('a.select-box-dropdown-option').each(function(index, option) {
            $(option)
                .click(function() {
                        _this.choice($(this).attr('rel'), $(this).html())
                        _this.close();
                    })
                .mouseover(function(index, option) {
                    return function() {
                    	_this.dom_options.removeClass('select-box-dropdown-option-current')
                    	_this.current_option = index;
                    	$(option).addClass('select-box-dropdown-option-current')
                        //_this.current_hightlight_position = num + 1;
                        //_this.hightlightCursor(false);
                    }
                }(index, option));
        });

 		return this.inited;
 	},

 	open: function(show_all)
 	{
 		var show_all = show_all || false;
 		if (show_all) {
 			this.dom_options.show();
 			//this.dom_input.val('');
 		}
 		this.dom_dropdown.show();
 		this.dom_dropdown_additional.show();
 		this.dom_dropdown_notfound.hide();
 		documentEventDispatcher.addListener(this, ['click', 'keypress']);
 	},

 	choice: function(id, val) {
 		this.callback(id, val);
 	},

    close: function()
    {
    	this.dom_dropdown.hide();
    	this.dom_input.css('z-index', 'auto');
        this.current_option = undefined;
        documentEventDispatcher.removeListener(this, ['click', 'keypress']);

        if (this.dom_input.val() == '' || this.dom_input.val() == this.dom_input.attr('title')) {
        	this.choice('', this.dom_input.attr('title'));
        }
    },

    cursorLocation: function(x, y)
    {
        var offset = this.dom_dropdown.offset();
        var width = this.dom_dropdown.get(0).offsetWidth; // suggest width
        var height = this.dom_dropdown.get(0).offsetHeight; // suggest height

        if (x < offset.left || x > offset.left + width || y < offset.top - 20 || y > offset.top + height) {
            return false;
        } else {
            return true;
        }
    },

    listenerEvent: function(e)
    {
        e = jEvent.fixEvent(e);
        if (e.type == 'click') {
           var res = this.cursorLocation(e.pageX, e.pageY);
           if (!res) {
               this.close();
           }
        } else if (e.type == 'keypress' && e.keyCode == 13) {
        	e.stopPropagation();
        	e.preventDefault();
        	if (this.current_option != undefined) {
				var option = $(this.dom_options.get(this.current_option));
				this.choice(option.attr('rel'), option.html());
        	}
            this.close();
        }
    },

    shapeDropdown: function(input_value, keyCode)
    {
    	if (input_value.length == 0) {
    		return;
    	}

        var needle = input_value.toLowerCase();
		var result_count = 0;
        this.dom_options.each(function(index, option) {
        	var option = $(option);
        	if (option.text().toLowerCase().indexOf(needle) == 0) {
        		result_count++;
        		option.show();
        	} else {
        		option.hide();
        	}
        });

        if (result_count == 0) {
            this.showNotFound();
        	//this.close();
        } else {
        	this.open();
        }
    },

    showNotFound: function()
    {
        this.dom_dropdown_notfound.show();
        this.dom_dropdown_additional.hide();
    },

	hightlightCursor: function(whereto)
	{
		var before = -1;
		var _this = this;
		//alert(Dump(_this.dom_options.get(0)));
		//return;
		this.dom_options.each(function(index, option) {

			var option = $(option)
			if (option.css('display') == 'block') {
				if (_this.current_option == undefined) {
					//alert(index)
			//		alert(22);
					option.addClass('select-box-dropdown-option-current');
					_this.current_option = index;
					return true;
				}

				if (whereto > 0 && index == _this.current_option && before != -1) { //cursor up
					$(_this.dom_options.get(before)).addClass('select-box-dropdown-option-current');
					option.removeClass('select-box-dropdown-option-current');
					_this.current_option = before;
					return true;
				} else if (whereto < 0 && before == _this.current_option) { //cursor down
					//alert([before, index]);
					$(_this.dom_options.get(before)).removeClass('select-box-dropdown-option-current');
					option.addClass('select-box-dropdown-option-current');
					_this.current_option = index;
					return true;

				}

				before = index;
			}
		});
		//alert(1);
		return false;
	}
}

function Window_Select(data)
{
    this.pc = Window;
    this.pc(data);

    for (var f in cWindow_Select) {
        this[f] = cWindow_Select[f];
    }

    this.data_options = data.options;

    this.inited = false;
    this.dom_dropdown   = $(_(this.name + '_dropdown')).hide();
    this.dom_dropdown_notfound   = $(_(this.name + '_notfound')).hide();
    this.dom_dropdown_additional = $(_(this.name + '_additional')).hide();
    this.dom_input = undefined;
    this.current_option = undefined;

}
