

function initAjaxLocalite (src,dest){
	if( $defined(src) && $defined(dest) ){
		
		var ul=new Element('ul',{'id':'choices',
						   		'styles':{
										'background-color':'#FFF',
										'list-style-type':'none',
										'position':'absolute',
										'display':'none',
										'padding':'5px',
										'border':'1px solid #666',
										'margin-left':'170px',
										'margin-top':'20px',
										'z-index':'1000'
										},
								'events':{
										'toggle':function(){
												if(ul.getStyle('display')!='none'){
													ul.setStyle('display','none');
												}
												else{
													ul.setStyle('display','block');
												}
											}
										}
								});
		
		
		ul.inject(document.body,"top");
		var coord=src.getCoordinates();
		ul.setPosition({'x':coord.left+coord.width-20,'y':coord.top+10});
		
		
		src.addEvents({
			'gorequest':function(){
				if( (''+(src.value)+'').length>=5 && !isNaN(src.value) ){
					var req=new Request.JSON({'url':'getLocalitesJSON.php',
							  'data':'cp='+src.value,
							  'method':'post',
							  'link':'ignore',
							  'onSuccess':function(response){
									ul.empty();			
									response.localites.each(function(e){
										var li=new Element('li',{'class':'choice'});
										var locInsee=e.LOC_INSEE;
										li.set('title',e.LOC_ACHEM+(($chk(locInsee) && !locInsee.contains(e.LOC_ACHEM) )?'::'+locInsee:''));
										li.set('html',(($chk(locInsee) && !locInsee.contains(e.LOC_ACHEM))?''+locInsee+'('+e.LOC_ACHEM+')':e.LOC_ACHEM))
										li.addEvent('click',function(){
											
											var ti=li.title.split('::');
											if(ti.length>1){
												$('adresse4').value=ti[1];
											}
											dest.value=ti[0];
											if($chk(dest.value)){
												dest.fireEvent('change');
											}
											//console.log(ti);
											//ul.setStyle('display','none');											
										});
										li.inject(ul);
									});
									ul.setStyle('display','block');
									src.blur();
							  }
							  
					});
					req.send();
				}
				else{
					ul.empty();
					ul.setStyle('display','none');
				}
			},			
			'keyup':function(){src.fireEvent('gorequest');}
		});
		dest.addEvents({
			'click':function(){
				ul.setStyle('display','none');
			},
			'change':function(){
				ul.setStyle('display','none');
			}
		});
	}
}

var OverlayFix = new Class({

	initialize: function(el) {
		if (Browser.Engine.trident) {
			this.element = $(el);
			this.relative = this.element.getOffsetParent();
			this.fix = new Element('iframe', {
				'frameborder': '0',
				'scrolling': 'no',
				'src': 'javascript:false;',
				'styles': {
					'position': 'absolute',
					'border': 'none',
					'display': 'none',
					'filter': 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)'
				}
			}).inject(this.element, 'after');
		}
	},

	show: function() {
		if (this.fix) {
			var coords = this.element.getCoordinates(this.relative);
			delete coords.right;
			delete coords.bottom;
			this.fix.setStyles($extend(coords, {
				'display': '',
				'zIndex': (this.element.getStyle('zIndex') || 1) - 1
			}));
		}
		return this;
	},

	hide: function() {
		if (this.fix) this.fix.setStyle('display', 'none');
		return this;
	},

	destroy: function() {
		if (this.fix) this.fix = this.fix.destroy();
	}

});

window.addEvent('domready',function(){
	initAjaxLocalite($('cp'),$('ville'));
});
