/* $Id: tsviewer.js 295 2010-05-10 00:50:46Z Pr0g $ */

var tsv_req = false;
var tsv_offset = 0;
var tsv_li_w = 0;
var tsv_w = 0;
var tsv_ul = null;
var tsv_hash = '';

function tsviewer_request() {
  if (tsv_req.readyState == 4) {
    if (tsv_req.status == 200) {
      tsv_ul = document.getElementById('tsv');
      var tsv_x_doc = tsv_req.responseXML;
      var tsv_x_node = tsv_x_doc.getElementsByTagName('status').item(0);
      var tsv_x_status = tsv_x_node.firstChild.data;
      if (tsv_x_status == 'ok' || tsv_x_status == 'notmodified') {
        tsv_x_node = tsv_x_doc.getElementsByTagName('token').item(0);
        ts_viewer_token = tsv_x_node.firstChild.data;
        tsv_x_node = tsv_x_doc.getElementsByTagName('hash').item(0);
        tsv_hash = tsv_x_node.firstChild.data;
        if (tsv_x_status == 'ok') {
          tsv_x_node = tsv_x_doc.getElementsByTagName('data').item(0);
          var tsv_list = tsv_x_node.firstChild.data;
          tsv_w = document.getElementById('ts_viewer').offsetWidth;
          tsv_ul.innerHTML = tsv_list;
          if (tsv_li_w == 0) {
            for (var i = 0; i < tsv_ul.childNodes.length - 1; i++) {
              if (tsv_ul.childNodes[i].childNodes[0].offsetWidth > tsv_li_w) {
                tsv_li_w = tsv_ul.childNodes[i].childNodes[0].offsetWidth;
              }
            }
          }
          if (tsv_li_w > tsv_w) {
            tsv_li_w += 5;
            tsv_ul.onmousemove = tsv_move;
          } else {
            tsv_ul.onmousemove = null;
          }
        }
      } else {
        tsv_ul.innerHTML = '<li>Widgetfehler</li>';
      }
    }
  }
}

function tsv_reload() {
  tsv_req.onreadystatechange = tsviewer_request;
  tsv_req.open('GET', '/gettsviewer.php?token=' + ts_viewer_token + '&hash=' + tsv_hash, true);
  tsv_req.send(null);
}

function init_tsviewer() {
  if (window.XMLHttpRequest) {
    tsv_req = new XMLHttpRequest();
    if (tsv_req.overrideMimeType) {
      tsv_req.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) {
    try {
      tsv_req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        tsv_req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (tsv_req) {
    tsv_req.onreadystatechange = tsviewer_request;
    tsv_req.open('GET', '/gettsviewer.php?token=' + ts_viewer_token, true);
    tsv_req.send(null);
    window.setInterval('tsv_reload()', 120000);
  }
}

function tsv_move(e) {
  if (!e) { e = window.event; }
  var mx = (typeof(e.offsetX) != 'undefined' && !window.opera ? e.offsetX : e.pageX - e.target.offsetLeft);
  var t = (e.target) ? e.target : e.srcElement;
  var tx = t.offsetLeft;
  if (tx < 0) { tx = tsv_offset; }
  mx += tx - tsv_ul.offsetLeft - tsv_offset;
  tsv_offset = Math.round(mx / tsv_w * (tsv_li_w - tsv_w));
  if (tsv_offset < tsv_li_w - tsv_w) { tsv_ul.style.marginLeft = '-' + tsv_offset + 'px'; }
}

function tsv_connect(version, channel) {
  var ts_link = null;
  if (version == 2) {
    var login_name = null;
    var login_pass = null;
    var nick = prompt('Bitte gib deinen Nickname ein:', '');
    if (nick != null) {
      if (confirm('Bist du auf dem Server registriert?')) {
        login_name = prompt('Bitte gib deinen Loginnamen ein:', '');
        if (login_name != null && login_name != '') {
          login_pass = prompt('Bitte gib dein Loginpasswort ein:', '')
        }
      }
      ts_link = 'teamspeak://' + ts_viewer_host + ':' + ts_viewer_port + '?channel=' + channel + '?nickname=' + nick;
      if (login_name == null || login_pass == null || login_name == '' || login_pass == '') {
        ts_link += '?password=' + ts_viewer_password;
      } else {
        ts_link += '?serverpassword=' + ts_viewer_password + '?loginname=' + login_name + '?password=' + login_pass;
      }
    }
  } else {
    if (confirm('Mit dem TeamSpeak Server verbinden?')) {
      ts_link = 'ts3server://' + ts_viewer_host + '?port=' + ts_viewer_port + '&password=' + ts_viewer_password + '&channel=' + channel;
    }
  }
  if (ts_link != null) {
    var ts_wnd = window.open(ts_link);
    ts_wnd.close();
  }
}
