var x,y;
var isDrag = false;
var DragEl = 0;
var TBLNR = 0;
var TBLS = new Array();
var Divs = new Array();


var firefox=document.getElementById&&!document.all;

document.onmousemove=mouseMove;
document.onmousedown=selectMouse;
document.onmouseup=function(){
  isDrag=false;
}

function selectMouse(Ereignis) {
  if (!Ereignis)
    var Ereignis = window.event;
  var el = (firefox) ? Ereignis.target : Ereignis.srcElement;
  var cNode = el.parentNode;
  while (cNode) {
    if (cNode.nodeType == 1 && cNode.nodeName == 'TABLE' && cNode.attributes['class'] && cNode.attributes['class'].nodeValue == 'etbl') {
      var etbl = cNode;
      break;
    }
    cNode = cNode.parentNode;
  }
  if (etbl) {
    if (etbl.style.zIndex != TBLS.length) {
      for (var i = 0; i < TBLS.length; i++) {
        if (TBLS[i].ref.style.zIndex > etbl.style.zIndex)
          TBLS[i].ref.style.zIndex -= 1;
      }
      etbl.style.zIndex = TBLS.length;
    }
    if ((el.nodeName == 'TD' && el.attributes['class'] && el.attributes['class'].nodeValue == 'etbl_title') || (el.parentNode.nodeName == 'TD' && el.parentNode.attributes['class'] && el.parentNode.attributes['class'].nodeValue == 'etbl_title')) {
      isDrag = true;
      for (var i = 0; i < TBLS.length; i++) {
        if (TBLS[i].ref.attributes['id'].nodeValue == etbl.attributes['id'].nodeValue)
          break;
      }
      //alert(etbl.attributes['id'].nodeValue+' ID: '+i);
      DragEl = i;
      TBLS[DragEl].posY = parseInt(TBLS[DragEl].ref.style.top);
      TBLS[DragEl].posX = parseInt(TBLS[DragEl].ref.style.left);
      x = Ereignis.clientX;
      y = Ereignis.clientY;
    }
  }
}

function mouseMove(Ereignis) {
  if (isDrag) {
    if (!Ereignis)
      Ereignis = window.event;
    npY = Ereignis.clientY + (TBLS[DragEl].posY - y);
    npX = Ereignis.clientX + (TBLS[DragEl].posX - x);
    if (TBLS[DragEl].ref.offsetWidth+npX-document.body.scrollLeft <= bwidth && npX-document.body.scrollLeft >= 5)
      TBLS[DragEl].ref.style.left = npX;
    if (TBLS[DragEl].ref.offsetHeight+npY-document.body.scrollTop <= bheight && npY- document.body.scrollTop >= 5)
      TBLS[DragEl].ref.style.top = npY;
  }
}

function show_div(name, title) {
  var newDiv = document.createElement('div');
  if (Divs[name])
    HtmlCode = Divs[name];
  else {
    var altDiv = document.getElementById(name+'Div');
    Divs[name] = altDiv.innerHTML;
    HtmlCode = Divs[name];
    altDiv.innerHTML = "";
  }
  newDiv.innerHTML = HtmlCode;
  if (newDiv.childNodes[0].nodeType == 3)
    newDiv.removeChild(newDiv.childNodes[0]);
  createExtraTBL(title, newDiv.childNodes, {width:500, padding:10});
}

function show_error(title, text) {
  var Div = document.createElement('div');
  Div.innerHTML = '<p style="font-size: 12px;">' + text + '</p>';
  createExtraTBL(title, Div.childNodes, {width:500});
}

if (typeof(f_offsetTop) == 'undefined') {
function f_offsetTop(el) {
  y = el.offsetTop
  for (e = el.offsetParent; e; e = e.offsetParent)
    y += e.offsetTop;
  return y
}
}

if (typeof(f_offsetLeft) == 'undefined') {
function f_offsetLeft(el) {
  x = el.offsetLeft
  for (e = el.offsetParent; e; e = e.offsetParent)
    x += e.offsetLeft;
  return x
}
}

function createExtraTBL(title, childnodes, params) {
  var width = (!params.width) ? 350 : params.width;
  var m_TBL = document.getElementById('mainTBL');
  var posX = (!params.posX) ? parseInt(f_offsetLeft(m_TBL)+(m_TBL.offsetWidth/2)-(width/2)+(TBLS.length*20)) : params.posX;
  var posY = (!params.posY) ? f_offsetTop(m_TBL)+250+(TBLS.length*20) : params.posY;
  TBLNR++;
  for (i = 0; i < TBLS.length; i++) {
    if (TBLS[i].title == title) {
      TBLS[i].ref.style.left = posX+'px';
      TBLS[i].ref.style.top = posY+'px';
      return false;
    }
  }

  // define table
  var Table = document.createElement('table');
  var attr = document.createAttribute('id');
  attr.nodeValue = 'extratbl' + TBLNR;
  Table.setAttributeNode(attr);
  attr = document.createAttribute('width');
  attr.nodeValue = width+'px';
  Table.setAttributeNode(attr);
  attr = document.createAttribute('class');
  attr.nodeValue = 'etbl';
  Table.setAttributeNode(attr);
  attr = document.createAttribute('border');
  attr.nodeValue = '0';
  Table.setAttributeNode(attr);
  attr = document.createAttribute('cellspacing');
  attr.nodeValue = '0';
  Table.setAttributeNode(attr);
  attr = document.createAttribute('cellpadding');
  attr.nodeValue = '0';
  Table.setAttributeNode(attr);
  Table.style.left = posX+'px';
  Table.style.top = posY+'px';

  // define colgroup
  var Colgroup = document.createElement('colgroup');
  var Col = document.createElement('col');
  Colgroup.appendChild(Col);
  Col = document.createElement('col');
  attr = document.createAttribute('width');
  attr.nodeValue = '22';
  Col.setAttributeNode(attr);
  Colgroup.appendChild(Col);
  Table.appendChild(Colgroup);

  var Tbody = document.createElement('tbody');
  Table.appendChild(Tbody);

  // define header row
  var Row = document.createElement('tr');
  var Cell = document.createElement('td');
  attr = document.createAttribute('class');
  attr.nodeValue = 'etbl_title';
  Cell.setAttributeNode(attr);
  var Title = document.createTextNode(title);
  NoBr = document.createElement('nobr');
  NoBr.appendChild(Title);
  Cell.appendChild(NoBr);
  Row.appendChild(Cell);
  Cell = document.createElement('td');
  attr = document.createAttribute('class');
  attr.nodeValue = 'etbl_close';
  Cell.setAttributeNode(attr);
  var Link = document.createElement('a');
  attr = document.createAttribute('href');
  attr.nodeValue = 'javascript:closeExtraTBL(' + TBLNR + ')';
  Link.setAttributeNode(attr);
  var Image = document.createElement('img');
  attr = document.createAttribute('src');
  attr.nodeValue = 'http://www.bigvibez.com/img/etbl_close.gif';
  Image.setAttributeNode(attr);
  attr = document.createAttribute('border');
  attr.nodeValue = '0';
  Image.setAttributeNode(attr);
  Link.appendChild(Image);
  Cell.appendChild(Link);
  Row.appendChild(Cell);
  Tbody.appendChild(Row);

  Row = document.createElement('tr');
  Cell = document.createElement('td');
  attr = document.createAttribute('colspan');
  attr.nodeValue = '2';
  Cell.setAttributeNode(attr);
  if (params.padding)
    Cell.style.padding = params.padding+'px';

  for (var i = 0; i < childnodes.length; i++) {
    Cell.appendChild(childnodes[i]);
  }

  Row.appendChild(Cell);
  Tbody.appendChild(Row);
  TBLS.push({'tblnr': TBLNR, 'title': title, 'width': width, 'posX': posX, 'posY': posY, 'ref': Table, 'params': params});
  Table.style.zIndex = TBLS.length;
  document.getElementById('content').insertBefore(Table, document.getElementById('content').firstChild);
}


function closeExtraTBL(tblid) {
  document.getElementById('content').removeChild(document.getElementById('extratbl'+tblid));
  for (var i = 0; i < TBLS.length; i++) {
    if (TBLS[i].tblnr == tblid)
      break;
  }
  if (typeof(TBLS[i].params.onClose) == 'function')
    TBLS[i].params.onClose(TBLS[i]);
  TBLS.splice(i, 1);
}
