var ComboBox = new Class({
	options: {
		onSelect:Class.empty,
		popupfx:{transition:Fx.Transitions.Quint.easeOut,duration:200},
		classes:{popup:'popup',combo:'combo'},
		size:{width:100,height:24},
		startindex:0
	},

	initialize: function(container,liste,options){
		if( $defined( options ) ) {this.setOptions(options);}

		this.popup = new Element('div',{
			'class':this.options.classes.popup,
			'styles':{
				visibility:'hidden',
				position:'absolute',
				zindex:1000
			}
		}).injectInside(document.body);
		this.popup.fx = new Fx.Morph(this.popup,{
			duration: this.options.popupfx.duration,
			transition: this.options.popupfx.transition,
			onComplete: function() {this.doAction('endfx');}.bind(this)
		});
		this.popup.fx.set({'height':'0px'});

		this.combo = container;
		this.combo.addEvent('click', function() {this.doAction('pop');}.bind(this));
		this.step = 'attente';

		this.pushList(liste,this.options.startindex);

		this.cid=new Date().getTime();
		
		if(!$defined(window.ComboBoxArray)) {
			window.ComboBoxArray = new Array();
		}

		window.ComboBoxArray.push(this);
	},
	doAction:function(todo,val) {
		switch(this.step) {
			case 'attente' :
				switch(todo) {
					case 'pop':
						window.ComboBoxArray.each(function(el) {
							if(el.cid!=this.cid) {
								el.hide();
							}
						}.bind(this));

/*
try {
var pos=$('region-haut').getPosition();
var pppp=$$('.region-container')[0].getPosition();
var aaaa=1;
}catch(e){}
						
var aa="";
try {
	var count=20;
	var p=this.combo;
	do {
		aa+=" "+ p.id+ ' : ' + p.getCoordinates().top+'\n';
		p=p.getParent();
	}while((p!=null)&&(--count>0));
}catch(e){}

var tt=this.combo.getPosition();*/
/*
		var aa=null;
		try {
			aa=$('region-haut').getPosition().y;
		}catch(e){}*/
						var pos = this.combo.getCoordinates(document.body);
						this.popup.setStyles({
							height:'0px',
							top:pos.top+pos.height,
							left:pos.left,
							visibility:'visible'
						});
						this.step = 'doshow';
						this.popup.fx.start({'height':this.options.size.height*this.liste.length});
						break;
				}
				break;
			case 'doshow' :
				switch(todo) {
					case 'endfx':
						this.step = 'selection';
						//window.addEvent('click', window.ComboBoxHide);
						break;
				}
				break;
			case 'dohide' :
				switch(todo) {
					case 'endfx':
						this.popup.setStyle('visibility','hidden');
						this.step = 'attente';
						break;
				}
				break;
			case 'selection' :
				switch(todo) {
					case 'click' :
						try {
							if(this.index!=val) {
								this.index = val;
								this.combo.set('html',this.liste[val].content);
								this.fireEvent('onSelect', this.liste[val].id);
							}
						}catch(e) {
						}
					case 'pop' :
						this.step = 'dohide';
						this.popup.fx.start({'height':'0px'});
						break;
				}
				break;
		}
	},

	pushList:function(liste,startindex) {
		this.liste=liste;
		this.index = startindex;
		
		this.popup.empty();
		this.liste.forEach( function(l,index) {
			new Element('a',{
				'class':this.options.classes.item,
				'href':'javascript:void(0);',
				'events':{
					'click': function() {this.doAction('click',index);}.bind(this)
				}
			}).injectInside(new Element('div').injectInside(this.popup)).set('html',l.content);
		}.bind(this));

		this.combo.empty();
		this.combo.set('html',this.liste[this.index].content);
	},
						 
	getIndex:function() {
		return(this.index);
	},
	getid:function() {
		return(this.liste[this.index].id);
	},
	show:function(hideifonscreen) {
		if(this.popup.getStyle('visibility')!='hidden') {
			if(hideifonscreen) {
				this.hide();
			}
			return;
		}
		if(this.step='attente') {
			this.doAction('pop');
		}
	},
	hide:function() {
		this.step='selection';
		this.doAction('pop');
	}
});
ComboBox.implement(new Options);
ComboBox.implement(new Events);


