/**
* @author andmas
*/
function tabToggle() {
    var clicked = this;
    var parentDiv = $(clicked).parent().parent().parent().parent();

    $(parentDiv).find("div.tab").each(function(i) {
        $(parentDiv).find("a.tab" + (i + 1)).removeClass("selected");
        if (clicked.className == "tab" + (i + 1)) {
            this.style.display = "block";
            $(parentDiv).find("a." + clicked.className).addClass("selected");
        } else {
            this.style.display = "none";
        }
    });
    return false;
}

$("div.tabbed").ready(function() {
    $("div.tabbed").each(function(i) {
        var tabContainer = this;
        var tabHeight = 0;
        var tabButtonHeight = 0;
        var oTabs = $(tabContainer).children("div.tab");

        $(tabContainer).find("div.tabs").append("<ul class=\"clearfix\"></ul>");

        // for each tab
        $(tabContainer).children("div.tab").each(function(k) {
            var tab = this;
            var thisTabHeight = $(tab).height();
            var thisTbContentHeight

            tabHeight = thisTabHeight < tabHeight ? tabHeight : thisTabHeight;

            var hFour = $(tab).find("h4");

            $(tabContainer).find("div.tabs").find("ul").append("<li><a href=\"#\" class=\"tab" + (k + 1) + "\">" + $(hFour).html() + "</a></li>");
            $(tabContainer).find("div.tabs ul li a.tab1").addClass("selected");
            $(tabContainer).find("a.tab" + (k + 1)).bind("click", tabToggle)
            $(hFour)[0].style.display = "none";
            if (k > 0) {
                $(tab)[0].style.display = "none";
            }

            if (hasXHR) {
                $(this).css("min-height", tabHeight + "px");
            }
            else {
                $(this).css("height", tabHeight + "px");
            }
        });

        $("div.tabbed div.tabs ul li a").each(function() {
            var tabButton = this;
            var thisTabButtonHeight = $(tabButton).height();
            tabButtonHeight = thisTabButtonHeight < tabButtonHeight ? tabButtonHeight : thisTabButtonHeight;
        });

        $("div.tabbed div.tabs ul li a").each(function() {
            if (hasXHR) {
                $(this).css("min-height", tabButtonHeight + "px");
            }
            else {
                $(this).css("height", tabButtonHeight + "px");

            }
        });
    });
});