// ie6 Detect
window.addEvent('domready', function()
{
  if( window.ie6 )
  {
    var alarm = new Element('div').addClass('alarm-ie6').injectInside( $(document.body) );
    alarm.setHTML('Vous utilisez actuellement Internet Explorer 6. Ce navigateur est dépassé.<br />Afin profiter au mieux des fonctionnalités de ce site nous vous invitons à le <a href="http://www.microsoft.com/france/windows/ie/" title="Mettre à jour" target="_blank">mettre à jour</a>. Merci de votre compréhension.');
  }
});


// pretty box
window.addEvent('domready', function()
{
  var lb = $('logbox');
  var lb_content = lb.getElements('div.le-contents')[0];
  var corners = lb.getElements('div.corner');
  var lb_form = lb.getElement('form');
    
  if( $(document.body).hasClass('index') )
  {
    var lb_content_h = [ 16 , lb_content.getCoordinates().height ];
    var lb_mt = [ lb.getStyle('margin-top').toInt() , ((178-lb_content_h[1])/2) + 62 ];
    var lb_ml = [ lb.getStyle('margin-left').toInt() , 724 ];
    var lb_w = [ lb.getCoordinates().width , 277 ];
  }
  else
  {
    var lb_content_h = [ 20 , lb_content.getCoordinates().height ];
    var lb_mt = [ lb.getStyle('margin-top').toInt() , '-44' ];
    var lb_ml = [ lb.getStyle('margin-left').toInt() , 722 ];
    var lb_w = [ lb.getCoordinates().width , 277 ];
  }
  
  var timer;
  
  var lb_Fx = new Fx.Styles
  (
    lb,

    {
      'duration': 300,
      'wait': false
    }
  );
  
  var lb_content_Fx = new Fx.Styles
  (
    lb_content,
    {
      'duration': 300,
      'wait': false
    }
  );
  
  lb.addEvent
  (
    'mouseenter', function()
    {
      $clear(timer);
      
      corners.addClass('page');
      
      lb_Fx.start
      (
        {
          'margin-top': lb_mt[1],
          'margin-left': lb_ml[1],
          'width': lb_w[1]
        }
      );
      
      lb_content_Fx.start
      (
        {
          'height': lb_content_h[1]
        }
      );
      
      
    }
  );
  
  if( $(document.body).hasClass('index') )
  {
    lb.fireEvent('mouseenter');
  }
  
  if( $(document.body).hasClass('page') )
  {
    lb.addEvent
    (
      'mouseleave', function()
      {
        timer =
        (
          function()
          {
            lb_Fx.start
            (
              {
                'margin-top': lb_mt[0],
                'margin-left': lb_ml[0],
                'width': lb_w[0]
              }
            );
            
            corners.removeClass('page');
            
            lb_content_Fx.start
            (
              {
                'height': lb_content_h[0]
              }
            );
          }
        ).delay(1000);
      }
    );

  }
  
  if( lb_form.hasClass('connection-failed-miserably') )
  {
    lb_Fx.set
      (
        {
          'margin-top': lb_mt[1],
          'margin-left': lb_ml[1],
          'width': lb_w[1]
        }
      );
  }
  else
  {
    lb_content.setStyle( 'height' , lb_content_h[0] );
  }
  
});


// pretty select
window.addEvent('domready', function()
{
  $$('#know-how select').each
  (
    function(el)
    {
      new Wd.Elements.Select(el);
      
      el.onchange = function()
      {
        if( el.value ) el.form.submit();
      }
    }
  );
});


// pretty formulations
window.addEvent('domready', function()
{
  $$('#formulations li').addEvents
  (
    {
      'mouseenter': function()
      {
        this.addClass('hover');
      },
      
      'mouseleave': function()
      {
        this.removeClass('hover');
      }
    }
  );
});


// pretty list-cleaner
window.addEvent('domready', function()
{
  var targets = $$('#prod-box ul, #content-right #misc-box ul, #content-right #know-prod ul');
  
  targets.each
  (
    function(el,i)
    {
      el.getLast().setStyle('border-bottom','none');
    }
  );
});


// pretty truc
window.addEvent('domready', function()
{
  // stop tout
  if( $$('div.row').length == 0){ return }
  
  // let's begin
  var rows = $$('div.row');
  rows.shift();
  
  var box = new Element('div').addClass('table-overload').injectBefore( rows[0] );
  var box_top = box.getCoordinates().top;
  
  var row_status = [39,79];
  var square_status = [11,69];
  var lnk_status = 53;
  
  box.setStyles
  (
    {
      height: row_status[1],
      opacity: 0
    }
  );
  
  var active_row;
  
  var boxFx = new Fx.Styles
  (
    box,
    {
      wait: false,
      duration: 400,
      transition: Fx.Transitions.Cubic.easeInOut
    }
  );
  
  var timer;
  
  rows.each
  (
    function(el,i)
    {
      var as = el.getElements('a');
      
      as.each
      (
        function(a)
        {
          if( a.getElement('small') ) a.getElement('small').inject(a);
        }
      );
      
      el.addEvent
      (
        'click', function(e)
        {
          new Event(e).stop();
          
          if( box.getStyle('padding-top') != '0px' )
          {
            return;
          }
          
          if( timer )
          {
            $clear(timer);
          }
          
          $clear(timer);
          var rowFx = new Fx.Styles
          (
            el,
            {
              wait: false,
              duration: 400,
              transition: Fx.Transitions.Cubic.easeInOut
            }
          );
          
          var clone = el.clone().setProperty('id','');
          
          clone.setStyles
          (
            {
              'margin-top': - ( row_status[0],row_status[1] )
            }
          );
          
          var cloneFx = new Fx.Styles
          (
            clone,
            {
              wait: false,
              duration: 400,
              transition: Fx.Transitions.Cubic.easeInOut
            }
          );
          
          clone.addEvent
          (
            'click', function(e)
            {
              new Event(e).stop();
              
              if( box.getStyle('padding-top') != '0px' )
              {
                return;
              }
              
              boxFx.start
              (
                {
                  height: row_status[0],
                  opacity: 0
                }
              );
              
              rowFx.start
              (
                {
                  height: row_status[0]
                }
              );
            }
          );
          
          var classname = el.getProperty('class');
          
          classname = classname.substr( classname.indexOf('f') , classname.length );
          
          var alteregal = $$('#formulations a.' + classname )[0];
                      
          var square = new Element('a').addClass('picto ' + classname ).setProperties({ 'title': alteregal.title, 'href': alteregal.href });
          
          square.setStyles
          (
            {
              'margin-right': -square_status[1]
            }
          );
          
          var squareFx = new Fx.Styles
          (
            square,
            {
              wait: false,
              duration: 400,
              transition: Fx.Transitions.Cubic.easeInOut
            }
          );
          
          var href = el.getElement('a');
          
          var lnk = new Element('a').addClass('know-more').setHTML(savoirplus).setProperties({'href': href.href });
          
          lnk.setStyles
          (
            {
              'margin-top': row_status[1]
            }
          );
          
          var lnkFx = new Fx.Styles
          (
            lnk,
            {
              wait: false,
              duration: 400,
              transition: Fx.Transitions.Cubic.easeInOut
            }
          );
          
          if( active_row )
          {
            var activeFx = new Fx.Styles
            (
              active_row,
              {
                wait: false,
                duration: 400,
                transition: Fx.Transitions.Cubic.easeInOut
              }
            );
            
            boxFx.start
            (
              {
                height: 0,
                opacity: 1,
                'padding-top': row_status[0]
              }
            ).chain
            (
              function()
              {
                box.empty();
                clone.injectInside(box);
                square.injectInside(box);
                lnk.injectInside(box);
              }
            );
            
            activeFx.start
            (
              {
                height: row_status[0]
              }
            ).chain
            (
              function()
              {
                rowFx.start
                (
                  {
                    height: row_status[1]
                  }
                );
                
                var row_top = el.getCoordinates().top;
                
                boxFx.start
                (
                  {
                    top: row_top - box_top,
                    height: row_status[1],
                    'padding-top': 0
                  }
                ).chain
                (
                  function()
                  {
                    cloneFx.start
                    (
                      {
                        'margin-top': 0
                      }
                    );
                    
                    squareFx.start
                    (
                      {
                        'margin-right': square_status[0]
                      }
                    );
                    
                    lnkFx.start
                    (
                      {
                        'margin-top': lnk_status
                      }
                    );
                  }
                );
              }
            );
          }
          else
          {
            rowFx.start
            (
              {
                height: row_status[1]
              }
            );
            
            var row_top = el.getCoordinates().top;
            
            box.setStyles
            (
              {
                top: row_top - box_top
              }
            );
            
            boxFx.start
            (
              {
                opacity: 1
              }
            ).chain
            (
              function()
              {
                box.empty();
                clone.injectInside(box);
                square.injectInside(box);
                lnk.injectInside(box);
                
                cloneFx.start
                (
                  {
                    'margin-top': 0
                  }
                );
                
                squareFx.start
                (
                  {
                    'margin-right': square_status[0]
                  }
                );
                
                lnkFx.start
                (
                  {
                    'margin-top': lnk_status
                  }
                );
              }
            );
          }
          
          active_row = el;
        }
      );
      
      el.addEvent
      (
        'mouseleave', function(e)
        {
          if( timer )
          {
            $clear(timer);
          }
        }
      );
      
      el.addEvent
      (
        'mouseenter', function(ev)
        {
          if( timer )
          {
            $clear(timer);
          }
          
          timer =
          (
            function()
            {
// el.fireEvent('click',ev);
            }
          ).periodical(700);
          
        }
      );
    }
  );
});


// pretty search-machine
window.addEvent('domready', function()
{
  var box = $$('#add-on div.search-box');
  
  var input = box.getElement('input');
  
  var initial_value = input.getProperty('value');
  
  var button = box.getElement('button');
  
  var data;
  
  var result;
  
  var list = $$('div.col ul li a');
  
  input.addEvent
  (
    'click', function()
    {
      if( this.value == initial_value[0] )
      {
        input.setProperty('value','');
      }
    }
  );
  
  input.addEvent
  (
    'keyup', function()
    {
      var value = new RegExp( this.value ,'gi');
      
      list.each
      (
        function(el)
        {
          el.getParent().removeClass('show');
          
          if(! this.value ) return;
          
          data = el.childNodes[0].nodeValue;
          
          result = data.match(value);
          
          if(! result ) return;
          
          el.getParent().addClass('show');
        },
        this
      );

    }
  );
});


// ufo
window.addEvent('domready', function()
{
  if($$('a.animflash').length)
  {
    $$('a.animflash').each
    (
      function(el,i)
      {
        var cont = new Element('div').setProperty('id','anim'+i);
        cont.injectAfter(el);

        // recuperation du href et du rel (largeur, hauteur, wmode, couleur de
		// fond)
  
        var fichier = el;
        var dimensions = el.rel;
      
        var reg=new RegExp("[ ,]+", "g");
        var tableau=dimensions.split(reg);
  
        var largeur = tableau[0];
        var hauteur = tableau[1];
        var wmodeanim = tableau[2];
        var couleurfond/* = tableau[3] */;
        var FO;
        
        var contid = cont.getProperty('id');
        
        el.setStyle('display','none');

        // Paramètres de l'objet flash
        var v_majorversion = '6';    // version minimum du plugin flash
        var v_build = '40';        // sous-version 'build'
        var v_id = 'animf'+i;      // id de l'objet flash
        var v_name = 'video'+i;      // nom de l'objet flash
        var v_swliveconnect = true;    // permet l'interaction avec du
										// javascript
        var v_play = 'true';      // lecture automatique
        var v_loop = 'false';      // lecture en boucle
        var v_menu = 'false';      // menu clic-droit
        var v_quality = 'best';      // qualité : low, high, autolow,
										// autohigh, best
        var v_scale = 'noborder';    // redimension de l'objet : showall,
										// noborder, exactfit
        var v_align;          // alignement de la vidéo au centre d'un des
								// côtés et crop ce qui dépasse : t, r, b, l
        var v_salign;          // alignement de la vidéo : t, r, b, l, tr, tl,
								// br, bl
        var v_wmode = wmodeanim;    // mode d'affichage : window, opaque,
									// transparent
        var v_bgcolor = couleurfond;  // couleur d'arrière plan de l'objet en
										// hexadecimal
        var v_base;            // specifie le repertoire de base des url
								// relatives
        var v_flashvars;        // variables à envoyer à l'objet
        var v_devicefont;        // specifie la police système à utiliser
        var v_allowscriptaccess;    // assure l'execution de scripts propres à
									// l'objets : allways, never, samedomain
        var v_seamlesstabbing;      // permet d'utiliser la touche TAB
									// normalement sur une page
        var v_allowfullscreen = 'true';  // permettre le plein écran
        var v_allownetworking;      // permet à l'objet de communiquer avec le
									// reste de l'environnement : all, internal,
									// none
    
        // Création de l'ufo (unobstrusive flash object)
        FO = {movie: fichier, width: largeur, height: hauteur, majorversion: v_majorversion, build: v_build, id: v_id, name: v_name, swliveconnect: v_swliveconnect, play: v_play, loop: v_loop, menu: v_menu, quality: v_quality, scale: v_scale, align: v_align, salign: v_salign, wmode: v_wmode, bgcolor: v_bgcolor, base: v_base, flashvars: v_flashvars, devicefont: v_devicefont, allowscriptaccess: v_allowscriptaccess, seamlesstabbing: v_seamlesstabbing, allowfullscreen: v_allowfullscreen, allownetworking: v_allownetworking};
        UFO.create(FO, contid );
      }
    );
  }
});


// calendar
window.addEvent('domready', function()
{
  var month = new Date().getMonth();
  var calendar = $$('#monthly-advice ul li');
  calendar.each
  (
    function(el,i)
    {
      if( i < month )
      {
        el.getElement('a').setStyle('cursor','default').setProperty('title','Indisponible');
        el.getElement('a').addEvent
        (
          'click', function(e)
          {
            new Event(e).stop()
          }
        );
      }
      
      if( i == month )  el.getFirst().addClass('present');
      if( i > month )    el.getFirst().addClass('future');
    }
  );
});


// vegediag link block
window.addEvent('domready', function()
{
  var calendar = $$('body.vegediag div.clickable');
  calendar.each
  (
    function(el)
    {
      var a = el.getElement('a');
      var t = a.title;
      
      el.setProperty('title',t);
      
      el.addEvent
      (
        'click', function()
        {
          document.location = a.href;
        }
      );
    }
  );
});


// pretty hider
window.addEvent('domready', function()
{
  var targets = $$('div.masque');
  targets.each
  (
    function(el)
    {
      if( el.getPrevious().hasClass('savoir-plus') )
      {
        var trigger = el.getPrevious();
      }
      
      else if( el.getPrevious().getElement('.savoir-plus') )
      {
        var trigger = el.getPrevious().getElement('.savoir-plus');
      }
      
      else
      {
        return;
      }
      
      var h = el.getCoordinates().height.toInt();
      var bt = el.getStyle('border-top-width').toInt();
      var bb = el.getStyle('border-bottom-width').toInt();
      var pt = el.getStyle('padding-top').toInt();
      var pb = el.getStyle('padding-bottom').toInt();
      var mt = el.getStyle('margin-top').toInt();
      var mb = el.getStyle('margin-bottom').toInt();
      
      var elFx = new Fx.Styles
      (
        el,
        {
          wait: false,
          duration: 400,
          transition: Fx.Transitions.Cubic.easeInOut
        }
      ).set
      ({
        'height': 0,
        'border-top-width': 0,
        'border-bottom-width': 0,
        'padding-top': 0,
        'padding-bottom': 0,
        'margin-top': 0,
        'margin-bottom': 0
      });
      
      trigger.addEvent
      (
        'click', function()
        {
          if( el.getStyle('height').toInt() == 0 )
          {
            elFx.start
            ({
              'height': h - (pt + pb),
              'border-top-width': bt,
              'border-bottom-width': bb,
              'padding-top': pt,
              'padding-bottom': pb,
              'margin-top': mt,
              'margin-bottom': mb
            });
          }
          else
          {
            elFx.start
            ({
              'height': 0,
              'border-top-width': 0,
              'border-bottom-width': 0,
              'padding-top': 0,
              'padding-bottom': 0,
              'margin-top': 0,
              'margin-bottom': 0
            });
          }
        }
      );
      
    }
  );
});


// Add favorites
window.addEvent('domready', function()
{
  if(! $$('a.ajoutfavoris').length) return;

  $$('a.ajoutfavoris').each
  (
    function(el,i)
    {      
      var title = document.title;
      var url = window.location.href;
      
      el.addEvent('click', function(e)
      {
        new Event(e).stop();
        
        if (window.sidebar) // firefox
        {
          window.sidebar.addPanel(title, url, "");
        }
        else if(window.opera && window.print) // opera
        {
          var elem = document.createElement('a');
          elem.setAttribute('href',url);
          elem.setAttribute('title',title);
          elem.setAttribute('rel','sidebar');
          elem.click();
        } 
        else if(document.all) // ie
        {
          window.external.AddFavorite(url, title);
        }
      }
    );
  });

});
