var blnChatFocus = false;
var intTimeOut = 50;
var strCharacters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz!@#$^&*()-_=+[]{}<>;:,./|\?";

/********************************************************************************************************/
/* Name         : ChatFocus()
/* Parameters   : None
/* Date Created : 2011-02-22
/* Description  :
/********************************************************************************************************/

function ChatFocus( blnFocus ) {
  try {
    blnChatFocus = blnFocus;
  }
  catch(e) {
    txt="There was an error in the script ChatFocus().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

/********************************************************************************************************/
/* Name         : goToPage()
/* Parameters   : None
/* Date Created : 2011-02-20
/* Description  :
/********************************************************************************************************/

function goToPage( strURL ) {
  try {
    window.location = strURL;
  }
  catch(e) {
    txt="There was an error in the script goToPage().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

/********************************************************************************************************/
/* Name         : ToggleIcon()
/* Parameters   : Object
/*              : Class name.
/* Date Created : 2011-02-23
/* Description  :
/********************************************************************************************************/

function ToggleIcon( objTag, strClass ) {
  try {
    objTag.className = strClass;
  }
  catch(e) {
    txt="There was an error in the script goToPage().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

/********************************************************************************************************/
/* Name         : typeScramble()
/* Parameters   : None
/* Date Created : 2011-01-23
/* Description  :
/********************************************************************************************************/

function typeScramble(strContainer, intLength) {
  try {
    strRandom = "";

    while ( strRandom.length < intLength ) {
      var intNum = Math.floor( Math.random() * strCharacters.length );
      strRandom += strCharacters.substring( intNum, intNum + 1 );
    }

    strRandom = strRandom.replace( "&", "&amp;" );
    strRandom = strRandom.replace( "<", "&lt;" );
    strRandom = strRandom.replace( ">", "&gt;" );

    var objDisplay = document.getElementById( strContainer );
    objDisplay.innerHTML = strRandom;
  }
  catch(e) {
    txt="There was an error in the script typeScramble().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }

  setTimeout( "typeScramble('" + strContainer + "', " + intLength + ")", intTimeOut );
}

/********************************************************************************************************/
/* Name         : initTTs()
/* Parameters   : None
/* Return Type  : None
/* Date Created : 2011-01-23
/* Description  :
/********************************************************************************************************/

function initTTs() {
  // Remove any previous tooltip incase we just reloaded the content. This removes just the tooltip
  // children from the body element.
  $('body > .tooltip').remove();

  // Initialise variables.
  var intTooltipCounter = 0;

  // Grab all the parent objects of objects with a tooltip class.
  $('.tooltip').parent().each(function() {
    intTooltipCounter += 1;

    // Get the unique id for the tooltip-parent or generate it in case it doesn't have one yet.
    var strParentID = $(this).attr('id');
    if ( !strParentID ) {
      strParentID = 'ttparent_' + intTooltipCounter;
      $(this).attr('id', strParentID);
    }

    // Give the tooltip an id that can be linked to its parent.
    var objTooltip = $(this).find('.tooltip');
    var strTooltipID = 'ttfor_' + strParentID;
    objTooltip.attr('id', strTooltipID);

    // Finally add the tooltip to the body.
    objTooltip.appendTo('body');

    // Create a special positioning event.
    function positionTT(event, id) {
      var ttoffsety = 0;
      var ttoffsetx = 0;
      var tt = $('#ttfor_' + id);

      if ( event.pageY > ($(window).height() / 2) ) {
        ttoffsety = tt.height() + 16;
      }

      if( event.pageX > ($(window).width() / 2) ) {
        ttoffsetx = tt.width() + 30;
      }

      tt.css({ left: (event.pageX + 15 - ttoffsetx), top: (event.pageY + 8 - ttoffsety) });
    }

    // Register various events.
    $(this).mouseenter(function(event) {
      var objTooltip = $('#ttfor_' + $(this).attr('id'));
      positionTT(event, $(this).attr('id'));
      objTooltip.show();
    });

    $(this).mouseleave(function() {
      $('div.tooltip').hide();
    });

    $(this).mousedown(function() {
      $('div.tooltip').hide();
    });

    $(this).mousemove(function(event) {
      positionTT(event, $(this).attr('id'));
    });
  });
}

/********************************************************************************************************/
/* Name         : (document).ready()
/* Parameters   : None
/* Return Type  : None
/* Date Created : 2011-02-22
/* Description  :
/********************************************************************************************************/

$(document).ready(function(){
  initTTs();

  $("#tab_chat").click(function() {
    $(this).removeClass("post_tabs_tab");
    $("#tab_log").removeClass("post_tabs_tab_active");

    $(this).addClass("post_tabs_tab_active");
    $("#tab_log").addClass("post_tabs_tab");

    $("#log").hide();

    $("#chat").show();
  });

  $("#tab_log").click(function() {
    $(this).removeClass("post_tabs_tab");
    $("#tab_chat").removeClass("post_tabs_tab_active");

    $(this).addClass("post_tabs_tab_active");
    $("#tab_chat").addClass("post_tabs_tab");

    $("#chat").hide();

    $("#log").show();
  });
});

/********************************************************************************************************/
/* Name         : ScrollChatUp()
/* Parameters   : None
/* Return Type  : None
/* Date Created : 2011-02-22
/* Description  :
/********************************************************************************************************/

function ScrollChatUp() {
  try {
    objChatTarget = document.getElementById('chat_target');

    objChatTarget.scrollTop = 0;
  }
  catch(e) {
    txt="There was an error in the script ScrollChatUp().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

/********************************************************************************************************/
/* Name         : SendChat()
/* Parameters   : None
/* Return Type  : None
/* Date Created : 2011-02-22
/* Description  :
/********************************************************************************************************/

function SendChat() {
  try {
    strChat = document.getElementById('chat_source').value;
    strChat = strChat.replace(/(^[\s\xA0]+|[\s\xA0]+$)/g, '');

    if ( strChat == "" ) {
      throw new Error("No empty chat messages allowed.");
    }

    $.post( "/srx/php/scripts/chat/set.php"
          , { msg: strChat }
          , function( data ) {
              $('#chat_target').html( data );
              ScrollChatUp();
              document.getElementById('chat_source').value = "";
            }
          );
  }
  catch(e) {
    txt="There was an error in the script SendChat().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

/********************************************************************************************************/
/* Name         : GetChat()
/* Parameters   : None
/* Return Type  : None
/* Date Created : 2011-02-22
/* Description  :
/********************************************************************************************************/

function GetChat() {
  try {
    $.get( "/srx/php/scripts/chat/get.php"
         , function( data ) {
             $('#chat_target').html( data );
           }
         );

    setTimeout( "GetChat()", 2000 );
  }
  catch(e) {
    txt="There was an error in the script GetChat().\n\n";
    txt+="Error description: (" + (e.number & 0xFFFF) + ") " + e.message + "\n\n";
    txt+="Click OK to continue.\n\n";
    alert(txt);
  }
}

