// Functions to fire on document.ready
$(document).ready(function(){    
    // Activate lightbox for portfolio gallery
    if ($('#portfolio_gallery').is(":visible")) {
        lightbox();
        gallery_photo();
    }
    
    // First paragraph dropcap in .entry_body
    dropCap();
});

function lightbox() {
    $('#portfolio_gallery a').lightBox();
}

function gallery_photo() {
    $('#portfolio_gallery a:nth-child(3n)').css('margin-right', 0);
}

function dropCap() {
    $(".entry_body > p:first-child").each(function (i) {
        var firstElement = $(this);
        var newElement = firstElement.clone(true);
        
        newElement.addClass("first_graph");
	    if($.browser.safari || $.browser.opera) { newElement.addClass("safari_opera");}
	    firstElement.replaceWith(newElement);
    });
}

function postComment(url) {
    $("#comment_form form").submit(function () {
        $.ajax({
            type: "POST",
            data: $("#comment_form form").serialize(),
            url: url,
            cache: false,
            dataType: "html",
            success: function(html, textStatus) {
                console.log(html);
                //$("#comment_form form").replaceWith(html);
                //postComment(url);
            },
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                $('#comment_form form').replaceWith('Your comment was unable to be posted at this time.  We apologise for the inconvenience.');
            }
        });
        return false;
    });
}