function WindowCollection() {
    this.Wins = Array(0);

    this.zIndex = 5000;

    this.Init = function() {
    };

    this.Add = function(Options, funcCloseCallBack, Context) {
        var l = this.Count();
        if (l == 100) {
            alert("У вас превышен лимит открываемых окон. Пожалуйста перезагрузите страницу для продолжения.");
            return false;
        }
        this.Wins[l] = new WindowItem(l, Options, funcCloseCallBack, Context);
        this.Wins[l].SetZIndex(this.zIndex + l * 10);
        this.UpWin(l);

        return l;
    };

    this.Count = function() {
        return this.Wins.length;
    };

    this.UpWin = function(id) {
        var i = 0;
        for (i = 0; i < this.Wins.length; i++) {
            if (i == id || this.Wins[i] == undefined)
                continue;

            this.Wins[i].SetZIndex(this.zIndex + i * 10);
        }

        this.Wins[id].SetZIndex(this.zIndex + 100 * 10);
    };

    this.win_close = function(id) {
        if (this.Wins[id] != undefined && id != null && id != undefined) {
            this.Wins[id].Dispose();
        }
    };

    this.CloseWin = function(id) {
        if (this.Wins[id] != undefined && id != null && id != undefined && this.Wins[id].IsDelete == 0) {
            this.Wins[id].Dispose();
        }
    };

    this.FindByName = function(Name) {
        var i = 0;
        for (i = 0; i < this.Wins.length; i++) {
            if (this.Wins[i].IsDelete == 1)
                continue;
            if (this.Wins[i].ObjFrame.id == Name) {
                return this.Wins[i];
            }
        }
        return false;
    };

    this.Init();
};

function WindowItem(Id, Options, funcCloseCallBack, Context) {
    this.Id = Id;
    this.Options = Options;
    if (funcCloseCallBack == undefined)
        this.funcCloseCallBack = null;
    else
        this.funcCloseCallBack = funcCloseCallBack;
    this.Context = Context;
    if (this.Options.onReturnData != undefined)
        this.onReturnData = this.Options.onReturnData;
    else
        this.onReturnData = null;

    this.Obj = new Element('div', {
        'id': 'window_' + this.Id,
        'class': 'window_div',
        'content': 'window_' + this.Id + '_content',
        'events': {
            "mousedown": function(a) {
                return function() {
                    page.wins.UpWin(a.Id);
                }
            } (this)
        }
    });

    this.IsDelete = 0;
    this.HeightTop = 41;
    this.zIndex = 5000;

    this.Drag = null;

    this.Init();
};

WindowItem.prototype.ReturnData = function(data) {
    if (this.onReturnData)
        this.onReturnData(data, this.Context, this);
};

WindowItem.prototype.Init = function() {
    if (this.Options.Title == undefined) {
        this.Options.Title = 'Window ' + this.Id;
    }

    this.ObjBorder = new Element("div", { "class": "window_div_div" });
    this.ObjBorder.inject(this.Obj);

    this.ObjTop = new Element("div", {
        "class": "window_top"
    });
    this.ObjTop.inject(this.ObjBorder, "top");

    this.ObjContent = new Element("div", {
        "class": "window_content"
    });
    this.ObjContent.inject(this.ObjBorder, "bottom");

    this.ObjTitle = new Element("div", {
        "class": "window_top_title"
    });
    this.ObjTitle.inject(this.ObjTop, "top");

    this.ObjIcon = new Element("img", {
        "src": "/themes/admin/images/pix.gif"
    });
    this.ObjIcon.inject(this.ObjTitle, "top");

    this.ObjTitleText = new Element("div", {
        "class": "window_top_title_text",
        "text": this.Options.Title,
        "events": {
            "mousedown": function(a) {
                return function() {
                    if (a.Options.WindowState == 1)
                        return;
                    a.Drag.attach();
                }
            } (this),
            "mouseup": function(a) {
                return function() {
                    a.Drag.detach();
                }
            } (this),
            "dblclick": function(a) {
                return function() {
                    if (a.Options.MaximizeBox == 0)
                        return;
                    if (a.Options.WindowState == 0) {
                        a.SetWindowState(1);
                    }
                    else if (a.Options.WindowState == 1) {
                        a.SetWindowState(0);
                    }
                }
            } (this)
        }
    });
    this.ObjTitleText.inject(this.ObjTitle, "bottom");

    this.ObjButtons = new Element("div", {
        "class": "window_top_bottons"
    });
    this.ObjButtons.inject(this.ObjTop, "top");

    this.ObjMaximize = new Element("img", {
        "src": "/themes/admin/images/window/maximize.gif",
        "events": {
            "click": function(a) {
                return function() {
                    if (a.Options.MaximizeBox == 0)
                        return;
                    if (a.Options.WindowState == 0) {
                        a.SetWindowState(1);
                    }
                    else if (a.Options.WindowState == 1) {
                        a.SetWindowState(0);
                    }
                }
            } (this),
            "mouseover": function(a) {
                return function() {
                    if (a.Options.MaximizeBox == 0)
                        return;
                    if (a.Options.WindowState == 0) {
                        a.ObjMaximize.src = "/themes/admin/images/window/maximize_over.gif";
                    }
                    else if (a.Options.WindowState == 1) {
                        a.ObjMaximize.src = "/themes/admin/images/window/normal_over.gif";
                    }
                }
            } (this),
            "mouseout": function(a) {
                return function() {
                    if (a.Options.MaximizeBox == 0)
                        return;
                    if (a.Options.WindowState == 0) {
                        a.ObjMaximize.src = "/themes/admin/images/window/maximize.gif";
                    }
                    else if (a.Options.WindowState == 1) {
                        a.ObjMaximize.src = "/themes/admin/images/window/normal.gif";
                    }
                }
            } (this)
        }
    });
    this.ObjMaximize.inject(this.ObjButtons, "top");

    this.ObjClose = new Element("img", {
        "src": "/themes/admin/images/window/close.gif",
        "events": {
            "mouseover": function() {
                this.src = "/themes/admin/images/window/close_over.gif";
            },
            "mouseout": function() {
                this.src = "/themes/admin/images/window/close.gif";
            },
            "click": function(a) {
                return function() {
                    a.Dispose();
                }
            } (this)
        }
    });
    this.ObjClose.inject(this.ObjButtons, "top");

    this.ObjMinimize = new Element("img", {
        "src": "/themes/admin/images/window/minimize.gif"
    });
    this.ObjMinimize.inject(this.ObjButtons, "bottom");

    this.Obj.inject(page.body, "bottom");

    this.ObjBg = new BackgrounScreen(page.body);

    if (this.Options.WindowState == undefined) {
        this.Options.WindowState = 0;
    }
    if (this.Options.MaximizeBox == undefined) {
        this.Options.MaximizeBox = 1;
    }
    if (this.Options.FormBorderStyle == undefined) {
        this.Options.FormBorderStyle = 0;
    }
    if (this.Options.IsBackgrounScreen == undefined) {
        this.Options.IsBackgrounScreen = 1;
    }
    if (this.Options.Content == undefined) {
        this.Options.Content = {
            'Type': 'text',
            'Url': ''
        };
    }

    this.Redraw();

    this.Drag = new Drag(this.Obj, {
        snap: 5,
        onSnap: function(el) {
            //el.setStyle("opacity", "0.6");
            //var n = el.getNext();
            //alert(el.getNext());
            //if (n.hasClass("window_content")) {
            //$(el.getProperty("content")).getChildren().setStyle("visibility", "hidden");
            //}
        },
        onComplete: function(el) {
            //el.setStyle("opacity", "1");
            //$(el.getProperty("content")).getChildren().setStyle("visibility", "visible");
        }
    });
    this.Drag.detach();

    if (this.Options.IsBackgrounScreen == 1) {
        this.ObjBg.Show();
    }

    if (this.Options.ToolBar == 1) {
        this.ObjToolbar = new ToolBar(this.ObjContent);
    }

    this.SetContent();
};

WindowItem.prototype.Redraw = function() {
    if (this.Options.FormBorderStyle == 0) {
        this.ObjTop.setStyle("display", "block");
        this.ObjTitle.setStyle("display", "block");
        this.ObjButtons.setStyle("display", "block");
        this.HeightTop = 41;
    }
    else if (this.Options.FormBorderStyle == 1) {
        this.ObjTop.setStyle("display", "none");
        this.ObjTitle.setStyle("display", "none");
        this.ObjButtons.setStyle("display", "none");
        this.HeightTop = 18;
    }
    else if (this.Options.FormBorderStyle == 2) {
        this.ObjTop.setStyle("display", "block");
        this.ObjTitle.setStyle("display", "none");
        this.ObjButtons.setStyle("display", "none");
        this.HeightTop = 41;
    }

    if (this.Options.WindowState == 0) {
        if (this.Options.Width != undefined)
            this.Obj.setStyle("width", this.Options.Width);
        if (this.Options.Height != undefined) {
            this.Obj.setStyle("height", "auto");
            var topsize = this.ObjTop.getSize();
            this.SetHeightContent(parseInt(this.Options.Height) - topsize.y);
        }
        var bsize = page.body.getSize();
        var bscrollsize = page.body.getScroll();

        if (this.Options.Top == "center" || this.Options.Top == undefined) {
            this.Obj.setStyle("top", ((bsize.y - parseInt(this.Obj.offsetHeight)) / 2) + bscrollsize.y);
        }
        else
            this.Obj.setStyle("top", this.Options.Top);

        if (this.Options.Left == "center" || this.Options.Left == undefined) {
            this.Obj.setStyle("left", ((bsize.x - parseInt(this.Obj.offsetWidth)) / 2) + bscrollsize.x);
        }
        else
            this.Obj.setStyle("left", this.Options.Left);

        this.Obj.removeClass("window_maximize");
        this.ObjMaximize.src = "/themes/admin/images/window/maximize.gif";
    }
    else if (this.Options.WindowState == 1) {
        this.Options.Left = this.Obj.getStyle("left");
        this.Options.Top = this.Obj.getStyle("top");

        this.Obj.addClass("window_maximize");
        this.ObjMaximize.src = "/themes/admin/images/window/normal.gif";

        var bscrollsize = page.body.getScroll();
        var bsize = page.body.getSize();
        var scs = page.body.getScrollSize();
        this.Obj.setStyle("top", "0px");
        this.Obj.setStyle("left", "0px");
        this.Obj.setStyle("width", "100%");
        var topsize = this.ObjTop.getSize();
        this.SetHeight(scs.y + topsize.y + 50);
        this.OldBodyTop = bscrollsize.y;
        var myFx = new Fx.Scroll(page.body).set(0, 0);
    }

    if (this.Options.MaximizeBox == 0) {
        this.ObjMaximize.src = "/themes/admin/images/window/maximize_disable.gif";
    }
    else {
        //this.ObjMaximize.src = "/themes/admin/images/window/maximize.gif";
    }
    this.ObjMinimize.setStyle("display", "none");

    if (this.Options.Icon != undefined && this.Options.Icon != "") {
        this.ObjIcon.src = this.Options.Icon;
        this.ObjIcon.setStyle("display", "block");
    }
    else
        this.ObjIcon.setStyle("display", "none");

    this.Obj.setStyle("z-index", this.zIndex);
    this.ObjBg.SetZIndex(this.zIndex - 1);
};

WindowItem.prototype.Resize = function(x, y) {
    if (this.Options.WindowState == 0 && this.Options.AutoSizeY) {
        var topsize = this.ObjTop.getSize();
        this.Options.Height = y + topsize.y;
        //this.SetHeightContent(y - topsize.y);
        this.Redraw();
    }
};

WindowItem.prototype.SetHeight = function(height) {
    height = parseInt(height);
    this.ObjContent.setStyle("height", height + "px");
};
WindowItem.prototype.SetHeightContent = function(height) {
    //height = parseInt(height);
    //var topsize = this.ObjTop.getSize();
    //height = height - topsize.y;
    //this.Options.Height = height;
    this.ObjContent.setStyle("height", height + "px");
};

WindowItem.prototype.Dispose = function() {
    if (this.funcCloseCallBack != null) {
        this.funcCloseCallBack(this, this.Context);
    }
    if (this.ObjFrame) {
        this.ObjFrame.dispose();
        this.ObjFrame.destroy();
    }
    this.Obj.empty();
    this.Obj.dispose();
    this.Obj.destroy();
    this.IsDelete = 1;
    if (this.Options.IsBackgrounScreen == 1) {
        this.ObjBg.Delete();
    }

    this.ObjClose = null;
    this.ObjMaximize = null;
    this.ObjMinimize = null;
    this.ObjTitle = null;
    this.ObjContent = null;
    this.ObjButtons = null;
    this.ObjTop = null;
    this.ObjIcon = null;
    this.ObjToolbar = null;
    this.ObjFrame = null;
    var myFx = new Fx.Scroll(page.body).set(0, this.OldBodyTop);
};

WindowItem.prototype.SetIcon = function(src_ico) {
    this.Options.Icon = src_ico;
    this.Redraw();
};

WindowItem.prototype.SetWindowState = function(value) {
    this.Options.WindowState = value;
    this.Redraw();
};

WindowItem.prototype.SetZIndex = function(value) {
    this.zIndex = value;
    this.Obj.setStyle("z-index", this.zIndex);
    this.ObjBg.SetZIndex(this.zIndex - 1);
};

WindowItem.prototype.SetContent = function(OptionsContent) {
    if (OptionsContent != undefined) {
        this.Options.Content = OptionsContent;
    }
    this.EmptyContent();
    if (this.Options.Content.Type == "link") {
        var temp = this;
        this.ObjFrame = new Element("iframe", {
            'id': 'window_' + this.Id + '_frame',
            'name': 'window_' + this.Id + '_frame',
            'win_id': this.Id,
            'class': 'window_iframe',
            'src': this.Options.Content.Url,
            'frameborder': '0',
            'scrolling': 'auto'/*,
            "events": {
                "load": function() {
                    //this.document.body.offsetHeight);
                    //temp.SetHeight(this.document.body.offsetHeight);
                }
            }*/
        });
        this.ObjFrame.inject(this.ObjContent);
    }
    else if (this.Options.Content.Type == "ajax") {
        this.ObjContent.set('load', { evalScripts: true });
        this.ObjContent.load(this.Options.Content.Url);
    }
    else if (this.Options.Content.Type == "html") {
        this.ObjContent.set('html', this.Options.Content.Html);
    }
    else if (this.Options.Content.Type == "object") {
        if (this.Options.Content.Object)
            this.Options.Content.Object.inject(this.ObjContent);
    }
    else if (this.Options.Content.Type == "text") {
        this.ObjContent.set('html', this.Options.Content.Text);
    }
};

WindowItem.prototype.EmptyContent = function() {
    this.ObjContent.empty();
};

WindowItem.prototype.Focus = function() {
    page.wins.UpWin(this.Id);
};

