
var jsSelect = {
	create_js_drop : function(id){
		var el;
		if (el = document.getElementById(id)){
			el.selectedOption = null;
			el.selectedOptionId = -1;
			el.firstShow = true;

			var divs = el.getElementsByTagName("div");
			el.dropInput = el.getElementsByTagName("input")[0];
			el.dropLink = divs[1];
			el.dropOptions = divs[2];

			el.dropOptions.optionlinks = el.dropOptions.getElementsByTagName("a");

                        el.dropInput.parentSelect = el;
			el.dropLink.parentSelect = el;
			el.dropOptions.parentSelect = el;

			el.dropInput.noBlurCheck = false;

                        whi.events.addEvent(el,'click',jsSelect.clickInnerDrop);

                        whi.events.addEvent(el.dropInput,'focus',jsSelect.onInputFocus);
                        whi.events.addEvent(el.dropInput,'blur',jsSelect.onInputBlur);

                        whi.events.addEvent(el.dropInput,'keydown',jsSelect.onInputKeypress);

			whi.events.addEvent(el.dropLink,'mousedown',jsSelect.clickDroplink);

			whi.events.addEvent(el.dropOptions,'mousedown',jsSelect.onOptionsClicked);

			if (el.dropOptions.optionlinks.length){
				for (var i=0; i<el.dropOptions.optionlinks.length; i++){
					el.dropOptions.optionlinks[i].parentSelect = el;
					el.dropOptions.optionlinks[i].optId = i;

					whi.events.addEvent(el.dropOptions.optionlinks[i],'mouseover',jsSelect.hoverOption);
					whi.events.addEvent(el.dropOptions.optionlinks[i],'focus',function(){return false;});
				}
			}
		}
	},
	clickDroplink : function(e){
		if (this.parentSelect.dropOptions.style.display != 'block'){
			jsSelect.showDropDown.apply(this);
		}else{
			jsSelect.hideDropDown.apply(this);
			whi.events.stopEvent(e);
		}
		if (this.parentSelect.dropInput.isFocused){
			this.parentSelect.dropInput.noBlurCheck = true;
		}
	},
	showDropDown : function(){
		if (!this.parentSelect.isShowen){
			this.parentSelect.dropOptions.style.display = 'block';
			whi.add_class(this.parentSelect.dropLink,'select_drop_link');
			this.parentSelect.isShowen = true;

                        var wid = whi.getElementWidth(this.parentSelect.dropOptions);
                        var sel_wid = whi.getElementWidth(this.parentSelect.dropLink);
                        if (sel_wid > wid) wid = sel_wid - 17;
                        
			if (this.parentSelect.firstShow && this.parentSelect.dropOptions.optionlinks.length){
				for (var i=0; i<this.parentSelect.dropOptions.optionlinks.length; i++){
					var opt_wid = whi.getElementWidth(this.parentSelect.dropOptions.optionlinks[i]);
					if (whi.browser.IE){
						if (opt_wid < wid){
							if (whi.browser.IE7){
								this.parentSelect.dropOptions.optionlinks[i].style.width = (wid-5)+"px";
							}
						}else{
							this.parentSelect.dropOptions.optionlinks[i].style.width = (wid-(whi.browser.IE6 ? 20 : 0))+"px";
						}
					}else if (opt_wid < wid){
						this.parentSelect.dropOptions.optionlinks[i].style.width = (wid-5)+"px";
					}
				}
				this.parentSelect.firstShow = false;
			}
		}
	},
	hideDropDown : function(){
		if (this.parentSelect.isShowen){
			this.parentSelect.dropOptions.style.display = 'none';
			whi.remove_class(this.parentSelect.dropLink,'select_drop_link');
			this.parentSelect.isShowen = false;
		}
	},
	onOptionsClicked : function(e){
		this.parentSelect.dropInput.noBlurCheck = true;
		whi.events.stopEvent(e);
		return false;
	},
	onInputKeypress : function(e){
		var code;
		if (!e) var e = window.event;
		if (e.keyCode) code = e.keyCode;
		else if (e.which) code = e.which;

		switch(code){
			case 9 :	//tab
					if (this.parentSelect.dropInput.nextFocusableElement){
						this.parentSelect.dropInput.nextFocusableElement.focus()
					}else{
						if (whi.browser.IE){
							this.parentSelect.dropInput.blur();
						}
					}
					break;
			case 40 :	//jos
			case 39 :	//dreapta
					jsSelect.selectOption.apply(this.parentSelect,[this.parentSelect.selectedOptionId+1]);
					break;
			case 38 :	//sus
			case 37 :	//stanga
					jsSelect.selectOption.apply(this.parentSelect,[this.parentSelect.selectedOptionId-1]);
					break;
			case 13 :
					if (this.parentSelect.selectedOptionId >=0 && this.parentSelect.selectedOptionId < this.parentSelect.dropOptions.optionlinks.length){
						window.location.href = this.parentSelect.dropOptions.optionlinks[this.parentSelect.selectedOptionId].getAttribute("href");
					}
					break;
		}
	},
	onInputFocus : function(e){
		this.isFocused = true;
		jsSelect.showDropDown.apply(this);
	},
	onInputBlur : function(e){
		this.isFocused = false;
		if (!this.noBlurCheck){
			this.noBlurCheck = false;
			setTimeout(jsSelect.checkInputBlur.bind(this.parentSelect),10);
		}
		this.noBlurCheck = false;
	},
	checkInputBlur : function(){
		if (this.dropInput.isFocused == false && !this.dropInput.noBlurCheck){
			jsSelect.hideDropDown.apply(this.dropInput);
		}
		this.dropInput.noBlurCheck = false;
	},
	clickInnerDrop : function(){
		//this pentru ca e direct pe container
		this.dropInput.focus();
	},
	selectOption : function(ind){
		ind = parseInt(ind) || 0;
		if (ind >=0 && ind < this.dropOptions.optionlinks.length){
			var opt = this.dropOptions.optionlinks[ind];
			if (this.selectedOption != opt){
				if (this.selectedOption){
					whi.remove_class(this.selectedOption,'hover_drop_link');
				}
				this.selectedOption = opt;
				this.selectedOptionId = opt.optId;
				whi.add_class(opt,'hover_drop_link');
			}
		}
	},
	hoverOption : function(){
		jsSelect.selectOption.apply(this.parentSelect,[this.optId]);
	}
}


jsRules.addRule("#submenu form.js_focusable_elements",function(){
	if (this.elements.length){
		var last_el = null;
		for (var i=0; i<this.elements.length; i++){
			if (last_el != null){
				last_el.nextFocusableElement = this.elements[i];
			}
		}
	}
});
                                                                                      
                                                                                      