var sb_sec = get_timestamp();
var sb_name;
var sb_updates;

function get_timestamp() {
    return parseInt(new Date().getTime() / 1000);
}

function shoutbox_save() {
    if (sb_sec > (get_timestamp() - spam_protection)) {
        alert("Please wait at least " + spam_protection + " seconds before you shout again.");
        sb_sec = get_timestamp();
        
        return false;
    }
    sb_sec = get_timestamp();
	
    $.post('/chat/ajax.php', {
        action: 'post',
        name: sb_name,
        message: $('#shoutbox_message').val(),
		fb_id: fb_user_id
    }, function() {});
}

function shoutbox_update() {
    $.get('/chat/ajax.php', {
        action: 'update',
        last: $('#shoutbox_box .shout-wrapper:first').attr('id')
    }, function() {
        if ($('#shoutbox_box .shout-wrapper').length > 500) {
            location.reload();
        }
    }, 'script');
    
    return true;
}

function shoutbox_bot(name) {
	
    sb_name = name;
    $.post('/chat/ajax.php', {
        action: 'bot',
        name: sb_name
    }, function() {
        shoutbox_update();
    });
}

function shoutbox_loader() {
    
	$('#shoutbox_loader').fadeIn('slow');
	
	$.get('/chat/ajax.php', {
        action: 'loader'
    }, function(data) {
        $('#shoutbox_loader').fadeOut('slow', function() {
            
			// it's possible for the login popup to appear and the
			// page underneath log the user in async/late -- if that happens
			// we need to clear out the previously loaded shoutbox_box_inner div
			if ($('#shoutbox_box_inner').length > 0)
				$('#shoutbox_box_inner').remove();
			
			$('#shoutbox_box_wrapper')
				.append(data);
				
            sb_updates = window.setInterval('shoutbox_update()', refresh);
			
            $('#shoutbox_box_inner')
				.animate({opacity: '1.0'}, 'slow');
			
            $('#shoutbox_message_counter')
				.css('left', ($('#shoutbox_message')
								.offset().left + $('#shoutbox_message')
								.width() - 208)
					 )
				.css('top', ($('#shoutbox_message')
								.offset()
								.top + 21)
					 );
				
            $('#shoutbox_message')
				.focus()
				.charcounter()
				.bind('focus', function() {
					if ($(this).val() == "\n") {
						$(this).val('');
					}
				})
				.bind('keypress', function (e) {
					if (e.which == 13 && e.shiftKey == false && $.trim($(this).val()) != '') {
						shoutbox_save($(this).val());
						e.preventDefault();
						$(this).val('');
					}
				});
			
            $('#shoutbox_submit').live('click', function() {
                shoutbox_save();
                $('#shoutbox_message').val('');
                
                return false;
            });
            $(window).bind('resize', function() {
                $('#shoutbox_message_counter')
					.css('left', ($('#shoutbox_message')
					.offset().left + $('#shoutbox_message')
					.width() - 208)).css('top', ($('#shoutbox_message')
					.offset().top + 21));
            });
        });
    });
}

$(document).ready(function() {
    if ($.browser.msie && $.browser.version <= 6) {
        $('img[src$=.png]').ifixpng();
    }
});

//---------------------------------------------------------
//
//	custom for facebook connect
//
//---------------------------------------------------------

// keeping this for user profile thumbnails
var fb_user_id;

/**
 * triggers when (a) the user logged into facebook via facebook
 * connect or (b) when the page is loaded with a valid session cookie
 * already active for this app - meaning the user is already logged 
 * into fb and has already authorized this app to access their stuff
 *
 * request the user's name from the api, then use it to initialize the
 * shoutbox
 */
function handleLogin(response)
{
	FB.api('/me', function(response) {
		fb_user_id = response.id;
		$('#login-instructions').fadeOut('slow');
		shoutbox_bot(response.name);
		$('#shoutbox_loader').fadeIn('slow');
		shoutbox_loader();
	});
}

/**
 * for logout the button will change per the fb js library
 * and we'll fade the instructions back in then remove the shoutbox
 * after it fades out
 */
function handleLogout(response)
{
	$('#login-instructions').fadeIn('slow');
	$('#shoutbox_box_inner').fadeOut('slow').remove();
}
