var winh=[];

function winhide(id) { // Скрыть окошко
 if ((window.navigator.userAgent.search('MSIE')>0) && (window.navigator.userAgent.search('6.0')>0)) {
  var dw = document.getElementById('w'+id);
 } else {
  var dw = document.getElementById('w'+id);
  if (dw.style.height=='14px') {
    dw.style.height=winh['w'+id];
  }
  else {
    winh['w'+id]=dw.style.height;
    dw.style.height='14px';
  }
  var bxe=dw.getElementsByTagName('div');
  for (var i=0; i<bxe.length; i++) { // Пробежим все дочерние DIV-ы
    if ((bxe[i].className=='m') || (bxe[i].className=='l')) {
     if (dw.style.height=='14px') bxe[i].style.visibility='hidden';
     else bxe[i].style.visibility='visible';
    }
    if (bxe[i].className=='b') { // Для кнопок
      var axe=bxe[i].getElementsByTagName('a');
      if (axe.length>0) {
        if (dw.style.height=='14px') axe[0].title='Развернуть';
        else axe[0].title='Свернуть';
      }
      var ixe=bxe[i].getElementsByTagName('img');
      if (ixe.length>0) {
        if (dw.style.height=='14px') ixe[0].src='/pic/max.png';
        else ixe[0].src='/pic/min.png';
      }
    }
  }
  dw.parentNode.replaceChild(dw,dw);
  $.get('ajax.php?win='+dw.id+'&s=h'+dw.style.height+'&w='+dw.parentNode.id,{}, function(data) { }); // Выполним ajax-запрос
 }
 return false;
}

var windis=[];
function sitedisable(id1) { // Запретить изменение сайта
  windis[id1]=document.createElement('div'); // Обозначение посадки
  windis[id1].id='windis';
  windis[id1].style.position='absolute';
  windis[id1].style.left='0px';
  windis[id1].style.top='0px';
  windis[id1].style.height=document.documentElement.offsetHeight+document.body.clientHeight;
  windis[id1].style.width=document.documentElement.offsetWidth+document.body.clientWidth;
//  windis[id1].style.background='#C4DAFF';
  windis[id1].style.zIndex=2;
 if (id1) windis[id1].innerHTML="<a onclick='return hideWin(\""+id1+"\")' style='position:absolute;right:0px;top:0px;width:100%;height:100%'>.</a>";
 document.body.appendChild(windis[id1]);
}

function hideWin(id1) { // Скроем окно и разрешим изменение сайта
  if (windis[id1]!=null) document.body.removeChild(windis[id1]);
  if (id1) hideEl(id1);
  if (id1=='wmedm') {
    if (windis['wmmark']!=null) document.body.removeChild(windis['wmmark']);
    windis['wmmark']=null;
    hideEl('wmmark');
  }
  if (id1=='wmmark') {
    if (windis['wmedm']!=null) document.body.removeChild(windis['wmedm']);
    windis['wmedm']=null;
    hideEl('wmedm');
  }
  return false;
}

var dragwin=null;
var dropwin=null;
var docwin=null;
var mCur=[];
var winpos=[];
var winoffs=[];

function getElementPosition(el1) {
  var w=el1.clientWidth;
  var h=el1.clientHeight;
  var elem=el1;
  var l = 0;
  var t = 0;
  while (elem) {
      l += elem.offsetLeft;
      t += elem.offsetTop;
      elem = elem.offsetParent;
  }
  return {"x":l,"y":t,"w":w,"h":h};
}

function mousePageXY(e) {
  var x = 0, y = 0;
  if (!e) e = window.event;
  if (e.pageX || e.pageY) {
    x = e.pageX;
    y = e.pageY;
  }
  else if (e.clientX || e.clientY) {
    x = e.clientX + (document.documentElement.scrollLeft || document.body.scrollLeft) - document.documentElement.clientLeft;
    y = e.clientY + (document.documentElement.scrollTop || document.body.scrollTop) - document.documentElement.clientTop;
  }
  if(dragwin){
    dragwin.style.top=mCur['y']-winoffs['y'];
    dragwin.style.left=mCur['x']-winoffs['x'];
    windrop();
  }
  return {"x":x, "y":y};
}

function windrop() { // найдем точку посадки для окна
  var a=-1;
  if ((mCur['x']>=getElementPosition(dropwin).x) && (mCur['x']<=getElementPosition(dropwin).x+dropwin.offsetWidth)) { // Над посадочным окном
    for (var i=0; i<dropwin.childNodes.length; i++) // Пробежим все дочерние элементы
      if ((dropwin.childNodes[i]!=docwin) && ((getElementPosition(dropwin.childNodes[i]).y+dropwin.childNodes[i].offsetHeight)>mCur['y'])) { // Вставим перед этим элементом
//         dropwin.parentNode.removeChild(dropwin);
         dropwin.insertBefore(docwin,dropwin.childNodes[i]);
         a=i;
         break;
      }
    if (a==-1) dropwin.appendChild(docwin);
  }
}

function winstartmove(id) { // Начнем перетаскивание окна
  if (!dragwin) {
    var dw = document.getElementById('w'+id);
    winpos=getElementPosition(dw); // Координаты окна
    var bxe=dw.getElementsByTagName('div');
    if ((bxe.length<2) || (getElementPosition(bxe[1])['x']>mCur['x'])) {
      dragwin=dw;
      dragwin.style.visibility='hidden';
//      alert(window.navigator.userAgent);
      if (window.navigator.userAgent.search('MSIE')>0) dragwin.style.filter="alpha(opacity=60)";
      else if (window.navigator.userAgent.search('Gecko')>0) dragwin.style.opacity=".6";
      dragwin.style.position='absolute';
      dragwin.style.height=winpos['h'];
      dragwin.style.width=winpos['w'];
      dragwin.style.zIndex=21;
      winoffs= {x:0,y:0}
      winoffs['x']=mCur['x']-winpos['x'];
      winoffs['y']=mCur['y']-winpos['y'];
      docwin=document.createElement('div'); // Обозначение посадки
      docwin.style.height=winpos['h'];
      docwin.style.width=winpos['w'];
      docwin.style.border='2px dashed #B6D9EF';
      dragwin.style.top=mCur['y']-winoffs['y'];
      dragwin.style.left=mCur['x']-winoffs['x'];
      docwin.style.zIndex=2;
      dropwin=dragwin.parentNode;
      dropwin.replaceChild(docwin,dragwin);
      document.body.appendChild(dragwin);
      dragwin.style.visibility='visible';
      sitedisable('');
      return false;
    } else {
//      winstopmove();
    }
  }
}

function winstopmove() { // Закончим перетаскивание окна
  if (dragwin) {
    dragwin.style.zIndex=2;
    if (window.navigator.userAgent.search('MSIE')>0) dragwin.style.filter="";
    else if (window.navigator.userAgent.search('Gecko')>0) dragwin.style.opacity="";
    dragwin.style.left=0;
    dragwin.style.top=0;
    dragwin.style.position='relative';
    docwin=dropwin.replaceChild(dragwin,docwin);
    var n=-1;
    for (var i=0; i<dropwin.childNodes.length; i++) // Пробежим все дочерние элементы
      if (dropwin.childNodes[i]==dragwin) n=i; // Остановимся на текущем элементе (что бы знать его номер в списке)
//    alert('ajax.php?win='+dragwin.id+'&n='+n+'&w='+dropwin.id);
    $.get('ajax.php?win='+dragwin.id+'&n='+n+'&w='+dropwin.id,{}, function(data) { }); // Выполним ajax-запрос
    hideWin('');
    dragwin=null;
  }
}

document.onmousemove = function(e) { mCur = mousePageXY(e); } ;
document.onmouseup = function () {winstopmove();} ;


