﻿document.observe("dom:loaded", function() {
    var tabs = $("villa-tabs");
    var sections = $("villa-tab-sections");

    if (tabs || sections) {
        tabs.select("li").each(function(tab, index) {
            tab.observe("click", function(e) {
                tabs.select(".open").invoke("addClassName", "closed").invoke("removeClassName", "open");
                Event.element(e).removeClassName("closed").addClassName("open");
                sections.childElements().invoke("hide");
                sections.childElements()[index].show();
            });
        });
    }

    setTabSelected = function(index) {
        var tabs = $("villa-tabs");
        var sections = $("villa-tab-sections");
        if (tabs.select("li")[index]) {
            tabs.select(".open").invoke("addClassName", "closed").invoke("removeClassName", "open");
            tabs.select("li")[index].removeClassName("closed").addClassName("open");
            sections.childElements().invoke("hide");
            sections.childElements()[index].show();
        }
    }
});
