function CModalWindow(objId, objName)
{
    this.f_debug = true;
    this.objId = objId;
    this.objName = objName
    this.f_init = false;
    this.init = CModalWindow_init;
    this.loadObj = CModalWindow_loadObj;
    this.show = CModalWindow_show;
    this.showWithDelay = CModalWindow_showWithDelay;
    this.hide = CModalWindow_hide;
    this.onClose = '';
    this.refresh = CModalWindow_refresh;
    this.onDragStart = CModalWindow_onDragStart;
    this.onDragDrop = CModalWindow_onDragDrop;
    this.onDragMove = CModalWindow_onDragMove;
    this.onResizeStart = CModalWindow_onResizeStart;
    this.onResizeStop = CModalWindow_onResizeStop;
    this.onResizeMove = CModalWindow_onResizeMove;
    this.sizeBeforeResize = new Array();
    this.setSize = CModalWindow_setSize;
    this.onResize = CModalWindow_onResize;
    this.debug = CModalWindow_debug;
    this.setAutoScroll = CModalWindow_setAutoScroll;
    this.setResizeable = CModalWindow_setResizeable;
    this.setContent = CModalWindow_setContent;
    this.f_noResize = true;
    this.f_autoScroll = false;
    this.is_resizing = false;
    this.is_dragging = false;
    this.oldOffsetLeft = 0;
    this.oldOffsetTop = 0;
    this.oldCloseOffsetLeft = 0;
    this.oldCloseOffsetTop = 0;
    this.minX = 0;
    this.minY = 0;
    this.minWidth = 100;
    this.minHeight = 100;
    this.init();
}

function CModalWindow_init()
{
    if (!this.loadObj())
        return false;
    this.f_init = true;
    eval('this.objTitle.onmousedown = function(event) { ' + this.objName + '.onDragStart(event); }');
    if (!this.f_noResize)
    {
        eval('this.objResize.onmousedown = function(event) { ' + this.objName + '.onResizeStart(event); }');
        this.objResize.style.cursor = 'move';
        this.objResize.style.backgroundImage = this.objResize.style.backgroundImage.toString().replace(/_22\.gif/, '_22_resizeable.gif');
    }
    else
    {
        this.objResize.style.cursor = 'default';
        this.objResize.style.backgroundImage = this.objResize.style.backgroundImage.toString().replace(/_22_resizeable\.gif/, '22.gif');
    }
    this.objTitle.style.cursor = 'move';
    if (this.objCloseImage)
        eval('this.objCloseImage.onmousedown = function() { ' + this.objName + '.hide(); }');
    this.is_dragging = false;
    //this.setSize(this.objMain.offsetWidth, this.objMain.offsetHeight);
    return true;
}

function CModalWindow_loadObj()
{
    var err = '';
    if (
        !(this.objMain = getEl(err = this.objId + '_main')) ||
        !(this.objTitle = getEl(err = this.objId + '_title')) ||
        !(this.objDocTopTable = getEl(err = 'documentTopTable')) ||
        !(this.objModalPixel = getEl(err = 'modalWindow_pixel')) ||
        !(this.objMainTopRight = getEl(err = this.objId + '_top_right')) ||
        !(this.objResize = getEl(err = this.objId + '_bottom_right')) ||
        !(this.objContent = getEl(err = this.objId + '_content')) ||
        !(this.objArea = getEl(err = this.objId + '_area'))
    )
    {
        alert('Modal window load error! Object not found: ' + err);
        return false;
    }
    this.objCloseImage = getEl(this.objId + '_close_image');
    return true;
}

function CModalWindow_show()
{
    if (!this.loadObj())
        return false;
    this.objArea.style.width = this.objMain.style.width;
    this.objArea.style.height = this.objMain.style.height;
    eval("this.objArea.innerHTML = this.objArea.innerHTML.replace(/<table id=\"" +
        this.objId + "_main\"/i, '<table width=100% height=100% id=\"" +
        this.objId + "_main\"');");
    if (!this.init())
        return false;
    this.objArea.style.display = 'block';
    this.objModalPixel.style.height = this.objDocTopTable.offsetHeight + 15;
    this.objModalPixel.style.width = this.objDocTopTable.offsetWidth;
    this.objModalPixel.style.display = 'block';
    if (this.objCloseImage)
        this.objCloseImage.style.display = 'block';
    this.refresh();
}

function CModalWindow_showWithDelay(delay)
{
    setTimeout(this.objName + '.show()', parseInt(delay) * 1000);
}

function CModalWindow_hide()
{
    this.objArea.style.display = 'none';
    this.objModalPixel.style.display = 'none';
    if (this.objCloseImage)
        this.objCloseImage.style.display = 'none';
    if (this.onClose.length)
        eval(this.onClose);
}

function CModalWindow_refresh(f_imageOnly)
{
	var browserWindow = jQuery(window);
	var browser_width = browserWindow.width();
	var browser_height = browserWindow.height();


    var newOffsetLeft, newOffsetTop;
    if (!this.is_resizing &&
        !this.is_dragging &&
        !f_imageOnly)
    {
		newOffsetLeft = parseInt((browser_width - jQuery(this.objArea).width())/2);	
		newOffsetTop = documentScrollPositionY() + parseInt((browser_height - jQuery(this.objArea).height())/2);

        if (newOffsetLeft != this.oldOffsetLeft)
            this.objArea.style.left = this.oldOffsetLeft = newOffsetLeft+'px';
        if (newOffsetTop != this.oldOffsetTop)
            this.objArea.style.top = this.oldOffsetTop = newOffsetTop+'px';
    }
    // close img
    if (this.objCloseImage)
    {
		newOffsetLeft = parseInt((browser_width + jQuery(this.objArea).width() - jQuery(this.objCloseImage).width())/2)-4;
		newOffsetTop = documentScrollPositionY() + parseInt((browser_height - jQuery(this.objArea).height())/2) - 7;	
		
        if (newOffsetLeft != this.oldCloseOffsetLeft)
            this.objCloseImage.style.left = this.oldCloseOffsetLeft = newOffsetLeft+'px';
        if (newOffsetTop != this.oldCloseOffsetTop)
            this.objCloseImage.style.top = this.oldCloseOffsetTop = newOffsetTop+'px';
    }
    if (this.f_autoScroll)
        setTimeout(this.objName + '.refresh();', 200);
}

function CModalWindow_onDragStart(ev)
{
    this.f_autoScroll = false;
    if (!ev)
        ev = event;
    this.is_dragging = true;
    this.dragged = false;
    this.cursorX = eventOffsetX(ev);
    this.cursorY = eventOffsetY(ev);
    this.windowWidth = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
    this.windowHeight = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
    this.width = this.objArea.offsetWidth;
    this.height = this.objArea.offsetHeight;
    this.dragOffsetLeft = this.cursorX - GetOffsetLeft(this.objArea);
    this.dragOffsetTop = this.cursorY - GetOffsetTop(this.objArea);
    document.body.style.cursor = 'move';
    eval('document.onmousemove = function(event) { ' + this.objName + '.onDragMove(event); }');
    eval('document.onmouseup = function() { ' + this.objName + '.onDragDrop(); }');
    this.debug('STARTED DRAGGING ' + this.objName + ', ' + this.cursorX + ':' + this.cursorY +
        ' / ' + this.dragOffsetLeft + ':' + this.dragOffsetTop);
}

function CModalWindow_onDragDrop()
{
    this.is_dragging = false;
    document.body.style.cursor = 'default';
    document.onmousemove = '';
    document.onmouseup = '';
    this.debug('DROPPED');
}

function CModalWindow_onDragMove(ev)
{
    if (!ev)
        ev = event;
    clearTextSelection();
    this.dragged = true;
    this.cursorX = eventOffsetX(ev);
    this.cursorY = eventOffsetY(ev);
    this.objArea.style.position = 'absolute';
    var newLeft = this.cursorX - this.dragOffsetLeft,
        newTop = this.cursorY - this.dragOffsetTop;
    //SetSystemMessage(this.obj.style.left + ':' + this.obj.style.top + '->' + newLeft + ':' + newTop);
    //    this.cursorY + ':' + this.dragOffsetTop);
    var tmp, obj;
    if (newLeft > this.minX)
        this.objArea.style.left = newLeft;
    else
        this.dragOffsetLeft = this.cursorX - GetOffsetLeft(this.objArea);
    if (newTop > this.minY)
        this.objArea.style.top = newTop;
    else
        this.dragOffsetTop = this.cursorY - GetOffsetTop(this.objArea);
    this.refresh();
    //SetSystemMessage(mheight + 'x' + mwidth);
    this.debug('MOVE: ' + this.objName + ' / ' + this.cursorX + ':' + this.cursorY + ' / ' +
        newLeft + ':' + newTop + ' / ' + this.dragOffsetLeft + ':' + this.dragOffsetTop);
}

function CModalWindow_onResizeStart(ev)
{
    if (this.f_noResize)
        return false;
    this.f_autoScroll = false;
    if (!ev)
        ev = event;
    this.is_resizing = true;
    this.resized = false;
    this.cursorX = eventOffsetX(ev);
    this.cursorY = eventOffsetY(ev);
    this.windowWidth = window.innerWidth ? window.innerWidth : document.body.offsetWidth;
    this.windowHeight = window.innerHeight ? window.innerHeight : document.body.offsetHeight;
    this.sizeBeforeResize[0] = this.width = this.objMain.offsetWidth;
    this.sizeBeforeResize[1] = this.height = this.objMain.offsetHeight;
    this.resizeOffsetLeft = GetOffsetLeft(this.objMain);
    this.resizeOffsetTop = GetOffsetTop(this.objMain);
    document.body.style.cursor = 'move';
    eval('document.onmousemove = function(event) { ' + this.objName + '.onResizeMove(event); }');
    eval('document.onmouseup = function() { ' + this.objName + '.onResizeStop(); }');
    this.debug('STARTED RESIZING ' + this.objName + ', ' + this.cursorX + ':' + this.cursorY);
}

function CModalWindow_onResizeStop()
{
    this.is_resizing = false;
    document.body.style.cursor = 'default';
    document.onmousemove = '';
    document.onmouseup = '';
    if (this.onResize)
        this.onResize(this.objArea.offsetWidth - this.sizeBeforeResize[0],
            this.objArea.offsetHeight - this.sizeBeforeResize[1]);
    this.debug('RESIZED');
}

function CModalWindow_onResizeMove(ev)
{
    if (!ev)
        ev = event;
    clearTextSelection();
    this.resized = true;
    this.cursorX = eventOffsetX(ev);
    this.cursorY = eventOffsetY(ev);
    this.objMain.style.position = 'absolute';
    var newWidth = this.cursorX - this.resizeOffsetLeft,
        newHeight = this.cursorY - this.resizeOffsetTop;
    //SetSystemMessage(this.obj.style.left + ':' + this.obj.style.top + '->' + newLeft + ':' + newTop);
    //    this.cursorY + ':' + this.dragOffsetTop);
    var tmp, obj;
    if (newWidth > this.minWidth)
    {
        this.objArea.style.width = newWidth;
        this.objMain.style.width = newWidth;
    }
    if (newHeight > this.minHeight)
    {
        this.objArea.style.height = newHeight;
        this.objMain.style.height = newHeight;
    }
    this.refresh();
    //SetSystemMessage(mheight + 'x' + mwidth);
    this.debug('REISZE: ' + this.objName + ' / ' + this.cursorX + ':' + this.cursorY + ' / ' +
        newWidth + 'x' + newHeight + ' / ' + this.objArea.style.width + 'x' + this.objArea.style.height);
}

function CModalWindow_debug(mes, f_add)
{
    if (!this.f_debug)
        return;
    f_add ? AddSystemMessage(mes) : SetSystemMessage(mes);
}

function CModalWindow_setAutoScroll(f)
{
    this.f_autoScroll = f ? true : false;
}

function CModalWindow_setResizeable(f)
{
    this.f_noResize = f ? false : true;
}

function CModalWindow_onResize()
{
}

function CModalWindow_setSize(width, height)
{
    this.objArea.style.width = parseInt(width) + 'px';
    this.objArea.style.height = parseInt(height) + 'px';
}

function CModalWindow_setContent(c)
{
    this.objContent.innerHTML = c;
}
