﻿jQuery(window).load(function () {
   InitializeAllImages();
});

function InitializeAllImages() {
   jQuery("img.hascaption").each(function () {
      if (jQuery(this).attr("alt")) {
         jQuery(this).wrap('<div class="figure"></div>')

         /* Margin-Top off the image has to be subtracted from the bottom        */
         /* in order to close the gap between the caption and the image          */
         .after('<p class="caption" style="bottom: -' + jQuery(this).css("margin-top") + '">' + jQuery(this).attr("alt") + '</p>')

         .removeAttr('alt');
      }
   });

   /* 2011-11-16, Alex          */
   /* Initialize the figure div */
   jQuery(".figure").width(function () { return jQuery(this).find('img').width(); });
   jQuery(".figure").height(function () { return jQuery(this).find('img').height(); });
   jQuery(".figure").css('float', function () { return jQuery(this).find('img').css('float'); });
   jQuery(".figure").css('margin-right', function () { return jQuery(this).find('img').css('margin-right'); });

   /* 2011-11-16, Alex                      */
   /* Initialize eventhandler for animation */
   jQuery(".figure")
      .mouseenter(function () { jQuery(this).find('.caption').slideToggle(); })
      .mouseleave(function () { jQuery(this).find('.caption').slideToggle(); });
}

function AddAnimatedCaption(img) {
   if (jQuery(img).attr("alt")) {
      jQuery(img).wrap('<div class="figure"></div>')

      /* Margin-Top off the image has to be subtracted from the bottom        */
      /* in order to close the gap between the caption and the image          */
      .after('<p class="caption" style="bottom: -' + jQuery(img).css("margin-top") + '">' + jQuery(img).attr("alt") + '</p>')

      .removeAttr('alt');

      jQuery(img).parent().width(function () { return jQuery(this).find('img').width(); });
      jQuery(img).parent().height(function () { return jQuery(this).find('img').height(); });
      jQuery(img).parent().css('float', function () { return jQuery(this).find('img').css('float'); });
      jQuery(img).parent().css('margin-right', function () { return jQuery(this).find('img').css('margin-right'); });

      jQuery(img).parent()
         .mouseenter(function () { jQuery(this).find('.caption').slideToggle(); })
         .mouseleave(function () { jQuery(this).find('.caption').slideToggle(); });
   }
}                                                                                                                                   
