/* $Id: items.js 497 2011-04-03 03:24:58Z Pr0g $ */

var show_tooltip = false;
var tooltip_visible = false;
var toolbox_init = false;
var toolbox_updating = true;
var toolbox_wnd = null;
var toolbox_content = null;
var toolbox_icon = null;

function init_tooltip() {
  toolbox_wnd = document.getElementById('tooltip_wnd');
  toolbox_content = document.getElementById('tooltip_content');
  toolbox_icon = document.getElementById('tooltip_image');
  toolbox_init = true;
}

function mousepos(event) {
  if (show_tooltip) {
    if(!event && window.event) {
      event = window.event;
    }
    var posx = event.clientX;
    var posy = event.clientY;
    var offset = (window.document.compatMode && window.document.compatMode == 'CSS1Compat') ? window.document.documentElement : window.document.body || null;
    var offsetx = document.documentElement.scrollLeft || document.body.scrollLeft;
    var offsety = document.documentElement.scrollTop || document.body.scrollTop;
    var new_top = posy + 20;
    var new_left = posx + 20;
    var wnd_height = (isNaN(window.innerHeight) ? offset.clientHeight : window.innerHeight);
    var wnd_width = (isNaN(window.innerWidth) ? offset.clientWidth : window.innerWidth);
    if (toolbox_wnd.offsetHeight + new_top + 16 > wnd_height) {
      new_top = wnd_height - toolbox_wnd.offsetHeight - 16;
    }
    if (toolbox_wnd.offsetWidth + new_left + 36 > wnd_width) {
      new_left = posx - 20 - toolbox_wnd.offsetWidth;
    }
    toolbox_wnd.style.top = (new_top + offsety) + 'px';
    toolbox_wnd.style.left = (new_left + offsetx) + 'px';
    if (!tooltip_visible) {
      tooltip_visible = true;
      toolbox_wnd.style.display = 'block';
      mousepos(event);
    }
  }
}

function show_tt_wnd(content, image) {
  if (!toolbox_init) {
    return;
  }
  document.onmousemove = mousepos;
  show_tooltip = true;
  toolbox_content.innerHTML = content;
  toolbox_icon.src = '/~upload/itemicons/56/' + image + '.jpg';
}

function show_tt_wnd_loading() {
  if (!toolbox_init) {
    return;
  }
  document.onmousemove = mousepos;
  show_tooltip = true;
  toolbox_content.innerHTML = (toolbox_updating ? 'Daten werden nachgeladen...' : 'Item nicht gefunden.');
}

function hide_tt_wnd() {
  show_tooltip = false;
  tooltip_visible = false;
  toolbox_wnd.style.display = 'none';
  toolbox_wnd.style.width = 'auto';
  toolbox_icon.src = '/~gfx/loader.gif';
}

var item_request = false;

function item_check_request() {
  if (item_request.readyState == 4) {
    if (item_request.status == 200) {
      var item_xml_doc = item_request.responseXML;
      var item_xml_node = item_xml_doc.getElementsByTagName('status').item(0);
      if (item_xml_node.firstChild.data == 'good') {
        item_xml_node = item_xml_doc.getElementsByTagName('item');
        for (var i = 0; i < item_xml_node.length; i++) {
          var item_obj = document.getElementById('item_new_' + item_xml_node.item(i).getAttribute('span_id'));
          item_obj.className = 'itt_bbcode itt_cq' + item_xml_node.item(i).getAttribute('quality');
          if (item_obj.innerHTML == '[]') {
            item_obj.innerHTML = '[' + item_xml_node.item(i).getAttribute('name') + ']';
          }
          eval('item_obj.onmouseover = function () { show_tt_wnd(\'' + item_xml_node.item(i).firstChild.data + '\', \'' + item_xml_node.item(i).getAttribute('icon') + '\') }');
          item_obj.onmouseout = function () { hide_tt_wnd(); }
        }
      }
      toolbox_updating = false;
      hide_tt_wnd();
    }
  }
}

function item_init_http_request() {
  if (window.XMLHttpRequest) {
    item_request = new XMLHttpRequest();
    if (item_request.overrideMimeType) {
      item_request.overrideMimeType('text/xml');
    }
  } else if (window.ActiveXObject) {
    try {
      item_request = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        item_request = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {}
    }
  }
  if (item_request) {
    item_request.onreadystatechange = item_check_request;
  }
}

function load_items() {
  init_tooltip();
  var i = 0;
  var item_list = '';
  while (true) {
    var item_obj = document.getElementById('item_new_' + ++i);
    if (item_obj == null) {
      break;
    }
    item_id = item_obj.getAttribute('rel');
    item_list += i + '~' + (item_id == null ? 0 : item_id) + '~' + item_obj.innerHTML + '/';
  }
  if (item_list != '') {
    item_list = 'items=' + item_list;
    item_init_http_request();
    if (item_request) {
      item_request.open('POST', '/getitem.php', true);
      item_request.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
      item_request.setRequestHeader('Content-length', item_list.length);
      item_request.setRequestHeader('Connection', 'close');
      item_request.send(item_list);
    }
  }
}
