var hpoformcompomentsComboOpen = false;
var hpoformcompomentsComboTimerOut = null;
var hpoformcompomentsComboLastSearch = null;
window.addEvent('domready', function() {

	document.getElements('dl.hpoformcompomentsCombo').each(function(item, index) 
	{			
		var key = item.getProperty('id');
		var input = item.getElement('dt').getElement('input');
		var link = item.getElement('dt').getElement('a');
		var list = item.getElement('dd');
		var dd = item.getElement('dd');
		var ul = dd.getElement('ul');
		var scroll = dd.getElement('.scroll');
		var handle = scroll.getElement('.handle');
		var inputLabel = input.value;
		
		// Pour contrer la fermeture automatique quand on clique "n'importe où" sauf dans la liste
		dd.addEvent('mouseover', function(e) {
			if ( hpoformcompomentsComboTimerOut )
				clearTimeout(hpoformcompomentsComboTimerOut);
			hpoformcompomentsComboTimerOut = setTimeout('hpoformcompomentsComboTimerOut = null', 200);
		});
		dd.addEvent('mousedown', function(e) {
			if ( hpoformcompomentsComboTimerOut )
				clearTimeout(hpoformcompomentsComboTimerOut);
			hpoformcompomentsComboTimerOut = setTimeout('hpoformcompomentsComboTimerOut = null', 200);
		});
		link.addEvent('mouseover', function(e) {
			if ( hpoformcompomentsComboTimerOut )
				clearTimeout(hpoformcompomentsComboTimerOut);
			hpoformcompomentsComboTimerOut = setTimeout('hpoformcompomentsComboTimerOut = null', 200);
		});
		link.addEvent('mousedown', function(e) {
			if ( hpoformcompomentsComboTimerOut )
				clearTimeout(hpoformcompomentsComboTimerOut);
			hpoformcompomentsComboTimerOut = setTimeout('hpoformcompomentsComboTimerOut = null', 200);
		});
		
		// Actions sur le champ texte
		input.addEvents({
			// entrée dans le champ
			focus: function() {
			/*
				if ( hpoformcompomentsComboTimer )
					return false;
				*/	
				if ( hpoformcompomentsComboOpen && hpoformcompomentsComboOpen != dd )
				{
					hpoformcompomentsComboOpen.setStyle('display', 'none');
					hpoformcompomentsComboOpen = false;
				}
				
				if ( input.value == inputLabel )
					input.value = '';
				
				HpoFormComponenetsCombo_getList(input.value, dd, key, false);
			},
			
			// Appui sur une touche
			keyup: function() {
				HpoFormComponenetsCombo_getList(this.value, dd, key, false);
			},
			
			// sortie du champ
			blur: function() {
				if ( this.value == '' )
					this.value = inputLabel;
			}
		});
		
		
		// Actions sur le lien
		link.addEvent('click', function(event) {
			
			if ( dd.getStyle('display') == 'block' )
			{		
				if ( input.value == '' )
					input.value = inputLabel;
					
				dd.setStyle('display', 'none');
				hpoformcompomentsComboOpen = false;
			}
			else
			{				
				if ( hpoformcompomentsComboOpen )
				{
					hpoformcompomentsComboOpen.setStyle('display', 'none');
					hpoformcompomentsComboOpen = false;
				}
				
				if ( input.value == inputLabel )
					input.value = '';
				
				HpoFormComponenetsCombo_getList(input.value, dd, key, true);
			}
			
			return false;
		});
		
	});
	
});

document.addEvent('mousedown', function() {
	if ( hpoformcompomentsComboOpen && 
		 hpoformcompomentsComboOpen && 
		 !hpoformcompomentsComboTimerOut )
	{
		hpoformcompomentsComboOpen.setStyle('display', 'none');
		hpoformcompomentsComboOpen = false;
	}
});

// Fonction générique pour appel ajax + affichage liste
function HpoFormComponenetsCombo_getList(text, dd, key, force)
{
	if ( ( text != '' && text.length > 2 ) || force == true )
	{
		if ( hpoformcompomentsComboLastSearch != text || force )
		{
			hpoformcompomentsComboLastSearch = text;
			if (MooTools.version == '1.2.1') 
			{
                req = new Request.HTML({
				    url: '/?type=355&tx_hpoformcomponents_pi1[key]='+ key +'&tx_hpoformcomponents_pi1[text]=' + text,
				    method: 'GET',
				    onSuccess: function(html) { 
					    dd.setStyle('display', 'block');
					    hpoformcompomentsComboOpen = dd;
					    
					    var ul = dd.getElement('ul');
					    ul.empty();
					    ul.adopt(html);
					    
					    HpoFormComponenetsCombo_makeScrollbar(dd);
					    
				    }
			    });
			    req.send();
            } 
            else 
            {
                new Ajax('/?type=355&tx_hpoformcomponents_pi1[key]='+ key +'&tx_hpoformcomponents_pi1[text]=' + text, {
                    method: 'get',
                    onComplete: function()
                    {
                        var contentHTML = this.response.text;
                        //$$('#'+container).setHTML(contentHTML);
                        
                        dd.setStyle('display', 'block');
                        hpoformcompomentsComboOpen = dd;
                        
                        var ul = dd.getElement('ul');
                        ul.empty();
                        ul.setHTML(contentHTML);
                        
                        HpoFormComponenetsCombo_makeScrollbar(dd);
                    }
                }).request();
            }
		}                              
	}
	else
		dd.setStyle('display', 'none');
}

function HpoFormComponenetsCombo_makeScrollbar(dd)//content, scrollbar, handle, horizontal, ignoreMouse) 
{
	var content = dd.getElement('ul');
	var scrollbar = dd.getElement('.scroll');
	var handle = scrollbar.getElement('.handle');
	var horizontal = false;
	var ignoreMouse = false;
    var steps = (horizontal ? (content.scrollWidth - content.offsetWidth) : (content.scrollHeight - content.offsetHeight))
	
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}