// When DOM is ready
$(function() {
    
    // Handle menu image hover action
    $('.menu li a').hover(function() {
    
        // Check if not the current page menu image
        if ($(this).parent().attr('class') != 'active') {
            
            // Get current link
            var currentLink = $(this)
            
            // Get current background image
            var currentImage = $(currentLink).children('img').attr('src')
            
            // Get image base url
            var baseImageUrl = currentImage.substring(0, (currentImage.length - 4))
            
            // Get new image url
            var newImageUrl = baseImageUrl + '-active.jpg'
            
            // Create image preloader object
            var tmpImageObject = $('<img />').load(function() {
                
                // Change image src
                $(currentLink).children('img').attr('src', newImageUrl)
                
            }).attr('src', newImageUrl);
            
        }
        
    });
    
    // Reset menu image
    $('.menu li a').mouseout(function() {
    
        // Check if not the current page menu image
        if ($(this).parent().attr('class') != 'active') {
            
            // Get current image
            var currentLink = $(this)
            
            // Get current image
            var currentImage = $(currentLink).children('img').attr('src');
            
            // Get image base url
            var baseImageUrl = currentImage.substring(0, (currentImage.length - 4))
            
            // Check if base image url valid
            if (baseImageUrl.substring((baseImageUrl.length - 7)) == '-active') {
                
                // Remove active from url
                baseImageUrl = baseImageUrl.substring(0, (baseImageUrl.length - 7))
                
            }
            
            // Get new image url
            var newImageUrl = baseImageUrl + '.jpg'
            
            // Change image src
            $(currentLink).children('img').attr('src', newImageUrl)
        
        }
    
    });
    
});

