/**
*
* @package mChat javascript code
* @version 1.0.2 of 26.04.2009
* @copyright (c) By Shapoval Andrey Vladimirovich (AllCity) ~ http://allcity.net.ru/
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
*
*/

// Sound function
function mChat_sound(file)
{
  if($.browser.msie)
  {
    // For IE ;)
    document.getElementById('mChatSound').innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" height="0" width="0" type="application/x-shockwave-flash"><param name="movie" value="' + file + '"></object>';
  }
    else
  {
    // For FireFox, Opera, Safari...
    $('#mChatSound').html('<embed src="' + file + '" width="0" height="0" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash"></embed>');
  }
}

// Toggle function
function mChat_toggle(id)
{
  // Togle :)
  $('#mChat' + id).slideToggle('fast');
}

// Send function
function mChat_add()
{
  // Refresh stop
  window.clearInterval(interval);
    // If message input empty stop Send function
    if($('#mChatMessage').val() == '')
    {
      // Error alert
      alert(mChatNoMessageInput);
      // Stop!
      return;
    }
      $.ajax({
        url: mChatFile,
        timeout: 10000,
        type: 'POST',
        async: false,
        data: $('#mChatForm').serialize(),
        success: function()
        {
          // Empty message input
          $('#mChatMessage').val('');
          // Run refresh function
          mChat_refresh();
        },
        error: function(XMLHttpRequest)
        {
          if(XMLHttpRequest.status == 400)
          {
            // Flood alert
            alert(mChatFlood);
          }
            else if(XMLHttpRequest.status == 403)
          {
            // No access alert
            alert(mChatNoAccess);
          }
            else if(XMLHttpRequest.status == 501)
          {
            // No message alert
            alert(mChatNoMessageInput);
          }
        }
      });
  // Start refresh
  interval = setInterval(function(){mChat_refresh()}, mChatRefresh);
}

// Edit function
function mChat_edit(id)
{
  // Refresh stop
  window.clearInterval(interval);
    var message = $('#edit' + id).val();
    var data = prompt(mChatEditInfo, message)
      if(data)
      {
        // AJAX request
        $.ajax({
          url: mChatFile,
          timeout: 10000,
          type: 'POST',
          async: false,
          data: {mode: 'edit', message_id: id, message: data},
          success: function(html)
          {
            // Replace old edited message to new with animation
            $('#mess' + id).fadeOut('slow', function(){
              $(this).replaceWith(html);
              // Animation
              $('#mess' + id).css('display', 'none').fadeIn('slow');
            });
          },
          error: function(XMLHttpRequest)
          {
            if(XMLHttpRequest.status == 403)
            {
              // No access alert
              alert(mChatNoAccess);
            }
              else if(XMLHttpRequest.status == 501)
            {
              // No message alert
              alert(mChatNoMessageInput);
            }
          }
        });
      }
  // Start refresh
  interval = setInterval(function(){mChat_refresh()}, mChatRefresh);
}

// Delete function
function mChat_delete(id)
{
  // Refresh stop
  window.clearInterval(interval);
    // AJAX request
    $.ajax({
      url: mChatFile,
      timeout: 10000,
      type: 'POST',
      async: false,
      data: {mode: 'delete', message_id: id},
      success: function()
      {
        // Animation ;)
        $('#mess' + id).fadeOut('slow', function(){
          // Remove message
          $(this).remove();
        });
        // Sound
        mChat_sound(mChatForumRoot + 'mchat/del.swf');
      },
      error: function()
      {
        // Not Extended alert
        alert(mChatNoAccess);
      }
    });
  // Start refresh
  interval = setInterval(function(){mChat_refresh()}, mChatRefresh);
}

// Refresh function
function mChat_refresh()
{
  var mess_id = 0;
    // Fix it all message deleted
    if($('#mChatData').find('div:first').not('#mChatNoMessage').attr('id') != undefined)
    {
      mess_id = $('#mChatData').find('div:first').attr('id').replace('mess', '');
    }
      // AJAX request
      $.ajax({
        url: mChatFile,
        timeout: 10000,
        type: 'POST',
        async: false,
        data: {mode: 'read', message_last_id: mess_id},
        beforeSend: function()
        {
          // Indicator status
          $('#mChat_load_img').show();
          $('#mChat_ok_img').hide();
          $('#mChat_error_img').hide();
        },
        success: function(html)
        {
          // If not empty run its part
          if(html != '')
          {
            // Prepend data to mChat
            $('#mChatData').prepend(html).find('div:first').not('#mChatNoMessage').css('display', 'none');
            // Animation ;)
            $('#mChatData div:first').not('#mChatNoMessage').fadeIn('slow');
            // Sound
            mChat_sound(mChatForumRoot + 'mchat/add.swf');
            // Hide no message if exist
            $('#mChatNoMessage').hide();
          }
          // setTimeout for IE fix
          setTimeout(function(){
            // Indicator status
            $('#mChat_load_img').hide();
            $('#mChat_ok_img').show();
            $('#mChat_error_img').hide();
          }, 1000);
        },
        error: function()
        {
          // Indicator status
          $('#mChat_load_img').hide();
          $('#mChat_ok_img').hide();
          $('#mChat_error_img').show();
          // Sound
          mChat_sound(mChatForumRoot + 'mchat/error.swf');
        },
        complete: function()
        {
          // Set no message
          if($('#mChatData').find('div:first').not('#mChatNoMessage').attr('id') == undefined)
          {
            $('#mChatNoMessage').show('slow');
          }
        }
      });
}

// ReFresh mChat
var interval = setInterval(function(){mChat_refresh()}, mChatRefresh);