$(document).ready(function(){
  //************************************************
  // PREFERENCE
  current_player = '';
  $(".reference-item .samples a").bind('click', function(elt){
    if (current_player !=''){
      $(current_player).html('');
    }
    current_player = $(this).parent().parent().parent().find('.player');
    $(current_player).flash(
      { src: '/assets/flash/player.swf', width: 300, height: 19, flashvars: { file: $(this).attr('href'), playlist: 'none', dock: 'none', icons: 'false', autostart: 'true' } },
      { version: 9 }
    )
    return false;
  })

  //************************************************
  // PHOTO GALLERY
  current = 0;
  visible_items = 8;
  wrapper_width = 595;
  
  nb_images = $("#gallery-navigation ul li").size();
  
  $("#gallery-navigation ul li a").each(function(item)
  {
    $(this).bind('click', function()
    {
      $('#image-'+current).removeClass('current');
      
      current = parseInt($(this).attr('id').substr(6));
      
      // Move thumbnail list position if necessary
      // Current position is superior to visible_items
      left_position = 0;
      if (current > visible_items)
      {
        left_position = (visible_items-current)*75;
      } else {
        left_position = 0;
      }
      $("#gallery-navigation ul").animate({left: left_position});

      $(this).addClass('current');
      
      $('#gallery-detail img').attr("src", $(this).attr('href'));
      galerie_title = $(this).find('img').attr('alt') !=" " ? $(this).find('img').attr('alt') : "&nbsp;"
      $('#gallery-title').html(galerie_title);
      
      prev = (current-1>0 ? current-1 : 1);
      next = (current+1<=nb_images ? current+1 : nb_images);

      return false;
    })
  })
  $('#gallery-detail .prev').bind('click', function(){
    $('#image-'+prev).click();
    return false;
  });
  $('#gallery-navigation .prev').bind('click', function(){
    $('#image-'+prev).click();
    return false;
  });
  $('#gallery-detail .next').bind('click', function(){
    $('#image-'+next).click();
    return false;
  });
  $('#gallery-navigation .next').bind('click', function(){
    $('#image-'+next).click();
    return false;
  });

  //  By default, image 1 is loaded
  $("#image-1").click();
  
  // Remove Submit button and add onChange event on select box
  // Change Photo Select look and feel

  $('#gallery-filter button').css('display', 'none');
  $('#gallery-filter select').bind('change', function(){
    $('#gallery-filter').submit();
  });
  $('#gallery-filter').jqTransform({imgPath:'/assets/js/jqtransformplugin/img/'});
  
  
  //************************************************
  // HARDWARE TOOLTIP
  $('.tooltip').tooltip({ 
    delay: 0, 
    showURL: false, 
    bodyHandler: function() { 
        return $("<img/>").attr("src", this.href); 
    } 
  });
  
});

