document.observe('dom:loaded', function () 
{
	// execute js function 
	fix_img_background();
	
	//active autocompletion
	_ac();
});

//set GlobalregisterHandler
var myGlobalHandlers = 
{
  onCreate: function() 
	{
      if (!$('chargement')) {
          creationIndicateur();
      }
      else {
          //update element position
          $('chargement').setStyle({
              top: document.viewport.getScrollOffsets().top + 'px'
          })
      }
      Element.show('chargement');
  },
  onComplete: function()
	{
      if (Ajax.activeRequestCount == 0) Element.hide('chargement');
  }
};
//register handler
Ajax.Responders.register(myGlobalHandlers);

var creationIndicateur = function () 
{
    var indicateur = document.createElement('div');

    Element.extend(indicateur);
    indicateur.id = 'chargement';
    indicateur.setStyle({
        display: 'none',
        position: 'absolute',
        right: '0px',
        top: document.viewport.getScrollOffsets().top + 'px',
        padding: '5px',
        color: '#ffffff',
        backgroundColor: 'red',
        fontWeight: 'bold'
    });
    indicateur.innerHTML = 'Chargement...';
    document.body.appendChild(indicateur);
} 
//-->

var fix_img_background = function () 
{
	$$('a').each(function(elt)
	{	
		var bool = false;
		var child = elt.firstDescendant();
		if( child != null )
		{
			if(child.tagName == 'IMG')
				bool = true;
		} 
		
		
		if (bool) 
		{
			elt.observe('mouseover', function(ev){
				elt.setStyle({backgroundColor:'#FFFFFF'});
			});
		}
		else
		{
			
		}
	});
};


var _ac = function(){
	 $$('.autocomplete').each(function(elt)
	 {
      var autocomplete_list_id = "autocomplete_choice_" + elt.id;
      
      //wrap div
      $(elt).wrap('span', {
          'class': 'ac_holder'
      });
      
      //set up options
      var options = {
          script: 'autocomplete_liste.php',
          meth: 'post',
          varname: 'value',
          json: true,
          shownoresults: true,
          noresults: '-- Tu cherches mal ! Recommence jeune padawan --',
          minchars: 1,
          useNotifier: true,
          timeout: 8000,
          setWidth: true,
          minWidth: 400,
          maxWidth: 400,
          callback: function(obj)
					{
						var path = document.location.pathname.split('/');
            // ajoute un champs caché pour récupérer l'id et l'image du produit
						document.location.href = "http://www.guerilla-asso.com/"+path[1]+"/product_info.php?products_id="+ obj.id;						
					}
      };
      
      //call autcomplete object
      new AutoComplete(elt.id, options);
   });
}
