$(document).ready(function() {
  /* Visuels */
  $('#visuels #thumbs img').mouseover(function(){
    attr = $(this).attr('target');
    $('#visuels #visuel').fadeOut('fast',function(){
      $(this).attr('src', attr);
      $(this).fadeIn('fast');
    });
  });
  
  
  /* PopIn, */
  
  // Show
  var showSwitch = '';
  $('.nm_closeClickOut').click(function(){
    showSwitch = $(this).attr('id');
  });
  
  $('body').click(function() {
    $('.nm_closeClickOut').each(function(){
      if($(this).attr('id') != showSwitch )
      {
        $(this).addClass('hide');
        $(this).css('display','none');
      }else{
        showSwitch = '';
      }
    });
  });
  
  $('.nm_showSwitch').click(function() {
    $(this).css('outline','none');
    var i = '1';
    while($(this).attr('target' + i))
    {
      showSwitch = $(this).attr('target' + i);
      var id = '#' + $(this).attr('target' + i);
      var mySwitch = $(this);
      if (!$(id).hasClass("hide")) {
        $(id).addClass('hide');
        $(id).css('display','none');
      }else{
        $(id).removeClass('hide');
        $(id).css('display','block');
      }
      i++;
    }
  });
  $('.showOnOver').mouseover(function() {
    $(this).css('outline','none');
    var i = '1';
    while($(this).attr('target' + i))
    {
      showSwitch = $(this).attr('target' + i);
      var id = '#' + $(this).attr('target' + i);
      var mySwitch = $(this);
      $(id).removeClass('hide');
      $(id).css('display','block');
      i++;
    }
  });

  $('.showOnOver').mouseout(function() {
    $(this).css('outline','none');
    var i = '1';
    while($(this).attr('target' + i))
    {
      showSwitch = $(this).attr('target' + i);
      var id = '#' + $(this).attr('target' + i);
      var mySwitch = $(this);
      $(id).addClass('hide');
      $(id).css('display','none');
      i++;
    }
  });

  // roll over
  $('.btnOnOff').mouseover(function() {
    src = $(this).attr('src');
    var regGif = new  RegExp("(.gif)", "g");
    var regJpg = new  RegExp("(.jpg)", "g");
    var regPng = new  RegExp("(.png)", "g");
    src = src.replace(regGif, '_on.gif');
    src = src.replace(regJpg, '_on.jpg');
    src = src.replace(regPng, '_on.png');
    $(this).attr('src', src);
  });
  // roll over
  $('.btnOnOff').mouseout(function() {
    src = $(this).attr('src');
    var regGif = new  RegExp("(_on.gif)", "g");
    var regJpg = new  RegExp("(_on.jpg)", "g");
    var regPng = new  RegExp("(_on.png)", "g");
    src = src.replace(regGif, '.gif');
    src = src.replace(regJpg, '.jpg');
    src = src.replace(regPng, '.png');
    $(this).attr('src', src);
  });



  //formulaire
  $('#BateauLongueur').change(function (){
    var Options = $('option', this);
    var nbOptions = Options.length;
    var value = $('option:selected', this).val();
    if(value == Options[nbOptions-1].innerHTML){
      $('.BateauLongueur1').show();
      $('.BateauLongueur1 > input').attr('value','préciser la longueur');
    }
  });
});


function changeBg(myBgs, index, max)
{
  //$('#bg').css('background-image','url(' + myBgs[index] + ')');
  if($('#bg' + index).length < 1)
  {
    $('#bgImg').append('<img src="' + myBgs[index] + '" id="bg' + index + '">');
  }
  $('#bg' + index).fadeIn('slow', function(){
    if(index == 0)
    {
      $('#bg' + (max-1)).fadeOut();
    }else{
      $('#bg' + (index-1)).fadeOut();
    }
    if(index < max-1)
    {
      index++;
    }
    else
    {
      index = 0;
    }
  });
  window.setTimeout(function(){
    changeBg(myBgs, index, max);
  }, 6000);
}
function changeBg1(myBgs, index, max)
{
  if(index == 0)
  {
    $('#bg').css('background-image','url(' + myBgs[index] + ')');
  }
  else{
    $('#bg').fadeOut(function(){
      $(this).css('background-image','url(' + myBgs[index] + ')').fadeIn();
    });
  }
  if(index < max-1)
    index++;
  else
    index = 0;
  window.setTimeout(function(){
    changeBg(myBgs, index, max);
  }, 6000);
}
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)
