$(function()
{
    var current_entries = 0;
    
    $(".tip-submit").click(function()
    {
        var tip_entry = $('input#tip-entry').val();
        tip_entry_clean = jQuery.trim(tip_entry);
        
        if (tip_entry_clean == '')
        {
            alert('Hov, du mangler vidst at indtaste noget sømand.');
        }
        else
        {
            $.ajax({
                type: 'POST',
                url: 'assets/process.php',
                data: 'data=' + tip_entry_clean,
                success: function(response)
                {
                    var obj = jQuery.parseJSON(response);
                    if ('success' == obj.message)
                    {
                        $('input#tip-entry').val('');
                        $.ajax({
                            type: 'GET',
                            url: 'assets/entries.php',
                            data: 'fade=' + obj.entry_id,
                            success: function(data)
                            {
                                $('div#page-entries').html(data);
                                $('div#entry-' + obj.entry_id).fadeIn('slow');
                            }
                        });
                    }
                }
            });
        }
        
        return false;

    });
    
    $('.page-pagination').click(function()
    {
        $.ajax({
            type: 'GET',
            url: 'assets/entries.php',
            data: 'entries=' + current_entries,
            success: function(data)
            {
                $('div#page-entries').append(data);
                current_entries = (current_entries + $(data).find('img').length);
            }
        });

        return false;
    });
    
    $('.loading').ajaxStart(function(){
        $(this).show();
    });
    
    $('.loading').ajaxStop(function(){
        $(this).hide();
    });
});
