(function($)
{
  $.fn.menu_png_hover = function( options )
  {
    var settings = $.extend({
      template_path: '/img/orchidee-vitale/'
    }, options);

    return this.each( function()
    {
      if ( ! $(this).hasClass( 'active' ) ) 
      {
        var img = $( 'img', $(this) ) ;
        img.template_path = settings.template_path ;

        $(this).hover(
          function()
          {
            var src = settings.template_path + img.attr( 'id' ) + '-hover.png' ;
            img.attr( 'src', src ) ; 
          },

          function()
          {
            var src = settings.template_path + img.attr( 'id' ) + '.png' ;
            img.attr( 'src', src ) ; 
          }
        );
      }
    });
  }

  $(function()
  {
    var template = "/img/orchidee-vitale/"  ;

    // hover for sidebar
    $( '#menu-left ul a' ).menu_png_hover() ;
    $( 'ul.science-bloc01-content a' ).menu_png_hover( {template_path: template  + "power-plants/" } ) ;
    $( 'ul.science-bloc02-content a' ).menu_png_hover( {template_path: template  + "power-plants/" } ) ;
    $( 'ul.science-bloc03-content a' ).menu_png_hover( {template_path: template  + "power-plants/" } ) ;

    // preload hover images
    $( 'body' ).append( '<div id="sidebar_preloader" style="display:none;"></div>' ) ;
    $( '#menu-left ul a' ).each( function()
    {
      var img = $( 'img', this ) ;
      img.each( function(){
        var src = $(this).template_path + $(this).attr( 'id' ) + '-hover.png' ;
        $( '#sidebar_preloader' ).append( '<img src="' + src + '" />' ) ;
      });
    }) ;
    $( '#sidebar_preloader' ).remove() ;

    // change the bg of active
    var active = $( '#menu-left a.active img', $(this) ) ;
    var src = template + active.attr( 'id' ) + '-hover.png' ;
    active.attr( 'src', src ) ; 

  });
})(jQuery);
