﻿$(function()
{
  // Re-align images with valid XHTML/CSS and apply drop shadow
  formatImages();
});

function formatImages()
{
  $("#content img").each(function(i)
  {
    var img = $(this);

    var $align = img.attr("align");

    // Affects only images with align attr
    switch ($align)
    {
      case "left":
        applyShadow(img, "left");
        break;
      case "right":
        applyShadow(img, "right");
        break;
    }

    img.removeAttr("align");

    if ($align.length > 0)
    {
      if (img.attr("title").length > 0)
      {
        applyCaption(img, img.attr("title"), img.attr("width"));
      }
    }

    function applyCaption(img, caption, width)
    {
      var extraSpace = 20;

      $("<em style=\"width: " + (width + extraSpace) + "px;\">" + caption + "</em>").appendTo(img.parent());
    }

    function applyShadow(img, dir)
    {
      img.wrap("<span class=\"img-shadow img-" + dir + "\"></span>");
    }
  });
}
