function DialogCollection()
{
    this.Dialogs = new Array();
    this.Overlay = null;
    this.AddDialog = _AddDialog;               
    this.NewDialog = _NewDialog;               
    this.DeleteDialog = _DeleteDialog;         
    this.GetDialogById = _GetDialogById;       
    this.DisableWorkArea = _DC_DisableWorkArea;
    this.EnableWorkArea = _DC_EnableWorkArea;  
    this.BringToFront = _BringToFront;         
    this.GetHighestDepth = _GHDD;   
    this.NormalizeDepths = _NormalizeDepths;   
    this.SetDepth = _SetDepth;                 
    this.pxGif = "resource/px.gif";            
}
function _AddDialog(dialog)
{
    if(dialog)
        this.Dialogs.push(dialog);
}
function _NewDialog(baseObject)
{
    var result = null;
    if(baseObject)
    {
        var dialog = new Dialog(baseObject);
        this.SetDepth(dialog, this.GetHighestDepth());
        this.AddDialog(dialog);
        result = dialog;
    }
    return result;
}
function _DeleteDialog(dialog)
{
    if(dialog)
    {
        var tmpArray = new Array();
        for(var i=0;i<this.Dialogs.length;i++)
            if(this.Dialogs[i] != dialog)
                tmpArray.push(this.Dialogs[i]);
        this.Dialogs = tmpArray;
    }
    this.NormalizeDepths(dialog.Depth);
}
function _GetDialogById(id)
{
    var result = null;
    if(id)
    {
        for(var i=0;i<this.Dialogs.length;i++)
            if(this.Dialogs[i].id == id)
                result = this.Dialogs[i];
    }
    return result;
}
function _DC_DisableWorkArea()
{
    if(this.Overlay == null)
    {
        var overlay = document.createElement("DIV");
        overlay.className = "overlay";
        overlay.style.width = document.body.offsetWidth+"px";
        overlay.style.height = document.body.offsetHeight+"px";
        if((navigator.userAgent.indexOf("IE 6") != -1)&&(navigator.userAgent.indexOf("Opera") == -1))
        {
            overlay.style.background = "url('"+this.pxGif+"') repeat left top";
            overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='overlay.png' , SizingMethod= 'scale' )";
        }
        document.body.attachNode(overlay);
        this.Overlay = overlay;
    }
    this.Overlay.style.display = "block";
}
function _DC_EnableWorkArea()
{
    this.Overlay.style.display = "none";
}
function _BringToFront(dialog)
{
    var old = dialog.Depth;
    this.SetDepth(dialog, this.GetHighestDepth());
    this.NormalizeDepths(old);
}
function _GHDD()
{
    var i = 100;
    var k = 0
    for(k = 0; k<this.Dialogs.length; k++)
    {
        if(i < this.Dialogs[i].Depth)
        {
            i = this.Dialogs[i].Depth;
        }
    }
    return (i+1);
}
function _NormalizeDepths(deletedDepth)
{
    for(var i =0; i< this.Dialogs.length; i++)
    {
        var dialog = this.Dialog[i];
        if(dialog.Depth > deletedDepth)
        {
            this.SetDepth(dialog, (dialog.Depth-1));
        }
        this.Dialog[i] = dialog;
    }
}
function _SetDepth(dialog, depth)
{
    dialog.Depth = depth;
    dialog.obj.style.zIndex = depth; 
}
//Класс Dialog представляет методы работы со всплывающими диалоговыми окнами (DIV)
function Dialog(baseObject) // Конструктор класса Dialog
{
    this.obj = baseObject; //Базовый объект 
    this.Depth = 1;
    //Методы изменения положения
    this.Center = _CenterDialog;
    this.CenterByBounds = _CenterDialogByBounds;
    this.Drag = _StartDragDialog;
    this.StopDrag = _StopDragDialog;
    //Сохранение позиции
    this.SavePosition = _SaveDialogPosition;
    this.RestorePosition = _RestoreDialogPosition;
    //Вспомогательный метод (создание подложки) помогает сделать страницу неактивной
    this.DisableWorkArea = _DisableWorkArea;
    //Служебные переменные
    this.Overlay = null; 
    this.timeOut;
    this._initX = 0;
    this._initY = 0;
    this.pxGif = "resource/px.gif";
}
function _DisableWorkArea()
{
    if(this.Overlay == null)
    {
        var overlay = document.createElement("DIV");
        overlay.className = "overlay";
        overlay.style.width = document.body.offsetWidth+"px";
        overlay.style.height = document.body.offsetHeight+"px";
        if((navigator.userAgent.indexOf("IE 6") != -1)&&(navigator.userAgent.indexOf("Opera") == -1))
        {
            overlay.style.background = "url('"+this.pxGif+"') repeat left top";
            overlay.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='overlay.png' , SizingMethod= 'scale' )";
        }
        this.obj.parentNode.insertBefore(overlay, this.obj);
        this.Overlay = overlay;
    }
    this.Overlay.style.display = "block";
}
function _SaveDialogPosition()
{
    var b = Sys.UI.DomElement.getBounds(this.obj);
    var name = "SavedPositionFor_"+this.obj.id;
    setCookie(name+"_x", b.x);
    setCookie(name+"_y", b.y-ScrollOffset()[0]); 
}
function _RestoreDialogPosition()
{
    var name = "SavedPositionFor_"+this.obj.id;
    var x = getCookie(name+"_x");
    var y = getCookie(name+"_y");
    if((x)&&(y))
    {
        this.obj.style.top = (Number(y)+Number(ScrollOffset()[0]))+"px";
        this.obj.style.left = Number(x)+"px";
    }
    else
    {
        var b = Sys.UI.DomElement.getBounds(this.obj);
        this.Center();
    }
}
function _StartDragDialog()
{
    var mX = _mouseX;
    var mY = _mouseY;
    var b = Sys.UI.DomElement.getBounds(this.obj);
    this._initX = mX-b.x;
    this._initY = mY-b.y;
    _DragDialog();
}
function _DragDialog()
{
    _currentDrag.obj.style.top = (_mouseY-_currentDrag._initY) + "px";
    _currentDrag.obj.style.left = (_mouseX-_currentDrag._initX) + "px";
    _currentDrag.timeOut = setTimeout("_DragDialog()", 0.1);    
}
function _StopDragDialog()
{
    clearTimeout(this.timeOut);
    this.SavePosition();
}

function _CenterDialogByBounds(width, height)
{
    var _width = GetSize("x");
    var _height = GetSize("y");
    if(this.obj)
    {
        this.obj.style.display = "block";
        var _ix = (_width/2)-(width/2);
        var _iy = ScrollOffset()[0]+(_height/2)-(height/2);
        this.obj.style.left = ((_ix > 0) ? _ix : 0)+"px";
        this.obj.style.top = ((_iy > 0) ? _iy : ScrollOffset()[0])+"px";
    }
}
function _CenterDialog()
{
    var _width = GetSize("x");
    var _height = GetSize("y");
    if(this.obj)
    {
        this.obj.style.display = "block";
        var _ix = (_width/2)-(this.obj.offsetWidth/2);
        var _iy = ScrollOffset()[0]+(_height/2)-(this.obj.offsetHeight/2);
        this.obj.style.left = ((_ix > 0) ? _ix : 0)+"px";
        this.obj.style.top = ((_iy > 0) ? _iy : ScrollOffset()[0])+"px";
    }
}
 var _currentDrag;       //!!!  <-  Супер важная вещь....!!!
function DragDialog(obj)
{
    if(obj)
    {
        _currentDrag = new Dialog(obj);
        _currentDrag.Drag();
    }
}
function StopDragDialog()
{
    _currentDrag.StopDrag();
}
function GetSize(axis)
{
    var x,y;	
    if (self.innerHeight) 
    {
         x = self.innerWidth;	    
         y = self.innerHeight;	
    } 
    else if (document.documentElement && document.documentElement.clientHeight)
    {	
         x = document.documentElement.clientWidth;	    
         y = document.documentElement.clientHeight;	
    } 
    else if (document.body) 
    {
         x = document.body.clientWidth;	    
         y = document.body.clientHeight;	
    }
    return (axis == "x") ? x : (axis == "y") ? y : 0;
}
function getMousePosition(e)
{
    return e.pageX ? 
    { 
        'x':e.pageX, 'y':e.pageY 
    } :
    { 
        'x':e.clientX + (document.documentElement ?
            document.documentElement.scrollLeft : document.body.scrollLeft),
        'y':e.clientY + (document.documentElement ?
            document.documentElement.scrollTop : document.body.scrollTop)
    };
};

var _mouseX = 0;
var _mouseY = 0;

function showMousePos(e)
{
    if (!e) e = event; 
    var mp = getMousePosition(e);
    _mouseX = mp.x;
    _mouseY = mp.y;
};
function init()
{
    document.onmousemove = showMousePos;
};
init();
function setCookie (name, value, expires, path, domain, secure) {
      document.cookie = name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
function getCookie(name) {
	var cookie = " " + document.cookie;
	var search = " " + name + "=";
	var setStr = null;
	var offset = 0;
	var end = 0;
	if (cookie.length > 0) {
		offset = cookie.indexOf(search);
		if (offset != -1) {
			offset += search.length;
			end = cookie.indexOf(";", offset)
			if (end == -1) {
				end = cookie.length;
			}
			setStr = unescape(cookie.substring(offset, end));
		}
	}
	return(setStr);
}
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
function ShowAtPosition(id)
{
    var obj = document.getElementById(id)
    if(obj)
    {
        var width = GetSize("x") + ScrollOffset()[1];
        var height = GetSize("y") + ScrollOffset()[0];
        obj.style.left = "0px";
        obj.style.top = "0px";
        obj.style.display = "block";
        var pos = findPos(obj);
        var mX = _mouseX - pos[0];
        var mY = _mouseY - pos[1];
        obj.style.left = ((width > Number(obj.offsetWidth + mX)) ? mX : Number(width-obj.offsetWidth)-pos[0] )+"px";
        obj.style.top = ((height > Number(obj.offsetHeight + mY)) ? mY : Number(height-obj.offsetHeight)-pos[1] )+"px";
    }     
}
function CreateBookmarkLink(url, title) 
{
     if (window.sidebar) 
     { 
        window.sidebar.addPanel(title, url,"");	
     } 
     else if( window.external ) 
     { 
        window.external.AddFavorite( url, title); 
     }	
     else if(window.opera && window.print) 
     {
     	return true; 
     } 
     return false;
}
function ScrollOffset()
{
    if(document.documentElement)
    {
        return [document.documentElement.scrollTop, document.documentElement.scrollLeft];
    }
    else
    {
        return [document.body.scrollTop, document.body.scrollLeft];
    }
}
function SwapClasses(class1, class2, obj)
{
    if(obj)
    {
        obj.className = obj.className == class1 ? class2 : class1;
    }
}