$(document).ready( function() {
  /**
   * Preview para imagenes
   */
  loadPreviewImgs();  
  loadEmptyImgs();

  
});

function loadEmptyImgs() {  
  $('td.thumb:not(:has(>img))').each( function() {
    $(this).html("<img src='"+ noImageThumb +"'/>");
  });
}

/**
 * Carga los event handlers para los preview
 */
function loadPreviewImgs() {  
  
  $('td.thumb > img').each( function() {    
     
    // Error Handler
    $(this).error( function() {
      $(this).attr('src', defThumb);
    });
    
    
    // Chapu FF para capturar el onError
    src = $(this).attr('src');
    $(this).attr('src', '');
    $(this).attr('src', src);

    $(this).mouseover( function() {
    
      id     = $(this).attr('id');
      prevId = 'prev-' + id;
      imgId  = 'img-' + prevId;

      // No muestra preview para errores
      if ($(this).attr('src') == defThumb) {
        $(this).attr('src', defThumb);
        /*return;*/
      }
      if ($("#" + prevId).length == 0) {
        
        path   = $(this).attr('prev');
        
        $("#" + id).before('<div class="prevImg loadingImg" id="' + prevId + '"></div>');
       
        $("<img src='" + path + "' id='" + imgId + "' />")
        .hide()
        .load( function() {
          $("#" + prevId).removeClass('loadingImg');
          $("#" + prevId).addClass('loaded');
          $(this).fadeIn("slow");
          $(this).mouseout( function() {
            $(this).fadeOut("slow");
            if ($("#" + prevId).hasClass('loadingImg')) {
              $("#" + prevId).removeClass('loadingImg');
            }
          });
        }).appendTo("#" + prevId);

      } else {
        if ($("#" + prevId).hasClass('loadingImg')) {
          $("#" + prevId).removeClass('loadingImg');
        }
        $("#" + imgId).fadeIn("slow");
      }
    });
  });
}

