var mooCombo = new Class({

	/* set and create the date picker text box */
	initialize: function(el){

		// Options defaults
	
		cbo = new Element('input', {'styles': {
			//top: pos.top,
			//left: pos.left,
			width: el.getCoordinates().width
		},
		'class':'comboBoo-label', 'value': el.options[0].text}).injectBefore(el);
		if(el.get('class').contains('Airport')){
			cbo.addClass('Airport');
		}
		cbo.oldCombo = el;
		
		cbo.oldCombo.setStyle('visibility', 'hidden');
			
		cbo.set({'id':el.get('name'), 'readonly':true});
		
		cbo.container = false;
		cbo.interval = null;
		cbo.active = false;
		cbo.onclick = cbo.onfocus = this.create.pass(cbo, this);
		cbo.addEvent((Browser.Engine.trident || Browser.Engine.webkit) ? 'keydown' : 'keypress', this.onCommand.bind(this))
		cbo.selected = 0;
	},
	
	/* create the calendar */
	create: function(cbo){
		if (cbo.container) return false;	
		/* create the outer container */		
		
		cbo.container = new Element('ul', {'class': 'comboBoo-list', 'id': 'choices-' + cbo.get('id')}).setStyles({top: cbo.getCoordinates().top - 7, left: cbo.getCoordinates().left, width: cbo.getCoordinates().width + 33}).injectBefore(cbo);
		
		cbo.test = this;
		
		if (cbo.oldCombo.length > 6) {
			cbo.container.setStyles({'overflow-y': 'scroll', height: 125});
		}
			
		for(i = 0; i < cbo.oldCombo.length; i++) {
			if ( cbo.oldCombo.options[i].text != 'State') {
				var el2 = new Element('li', {'id': i,
					'events': {
						'click': function(){
							cbo.set('value', this.get('text'));
							cbo.oldCombo.selectedIndex = this.id;
							cbo.test.remove(cbo);
						},
						'mouseover': function(){
							this.addClass('choice-selected');
						},
						'mouseout': function(){
							this.removeClass('choice-selected');
						}
					}
				}).set('html', cbo.oldCombo.options[i].text);
				el2.injectInside(cbo.container);
			}
		};
		var test = function(){
			if($defined(cbo.test)){
				cbo.test.remove(cbo)
			}
		};
		/* create timers */
		
		cbo.container.onscroll = function(){
			$clear(cbo.interval);
		};
		cbo.container.onmouseover = cbo.onmouseover = function(){
			$clear(cbo.interval);
		};
		cbo.container.onmouseout = cbo.onmouseout = cbo.onblur = function(){
			cbo.interval = test.delay(500);
		}.bind(this);
	},
	onCommand: function(e) {
		if (!e && this.focussed) return this.prefetch();
		if (e && e.key && !e.shift) {
			switch (e.key) {
				case 'enter':
					if($defined(document.getElement('.comboBoo-list'))){
						document.getElement('.comboBoo-list').destroy();
					}
					return false;
				case 'up': 
					if(cbo.selected - 1 != -1){
						cbo.selected = cbo.selected - 1;
					}
					if($defined(document.getElements('.comboBoo-list li'))){
					document.getElements('.comboBoo-list li').each(function(itm, i){
						if(i == cbo.selected){
							itm.getParent().scrollTop = itm.getCoordinates(itm.getParent()).top;
							itm.addClass('choice-selected');
							document.getElement('select[name=' + $(itm.getParent().id.replace('choices-', '') + ']')).selectedIndex = i;
							$(itm.getParent().id.replace('choices-', '')).set('value', itm.get('text'));
						}else{
							itm.removeClass('choice-selected');
							
						}
					});
					}
					return false;
				break;
				case 'down':
					if(document.getElements('.comboBoo-list li').length > cbo.selected + 1){
						cbo.selected = cbo.selected + 1;
					}
					document.getElements('.comboBoo-list li').each(function(itm, i){
						if(i == cbo.selected){
							itm.getParent().scrollTop = itm.getCoordinates(itm.getParent()).top;
							itm.addClass('choice-selected');
							document.getElement('select[name=' + $(itm.getParent().id.replace('choices-', '') + ']')).selectedIndex = i;
							$(itm.getParent().id.replace('choices-', '')).set('value', itm.get('text'));
						}else{
							itm.removeClass('choice-selected');
							
						}
					});
					
					return false;
				break;
			}
		}
		return true;
	},
	/* Format the returning date value according to the selected formation */
	formatValue: function(dp, year, month, day){
		
	},
	
	/* Remove the calendar from the page */
	remove: function(cbo){
		$clear(cbo.interval);
		cbo.interval = false;
		cbo.active = false;
		if($defined(cbo.container)){
			if($defined(cbo.container.destroy)){
				cbo.container.destroy();
			}
		}
		cbo.container = false;
	}
});


