/* ----------------------------------------------------------------------------------------------------

   TabbedPanelGroup

---------------------------------------------------------------------------------------------------- */

Type.registerNamespace("uSwitch.Web.UI.WebControls");

// Constructor
uSwitch.Web.UI.WebControls.TabbedPanelGroup = function(element) {
    uSwitch.Web.UI.WebControls.TabbedPanelGroup.initializeBase(this, [element]);
}

uSwitch.Web.UI.WebControls.TabbedPanelGroup.prototype = {

    initialize: function() {
        uSwitch.Web.UI.WebControls.TabbedPanelGroup.callBaseMethod(this, 'initialize');

        var element = $(this.get_element());
        var tabbedarea = $(element.children(".panels"));

        var lis = element.find(".tabs ul").children("li");
        var divs = tabbedarea.find("div[class^='us-tabbed-panel']");

        var tabs = new Array();
        var buttons = new Array();
        var panels = new Array();

        lis.each(function(i) {
            tabs[i] = $(lis[i]);
            var butt = tabs[i].children("a")[0];
            
            if (typeof(butt) !== "undefined") {
                buttons[i] = $(butt);
            }
        });

        divs.each(function(i) {
            panels[i] = $(divs[i]);
        });

        buttons = $(buttons);
        panels = $(panels);
        tabs = $(tabs);

        buttons.each(function(i) {
            buttons[i].click(function() {
                var showEle = panels[i];
                var hideEle = null;

                panels.each(function() {
                    if (this.hasClass("visible")) {
                        hideEle = this;
                    }
                });

                if (showEle == hideEle || showEle == null || hideEle == null)
                    return false;

                hideEle.addClass("hidden").removeClass("visible");
                showEle.addClass("visible").removeClass("hidden");

                tabs.each(function(x) {
                    if (x == i)
                        tabs[x].addClass("on");
                    else
                        tabs[x].removeClass("on");
                });

                tabs[i].blur();
                buttons[i].blur();

                return false;
            });
        });
    },

    // Release resources before control is disposed.
    dispose: function() {
        var element = this.get_element();

        uSwitch.Web.UI.WebControls.TabbedPanelGroup.callBaseMethod(this, 'dispose');
    }
}
uSwitch.Web.UI.WebControls.TabbedPanelGroup.registerClass('uSwitch.Web.UI.WebControls.TabbedPanelGroup', Sys.UI.Control);
if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

