﻿/// <reference path="jquery-1.5-vsdoc.js" />
$(document).ready(function () {
    //classname of the element, the sum of left- AND rightmargin the element has
    $.fn.fitContentsp = function (classname, sumMarginlr) {
        var totalWidth = $(this).width();
        var $tabs = $(this).children(classname);
        var totalMarginlr = sumMarginlr * $tabs.length;
        var totalWidthNoMargin = totalWidth - totalMarginlr
        var w_ideal = Math.floor(totalWidthNoMargin / $tabs.length);
        var usedWidth = 0;
        var toSmallCount = 0;
        var i = 0;

        $tabs.each(function () {
            $(this).css('width', w_ideal);
            var ow = $(this).width();
            if (ow > w_ideal) {
                usedWidth += ow
            } else toSmallCount += 1;

            if (i == 0) {
                $(this).addClass('left');
            } else if (i > 0 && i < $tabs.length - 1) {
                $(this).addClass('middle');
            } else $(this).addClass('right');

            i++;
        });

        var w = Math.floor((totalWidthNoMargin - usedWidth) / toSmallCount)

        if (w > 0) {
            $tabs.each(function () {
                var ow = $(this).outerWidth();
                if (ow <= w_ideal) {
                    $(this).width(w);
                    usedWidth += w
                };
            });
        };

        var spare = (totalWidthNoMargin - usedWidth);
        if (spare > 0) {
            $tabs.each(function () {
                if (spare > 0) {
                    $(this).width($(this).width() + 1);
                    spare -= 1;
                };
            });
        };
    };

    $.fn.equalizeDivColumnsSP = function (classname) {
        var $container = $(this);
        var maxHeight = $container.height();
        var maxWidth = $container.width();

        setHeightOfChildrenDivs($container);

        function setHeightOfChildrenDivs($container) {
            var w = $(classname, $(this)).width();
            $container.children('div').each(function () {

                var bottomWidth = 0;
                $(classname, $(this)).height(maxHeight);


                //recursive for direct children divs
                $('div.topblock-bottom').each(function () {
                    bottomWidth = $(this).width();
                    $(this).width(bottomWidth);
                    $(this).height(maxHeight);
                    $(this).css('padding', '0 15px !important');

                });
            });
        };
    };
});
