var TAC_MenuBubble              = new Class({
    timer: null,

    initialize: function(content) {
        this.content            = {};
        $H(content).each(function(el, key) {
            var html            = '';
            if (el.pages.length) {
                for (var loop = 0; loop < el.pages.length; loop++) {
                    html        += '<div class="menu"><a href="' + el.pages[loop].url + '">' + el.pages[loop].title + '</a></div>';
                }
                this.content[key] = html;
            }
        }, this);
        this.bubble             = new Element('div', {
            'id': 'nav-bubble',
            'styles': {
                'overflow': 'hidden'
            },
            'events': {
                'mouseover': this.over.bind(this, null),
                'mouseout': this.out.bind(this, null)
            }
        })
            .inject($('content'));
        this.text               = new Element('div')
            .inject(this.bubble);
        this.bubble.set('morph', {transition: Fx.Transitions.Circ.easeOut, duration: 300});
        this.bubble.get('morph').set({
            'height': 1,
            'top': 11
        });
        this.bubble.get('tween').set('opacity', 0);
        this.menus              = $('nav').getElements('a');
        this.menus.each(function(menu) {
            menu.addEvent('mouseover', this.over.bind(this, [menu]));
            menu.addEvent('mouseout', this.out.bind(this, [menu]));
        }, this);
    },

    over: function(menu) {
        if ($(menu)) {
            var id                  = menu.get('id').replace(/menu\-/, '');
            if (this.content[id]) {
                $clear(this.timer);
                this.text.set('html', this.content[id]);
                this.text.getElements('a').each(function(a) {
                    a.addEvent('mouseover', function() {
                        this.setStyles({ 'color': '#ffffff', 'background-color': '#333333' });
                    });
                    a.addEvent('mouseout', function() {
                        this.setStyles({ 'color': '#aaaaaa', 'background-color': '#000000' });
                    });
                });
                var h               = this.text.getCoordinates().height;
                var y               = menu.getCoordinates().top - (h / 2);
                if (y < 11) {
                    y               = 11;
                }
                this.bubble.get('tween').start('opacity', 0.9);
                this.bubble.get('morph').start({
                    'height': h,
                    'top': y
                });
            }
        } else {
            $clear(this.timer);
        }
    },

    out: function(menu) {
        if ($(menu)) {
            var id                  = menu.get('id').replace(/menu\-/, '');
            if (this.content[id]) {
                this.timer          = this._out.delay(1500, this, [menu]);
            } else {
                this._out(menu);
            }
        } else {
            this.timer              = this._out.delay(1000, this, [menu]);
        }
    },

    _out: function(menu) {
        this.bubble.get('tween').start('opacity', 0);
    }
});

var TAC_WineItem                = new Class({

    orderTotal: null,

    initialize: function() {
        this.orderTotal         = $('wine_total');
        this.els                = $$('.wine-item');
        this.els.each(function(el) {
            var wineId          = el.get('id');
            var winePrice       = $(wineId + '-price');
            var wineTotal       = $(wineId + '-total');
            el.store('price', winePrice.get('text'));
            if (winePrice && wineTotal && this.orderTotal) {
                el.addEvent('keyup', function() {
                    this.refresh();
                }.bind(this));
            }
        }, this);
        this.refresh();
    },

    refresh: function (e) {
        var total               = 0;
        this.els.each(function(el) {
            var wineId          = el.get('id');
            var winePrice       = $(wineId + '-price');
            var wineTotal       = $(wineId + '-total');
            var subtotal            = winePrice.get('text') * el.get('value');
            if (subtotal > 0) {
                wineTotal.set('text', '$' + subtotal.toFixed(2));
            } else {
                wineTotal.set('text', '');
            }
            total               += el.get('value') * el.retrieve('price');
        }, this);
        this.orderTotal.set('value', '$' + total.toFixed(2));
    }
});


window.addEvent('domready', function() {
    $$('a.external').each(function(a) {
        a.set('target', '_blank');
    });
    $$('a.vr').each(function(el) {
        el.addEvent('click', function() {
            target = 'vr';
            link = this.href;
            scrx = screen.width;
            scry = screen.height;
            width = 640;
            height = 495;
            winX = (scrx/2)-(width/2);
            winY = (scry/2)-(height/2);
            enlargementWin = open(link, target, 'directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no,scrollbars=no,width='+width+',height='+height+',left='+winX+',top='+winY+',screenX='+winX+',screenY='+winY+',resizeable=no');
            enlargementWin.moveTo(winX,winY);
            enlargementWin.focus();
            return false;
        });
    });
    $$('a.reveal').each(function(el) {
        var tgt                 = el.get('href');
        if (tgt.indexOf('#') !== -1) {
            tgt                 = $(tgt.substring(tgt.indexOf('#') + 1));
            if (tgt) {
                el.addEvent('click', function(e) {
                    e           = new Event(e);
                    e.stop();
                    tgt.get('tween').start('opacity', 1);
                });
                tgt.removeClass('hidden');
                tgt.setStyles({
                    display: 'block',
                    opacity: 0
                });
            }
        }
    });
});

