if(typeof(Goweb) == "undefined") {
	var Goweb = Class.create();
}

Goweb.ComboBox = Class.create({
	initialize: function(name, options) {
		this.options = {
                width: '200px', // largura do elemento
                height: '200px', // altura do elemento
                scrollValue: 0.1, // fazer scroll em 10% do total, em cada click ou scroll da roda do rato
				delay: 0.2 // delay do elemento
		};
		Object.extend(this.options, options || {});
		this.name = $(name);
		this.comboBoxElements = $$('#' + name + ' .goweb_combobox_elements')[0];
		this.comboBoxSelectedElement = $$('#' + name + ' .goweb_combobox_selected')[0];
		this.comboBoxElements.setStyle({'display': 'none'});
		this.comboBoxButton = $$('#' + name + ' .goweb_combobox_button')[0];
		this.comboBoxElements.select('li').invoke("observe", 'click', this.selectElement.bindAsEventListener(this));
		if (!this.comboBoxElements){
			throw "Goweb.ComboBox requires '.goweb_combobox_elements' to be defined";
		}
		if (!this.comboBoxSelectedElement){
			throw "Goweb.ComboBox requires '.goweb_combobox_selected' to be defined";
		}
		if (!this.comboBoxButton){
			throw "Goweb.ComboBox requires '.goweb_combobox_button' to be defined";
		}

		this.comboBoxScroller = $$('#' + name + ' .goweb_combobox_scroll')[0];
		this.comboBoxTrackContainer = $$('#' + name + ' .track')[0];
		this.comboBoxHandleContainer = $$('#' + name + ' .handle')[0];

		this.comboBoxButtonLess = $$('#' + name + ' .button_less')[0];
		this.comboBoxButtonMore = $$('#' + name + ' .button_more')[0];
		if (this.comboBoxButtonLess){
			this.comboBoxButtonLess.observe('click', this.scrollLess.bind(this));
		}
		if (this.comboBoxButtonMore){
			this.comboBoxButtonMore.observe('click', this.scrollMore.bind(this));
		}
		if (this.comboBoxScroller) {
	    	if (this.comboBoxTrackContainer.getStyle('width')){
	    		this.comboBoxTrackContainer.style.width = this.comboBoxTrackContainer.getStyle('width');
	    	}
	    	if (this.comboBoxHandleContainer.getStyle('width')){
	    		this.comboBoxHandleContainer.style.width = this.comboBoxHandleContainer.getStyle('width');
	    	}
	    	if (this.comboBoxTrackContainer.getStyle('height')){
	    		this.comboBoxTrackContainer.style.height = this.comboBoxTrackContainer.getStyle('height');
	    	}
	    	if (this.comboBoxHandleContainer.getStyle('height')){
	    		this.comboBoxHandleContainer.style.height = this.comboBoxHandleContainer.getStyle('height');
	    	}

	        this.slider = new Control.Slider(this.comboBoxHandleContainer,this.comboBoxTrackContainer, {
	            axis: 'vertical',
	            onSlide: this.onChange.bind(this),
	            onChange: this.onChange.bind(this)
	        });
	
	    	if (this.comboBoxScroller.getStyle('overflow') != "hidden"){
	    		this.comboBoxScroller.style.overflow = "hidden";
	    		if (Prototype.Browser.IE) {
	    			this.comboBoxScroller.style.position = "relative";
	    		}
	    	}

    		this.comboBoxScroller.style.width = this.comboBoxScroller.getStyle('width') ? this.comboBoxScroller.getStyle('width') : this.options.width;
	
    		this.comboBoxScroller.style.height = this.comboBoxScroller.getStyle('height') ? this.comboBoxScroller.getStyle('height') : this.options.height;
    		this.comboBoxElements.setStyle({'display': 'block'});
    		var elementsScrollHeight = this.comboBoxScroller.scrollHeight;
    		var elementsOffsetHeight = this.comboBoxScroller.offsetHeight;
    		var newHeight = this.comboBoxElements.select('ul')[0].offsetHeight;
    		this.comboBoxElements.setStyle({'display': 'none'});
	    	if (!Prototype.Browser.IE && elementsScrollHeight <= elementsOffsetHeight) {
	    		this.comboBoxElements.select('li').invoke('setStyle', 'width: ' + this.comboBoxScroller.getStyle('width'));
	    		this.comboBoxScroller.style.width = (parseInt(this.comboBoxTrackContainer.getStyle('width').split("px", 1)) + parseInt(this.comboBoxScroller.getStyle('width').split("px", 1))) + 'px';
	    		this.comboBoxElements.style.height = newHeight + 'px';
	    		this.slider.setDisabled();
	    		this.comboBoxHandleContainer.hide();
	    		this.comboBoxHandleContainer.setStyle('visibility: hidden; width: 0; height: 0; background: none;');
	    		this.comboBoxButtonLess.hide();
	    		this.comboBoxButtonMore.hide();
	    		this.comboBoxTrackContainer.hide();
	    	}
		}
		Event.observe(this.comboBoxButton, 'click', this.click.bindAsEventListener(this));
		Event.observe(this.comboBoxSelectedElement, 'click', this.click.bindAsEventListener(this));
	},

	click: function() {
		if (this.comboBoxElements.style.display == 'none'){
			$$('.goweb_combobox_elements').invoke('setStyle', 'display: none'); 
		}
		Effect.toggle(this.comboBoxElements, 'slide', { delay: this.options.delay });
	},

	selectElement: function(e) {
		var el = e.element();
		this.comboBoxElements.select('li').invoke('removeClassName', 'selected'); 
		this.comboBoxSelectedElement.update(el.innerHTML);
		$(el).addClassName('selected');
		this.click();
	},

	scrollMore: function(){
		this.slider.setValue(this.slider.value+this.options.scrollValue);
	},

	scrollLess: function(){
		this.slider.setValue(this.slider.value-this.options.scrollValue);
	},

    onChange: function(value){
		this.comboBoxScroller.scrollTop = Math.round(value/this.slider.maximum*(this.comboBoxScroller.scrollHeight-this.comboBoxScroller.offsetHeight));
    }

});