// On-load functions ####################################################################

$(document).ready(function(){
    initNav();
    imgFix();
    loadContactForm();
    loadComments();
    loadGalleries();
    $('#dp_1').show();
});

// InitNav ##############################################################################

function initNav(){
    $('nav ul li').each(function(){        
        var node = $(this);
        // Transform node
        node.prepend('<div class="nav_slider"></div>');
        if(!node.hasClass('active')){
            // Slide BG Down
            node.mouseover(function(){
                if(!$.data(node,'pause')){
                    $.data(node,'pause',true);
                    node.children('.nav_slider').animate({ 'margin-top':'0' }, 300, function(){ $.data(node,'pause',false); });
                }
            });
            
            // Slide BG Up
            node.mouseout(function(){
                node.children('.nav_slider').animate({ 'margin-top':'-60px' }, 300 );
            });
        }
    });
}

// Image margin (float) fix #############################################################
function imgFix(){
    $('#content img').each(function(){
        var a = $(this).attr('align');
        if(a=='left'){ $(this).css('marginLeft','0px'); }
        if(a=='right'){ $(this).css('marginRight','0px'); }
        if(a=='undefined'){ $(this).css('marginLeft','0px'); }
    });
}

// Comments #############################################################################

function loadComments(){
    var id = $('#comment_block').attr('rel');
    $('#comment_block').load('modules/comments.php?id='+id);
}

function submitComment(id){
    var author = $('#author').val();
    var comment = $('#comment').val();
    
    if (author == '' || comment == '') {
        $('.notification').show();
        $('.notification').fadeOut(8000);
    }
    else {
        $('.notification').hide();
        $.post('modules/comment_save.php?id='+id, $('#comment_form').serializeArray());
        $('#comment_block ul').append('<li><span class="cmt_author">' + author + ' just posted:</span><p>' + comment + '</p></li>');
        // Clear fields
        $('#author').focus();
        $('#comment').val('').focus();
    }
}

function deleteComment(id){
    $('#cmt'+id).fadeOut(200);
    $.get('modules/comment_delete.php?id='+id);
}

// Galleries ##############################################################################

function loadGalleries(){
    $('.gallery').each(function(){
        var r = $(this).attr('rel');
        $(this).load('/modules/gallery.php?gallery='+r);
    });
    
    $('.slider').each(function(){
        var r = $(this).attr('rel');
        $(this).load('/modules/slider.php?gallery='+r);
    });
    
    
    $('.fader').each(function(){
        var r = $(this).attr('rel');
        $(this).load('/modules/fader.php?gallery='+r);
    });
}

// Contact form ###########################################################################

function loadContactForm(){
    $('#contact_form').load('modules/contact_form.php');
}

function sendEmail(){
    $('#btn_send').val('Sending...');
    $.post('modules/contact_form.php?submit=t', $('#email_form').serializeArray(), function(data){
      $('#contact_form').html(data);$('.notification').fadeOut(8000);
    });
}
