var WxTab= Class.create({
    initialize: function(wContainer, wOptions) {
        this.container= $(wContainer);
        this.options= Object.extend({
            tabSelector: '.tabs li',
            tabContentSelector: '.tab_content',
            currentTab: 'current',
            initialTab: 0
        }, wOptions || {});

        this.tabArray= this.container.select(this.options.tabSelector);
        this.tabContentArray= this.container.select(this.options.tabContentSelector);

        this.tabArray.each(function(tab, index) {
            tab.observe('click', this.activate.bind(this).curry(index));
        }, this);

        this.activate(this.getInitialTabIndex());

        Event.observe(window, 'unload', this.destroy.bind(this));
    },

    destroy: function() {
        this.tabArray.invoke('stopObserving', 'click');
        this.tabArray= this.tabContentArray= this.container= null;
    },

    activate: function(index, event) {
        if (event) {
            Event.stop(event);
        }

        this.tabArray.without(this.tabArray[index]).invoke('removeClassName', this.options.currentTab);
        this.tabArray[index].addClassName(this.options.currentTab);

        this.tabContentArray.without(this.tabContentArray[index]).invoke('hide');
        this.tabContentArray[index].show();
    },

    getInitialTabIndex: function() {
/*
        // Code partially taken from Fabtabulous (http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/)
        if (document.location.href.match(/#(\w.+)/)) {
            var i, len, el;

            var loc= RegExp.$1;
            for (i= 0, len= this.tabArray.length; i < len; i++) {
                el= this.tabArray[i].tagName.toLowerCase() != 'a' ? this.tabArray[i].down('a') : el;

                if (el && el.href && el.href.match(/#(\w.+)/)[1] == loc) {
                    return i;
                }
            }
        }
*/
        if (this.options.initialTab == -1) {
            return Math.round(Math.random() * (this.tabArray.length - 1));
        }

        return this.options.initialTab;
    }
} );
