
var contextMenuX = null;
var contextMenuY = null;


/**
 *
 */
function documentClick(e) {
    var t;

    if (!e) {
      e = window.event;
    }

    if (e.target) {
      t = e.target;
    } else if (e.srcElement) {
      t = e.srcElement;
    }

    if (t.nodeType == 3) {
      // defeat Safari bug
      t = t.parentNode;
    }

    if (t.className.indexOf('GridTableCell') < 0 && t.id != 'GameContextMenu' && !hasParentWithId(t, 'GameContextMenu')) {
      hideBlock('GameContextMenu');
    }
}


/**
 *
 */
function makeAjaxCall(method, url, data, callbackFunction) {
  var req = null;

  if (window.XMLHttpRequest) {

    req = new XMLHttpRequest();

  } else if (window.ActiveXObject) {

    req = new ActiveXObject("Microsoft.XMLHTTP");

  } else {

    return;

  }

  req.onreadystatechange = callbackFunction;
  req.open(method, url, true);

  if (method.toLowerCase() == "post" && data != null) {

    req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8'); 

  } else {

    data = null;

  }

  req.send(data);
}


/**
 *
 */
function addBBCodeToTextArea(txtareaId, bbStart, bbEnd) {
  var textarea = document.getElementById(txtareaId);

  if (textarea == null) {
    return;
  }

  if (document.selection && document.selection.createRange) {

    if (document.selection.type == 'None') {

      textarea.selectionStart = -1;
      textarea.selectionEnd = -1;

    } else {

      // The current selection
      var range = document.selection.createRange();
      var t = range.text;
      range.text = bbStart + t + bbEnd;
      textarea.focus();
      return;

    }

  }

  if (textarea.selectionStart != null && textarea.selectionEnd != null && textarea.selectionStart >= 0 && textarea.selectionEnd >= 0) {

    var start = textarea.selectionStart;
    var end = textarea.selectionEnd;
    var scrollTop = textarea.scrollTop;

    var s1 = (textarea.value).substring(0, start);
    var s2 = (textarea.value).substring(start, end);
    var s3 = (textarea.value).substring(end);

    textarea.value = s1 + bbStart + s2 + bbEnd + s3;

    textarea.selectionStart = start;
    textarea.selectionEnd = end + bbStart.length + bbEnd.length;
    textarea.scrollTop = scrollTop;

  } else {

    textarea.value = textarea.value + bbStart + bbEnd;

  }

  textarea.focus();
}


/**
 *
 */
function checkAllCheckboxes(list) {
  if (list) {
    for (var i = 0; i < list.length; i++) {
      list[i].checked = true;
    }
  }
}


/**
 *
 */
function uncheckAllCheckboxes(list) {
  if (list) {
    for (var i = 0; i < list.length; i++) {
      list[i].checked = false;
    }
  }
}


/**
 *
 */
function showBlock(id) {
  document.getElementById(id).style.display = 'block';
}


/**
 *
 */
function hideBlock(id) {
  document.getElementById(id).style.display = 'none';
}


/**
 *
 */
function toggleBlock(id) {
  var element = document.getElementById(id);

  if (element.style.display == 'block') {
    element.style.display = 'none';
  } else {
    element.style.display = 'block';
  }
}


/**
 *
 */
function getPageXOffset() {
  var xOffset = 0;

  if (document.documentElement && (document.documentElement.scrollLeft > 0 || document.documentElement.scrollTop > 0)) {

    xOffset = document.documentElement.scrollLeft;

  } else if (document.body.scrollLeft > 0 || document.body.scrollTop > 0) {

    xOffset = document.body.scrollLeft;

  } else if (window.pageXOffset > 0 || window.pageYOffset > 0) {

    xOffset = window.pageXOffset;

  }

  return xOffset;
}


/**
 *
 */
function getPageYOffset() {
  var yOffset = 0;

  if (document.documentElement && (document.documentElement.scrollLeft > 0 || document.documentElement.scrollTop > 0)) {

    yOffset = document.documentElement.scrollTop;

  } else if (document.body.scrollLeft > 0 || document.body.scrollTop > 0) {

    yOffset = document.body.scrollTop;

  } else if (window.pageXOffset > 0 || window.pageYOffset > 0) {

    yOffset = window.pageYOffset;

  }

  return yOffset;
}


/**
 *
 */
var stayTopLeftElements = new Array();
var stayTopLeftCount = 0;


/**
 *
 */
function stayTopLeft(id, startX, startY, lagFactor) {
  var element = document.getElementById(id);

  stayTopLeftElements[stayTopLeftCount] = element;
  element.x = startX;
  element.y = startY;

  element.repositionElement = function(lag) {
    var xOffset = getPageXOffset();
    var yOffset = getPageYOffset();

    this.x += (xOffset + startX - this.x) / lag;
    this.y += (yOffset + startY - this.y) / lag;

    this.style.left = this.x + 'px';
    this.style.top = this.y + 'px';
  }

  element.repositionElement(1);
  setInterval("stayTopLeftElements[" + stayTopLeftCount + "].repositionElement(" + lagFactor + ")", 10);
  stayTopLeftCount++;
}


/**
 *
 */
var dialogElements = new Array();
var dialogCount = 0;


/**
 *
 */
function createYesNoDialog(title, msg, action, category, x, y) {
  var parent = document.getElementById('Container');
  var dialog = null;
  var dialogNumber = dialogCount++;
  var dialogId = 'DialogWindow' + dialogNumber;

  if (!parent || !document.createElement || !parent.appendChild || !parent.removeChild) {
    return null;
  }

  for (var i = 0; i < dialogNumber; i++) {
    dialog = dialogElements[i];

    if (category && dialog.dialogCategory == category) {
      removeDialog(dialog.id);
    }
  }

  dialog = document.createElement('div');
  dialog.className = 'DialogWindow';
  dialog.id = dialogId;
  dialog.innerHTML = ''
    + '<div class="DialogTitle">' + title + '</div>'
    + '<div class="DialogImage"></div>'
    + '<div class="DialogMessage">' + msg + '</div>'
    + '<div class="DialogButtonArea">'
    + '  <div class="DialogButton"></div>'
    + '  <div class="DialogButton">'
    + '    <a href="#" onclick="removeDialog(\'' + dialogId + '\'); return false;">No</a>'
    + '  </div>'
    + '  <div class="DialogButton">'
    + '    <a href="#" onclick="' + action + '; hideBlock(\'' + dialogId + '\'); return false;">Yes</a>'
    + '  </div>'
    + '</div>';
  dialog.dialogCategory = category;

  dialogElements[dialogNumber] = dialog;
  parent.appendChild(dialog);
  stayTopLeft(dialogId, x, y, 4);

  return dialog;
}


/**
 *
 */
function removeDialog(id) {
  var parent = document.getElementById('Container');
  var dialog = document.getElementById(id);

  if (!parent || !dialog || !document.createElement || !parent.appendChild || !parent.removeChild) {
    return false;
  }

  parent.removeChild(dialog);

  return true;
}


/**
 *
 */
function dummyRequest() {
  makeAjaxCall('GET', '../dummy.html', null, dummyRequestCallback);
}


/**
 *
 */
function dummyRequestCallback() {
}


/**
 *
 */
function getElementsByClass(searchClass, node, tag) {
  var classElements = new Array();

  if (node == null) {
    node = document;
  }

  if (tag == null) {
    tag = '*';
  }

  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');

  for (i = 0, j = 0; i < elsLen; i++) {

    if (pattern.test(els[i].className)) {

      classElements[j] = els[i];
      j++;

    }

  }

  return classElements;
}


/**
 *
 */
function hasParentWithId(node, id) {
  var parent = node.parentNode;

  while (parent != null) {
    if (parent.id == id) {
      return true;
    }

    parent = parent.parentNode;
  }

  return false;
}


/**
 *
 */
function initialize() {
  document.onclick = documentClick;
}

