﻿(function ($) {
    $.fn.tabs_setup = function (variables) {
        return this.each(function () {
            RegisterTabs(variables);
        });
    };
})(jQuery);

function RegisterTabs(variables) {
    $(variables.tab).append("<div id=\"tab_content\" class=\"" + variables.tab_content_css + "\"></div>");
    variables.activeColor = $("." + variables.tab_content_css).css("background-color");
    variables.inactiveColor = $("." + variables.tab_content_css).css("border-top-color");
    var children = $("li", variables.tab);
    children.each(function () {
        var obj = $($(this).find(":first").find(":first").attr("href"));
        obj.css({ display: "none" });
        var html = obj.clone();
        obj.remove();
        $("#tab_content", variables.tab).append(html);
        $(this).click(function () {
            children.each(function () {
                $(this).find(":first").find(":first").css({ backgroundColor: variables.inactiveColor });
                $(this).find(":first").find(":last").css({ backgroundColor: variables.inactiveColor });
            });
            $("#tab_content", variables.tab).children().css({ display: "none" });
            $($(this).find(":first").find(":first").attr("href")).css({ display: "block" });
            $(this).find(":first").find(":first").css({ backgroundColor: variables.activeColor });
            $(this).find(":first").find(":last").css({ backgroundColor: variables.activeColor });
            return false;
        });
    });
    if (children.length > 0) {
        $("#tab_content", variables.tab).find(":first").css({ display: "block" });
        var bgColor = $("." + variables.tab_content_css).css("background-color");
        var marginTopWidth = $("." + variables.tab_content_css).css('border-top-width');
        if (marginTopWidth === "0px") {
            marginTopWidth = "1px";
        }
        var o = $("." + variables.tab_content_css);
        children.each(function () {
            AddExtraBottomBorderToTabs($(this), bgColor, marginTopWidth);
            $(this).find(":first").find(":first").css({ backgroundColor: variables.inactiveColor, borderColor: variables.inactiveColor });
            $(this).find(":first").find(":last").css({ backgroundColor: variables.inactiveColor });
        });
        $(children[0]).find(":first").find(":first").css({ backgroundColor: variables.activeColor });
        $(children[0]).find(":first").find(":last").css({ backgroundColor: variables.activeColor });
    }
}

function AddExtraBottomBorderToTabs(tab, bgColor, marginTopWidth) {
    var width = tab.width() - 2;
        tab.append("<div class=\"testing_tab\" style=\"width:"+width+"px;background-color:"+bgColor+";height:"+marginTopWidth+";\"></div>");
}