$(document).ready(function() {    
    
    $('#mainnav li.multi').hoverIntent({
        over: startMenuHover,
        out: endMenuHover,
        timeout: 1500,
        sensitivity: 4,
        interval: 1
    });
    
    function startMenuHover() {   
        

        
        $(this).find('a').stop(true,true).first().hide(300);
        if (jQuery.browser.msie === true) {
            
            $(this).find('ul').stop(true,true).show(300);
            
        }  else {
            
            $(this).find('ul').stop(true,true).slideDown(300);
            
        }
            
                
        $(this).addClass('multiopen');
    }
    
    function endMenuHover() { 
        
        if (jQuery.browser.msie === true) {
            
            $(this).find('ul').stop(true,true).hide(300);   
            
        } else {
        
            $(this).find('ul').stop(true,true).slideUp(300);
        
        }
        
        $(this).find('a').first().show(300);
        $(this).removeClass('multiopen');
        
    }
    
    $('#newslettersignup').click( function() {
        $(this).val('');
    })
    
    $('ul.slidedown h3').toggle( 
        function () {
            $(this).parent().find('p').slideDown();
        }, 
        function() {
            $(this).parent().find('p').slideUp();
        }
    );
        
    $('ul.slidedown li').hover( 
        function () {
            $(this).find('h3').css({'color':'#58692f'});
        }, 
        function() {
            $(this).find('h3').css({'color':'#879D50'});
        }
    );
        
    $('#newslettersignupbutton').click(function(e){
        
        e.preventDefault();
        
        newsletter_submit();
        
    });
        
        
    $('#newslettersignupform').submit(function(e) {
        
        e.preventDefault();
        
        newsletter_submit();
        

    });        
    
});

function newsletter_submit() {
    
        
    
        $('#response').fadeOut(0).html('<span class="success">Adding email address...</span>').fadeIn();

        // Prepare query string and send AJAX request
        $.ajax({
            url: 'newsletter/store-address.php',
            data: 'ajax=true&email=' + escape($('#newslettersignup').val()),
            success: function(msg) {   
                $('#response').html(msg);
            },
            error: function(msg) {
                alert('A problem ocurred.  Please call us and let us know our site is being annoying!');                
            }
        });
        
        $('#response').delay(5000).fadeOut();

        return false;    
    
}
