///<reference path="global.js" />
///<reference path="uielements.js" />
///<reference path="main.js" />

DoorSelect =
{
    Init: function() {
        DoorSelect.Div = $G.Append(document.body, $G.Tag("div", "door-popup", "door-popup"));
        DoorSelect.Div.Pic = $G.Append(DoorSelect.Div, $G.Tag("div", "picdoor", "pic"));
    },

    Div: null,
    SelectAction: function() {
        switch ($UI.Hotkey.Pressed[0]) {
            case $UI.Hotkey.Keys.Esc:
                return DoorSelect.Close();
                break;
            default:
                return null;
        }
    },

    Show: function(obj) {
        if (DoorSelect.Div) {
            DoorSelect.Div.style.backgroundImage = "url(resource/img/loader.gif)";            

            $UI.Hotkey.Register($UI.Hotkey.Keys.Esc, function() { DoorSelect.SelectAction(); }, "Close");
            if (obj.innerHTML != "") {
                var _gTop = Math.round($G.Window.Height() / 2 + $G.Window.Scroll.Top()) - 34;
                var _gLeft = Math.round($G.Window.Width() / 2 + $G.Window.Scroll.Left()) - 34;
                DoorSelect.Div.style.width = "68px";
                DoorSelect.Div.style.height = "68px";

                DoorSelect.Div.style.overflow = "hidden";
                DoorSelect.Div.style.top = _gTop + "px";
                DoorSelect.Div.style.left = _gLeft + "px";
                DoorSelect.Div.Pic.style.display = "none";
                DoorSelect.Div.style.display = "block";

                $G.Append(DoorSelect.Div.Pic, obj);
                //добавил сейчас            
                DoorSelect.Div.focus();
                //затемняем
                $FX.Time(500).Page.FadeIn("#000000", 60, DoorSelect.LoadCheck.bind({ obj: obj }));
            }
        }
        else {
            obj.style.display = "none";
        }

    },
    LoadCheck: function() {
        if (this.obj.innerHTML != "" && $G.Get.ById('DoorImg').complete && $G.Get.ById('DoorDoubleCheckBox').complete && $G.Get.ById('img').complete) {
            $UI.Hotkey.Register($UI.Hotkey.Keys.Esc, function() { DoorSelect.SelectAction(); }, "Close");
            DoorSelect.Current = this;
            DoorSelect.Div.style.backgroundImage = "url(resource/img/px.gif)";

            DoorSelect.Div.Pic.style.display = "block";

            var _gTop = Math.round($G.Window.Height() / 2 + $G.Window.Scroll.Top());
            var _gLeft = Math.round($G.Window.Width() / 2 + $G.Window.Scroll.Left());
            $G.Append(DoorSelect.Div.Pic, this.obj);

            DoorSelect.Div.Pic.focus();

            var content = document.getElementById("DoorForm");

            $FX.Time(200).AnimateStyle(
                DoorSelect.Div.Pic,
                "height",
                content.offsetHeight,
                content.offsetHeight,
                function() {
                    DoorSelect.Div.style.height = DoorSelect.Div.Pic.style.height;
                    var height = parseInt(DoorSelect.Div.Pic.style.height);
                    DoorSelect.Div.style.top = Math.round(_gTop - (height / 2)) + "px";

                    $FX.Time(400).AnimateStyle(
                    DoorSelect.Div.Pic,
                    "width",
                    content.offsetWidth,
                    content.offsetWidth,
                        function() {
                            DoorSelect.Div.style.width = DoorSelect.Div.Pic.style.width;
                            var width = parseInt(DoorSelect.Div.Pic.style.width);
                            DoorSelect.Div.style.left = Math.round(_gLeft - (width / 2)) + "px";
                        } .bind(this),
                        function(f) {
                            if (f != null) {
                                DoorSelect.Div.style.width = f + "px";
                                DoorSelect.Div.style.left = Math.round(_gLeft - (f / 2)) + "px";
                            }
                        }, "AW");
                } .bind(this),
                function(f) {
                    if (f != null) {
                        DoorSelect.Div.style.height = f + "px";
                        DoorSelect.Div.style.top = Math.round(_gTop - (f / 2)) + "px";
                    }
                    }, "AH");
            DoorSelect.Div.Pic.focus();
        }
        else {
            setTimeout(DoorSelect.LoadCheck.bind(this), 100);
        }
    },

    Close: function() {
        $UI.Hotkey.UnRegister($UI.Hotkey.Keys.Esc, "Close");
        $G.SetStyle("display", "none", DoorSelect.Div.Close, DoorSelect.Div.Pic);
        DoorSelect.Div.style.backgroundImage = "url(resource/img/px.gif)";
        var cTop = Math.round($G.Window.Height() / 2) + $G.Window.Scroll.Top();
        var cLeft = Math.round($G.Window.Width() / 2) + $G.Window.Scroll.Left();
        var dLeft = cLeft - $G.GetStyle(DoorSelect.Div, "left", true);
        var dTop = cTop - $G.GetStyle(DoorSelect.Div, "top", true);
        $FX.Time(300).Page.FadeOut("#000000");
        $FX.Time(300).AnimateStyle(DoorSelect.Div, "width", -$G.GetStyle(DoorSelect.Div, "width", true), 0, function() { DoorSelect.Div.style.display = "none"; }, null, "GW");
        $FX.Time(300).AnimateStyle(DoorSelect.Div, "height", -$G.GetStyle(DoorSelect.Div, "height", true), 0, null, null, "GH");
        $FX.Time(300).AnimateStyle(DoorSelect.Div, "top", dTop, cTop, null, null, "GT");
        $FX.Time(300).AnimateStyle(DoorSelect.Div, "left", dLeft, cLeft, null, null, "GL");
        DoorSelect.Div.Pic.style.display = "none";
        DoorSelect.Div.style.display = "none";        
        DoorSelect.Div.style.backgroundImage = "url(resource/img/loader.gif)";
    }
}
DoorSelect._GalleryingLoad = window.onload;
window.onload = function(e){  
    if (DoorSelect._GalleryingLoad)
        DoorSelect._GalleryingLoad(e);
    DoorSelect.Init();
}
