function initNav() {

    var $subMenus = $("#menu ul li ul");

    if ($subMenus.length > 0) {
        
        $subMenus.each(function() {
            $($(this).parent().get(0)).hover(
                
                function() {
               
                    $(this).find("ul").stop(true, true).fadeIn();
                   
                    if($.browser.msie && $.browser.version < 7) {
                        $('select').hide ();
                    }
                },
                    
                function() {
                   
                    $(this).find("ul").stop(true, true).fadeOut();
                   
                    if($.browser.msie && $.browser.version < 7) {
                        $('select').show ();
                    }
            });
        });
    }
}

function rotateBanners() {
    
    var active = $( '#banner .active' );
    var next = active.next();
    
    if( active.hasClass( 'last' ) == true ) {
        next = $( '#banner .first' );
    }
    
    active.fadeOut( 2500 ).removeClass( 'active' );
    
    next.fadeIn( 2500 ).addClass( 'active' );
    
    
    setTimeout( rotateBanners, 6000 );
}

$( function() {
    
    initNav();
    
    var rand = Math.floor(Math.random()*4)    
    $( '#banner img' ).eq( rand ).show().addClass( 'active' );    
    
    setTimeout( rotateBanners, 5000 );
    
    $( '#launch-lease-calc' ).click( function() {
        
        var res = $( '#calc-residual' ).text();
        var len = $( '#calc-length' ).val();
        
        res = res.replace( '$', '' );
        res = res.replace( ',', '' );
        
        if( ! res > 0 ) {
            
            res = 0;
        }
        
        var centerWidth = (window.screen.width - 600) / 2;
        var centerHeight = (window.screen.height - 400) / 2;
        
        try {
            var popup = window.open ('/lease-calc/' + res + '/' + len, '', 'status=0,toolbar=0,width=600,height=400,scrollbars=0,top=' + centerHeight + ',left=' + centerWidth);
            popup.focus();
            
        } catch (ex) {
            alert ('Your popup blocker prevented the window from opening. Please disable your popup blocker.');
        }
    });
})
