/* Class to handle main nav 'hover bubbles' and the login box */
var HoverBubble = new Class({
    initialize: function(target, buttons) {
        this.bubble = $(target);
        this.arrow = $E('#'+target+' .arrow');
        this.header = $E('#'+target+' .bubble h3');
        this.fx = new Fx.Style(this.bubble, 'opacity', {duration: 100, wait: false}).set(0);
        this.buttons = buttons;
        this.bindButtons(this.buttons);
        if ($('log-in-hover') && $('btn-log-in')) {
            this.login = true;
            this.loginBox = $('log-in-hover');
            this.loginBtn = $('btn-log-in');
            this.closeBtn = $('log-in-close');
            this.loginFx = new Fx.Style(this.loginBox, 'opacity', {duration: 200, wait: false}).set(0);
            this.loginBtn.addEvent('click', this.toggleLogin.bind(this));
            this.closeBtn.addEvent('click', this.toggleLogin.bind(this));
        }else{
            this.login = false;
        }
    },
    bindButtons: function(buttons) {
        $each(this.buttons, function (set, key) { 
            var args = [ set.pos, set.hpos, set.text ];
                $('btn-' + key).addEvents({
                        'mouseenter' : this.show.bind(this, args),
                        'mouseleave' : this.hide.bind(this)
                });
        }, this);
    },
    show: function(pos, hpos, text) {
        if (!this.login || (this.login && !this.loginVisible())) {
            this.visible = true;
            this.header.setText(text);
            if (this.bubble.id == 'hover-bubble-front') {
            	this.arrow.setStyle('background-position', pos+'px 0');
            } else {
            	this.arrow.setStyle('margin-left', pos+'px');
            	this.bubble.setStyle('left', hpos+'px');
            }
            this.fx.start(1);
        }
    },
    hide: function(event) {
        this.fx.start(0).chain(function() { this.visible = false }.bind(this));
    },
    toggleLogin: function() {
        if (this.login) {
            if (this.loginVisible()) {
                this.loginFx.start(0);
            }else{
                this.loginFx.start(1);
            }
        }
    },
    loginVisible: function() {
        return this.loginFx.now > 0;
    }
});

HoverBubble.implement(new Events);

function notop() { }