
var thisItemCommentCount = 0;
var commentItemId = 0;
var displayedCommentCount = 0;
var commentProcessing = false;
var home_feed_page = 1;
var footer_guy_has_dropped = false;

var this_id         = false;
var item_previous   = false;
var item_next       = false;

$(document).ready(function() {
    
    $('#new_comment_form').submit(function (event) {
        event.preventDefault();
        submit_comment();
    });
    
    $('#home_feed_load_more').click(function (event) {
        event.preventDefault();
        update_home_feed();
    });
    
    $('#footer_pic').draggable({
        addClasses: false, 
        start: function() {
            $('#footer_door_wrapper').fadeIn();
        },
        stop: function() {
            if (!footer_guy_has_dropped) $('#footer_door_wrapper').fadeOut();
        }
    });
    
    $('.footer_door').droppable({
        hoverClass: "footer_door_hover",
        drop: function(event, ui) {
            footer_guy_choose_door($(this));
        }
    });
    
    $('#footer_login_link').click(function (event) {
        event.preventDefault();
        $('#footer_login').toggle();
        $('#login_pass').focus();
    });
    
    $('#footer_login_form').submit(function (event) {
        event.preventDefault();
        footer_login();
    });
    
    // Load previous or next photo in gallery on keydown
    $(document).keydown(function(e) {
        if (this_id && item_previous && e.keyCode == 37) { // Prev photo on left arrow
            window.location.href = '/?id=2_'+this_id+'&p='+item_previous;
        } else if (this_id && item_next && e.keyCode == 39) { // Next photo on right arrow
            window.location.href = '/?id=2_'+this_id+'&p='+item_next;
        }
    });
    
});

function footer_guy_choose_door(door) {
    $('#footer_pic').hide();
    footer_guy_has_dropped = true;
    
    msgs        = new Array("There's no good reason for picking that door! I'm disappointed.", "Did you expect a prize behind that door? I'm sorry, but no.", "Ooooo, so close. It was the other door.", "Your fortune: today you will choose the wrong door.", "Your fortune: good things will happen to others.", "Your fortune: you won't get hit by a bus today.", "Your fortune: magical creatures live under your bed.", "Your fortune: an unexpected visitor will bring you coffee.", "Your fortune: today is the first day of the rest of the week.", "I do believe you were just guessing. Try again, using skill.", "Please. Would Chuck Norris have picked that door?", "There's a 33% chance you picked the correct door.", "Nicolas Cage picked that door, and look what happened.", "Gary Busey picked that door, and look what happened to him.", "Oh dude! There was a unicorn behind the other door!", "Lucky you! There was a yeti behind the other door!", "Great Scott! There's a kraken behind that door! RUN!!!1!", "I'm not totally sure that was the door you meant to pick.", "I'm sorry, that door is locked. Do you have a key?", "That door leads to the restroom. Please flush when you're done.", "There's a sleeping bear behind that door. Please don't go in.", "Forget the doors for a second... let's talk about Bieber.", "Is there any chance I could borrow some change for a soda?", "Congratulations, you've chosen the most awesomest door of all.", "One time this door was the winner. Not this time. Sry.", "Lol, you have to watch my sister's cats for a month.", "You seem to have a lot of time on your hands.", "Yeah! You picked my favorite door! Awesome.", "Would it upset you to know these messages are all random?", "There's a slight chance you picked the correct door.", "If I had more time, you'd see a colorful animation here.", "You win! Pretend that there are flashing lights and sounds.", "There are many messages that load at random. Read them all!", "We're sorry, but you failed. It's embarrassing really.", "Dude! Dude! Dude! Dude! Dude! Dude! That was the door!", "Well, you can't say you didn't try. But you did fail.");
    rand_num    = Math.floor(Math.random() * msgs.length);
    msg_id      = Math.floor(Math.random()*5) + 1;

    $('#footer_door_wrapper').fadeOut('fast', function() {
        $('#footer_door_wrapper').addClass('footer_door_msg'+msg_id).html("<div class='footer_door_cloud_msg'><table width='100%' height='100%' border='0' cellpadding='0' cellspacing='0'><tr><td align='center' valign='middle'>"+msgs[rand_num]+"</td></tr></table></div>");
        $('#footer_door_wrapper').fadeIn('fast').delay(2700).fadeOut(1200);
    });
}

function update_home_feed() {
    home_feed_page++;
    $.ajax({
        url: '/'+base_dir+'/ajax/update_home_feed.php',
        type: 'POST',
        data: "page="+home_feed_page,
        success: function(data) {
            $('#home_feed_inner').append(data);
        }
    });
}

function image_link(url1, url2, event) {
    if (document.body.clientWidth) {
        thisX = event.clientX;
        if (window.event) thisX = window.event.x;
        if (thisX < (document.body.clientWidth / 2)) {
            if (url1) window.location=url1;
        } else {
            if (url2) window.location=url2;
        }
    }
}

function footer_login() {
    home_feed_page++;
    $.ajax({
        url: '/'+base_dir+'/ajax/footer_login.php',
        type: 'POST',
        data: "p=33s" + $('#login_pass').val() + '5jeff',
        success: function(data) {
            $('#login_pass').val('');
            if (data == 'OK') {
                window.location.href = '/admin2/';
            } else {
                alert('Sorry.');
            }
        }
    });
}

function submit_comment() {

    if (!commentProcessing) {
    
        commentStartProcessing();
        
        errMsg = false;
        if (!$('#comment_text').val()) {
            errMsg = 'Please enter a comment.';
        }

        if (errMsg) {
            update_comment_error(errMsg);
            commentEndProcessing();
        } else {
            
            $.ajax({
                url: '/'+base_dir+'/ajax/comment_process.php',
                type: 'POST',
                data: {comment: $('#comment_text').val(), name: $('#comment_name').val(), commentItemId: commentItemId},
                success: function(data) {
                    response_parts = data.split(" ");
                    if (response_parts[0] == 'okgo') {
                        newCommentString = response_parts[2] + ' Comment';
                        if (response_parts[2] != 1) newCommentString += 's';
                        $('#comment_count').html("<h3>" + newCommentString + "</h3>");
                        $('#comment_text').val('');
                        $('#comment_name').val('');
                        $('#comment_error').hide();
                        $.ajax({
                            url: '/'+base_dir+'/ajax/comments_load_new.php',
                            type: 'POST',
                            data: {commentItemId: commentItemId, lastLoadedCommentId: $('#last_loaded_comment_id').val(), newCommentId: response_parts[1]},
                            success: function(data) {
                                $('#comment_wrapper').append(data);
                                commentEndProcessing();
                            }
                        });
                        
                        $('#last_loaded_comment_id').val(response_parts[1]);
                    
                    } else {
                        update_comment_error(data);
                        commentEndProcessing();
                    }
                }
            });
        }
    
    }
}


function preload(arrayOfImages) {
    $(arrayOfImages).each(function() {
        $('<img/>')[0].src = this;
    });
}


function commentStartProcessing() {
    commentProcessing = true;
    processorStart('processor_post_comment');
    $('#comment_text').attr('disabled', true); 
    $('#comment_name').attr('disabled', true); 
    $('#comment_submit').hide();
    $('#comment_submit_disabled').show();
}

function commentEndProcessing() {
    commentProcessing = false;
    processorStop('processor_post_comment');
    $('#comment_text').removeAttr('disabled'); 
    $('#comment_name').removeAttr('disabled'); 
    $('#comment_submit').show();
    $('#comment_submit_disabled').hide();
}

function processorStart(id) {
    document.getElementById(id).innerHTML = "<img src='/images/processing3.gif' width='25' height='25' alt='Processing'>";
}
function processorStop(id) {
    $('#'+id).html("<img src='/images/processing_done4.gif' width='25' height='25' alt='Done'>");
}

function update_comment_error(errMsg) {
    if (!errMsg) errMsg = 'There was an error - please try again.';
    if ($('#comment_error').is(':hidden')) {
        $('#comment_error_inner').html(errMsg);
        $('#comment_error').slideDown('fast');
    } else {
        $('#comment_error').animate({opacity:0.1}, 150, function() {
            $('#comment_error_inner').html(errMsg);
            $('#comment_error').animate({opacity:1.0}, 150);
        });
    }

}

