var _umlparents = [];
var _umlcoords = [];
var _umlhave = '';
var _umloffX, _umloffY, _umlscrX, _umlscrY;
var _uml_ie = 0;
var _umlunable = 0;

function openDiv(n, t, w, h, bc, bt, htmlurl, cont, align, valign, autoclose, hideclose, fadeoff) {
    // autoclose = off, hideclose = off,
	var scrw = document.body.clientWidth > 0 ? document.body.clientWidth : window.innerWidth > 0 ? window.innerWidth : document.body.offsetWidth;
    var scrh = document.body.clientHeight > 0 ? document.body.clientHeight : window.innerHeight > 0 ? window.innerHeight : document.body.offsetHeight;
    var x = (scrw - w) / 2;
    var y = (scrh - h) / 2 - 20;
    if (document.getElementById('newLayer' + n) == null) {
        var o = document.createElement('div');
        o.id = 'newLayer' + n;
        if (!$.browser.msie) $("#div_for_popup").append(o);
        else document.body.appendChild(o);
    } else {
        document.getElementById('newLayer' + n).style.display = '';
    }
    //alert(hideclose);
    //переменная определяет надо ли показывать кнопку "закрыть"
    if (hideclose == 1 && hideclose != "off") hideclose = '';
    else if (hideclose == "refresh") hideclose = '<a href="javascript://" onclick="unFadeToBody(\'newLayer' + n + '\'); getTrMap(\'' + globaljstid + '\', \'' + globaljsses + '\');"><img border="0" onmouseover="this.src=\'' + htmlurl + '/images/krest2.gif\'" onmouseout="this.src=\'' + htmlurl + '/images/krest1.gif\'" src="' + htmlurl + '/images/krest1.gif" width="12" height="12" title="Закрыть окно"></a>';
    else hideclose = '<a href="javascript://" onclick="unFadeToBody(\'newLayer' + n + '\');"><img border="0" onmouseover="this.src=\'' + htmlurl + '/images/krest2.gif\'" onmouseout="this.src=\'' + htmlurl + '/images/krest1.gif\'" src="' + htmlurl + '/images/krest1.gif" width="12" height="12" title="Закрыть окно"></a>';
    
    document.getElementById('newLayer' + n).innerHTML = '<div id="outLayer' + n + '" style="position:absolute; z-index:4; top: ' + y + '; left: ' + x + '; width: ' + w + 'px; height: ' + h + 'px;">' +
		'<table cellpadding="0" cellspacing="0" width="100%" height="100%" style="border:1px solid #B60808;"><tr>' +
		'<td width="100%" height="20px" style="padding: 5px; background:url(\'http://'+window.location.hostname+'/images/fon/gr/5.jpg\'); border-bottom:1px solid #B60808;">' +
		'<table cellpadding="0" cellspacing="0" border="0" width="100%" style="color: #FFFFFF; font-weight: bold; font-size: 8pt; font-family: Tahoma,Arial;">' +
		'<tr><td style="color: white;" valign="top" id="layerTitle' + n + '">' +
		'' + t + '' +
		'</td><td width="13px" valign="top" align="right">' +
		'' + hideclose + '' +
		'</td></tr></table></td></tr><tr>' +
		'<td valign="' + valign + '" align="' + align + '" style="padding: 5px; background: #DFD297; height: ' + (h-20) + 'px;">' +
		'' + cont + '' +
	'</td></tr></table></div>';

    if (fadeoff != "off") fadeToBody("newLayer" + n);
    _umlRegMover('layerTitle' + n, 'outLayer' + n);
    if (autoclose && autoclose != 'off') {
        //alert(autoclose);
        //alert('Вызван автоклоуз');
        setTimeout("unFadeToBody('newLayer" + n + "')", autoclose);
    }
}
function autoclose(i) {

}

if (document.addEventListener) {
    document.addEventListener('mouseup', _umlUp, false);
    document.addEventListener('mousemove', _umlMove, false);
}

else if (window.attachEvent) {
    document.attachEvent('onmouseup', _umlUp);
    document.attachEvent('onmousemove', _umlMove);
    window.attachEvent('onscroll', _umlScroll);
    _uml_ie = 1;
} else {
    _umlunable = 1;
}

function _umlGetCoord(obj) {
    if (!obj) return { 'left': 0, 'top': 0 };
    var left_offset = obj.offsetLeft;
    var top_offset = obj.offsetTop;
    if (!left_offset && !top_offset && obj.offsetParent == null) {
        left_offset = parseInt(obj.style.left);
        top_offset = parseInt(obj.style.top);
    } else
        while ((obj = obj.offsetParent) != null) {
        left_offset += obj.offsetLeft;
        top_offset += obj.offsetTop;
    }

    return { 'left': left_offset, 'top': top_offset };
}

function _umlRegMover(mover, movparent) {
    if (_umlunable) return;
    var it = document.getElementById(mover);
    var itpar = document.getElementById(movparent);
    if (!it || !itpar) return;
    if (!_uml_ie && itpar.style.position != 'fixed') { itpar.style.position = 'fixed'; }
    _umlparents[mover] = itpar;
    if (_uml_ie) {
        _umlscrX = document.body.scrollLeft;
        _umlscrY = document.body.scrollTop;
        var pos = _umlGetCoord(itpar);
        _umlcoords[mover] = [pos['left'] + _umlscrX, pos['top'] + _umlscrY];

        itpar.style.left = _umlcoords[mover][0] + 'px';
        itpar.style.top = _umlcoords[mover][1] + 'px';

        it.attachEvent('onmousedown', _umlDown);
    }
    else it.addEventListener('mousedown', _umlDown, false);
    it.style.cursor = "move";
}

function _umlMove(e) {
    if (_uml_ie) e = event;
    if (!_umlhave) return;
    if (_uml_ie && (e.button & 1) == 0) { _umlUp(e); return; }

    _umlparents[_umlhave.id].style.left = (_umloffX + e.clientX) + 'px';
    _umlparents[_umlhave.id].style.top = (_umloffY + e.clientY) + 'px';
    if (window.getSelection) {
        window.getSelection().removeAllRanges();
    } else if (document.selection) {
        if (document.selection.type != 'none') document.selection.empty();

    }
}

function _umlScroll(e) {
    if (_uml_ie) e = event;
    for (var i in _umlcoords) {
        var newx = _umlcoords[i][0] + document.body.scrollLeft - _umlscrX;
        if (newx + _umlparents[i].offsetWidth < document.body.scrollWidth) _umlparents[i].style.left = newx + 'px';
        var newy = _umlcoords[i][1] + document.body.scrollTop - _umlscrY;
        if (newy + _umlparents[i].offsetHeight < document.body.scrollHeight) _umlparents[i].style.top = newy + 'px';
    }
}


function _umlDown(e) {
    if (_uml_ie) {
        if (!_umlparents[event.srcElement.id]) return;
        e = event;
        if (e.button && (e.button & 1) != 1) return;
        _umlhave = e.srcElement;
    } else {
        _umlhave = this;
        e.stopPropagation();
    }
    var pos = _umlGetCoord(_umlparents[_umlhave.id]);
    _umloffX = pos['left'] - e.clientX;
    _umloffY = pos['top'] - e.clientY;
    if (_uml_ie) {
        _umlscrX = document.body.scrollLeft;
        _umlscrY = document.body.scrollTop;
        var pos = _umlGetCoord(_umlparents[_umlhave.id]);
        _umlcoords[_umlhave.id] = [pos['left'], pos['top']];
    }
}

function _umlUp(e) {
    if (!_umlhave) return;
    if (_uml_ie) {
        var pos = _umlGetCoord(_umlparents[_umlhave.id]);
        _umlcoords[_umlhave.id] = [pos['left'], pos['top']];
    }
    _umlhave = 0;
}
