﻿function e(id){ 
  return document.getElementById(id) || null;
}

function changeDisplayState (id,img) {
  var e=document.getElementById(id);
  var img1 = document.getElementById(img);
  if (e.style.display == 'none' || e.style.display ==""){
    e.style.display = 'block';
    img1.setAttribute("src", "files/minus.gif");
  }
  else{
    e.style.display = 'none';
    img1.setAttribute("src", "files/plus1.gif");
  }
}

function show_detail (id,img) {
  var e = document.getElementById(id);
  var img1 = document.getElementById(img);
  if (e.style.display == 'none' || e.style.display ==""){
    e.style.display = 'block';
    img1.setAttribute("src", "files/minus.gif");
  }
  else{
    e.style.display = 'none';
    img1.setAttribute("src", "files/plus1.gif");
  }
  return false;
}


function hiddenTR(vtrGroup, show_el, hide_el){
  trGroupElements = document.getElementsByTagName('TR');
  for (var i=0; i < trGroupElements.length; i++){
    if (trGroupElements[i].attributes['group'].value == vtrGroup){
      trGroupElements[i].style.display='none';
    }
  
  }
  var s=document.getElementById(show_el);
  s.style.display = 'inline';
  var h=document.getElementById(hide_el);
  h.style.display = 'none';
}

function showTR(vtrGroup, show_el, hide_el){
  trGroupElements = document.getElementsByTagName('TR');
  for (var i=0; i < trGroupElements.length; i++){
    if (trGroupElements[i].attributes['group'].value == vtrGroup){
      trGroupElements[i].style.display='block';
    }
  }
  var s=document.getElementById(show_el);
  s.style.display = 'inline';
  var h=document.getElementById(hide_el);
  h.style.display = 'none';
}

function live_chat(chats)
{
 var me = this, active = [], load_listeners = [], update_listeners = [];
 
 this.online = function()
 {
  return active.length;
 }
 
 this.open = function(n)
 {
  if (typeof n == "undefined")
   n = active.length ? active[0] : 0;
  if (chats[n])
   window["ps" + chats[n].id + "ow"]();
 }
 
 this.open_random = function()
 {
  var n;
  if (arguments.length)
  {
   for (var i = 0; i < arguments.length; i++)
    if ((i + 1) in active && Math.random() * 100 <= arguments[i])
     n = i + 1;
  }
  else if (active.length)
   n = active[Math.floor(Math.random() * (active.length + 1))];
  me.open(n);
 }
 
 this.onload = function(f)
 {
  if (typeof f != "function") return
  for (var i in load_listeners)
   if (load_listeners[i] == f) return;
  load_listeners.push(f);
  if (active.length) f(chats, active);
 }
 
 this.onupdate = function(f)
 {
  if (typeof f != "function") return
  for (var i in update_listeners)
   if (update_listeners[i] == f) return;
  update_listeners.push(f);
  //if (active.length) f(chats, active);
 }
 
 this.remove = function(f)
 {
  for (var i in load_listeners)
   if (load_listeners[i] == f)
    load_listeners.splice(i, 1);
  for (var i in update_listeners)
   if (update_listeners[i] == f)
    update_listeners.splice(i, 1);
 }
 
 this.status = function()
 {
  var loaded = false;
  for (var i in chats)
   if (typeof window["ps" + chats[i].id + "ol"] != "undefined")
    loaded = true;
  if (!loaded) return false;
  active = [];
  for (var i in chats)
   if (window["ps" + chats[i].id + "ol"])
    active.push(i);
  return true;
 }
 
 var checks = 0, checker = setInterval(function() {
  if (checks++ < 60 && !me.status()) return;
  clearTimeout(checker);
  for (var i in load_listeners)
   load_listeners[i](chats, active);
  setInterval(function() {
   var l = active.length;
   me.status();
   if (active.length != l)
    for (var i in update_listeners)
     update_listeners[i](chats, active);
  }, 10000);
 }, 1000);
}

