﻿/// <reference path="jquery-1.3.2.min-vsdoc.js" />


// this function should be called in $(window).load() and not the regular $() or $(documnet).load() because it needs to be run after the images have loaded
(function($) {
    $.fn.imageCaptions = function() {
        return $(this, 'img').each(function() {
            var caption = $(this).attr("title");
            if (caption != null && caption != "") {
                $(this).before('<span class="imageCaption"></span>');
                var container = $(this).prev("span");

                //transfer layout related styles
                $(this).transferStyleTo(container, "float");
                $(this).transferStyleTo(container, "position");
                $(this).transferStyleTo(container, "clear");
                $(this).transferStyleTo(container, "left");
                $(this).transferStyleTo(container, "right");
                $(this).transferStyleTo(container, "top");
                $(this).transferStyleTo(container, "bottom");
                //doesnt work in chrome need to use specific sides
                //$(this).transferStyleTo(container, "margin");
                $(this).transferStyleTo(container, "margin-left");
                $(this).transferStyleTo(container, "margin-right");
                $(this).transferStyleTo(container, "margin-top");
                $(this).transferStyleTo(container, "margin-bottom");

                container.css("display", "inline-block");
                container.width($(this).width());
                container.append(this);
                $(this).css("display", "block");
                container.append("<span>" + caption + "</span>");
            }
        });
    }
})(jQuery);

(function($) {
    $.fn.transferStyleTo = function(element, propertyName) {
        return $(this).each(function() {
            var sProperty = $(this).css(propertyName);
            if (sProperty != null && sProperty != "")
                $(element).css(propertyName, sProperty);

            $(this).css(propertyName, "");
        });
    }
})(jQuery);