/*
 * Fabtabulous! Simple tabs using Prototype
 * http://tetlaw.id.au/view/blog/fabtabulous-simple-tabs-using-prototype/
 * Andrew Tetlaw
 * version 2 2008-08-10
 * http://creativecommons.org/licenses/by-sa/2.5/
 */
var Fabtabs = Class.create({

    unset: function()
    {
        if (typeof this.timer != 'undefined' && this.timer) {
            clearInterval(this.timer);
        }
    },

    initialize : function(element,options) {

        var parent = this.element = $(element);
        var self   = this;

        if (!this.element) {
            return;
        }

        this.options = Object.extend({
            hover: true,
            remotehover: false,
            anchorpolicy: 'allow-initial' // 'protect', 'allow', 'allow initial', 'disable'
        }, options || {});

        this.menu = this.element.select('a');

        this.hrefs = this.menu.map(function(elm){
            return elm.href.match(/#(\w.+)/) ? RegExp.$1 : null;
        }).compact();


        var hrefs = this.hrefs;
        self.currentTab = 0;
        self.currentElement = this.getInitialTab();

        var timer, hover = false;

        function startTimer() {

            timer = window.setInterval(function(){

                if (hover) {
                    return;
                }

                self.off(self.currentElement);

                self.currentTab++;

                if (self.currentTab >= hrefs.length) {
                    self.currentTab = 0;
                }

                self.currentElement = self.element.select('a[href="#'+ hrefs[self.currentTab] +'"]')[0];
                self.on(self.currentElement);

            }, 3000);
        }

        startTimer();

        this.on(self.currentElement);

        var onMouseover = function(event) {
            hover = true;
        };

        var onMouseout = function(event) {
            hover = false;
        };

        var onLocal = function(event) {
            onMouseover(event);
            if(this.options.anchorpolicy !== 'allow'){
                event.stop();
            }
            var elm = event.findElement("a");
            this.activate(elm);
            if(this.options.anchorpolicy === 'protect') {
                window.location.hash = '.'+this.tabID(elm);
            }
        };

        var onRemote = function(event) {
            if(this.options.anchorpolicy !== 'allow'){
                event.stop();
            }
            var trig = event.findElement("a");
            this.activate(this.tabID(trig));
            if(this.options.anchorpolicy === 'protect') {
                window.location.hash = '.'+this.tabID(elm);
            }
        };

        this.element.observe('click', onLocal.bindAsEventListener(this));

        if(this.options.hover) {
            this.menu.each(function(elm){
                elm.observe('mouseover', onLocal.bindAsEventListener(this));
                elm.observe('mouseout', onMouseout.bindAsEventListener(this));
            }.bind(this));
        }

        var triggers = [];


        this.hrefs.each(function(id){
            $(id).onmouseover = onMouseover;
            $(id).onmouseout = onMouseout;

            $$('a[href="#' + id + '"]').reject(function(elm){
                return elm.descendantOf(parent);
            }).each(function(trig){
                triggers.push(trig);
            });
        });

        triggers.each(function(elm){
            elm.observe('click', onRemote.bindAsEventListener(this));
            if(this.options.remotehover) {
                elm.observe('mouseover', onRemote.bindAsEventListener(this));
            }
        }.bind(this));

    },
    activate: function(elm) {
        this.off(this.currentElement);
        self.currentTab = parseInt(this.tabID(elm).replace('tab', '')) - 1;

        if(typeof elm == 'string') {
            elm = this.element.select('a[href="#'+ elm +'"]')[0];
        }
        this.currentElement = elm;

        this.on(elm);
        this.menu.without(elm).each(this.off.bind(this));
    },
    off: function(elm) {
        $(elm).removeClassName('active-tab');
        $(this.tabID(elm)).removeClassName('active-tab-body');
    },
    on: function(elm) {
        $(elm).addClassName('active-tab');
        $(this.tabID(elm)).addClassName('active-tab-body');
    },
    tabID: function(elm) {
        return elm.href.match(this.re)[1];
    },
    getInitialTab: function() {
        if(this.options.anchorpolicy !== 'disable' && document.location.href.match(this.re)) {
            var hash = RegExp.$1;
            if(hash.substring(0,1) == "."){
                hash = hash.substring(1);
            }
            return this.element.select('a[href="#'+ hash +'"]')[0];
        } else {
            return this.menu.first();
        }
    },
    re: /#(\.?\w.+)/
});
