jQuery(document).ready(function($){
    //alert('all loaded');

    // initialize some variables
    var winWidth = $(window).width();
    //var winHeight = $(window).height();
    var bannerWidth = 0;
    var postsWidth = 0;
    var postWidth = 0;
    var animating = false;
    var postsOffset = 0;
    //var initialPost = $('.post.current').attr('id');
    var firstPost = 0;
    var currentPost = 0;
    //var postCount = $('.post').length;
    //var copiedId = 0;
    var lastPost = $('.post').length - 1;

    // the neccessary action to move to the next post
    function nextPost() {
        if ($('#comments-container').length > 0) {
            $('.post-comments').removeClass('viewing');
            $('#comments-container').remove();
        }
        
        if (!$('.post:eq(' + lastPost + ')').hasClass('.current')) {
            animating = true;
            postsOffset = $('#posts-container').offset();
            $('.post:eq(' + currentPost + ')').removeClass('current');
            currentPost++;
            $('.post:eq(' + currentPost + ')').addClass('current');

            $('#posts-container').animate({
                left: (postsOffset.left - postWidth) + 'px'
            }, function() { animating = false; });
        } else {
            animating = true;
            $('.post:eq(' + currentPost + ')').removeClass('current');
            currentPost = 0;
            $('.post:eq(' + currentPost + ')').addClass('current');
            
            $('#posts-container').animate({
                left: (winWidth - postWidth) / 2 + 'px'
            }, function() { animating = false; });
        }
    }

    // the neccessary actions to move to the previous post
    function previousPost() {
        if ($('#comments-container').length > 0) {
            $('.post-comments').removeClass('viewing');
            $('#comments-container').remove();
        }
        
        if (!$('.post:eq(' + firstPost + ')').hasClass('current')) {
            animating = true;
            postsOffset = $('#posts-container').offset();
            $('.post:eq(' + currentPost + ')').removeClass('current');
            currentPost--;
            $('.post:eq(' + currentPost + ')').addClass('current');
            $('#posts-container').animate({
                left: (postsOffset.left + postWidth) + 'px'
            }, function() { animating = false; });
        }
    }

    // center banner in browser window
    bannerWidth = $('#banner').width();
    $('#banner').css('left', (winWidth - bannerWidth) / 2);

    // find the combined width of all the posts
    postsWidth = $('.post').outerWidth(true) * $('.post').length;
    $('#posts-container').css('width', postsWidth);

    // center the first post by moving the posts container
    postWidth = postsWidth/$('.post').length;
    $('#posts-container').css('left', (winWidth - postWidth) / 2);

    // getting to the next post with the mouse
    $('#next').click(function(event) {
        event.preventDefault();
        if (animating != true) {
            nextPost();
        } else {
            return;
        }
    })

    // getting to the previous post with the mouse
    $('#previous').click(function(event) {
        event.preventDefault();
        if (animating != true) {
            //animating = true;
            previousPost();
        } else {
            return;
        }
    });

    // getting to the next post with the arrow keys
    $(this).keyup(function(event) { //right arrow key
        if (event.keyCode == 39 && animating != true) {
            //animating = true;
            nextPost();
        } else if (event.keyCode == 37 && animating != true) { // left arrow key
            //animating = true;
            previousPost();
        } else { //neither right nor left
            return;
        }
    });

    $('.post-comments').live('click',function(event) {
        event.preventDefault();
        $(this).addClass('viewing');
        $(this).prepend('<img src="wp-content/themes/ghostwriter/images/loader.gif" class="loader" />');
        var commentUrl = $(this).attr('href');
        var commentContainer = $(this).parents('.post').html();
        $(this).parents('.post').load(commentUrl + ' #comments-container', function(responseText, textStatus, XMLHttpRequest) {
            if (textStatus == 'success') {
                $(this).prepend(commentContainer);
                $('#comments').children().fadeOut(function() {
                    $('#comments').animate({
                        top: '0'
                    }, 500, 'easeInOutCirc', function() {
                        $('img.loader').remove();
                        $('#comments').children().fadeIn();
                    });
                });
            }
        });
    });

    $('#commentform').live('submit', function(event) {
        event.preventDefault();
        $(this).parents('.post').children('.post-footer').children('.post-comments').prepend('<img src="wp-content/themes/ghostwriter/images/loader.gif" class="loader" />');
        var formData = $(this).serialize();
        var actionUrl = $(this).attr('action');
        var commentContainer = $(this).parents('.post');
        var commentUrl = $(this).parents('.post').children('.post-footer').children('.post-comments').attr('href');
        $('#comments-container').remove();
        var commentText = commentContainer.html();
        $.post(actionUrl, formData, function(data, textStatus) {
            if (textStatus == 'success') {
                commentContainer.load(commentUrl + ' #comments-container', function(reponseText, textStatus, XMLHttpRequest) {
                    if (textStatus == 'success') {
                        $(this).prepend(commentText);
                        $('#comments').animate({
                            top: '0'
                        }, function() {
                            $('img.loader').remove();
                            $('#comments-container').fadeIn('fast');
                        });
                    }
                });
            }
        });
    });
});