﻿/*
This script finds all images containing an alt tag with content in it and turns the alt tag into a caption.
*/

$(document).ready(function () {
    //get images reference, all images that have an alt for the caption
    var images = $("img[alt!=\"\"]");

    //wrap each selected image in a wrapper
    $(images).wrap("<div class=\"imagewrapper\"></div>");

    $(".imagewrapper").each(function (i) {
        //get width, clean it, add a few px to it
        var rawWidth = $(this).children("img").css("width");
        var width = parseInt(rawWidth.substring(0, rawWidth.length - 2)) + parseInt(2);

        //create caption and align elements
        $(this)
            .append(
                $("<div></div>")
                    .addClass("imagecaption")
                    .css("float", $(this).children("img").css("float"))
                    .text($(this).children("img").attr("alt"))
            )
            .css("float", $(this).children("img").css("float"))
            .css("width", width + "px");
    });
});
