var addressBarTest = new Class({
    previous: null,
    loginBox: null,
    wrapper: new Element('div', {
        styles: {
            'position': 'absolute',
            'width': '918px',
            'height': '101px',
            'overflow': 'hidden',
            'display': 'none',
            'background': 'white'
        }
    }),
    
    initialize: function() {
        window.addEvent('domready', this.init.bind(this));
    },
    
    init: function() {
        $$("td[colspan=4]").getParent().removeClass('banner_even').addClass('banner_odd');
        
        if (!$('login-box')) {
            return;
        }
        
        if (Browser.Engine.trident) {
            this.initIe6();
        }
        
        this.createVariableTable();
        
        this.loginBox = $('login-box');
        this.wrapper.adopt(this.loginBox.setStyle('display', 'block'))
                    .inject(document.body);
        
        this.loginBox.getElement('.login-box').setStyles({
            'position': 'absolute',
            'top': 10
        });
        
        $$('.x-button').addEvent('click', this.close.bindWithEvent(this));
        
        $$('.banner_order_link').addEvent('click', this.show.bindWithEvent(this));
    },
    
    initIe6: function() {
        $$('h4 .info').addEvent('mouseenter', function() {
            this.getElement('div').setStyle('display', 'block');
        });
        
        $$('h4 .info').addEvent('mouseleave', function() {
            this.getElement('div').setStyle('display', 'none');
        });
    },
    
    close: function(e) {
        new Fx.Morph(this.wrapper, {
            duration: 350,
            transition: Fx.Transitions.Quad.easeOut,
            onComplete: function() {
                this.element.setStyles({
                    'display': 'none',
                    'margin-top': 2
                });
            }
        }).start({
            'opacity': 0
        });
        $$('tr.selected').removeClass('selected').fade('in');
        this.previous.fade('in');
    },
    
    show: function(e) {
        e.stop();
        
        var query = $(e.target).getParent('a').href.match(/\?.*$/).toString();
        
        var form = this.loginBox.getElement('form');
        
        if (form.action.indexOf('?') === -1) {
            form.action += query;
        } else {
            form.action = form.action.replace(/(\?.*)$/, query);
        }
        
        var table = $(e.target).getParent('tr').addClass('selected');
        
        this.loginBox.getElement('.market-cta').innerHTML = table.getPrevious('tr').getElement('.banner_plan_col').innerHTML.trim();
        
        if (this.previous) {
            this.previous.fade('in').removeClass('selected');
        }
        this.previous = table.fade('out');
        
        var coords = table.getCoordinates();
        
        new Fx.Morph(this.wrapper, {
            duration: 500
        }).start({
            'opacity': [0, 1]
        });
        
        this.wrapper.setStyles({
            'opacity': 0,
            'left': coords.left + 5 + (Browser.Engine.webkit ? 1 : 0),
            'top': coords.top - 19
        });
        
        new Fx.Morph($$('#login-box div.login-box')[0], {
            duration: 500,
            transition: Fx.Transitions.Quad.easeOut
        }).start({
            'right': [-600, -1]
        });
        
        this.wrapper.setStyle('display', 'block');
    },
    
    createVariableTable: function() {
        var heading = $E('h4.fixed-rate');
        var table = $E('table');
        
        var duplicateTable = table.clone()
                                  .inject($E('h4.variable-rate'), 'after');
        
        table.getElements('td.banner_plan_col').forEach(function(row) {
            if (row.innerHTML.indexOf('Fixed') === -1) {
                this.removeRow(row);
            }
        }, this);
        
        duplicateTable.getElements('td.banner_plan_col').forEach(function(row) {
            if (row.innerHTML.indexOf('Fixed') !== -1) {
                this.removeRow(row);
            }
        }, this);
    },
    
    removeRow: function(tr) {
        tr.getParent('tr').getNext('tr').dispose();
        tr.dispose();
    }
});
