var userMaskd = true;
var passMaskd = true;
var overMenu = false;

window.addEvents({
	domready : function() {
		this.fireEvent('mask','hide',500);
		
		var roImages = new Asset.images([
			'/img/topbar_start_over.gif',				
			'/img/topbar_bg_over.gif',
			'/img/agentlogin_over.png',
			'/img/logout.png',
			'/img/faqs_over.png',
			'/img/providers_over.png',
			'/img/businesstelecom_over.png',
			'/img/busboxbutton_bg.gif',
			'/img/menubutton_bg.gif'
		]);
		
        var fx = new Fx.Morph($('UserPasswd'), {
            duration: 200,
            link: 'chain'
        });
		
        $('UserPasswd').addEvents({
            'focus': function(){
                passMaskd = false;
                fx.start({
                    'background-image': null,
                    'background-color': '#dffd96',
                    'color': '#000'
                });
            },
            'keydown': function(){
                this.setStyle('background-image', null);
            },
            'blur': function(){
                if (this.value == '') {
                    passMaskd = true;
                    fx.start({
                        'background-color': '#e8f3d8',
                        'color': '#888'
                    }).chain(function(){
                        this.start({
                            'background-image': 'url(/img/password_bg.gif) no-repeat'
                        });
                    });
                }
                else {
                    fx.start({
                        'background-color': '#e8f3d8',
                        'color': '#888'
                    });
                }
            }
        });

        var fx2 = new Fx.Morph($('UserUsername'), {
            duration: 200,
            link: 'chain'
        });
        $('UserUsername').addEvents({
            'focus': function(){
                userMaskd = false;
                fx2.start({
                    'background-image': null,
                    'background-color': '#dffd96',
                    'color': '#000'
                });
            },
            'keydown': function(){
                this.setStyle('background-image', null);
            },
            'blur': function(){
                if (this.value == '') {
                    userMaskd = true;
                    fx2.start({
                        'background-color': '#e8f3d8',
                        'color': '#888'
                    }).chain(function(){
                        this.start({
                            'background-image': 'url(/img/username_bg.gif) no-repeat'
                        });
                    });
                }
                else {
                    fx2.start({
                        'background-color': '#e8f3d8',
                        'color': '#888'
                    });
                }
            }
        });
		
		/* iPhone toll free link */
		if (Browser.Platform.ipod){
			var tfNum = new Element('a', { href: 'tel:8003119161' });
			tfNum.wraps($('tollfree'));
		}

		/* Main Navbar */
		var businessTelecomBtn = $('business');
		var providersBtn = $('providers');
		var faqsBtn = $('faqs');
		var agentloginBtn = $('agentlogin');
		var agentlogoutBtn = $('agentlogout');
		var agentareaBtn = $('agentarea');
		var agentsBtn = $('agents');
		
        var loginSlide = new Fx.Slide('loginBox', {
            duration: 300,
            transition: Fx.Transitions.Pow.easeOut,
            link: 'cancel'
        }).hide();
        var busTelecomEffect = new Fx.Morph('busTelecomContainer', {
            duration: 300,
            transition: Fx.Transitions.Pow.easeOut,
            link: 'cancel'
        });	

		var regex = new RegExp("/login");
		var results = regex.exec(window.location.href);

        if (results == "/login") {
            $('loginContainer').setStyle('display', 'block');
            if (Browser.Engine.gecko) {
                loginSlide.slideOut().chain(function(){
                    this.show.delay(1000, this);
                    $('UserUsername').focus();
                });
            }
            else {
                loginSlide.toggle().chain(function(){
                    $('UserUsername').focus();
                })
            }
        };

        $("busTelecomContainer").addEvents({
            mouseenter: function(){
                overMenu = true;
            },
            mouseleave: function(){
                this.fireEvent('delayedhide', 'hide', 500);
                overMenu = false;
            },
            delayedhide: function(){
                if ($('busTelecomContainer').getSize().y > 185 && !overMenu) {
                    busTelecomEffect.start({
                        'height': [185, 0],
                        'border-width': '0 0 0 1px'
                    });
                }
            }
        });

        businessTelecomBtn.addEvents({
            mouseenter: function(){
                this.getChildren('img').setProperty('src', '/img/businesstelecom_over.png');
                this.fireEvent('showmenu', 'hide');
            },
            mouseleave: function(){
                this.getChildren('img').setProperty('src', '/img/businesstelecom.png');
                this.fireEvent('delayedhide', 'hide', 500);
            },
            click: function(){
                this.fireEvent('showmenu', 'hide');
            },
            showmenu: function(){
                if ($('busTelecomContainer').getSize().y == 0) {
                    busTelecomEffect.start({
                        'height': [0, 185],
                        'border-width': '0 0 1px 1px'
                    });
                }
            },
            delayedhide: function(){
                if ($('busTelecomContainer').getSize().y > 185 && !overMenu) {
                    busTelecomEffect.start({
                        'height': [185, 0],
                        'border-width': '0 0 0 1px'
                    });
                }
            }
        });
		
        providersBtn.addEvents({
            mouseenter: function(){
                this.getChildren('img').setProperty('src', '/img/providers_over.png');
            },
            mouseleave: function(){
                this.getChildren('img').setProperty('src', '/img/providers.png');
            }
        });
		
        faqsBtn.addEvents({
            mouseenter: function(){
                this.getChildren('img').setProperty('src', '/img/faqs_over.png');
            },
            mouseleave: function(){
                this.getChildren('img').setProperty('src', '/img/faqs.png');
            }
        });
		        
        if (agentareaBtn) {
            agentareaBtn.addEvents({
                mouseenter: function(){
                    this.getChildren('img').setProperty('src', '/img/agentarea_over.png');
                },
                mouseleave: function(){
                    this.getChildren('img').setProperty('src', '/img/agentarea.png');
                }
            });
        }

        if (agentsBtn) {
            agentsBtn.addEvents({
                mouseenter: function(){
                    this.getChildren('img').setProperty('src', '/img/agents_over.png');
                },
                mouseleave: function(){
                    this.getChildren('img').setProperty('src', '/img/agents.png');
                }
            });
        }
		
        if (agentloginBtn) {
            agentloginBtn.addEvents({
                mouseenter: function(){
                    this.getChildren('img').setProperty('src', '/img/agentlogin_over.png');
                },
                mouseleave: function(){
                    this.getChildren('img').setProperty('src', '/img/agentlogin.png');
                },
                click: function(e){
                    e = new Event(e);
                    $('loginContainer').setStyle('display', 'block');
                    loginSlide.toggle().chain(function(){
                        $('UserUsername').focus();
                    })
                    e.stop();
                }
            });
        }
        else {
            agentlogoutBtn.addEvents({
                mouseenter: function(){
                    this.getChildren('img').setProperty('src', '/img/logout_over.png');
                },
                mouseleave: function(){
                    this.getChildren('img').setProperty('src', '/img/logout.png');
                }
            });
        }

        /* Top Navbar */
        var aboutTopnav = $('tn_about');
        var newsTopnav = $('tn_news');
        var contactTopnav = $('tn_contact');
        aboutTopnav.addEvents({
            mouseenter: function(){
                this.getPrevious().getChildren('img').setProperty('src', '/img/topbar_start_over.gif');
                this.setStyle('background-image', 'url(/img/topbar_bg_over.gif)');
            },
            mouseleave: function(){
                this.getPrevious().getChildren('img').setProperty('src', '/img/topbar_start.gif');
                this.setStyle('background-image', 'url(/img/topbar_bg.gif)');
            }
        });
        aboutTopnav.addEvents({
            mouseenter: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg_over.gif)');
            },
            mouseleave: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg.gif)');
            }
        });
        newsTopnav.addEvents({
            mouseenter: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg_over.gif)');
            },
            mouseleave: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg.gif)');
            }
        });
        contactTopnav.addEvents({
            mouseenter: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg_over.gif)');
            },
            mouseleave: function(){
                this.setStyle('background-image', 'url(/img/topbar_bg.gif)');
            }
        });
		
	},
    mask: function(){
        if (userMaskd) {
            if ($('UserUsername').value == '') {
                $('UserUsername').setStyle('background', '#e8f3d8 url(/img/username_bg.gif) no-repeat');
            }
            else {
                $('UserUsername').setStyle('background', '#e8f3d8');
            }
        }
        if (passMaskd) {
            if ($('UserPasswd').value == '') {
                $('UserPasswd').setStyle('background', '#e8f3d8 url(/img/password_bg.gif) no-repeat');
            }
            else {
                $('UserPasswd').setStyle('background', '#e8f3d8');
            }
        }
        this.fireEvent('mask', 'hide', 500);
    }
});


