/*  Norcode.org Ajax Chat v1.0 - Chat via the web with Js/jQuery
    Copyright (C) 2009  Tom A. Barstad
      This program is released under the GPL License terms
      Read the LICENSE file for more information.
*/

function include(file) {
  var head = document.getElementsByTagName('head')[0];
  var script = document.createElement('script');
  script.setAttribute('type', 'text/javascript');
  script.setAttribute('src', file);
  head.appendChild(script);
}

var lastid = 0;
var loading_c = 1;
var loading_u = 1;
var snd = true;
var kicked = false;
var rcvTimer = 0;



include("js/userlist.js");
include("js/chatcontent.js");

function msgSound() {
  if (snd == true) {
    $('#tmp').html('<embed src="http://offload.tegnelyst.no/tlnymelding.wav" hidden="true"'+
                   'autostart="true" loop="false" width="0" height="0">');
   
  }
}

function toggleSnd() {
  if (snd == false) {
    // enable
    $("#sndbtn").val("[X] Lydvarsling");
    snd = true;
  } else {
    // disable
    $("#sndbtn").val("[ ] Lydvarsling");
    snd = false;
  }
}

function closeChat() {
  if (kicked == true) { return; } // Indicates that we're kicked...
  $.get('dycont.php?hndl=logout&chan='+channel, function(data) {
    self.close(); /* Remove if you don't want to close window on closeChat.
                      Mostly meant for popup-window-chat.  */
   
    window.location.reload(true); // Reload back to login.
  });
}

function sendChatText() {
  if (kicked == true) { return; } // Indicates that we're kicked...
  if (loading_c != 1) { // Ready to send text (chat content not loading)
    var text = $('#textline').val();
    $('#textline').val('');
    if (text != "") {
//      stopRCV();
      $.post('dycont.php?hndl=sendChatText&chan='+channel,{
        d1: text
      }, function(data) {
//        resetRCV();
      });
    }
  }
}

function initChat() {
  if (kicked == true) { return; } // Indicates that we're kicked...
  $.get('dycont.php?hndl=login&chan='+channel, function(data) {
    updateUserList();
    rcvTimer = setTimeout(function(){updateContent()},500);
  });
}
