﻿// Elements

function __el (id) {
  return document.getElementById (id);
}

function __el_of (el,tag) {
  return el.getElementsByTagName (tag);
}

function _getX (el) {
  if (el.style && parseInt (el.style.left) && el.style.position == 'absolute') return parseInt (el.style.left);
  var x = 0;
  while (el) {
    x += parseInt (el.offsetLeft) + parseInt (el.clientLeft);
    el = el.offsetParent;
  }
  return x;
}
//function _getX (el) {
//  if (!el) return 0;
//  if (el.style && parseInt (el.style.left) && el.style.position == 'absolute') return parseInt (el.style.left);
//
//  return el.offsetLeft + _getX (el.offsetParent);
//}

function _getY (el) {
  if (el.style && parseInt (el.style.top) && el.style.position == 'absolute') return parseInt (el.style.top);
  var y = 0;
  while (el) {
    y += parseInt (el.offsetTop) + parseInt (el.clientTop);
    el = el.offsetParent;
  }
  return y;
}

function _width (obj) {
  if (obj.style && parseInt (obj.style.width)) return parseInt (obj.style.width);
  return obj.offsetWidth;
}

function _height (obj) {
  if (obj.style && parseInt (obj.style.height)) return parseInt (obj.style.height);
  return obj.offsetHeight;
}

function debug (el) {
  var str = '';
  for (k in el) {
    str += k + ' = ' + el[k] + '<br/>';
  }
  __el ('debug_div').innerHTML = str;
}







// Ajax

var Request = null;
var Request_loading_div = null;
var AjaxQueue = new Array ();
var waiting = 0;

function CreateRequest()
{
//  if (Request) return Request;

  if (window.XMLHttpRequest) {
    Request = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      Request = new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch (CatchException) {
      Request = new ActiveXObject("Msxml2.XMLHTTP");
    }
  }

  if (!Request) {
    alert("Невозможно создать XMLHttpRequest");
  }
}


function sendQRequest (path,param,result_func) {
  if (waiting == 0) {
    sendRequest (path,param,result_func);
  } else {
    AjaxQueue.push ({path: path,param: param,result_func: result_func});
  }
}

var loaded_xml = {};
function sendRequest (path,param,result_func,from_process) {
  if (waiting == 1 && !from_process) return;

  waiting = 1;
  CreateRequest ();

  Request.onreadystatechange = function() {
    if (Request.readyState == 4) {
      loaded_xml[Request.my_path] = Request.responseText;
      if (result_func) { result_func (Request.responseText); }

      if (AjaxQueue.length) {
        var obj = AjaxQueue.shift ();
        sendRequest (obj.path,obj.param,obj.result_func,1);
      } else {
        waiting = 0;
      }
    }
  }

  Request.open('POST','/pages/' + path,true);

  var param_str = 'empty_param_for_request=junk&';
  for (nm in param) {
    param_str += nm + "=" + encodeURIComponent(param[nm]) + "&";
  }


  Request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
  Request.setRequestHeader("Content-Length", param_str.length);

  Request.send(param_str);
}

function getResult () {
  eval ("var req_values = " + Request.responseText);
  return req_values;
}



// Modals

var modalResult = {};
var modalHandler;
var modalDiv;

function showModalWindow (div_capt) {
  modalDiv = __el(div_capt);

  var mbg = __el('modal_background');

  __el('modal_background').style.display = 'block';
  modalDiv.style.display = 'block';

}

function hideModalWindow () {
  if (__el('modal_background')) __el('modal_background').style.display = 'none';
  if (modalDiv) modalDiv.style.display = 'none';

  if (modalHandler) {
    modalHandler (modalResult);
  }
  modalHandler = null;
}



function _place_modal_bg () {
  var mbg = __el('modal_background');
  if (!mbg) return;

  var d = document.body;
  if ((document.compatMode) && (document.compatMode == "CSS1Compat")) d = document.documentElement;
  if (document.body.scrollTop > d.scrollTop) d = document.body;

  var minx = parseInt(d.scrollLeft);
  var maxx = minx + parseInt(d.clientWidth);
  var miny = parseInt(d.scrollTop);
  var maxy = miny + parseInt(d.clientHeight);

  mbg.style.top = miny + 'px';
  mbg.style.left = minx + 'px';
  mbg.style.width = (maxx-minx) + 'px';
  mbg.style.height = (maxy-miny) + 'px';
  

  var _d_l = minx + parseInt ((maxx-minx) / 2) - 150;
  var _d_t = miny + parseInt ((maxy-miny) / 2) - 85;


  if (modalDiv) {
    modalDiv.style.left = _d_l + 'px';
    modalDiv.style.top = _d_t + 'px';
  }

  setTimeout ('_place_modal_bg()',100);
}

