$(document).ready(function(){

    $('#to_top, .top').click(function() {
        
        $('html,body').animate({scrollTop: 0}, 1000);
        
        return false;
        
    });
    
    $('a.about').click(function() {
        
        if($(this).attr('href') != '/index.php#about') {
        
            var target = $('#about').offset().top;
            
            $('html,body').animate({scrollTop: target}, 1000);
    
            return false;
        
        }
        
    });
    
    $('a.portfolio').click(function() {
    
        if($(this).attr('href') != '/index.php#portfolio') {
        
            var target = $('#portfolio').offset().top;
            
            $('html,body').animate({scrollTop: target}, 1000);
    
            return false;
        
        }
        
    });
    
    $('a.contact').click(function() {
    
        if($(this).attr('href') != '/index.php#contact') {

            var target = $('#footer').offset().top;
    
            $('html,body').animate({scrollTop: target}, 1000);
    
            return false;
        
        }
        
    });
    
    if($(window).scrollTop() > 48) {

         $('#to_top').show();
         
    }
    
    $(window).scroll(function() { 
        
        if($(this).scrollTop() > 48 && $('#to_top').css('display') == 'none') {

            $('#to_top').fadeIn();
            
        } else if ($(this).scrollTop() > 48) {
        
            $('#to_top').show();
        
        } else {
            
            $('#to_top').fadeOut();
            
        }
        
    });
    
    $("#slides").easySlider({
        auto: false,
        continuous: true,
        numeric: false,
    });
    
    $('#contactForm').submit(function(){
    
        var name,
            email,
            message,
            notification;

        name = $('#name').val();
        email = $('#email').val();
        message = $('#message').val();
        
        if (name == '' || message == '' || email == '') {
        
            $('#notification').addClass('error').text('All fields are required. Please try again.');
            $('#notification').fadeIn();
        
        } else { 
        
            $.post(
                'mailer.php',
                {
                    'name'      : name,
                    'email'     : email,
                    'message'   : message,
                    'action'    : 'email'
                },
                function(data) {
                
                    if(data == 'success') {
                        
                        $('#notification').addClass('success').text('Thank you for your email. I will get back to you as soon as possible.');
                        
                        $('#name').val('');
                        $('#email').val('');
                        $('#message').val('');
                        
                    } else {
                    
                        $('#notification').addClass('error').text('An error has occurred while attempting to send your email.');
                        
                    }
                    
                    $('#notification').fadeIn();
                    
                },
                'text'
            );

        }
        
        return false;
        
    });
    
});
