$(document).ready(function(){
    $("#nav").lavaLamp({
        fx: "backout",
        speed: 700
    });
    if ($('#slider').length) {
        setInterval(fadeImage, 5000);
    }
    
    $(".thumbnail").click(showPopup);
    $('#popup-overlay img.close').click(function(e) {
        $('#popup-overlay, #overlayBackground').fadeOut();
    });
    
    $('#overlayBackground').css('opacity', 0.5);
    $('#overlayBackground').click(function(){
        $('#overlayBackground, #contact-overlay, #popup-overlay, #message-overlay').fadeOut();
    });
    
    $('#img-sc, #img-ms').mouseenter(function(e) {
        var cls = $(e.target).attr('id') == 'img-sc' ? 'sc' : 'ms';
        $('div.img-shadow.'+cls).addClass('active');
    });
    
    $('#img-sc, #img-ms').mouseleave(function(e) {
        var cls = $(e.target).attr('id') == 'img-sc' ? 'sc' : 'ms';
        $('div.img-shadow.'+cls).removeClass('active');
    });
});

function fadeImage() {
    var img = $('#slider img.active');
    if (!img.length) {
        img = $('#slider img').eq(0);
    }
    
    img.removeClass('active').fadeOut('slow', function() {
        var next = $(this).next();
        if (!next.length) {
            next = $('#slider img').eq(0);
        }
        
        next.addClass('active').fadeIn('slow');
    });
    
}

function showPopup(e) {
    e.preventDefault();
    
    var id = $(e.target).closest('.thumbnail').attr('id');
    id = /thumb-(.*)/.exec(id);
    id = id[1];
    $('#popup-content').html(
        $('#popups-hidden-content #popup-' + id).html()
    );
        
    $('#popup-overlay h1').html($('#popups-hidden-content #popup-' + id).attr('title'));
        
    $('#popup-overlay').css({top:'50px',left:'50%',margin:'0px 0 0 -'+($('#popup-overlay').outerWidth() / 2)+'px'});
    $('#popup-overlay, #overlayBackground').fadeIn();
}
