
function timestamp() {
  var date = new Date();
  return date.getTime();
}

function launchChatWindow(is_live_chat_window)
{
  var request_url = "http://support.playukinternet.com/support/users/tickets.php?op=add";
  var mywidth   = 780;
  var myheight  = 600;

  if(is_live_chat_window)
  {
    request_url = "/sfChat/register.html";
    mywidth   = 770;
    myheight  = 570;
  }

  var new_win = window.open(request_url, timestamp(), "scrollbars,resizable,width=" + mywidth + ",height=" + myheight);
  //new_win.focus();
}

var previous_status = "";

function checkOnlineOperators()
{
  var data =
  {
    "mode" : "checkOnlineOperators"
  }
  var params =  $j.toJSON(data);

  $j.post("/frontend.php/sfChat/update.html", { data: params}, displayOperatorStatus, "text");

  // we will check again for online operators
  setTimeout("checkOnlineOperators();", 30000);
}

/* function that shows if there are any operators online */
function displayOperatorStatus(res)
{
  var obj = $j.evalJSON(res);

  if(obj.status != previous_status)
  {
    $j("#imgLiveSupport").attr("src", "/sfLiveChatPlugin/images/icons/" + obj.status + "-" + obj.culture + ".gif");
    previous_status = obj.status;

    $j("#imgLiveSupport").unbind("click");

    if(obj.status == "online")
    {
      $j("#imgLiveSupport").bind("click", function(){
        launchChatWindow(true);
      });
    }
    else
    {
      $j("#imgLiveSupport").bind("click", function(){
        launchChatWindow(false);
      });
    }
  }
}

function launchAdminLiveChatWindow()
{
  var w=window.open('/backend.php/sfChat/admin', 'myWindow', 'width=955,height=570,left=150,top=150,scrollbars=yes');
  w.focus();
}
