// JavaScript Document

//var timeout = <?= $shoutbox_refresh ?>;
var timeout = 15;

try
{
  if ( jQuery )
  {
    // Set timer for updating shoutbox
    jQuery( document ).ready(
      function()
      {
        doShoutBoxUpdate();
      }
    );
  }
}
catch ( e ) {
//  alert( e );
}

function updateShoutbox()
{
  try
  {
    var url = '/extra/shoutbox.php?' + String( Math.floor( Math.random() * 100000 ) );

    var elem = jQuery( '#shoutbox-items' );

    elem.load( url, {}, function() {
      if ( jQuery().jScrollPane )
      {
        elem.jScrollPane( { showArrows: true } );
      }
    } );

  }
  catch ( e ) {
//    alert( e );
  }
}

function doShoutBoxUpdate()
{
  try
  {
    updateShoutbox();

//    alert( timeout * 1000 );

    setTimeout( doShoutBoxUpdate, timeout * 1000 );
  }
  catch ( e ) {}
}

function beforeMessageSent()
{
  try
  {
    jQuery( '#shoutbox-form' ).hide();
    jQuery( '#shoutbox-loader' ).show();
  }
  catch ( e ) {}
}

function afterMessageSent()
{
  try
  {
    updateShoutbox();

    jQuery( 'textarea#shoutbox_message' ).val( '' );
    jQuery( '#shoutbox-loader' ).hide();
    jQuery( '#shoutbox-form' ).show();
  }
  catch ( e ) {}
}

function bindSubmitEvent( uniqueId, sendUrl )
{
  try
  {
    jQuery( 'form#' + uniqueId ).bind(
      'submit',
      function( event ) {

        beforeMessageSent();

          var formdata = jQuery( 'form#' + uniqueId ).serializeArray();

          jQuery.ajax( {
            type: "POST",
            url: sendUrl,
            data: formdata,
            success: function( msg ) { afterMessageSent(); }
            }
          );

        return false;
      }
    );
  }
  catch ( e ) {}
}

function checkMsg( msg )
{
  try
  {
    var val = jQuery( '#shoutbox_message' ).val()

    if ( !val || val.trim() == '' )
    {
      jQuery( '#shoutbox_message' ).val( '' );
      alert( msg );
      return false;
    }
  }
  catch ( e ) {}

  return true;
}
