﻿/// <reference path="jquery-1.5.2.min.js" />
(function ($) {
    //classname of the element
    $.fn.fitContentsp = function (classname) {
        var blockWidth = 220;
        var imgHeight = 150;
        var totalWidth = $(this).width();
        var $tabs = $(this).children(classname);
        var usedWidth = 0;
        var i = 0;

        var unusedWidth = (totalWidth - (blockWidth * $tabs.length));
        var marginToUse = Math.floor(unusedWidth / ($tabs.length - 1));

        $tabs.each(function () {
            $(this).css('width', blockWidth);
            usedWidth += blockWidth;
            if (i == 0) {
                $(this).addClass('left');
                $(this).css('margin-right', marginToUse)
            } else if (i > 0 && i < $tabs.length - 1) {
                $(this).addClass('middle');
                $(this).css('margin-right', marginToUse)
            } else {
                $(this).addClass('right');
                $(this).css('margin', '0')
            }
            i++;
        });

        var spare = (totalWidth - (usedWidth + unusedWidth));
        if (spare > 0) {
            $tabs.each(function () {
                if (spare > 0) {
                    $(this).width($(this).width() + 1);
                    spare -= 1;
                };
            });
        };
    };
})(jQuery);
