Aheuh.SelectHtml = Class.create({
	
	initialize: function (params) {		
		this._params = params;
		this._onChangeFunction = params.onChangeFunction;
		this.isOpen = this._closeOnClick = this._keyActive = this.changeFunctionActive = false;
		this._styles = new Array();
		this._datas = new Array();
		this.keyIndex = {};
		this._select = $(params.selectHtml);
		this._select.setStyle({ visibility:'hidden' });
		this._index = this.keyCntCheck = 0;
		this._keyInterval = null;
		this._keyCurrentString = '';
	},
	
	init: function () {
		
		this._writeHtml = $(this._params.selectWrite);
		this._zIndex = this._writeHtml.getStyle('zIndex');
			
		this._setStyles();
		this._style = this._styles[this._params.style || 0];
		this._parseDatas(this._select);
		
		this._write();		
			
		this._main = $$('#'+this._params.selectWrite+' .selectMain')[0];
		this._mask = $$('#'+this._params.selectWrite+' .selectMask')[0];		
		this._content = $$('#'+this._params.selectWrite+' .selectOptions')[0];
		
//		Evol 34 FB 30/11/2009
//		Avec Internet Explorer on accelere l'ascenseur
		if(navigator.appName.substring(0,9) == 'Microsoft')
		this._scroller =  new Aheuh.Scroller("vertical",{
			container: $$('#'+this._params.selectWrite+' .selectOptions')[0],
			mask: $$('#'+this._params.selectWrite+' .selectMask')[0],
			scUp: $$('#'+this._params.selectWrite+' .selectUp')[0],
			scDn: $$('#'+this._params.selectWrite+' .selectDn')[0],
			cursorV: $$('#'+this._params.selectWrite+' .selectCursor')[0],
			scrollbar: $$('#'+this._params.selectWrite+' .selectScrollbar')[0],
			scrollbarBgV: $$('#'+this._params.selectWrite+' .selectScrollbarBgV')[0],
			scrollElems: $$('#'+this._params.selectWrite+' .scrollElems')[0]
		},10,25);
		
//		Avec les autres navigateurs
		else
			this._scroller =  new Aheuh.Scroller("vertical",{
			container: $$('#'+this._params.selectWrite+' .selectOptions')[0],
			mask: $$('#'+this._params.selectWrite+' .selectMask')[0],
			scUp: $$('#'+this._params.selectWrite+' .selectUp')[0],
			scDn: $$('#'+this._params.selectWrite+' .selectDn')[0],
			cursorV: $$('#'+this._params.selectWrite+' .selectCursor')[0],
			scrollbar: $$('#'+this._params.selectWrite+' .selectScrollbar')[0],
			scrollbarBgV: $$('#'+this._params.selectWrite+' .selectScrollbarBgV')[0],
			scrollElems: $$('#'+this._params.selectWrite+' .scrollElems')[0]
		},10,10);
			
		this._scroller.init();
	
		this._setEvents();
		this.setSelectedIndex(this._index);
		this._scroller.showScrollElems("vertical","hidden");

	},
	
	_setStyles: function () {
		var styles = Aheuh.SelectHtmlConfig.styles;
		for (var i in styles) {
			this._styles.push({ name:i, html:styles[i] });
		}
	},
	
	_parseDatas: function (o) {
		//NH : Fix des erreurs de doublons dans les listes (corrige les appels multiples à init())
		this.keyIndex = {};
		this._datas = new Array();
		this._index = this.keyCntCheck = 0;
		
		for (var i=0;i<o.length;i++) {
			if (o[i].selected) {
				this._index = i;
			}
			this._add({ data:o[i].value, label:o[i].innerHTML });
		}
	},
	
	_write: function () {
		
		this._writeHtml.innerHTML = this._style.html;
		var writeOptions = $$('#'+this._params.selectWrite+' .selectOptions')[0];
		
		var options = '<ul>';
		for (var i=0;i<this._datas.length;i++) {
			if(this._datas[i].label == "CITROEN" || this._datas[i].label == "FIAT" || this._datas[i].label == "FORD" || this._datas[i].label == "OPEL" || this._datas[i].label == "PEUGEOT" || this._datas[i].label == "RENAULT" || this._datas[i].label == "TOYOTA" || this._datas[i].label == "VOLKSWAGEN")
			options += '<li id="'+i+''+this._select.id+'"><a href="#0" style="color:#49961F">' + this._datas[i].label + '</a></li>';
		else
			options += '<li id="'+i+''+this._select.id+'"><a href="#0">' + this._datas[i].label + '</a></li>';
		}
		options += '</ul>';
		writeOptions.innerHTML = options;		
		
	},
	
	_add: function (o) {
		this._addKeyIndex(o.label.charAt(0).toLowerCase(),this._datas.length,o.label);
		this._datas.push(o);
	},
	
	_addKeyIndex: function (key,index,label) {
		if (!this.keyIndex[key]) { this.keyIndex[key] = {infos:[]}; }
		this.keyIndex[key].infos.push({ index:index, label:label });
	},
	
	_setEvents: function () {
		
		var tmp = this._content.getElementsByTagName("a");
		
		for (var i=0;i<tmp.length;i++) {
			tmp[i].className = "s"+i;
			Event.observe(tmp[i], "click", this._onItemClickEvents.bindAsEventListener(this));
			Event.observe(tmp[i], "mouseover", this._onItemOverEvents.bindAsEventListener(this));
			Event.observe(tmp[i], "mouseout", this._onItemOutEvents.bindAsEventListener(this));
		}
		
		Event.observe(this._main, "click", this._onMainClick.bindAsEventListener(this));
		Event.observe(this._main, "click", Event.SelectionClear);
		Event.observe(this._main, "mouseover", this._onMainOver.bindAsEventListener(this));
		Event.observe(this._main, "mouseout", this._onMainOut.bindAsEventListener(this));		
		Event.observe(this._main, this._scroller._mouseWheelEvent, this._onMainMouseWheel.bindAsEventListener(this));
		
		Event.observe(this._writeHtml, "mouseover", this._onWriteMouseOver.bindAsEventListener(this));
		Event.observe(this._writeHtml, "mouseout", this._onWriteMouseOut.bindAsEventListener(this));
		
		Event.observe(document, "mouseup", this._checkToClose.bindAsEventListener(this));
		Event.observe(document, "keydown", this._setSelectedKeyboardIndex.bindAsEventListener(this));
		
	},
	
	_onItemClickEvents: function (e) {
		this._content.style.visibility = this._mask.style.visibility = "hidden";
		this.isOpen = this._closeOnClick = this._keyActive = false;
		this.setSelectedIndex(parseInt(Event.element(e).parentNode.id)); 
		this._scroller.reload();
		this._scroller.showScrollElems("vertical","hidden");
	},
	_onItemOverEvents: function (e) {
		if (this.itemMouseOver!="") { Element.removeClassName($(this.keyItemSelected), "on"); }
		this.itemMouseOver = Event.element(e);
	},
	_onItemOutEvents: function () {
		this.itemMouseOver = "";
	},
	
	_onMainClick: function () {
		this._scroller.reload();
		this._scroller.moveToAnchor(this._index+this._select.id);
		var visibility = (this.isOpen) ?'hidden' :'visible';	
		this._showHideDatas(visibility);
	},
	_onMainOver: function () {
		this._keyActive = true;
	},
	_onMainOut: function () {
		if (!this.isOpen){  this._keyActive = false; }
	},
	_onMainMouseWheel: function (e) {
		(Event.Wheel(e)==-1) ?this.setSelectedIndex(this._index+1) :this.setSelectedIndex(this._index-1);
	},
	
	_onWriteMouseOver: function () {
		if (this.isOpen) { this._keyActive = true; this._closeOnClick = false; }
	},
	_onWriteMouseOut: function () {
		if (this.isOpen) { this._keyActive = false; this._closeOnClick = true; }
	},
	
	_showHideDatas: function (visibility) {
		this._content.style.visibility = this._mask.style.visibility = visibility;
		if (this._scroller.scrollingV) { 
			(!this.isOpen) ?this._scroller.viewScrollElems("vertical","visible","noChange") :this._scroller.showScrollElems("vertical","hidden");
		}
		else {
		    // 16/12/2008   HJ
		    // Bug : Ascenceur reste afficher sur click du div main
		    if (this.isOpen) {
		        this._scroller.showScrollElems("vertical","hidden");
		    }
		}
		this.isOpen = this._keyActive = (visibility=="hidden") ?false :true;		
		var zIndex = (this.isOpen) ?Number(this._zIndex)+1 :this._zIndex;		
		this._writeHtml.setStyle({ zIndex:zIndex })		
		this.itemMouseOver = "";
	},
	
	getSelectedData: function () {
		return this._datas[this._index].data;
	},
	
	getSelectedLabel: function () {
		return this._datas[this._index].label;
	},
	
	setSelectedIndex: function (index) {
		if (index>=0&&index<this._datas.length) {
			Element.removeClassName(this._content.getElementsByTagName('a')[this._index], "on");
			this._index = index;
			this._main.innerHTML = this.getSelectedLabel();			
			Element.addClassName(this._content.getElementsByTagName('a')[index], "on");
			this._select.selectedIndex = index;			
			if (this._onChangeFunction&&this.changeFunctionActive) { this._onChangeFunction(); }
			if (!this.changeFunctionActive) { this.changeFunctionActive = true; }
		}
	},
	
	setSelectedData : function (data) {
		this.changeFunctionActive = false;
		for (var i=0;i<this._datas.length;i++) { if (this._datas[i].data==data) { this.setSelectedIndex(i); }}
	},
	
	_checkToClose: function () {
		if (this._closeOnClick&&this.isOpen) {
			this._showHideDatas("hidden");
			this._scroller.showScrollElems("vertical","hidden");
		}
	},
	
	_setKeyInterval: function () {
		if (!this._keyInterval) {
			this._keyInterval = new PeriodicalExecuter(this._checkKeyInterval.bind(this),0.5);
		}
		else {
			this._keyInterval.stop();
			this._keyInterval.callback = this._checkKeyInterval.bind(this);
			this._keyInterval.registerCallback();
		}
	},
	
	_checkKeyInterval: function () {
		this._keyCurrentString = this.currentKey = '';
		this._keyInterval.stop();
	},
	
	_setSelectedKeyboardIndex: function (e) {

		if (this._keyActive) {
			
			e.stop();
			
			var keyCode = (window.event) ?e.keyCode :e.which;
			var keyCodeLabel = String.fromCharCode(keyCode).toLowerCase();
			
			this._setKeyInterval();
			this._keyCurrentString += keyCodeLabel;			
			var firstKey = this._keyCurrentString.charAt(0);
			
			if (keyCode==13&&this.isOpen) {
				if (this.itemMouseOver!="") this.setSelectedIndex(parseInt(this.itemMouseOver.parentNode.id));
				this._showHideDatas("hidden");
			}
			
			if (this.keyIndex[firstKey]) {
				
				this.keyCntCheck = this._getKeyCntCheck(keyCodeLabel,this.keyIndex[firstKey].infos);
				
				if (this.keyCntCheck!=-1) {
				
					if (this.keyItemSelected!="") {
						Element.removeClassName($(this.keyItemSelected), "on");
					}
					Element.removeClassName(this._content.getElementsByTagName("div")[this._index], "on");
					
					this.keyItemSelected = this.keyIndex[firstKey].infos[this.keyCntCheck].index + this._select.id;
					Element.addClassName($(this.keyItemSelected), "on");
					
					this._scroller.moveToAnchor(this.keyItemSelected);
					this.currentKey = keyCodeLabel;
					this.itemMouseOver = $(this.keyItemSelected);
	                
	                // EVOL 34 FB 30/11/2009
//					this.setSelectedIndex(this.keyIndex[firstKey].infos[this.keyCntCheck].index);
				
				}
			
			}
			
			if (keyCode==38&&this._index>0) {
				this.setSelectedIndex(this._index-1);
				this._scroller.moveToAnchor(this._content.getElementsByTagName('li')[this._index].id);
			}
			else if (keyCode==40&&this._index<this._datas.length) {
				this.setSelectedIndex(this._index+1);
				this._scroller.moveToAnchor(this._content.getElementsByTagName('li')[this._index].id);
			}
			
		}
		
	},
	
	_getKeyCntCheck: function (key,keyArray) {
		if (this.currentKey==key && this._keyCurrentString.length==1 || this.currentKey == '') {
			return (this.keyCntCheck<this.keyIndex[key].infos.length-1) ?this.keyCntCheck+1 :0;
		}
		else if (this._keyCurrentString.length>1) {
			for (var i=0;i<keyArray.length;i++) {
				if (keyArray[i].label.substring(0,this._keyCurrentString.length).toLowerCase()==this._keyCurrentString) {
					return i;
				}
			}
		}
		return -1;
	}
	
});