﻿/// <reference path="effects.js" />
/// <reference path="UIElements.js" />
/// <reference path="Controls.js" />
MenuObject =
{
    resize: function(element)
    {
        var whole = $G.GET(element, "up:3");
        var resizeItem = $G.GET(element, "up:2,next:1");
        var diff = "", bord = "";
        var flag;

        if (whole.offsetHeight < resizeItem.offsetHeight)
        {
            // Увеличиваем
            diff = resizeItem.offsetHeight + 10;
            bord = resizeItem.offsetHeight + 30;
            flag = 1;
        }
        else
        {
            // Уменьшаем
            diff = -resizeItem.offsetHeight - 10;
            bord = 25;
            flag = 0;
        }
        if ($G.GetStyle(whole, "height", true) == 0)
            $G.SetStyle("height", whole.offsetHeight + "px", whole);
        $FX.Time(500).AnimateStyle(whole, "height", diff, bord, function() { MenuObject.exchange(element, flag); }, "", "deghnasdhf23nch435gksdf");
    },

    exchange: function(element, flag)
    {
        var clickImage = $G.GET(element, "up:1");
        if (flag == 1)
        {
            clickImage.className = "menuMinimize";
        }
        else
        {
            clickImage.className = "menuResize";
        }
    }
}


ListControl =
{
    reSize: function(element)
    {
        var reSizeItem = $G.GET(element, "up:2,next:1");
        var clickImage = $G.GET(element, "up:1");
        if (reSizeItem.style.display == 'none')
        {
            reSizeItem.style.display = 'block';
            clickImage.className = "minimize";
        }
        else
        {
            reSizeItem.style.display = 'none';
            clickImage.className = "restore";
        }
    },

    reSizeRows: function(bigelement)
    {
        var divs = $G.Get.ByTag(bigelement, "div", false);
        var divsCount = divs.length;
        for (var i = 0; i < divsCount; i++)
        {
            var grey = $G.GET(divs[i], "div:0");
            var white = $G.GET(divs[i], "div:1")
            var name = $G.GET(grey, "div:1");
            grey.style.height = "auto";
            white.style.height = "auto";
            if (grey.offsetHeight < white.offsetHeight)
            {
                grey.style.height = white.offsetHeight + "px";
            }
            var first = $G.GET(grey, "div:0");
            if ($G.ClassName.Has(first, "itemMoveBlock"))
            {
                first.style.height = grey.offsetHeight + "px";
            }
        }
    }
}
$E = {
    Init: function()
    {
        this.FixPng();
        this.FixHeight();
        for (var f in $E)
        {
            if (!Function.prototype.isPrototypeOf($E[f]))
            {
                if ($E[f] && $E[f].Init)
                    $E[f].Init();
            }
        }
    },
    FixHeight: function()
    {
        if ($G.Browser.Detect.ie)
        {
            var left = $G.Get.ById("main_left");
            var right = $G.Get.ById("main_right");
            if (left && right)
            {
                var delta = right.offsetHeight - left.offsetHeight;
                var holder = $G.Get.ById("height_fixer");
                if (holder)
                    holder.style.height = ((delta > 0 ? delta : 0) + 32) + "px";
            }
        }
    },
    FixPng: function(obj)
    {
        if ($G.Browser.Detect.ie6)
        {
            var obj = obj ? obj : document;
            var _imgs = obj.getElementsByTagName("img");
            for (var i = 0; i < _imgs.length; i++)
            {
                if (_imgs[i].src.toLowerCase().indexOf(".png") != -1)
                {
                    var _img = _imgs[i];
                    var h = _img.height;
                    var w = _img.width;
                    if (h && w)
                    {
                        var src = _img.src;
                        _img.src = A2.UI.Url + "/resource/px.gif";
                        _img.style.width = w + "px";
                        _img.style.height = h + "px";
                        _img.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', SizingMethod='scale')";
                    }
                }
            }
        }
    },
    ToolTip: function(text, nohide, width, x, y)
    {
        var h = $G.Window.Height();
        var w = $G.Window.Width();
        var menu = $G.Get.ById("ToolTip");
        if (menu)
            menu.parentNode.removeChild(menu);
        menu = $G.Tag("div", null, "ToolTip");
        var _menu = new A2.UI.Control(menu);
        if ($G.isString(text))
            text = new A2.UI.Control($G.Tag("p", null, null, text));
        _menu.AddControl(text);
        if (width)
            $G.SetStyle("width", width + "px", menu);
        var ie6 = $G.Browser.Detect.ie && !$G.Browser.Detect.ie7;
        if (!ie6)
            $G.SetOpacity(menu, 0);
        $G.Append(document.body, menu);
        $G.ShowAtPosition(menu, false, 5 + (x || 0), 5 + (y || 0));
        if (!nohide)
            menu.onmouseout = $E.ToolTipHide;
        if (!ie6)
        {
            $FX.Time(300).FadeIn(menu, function() { }, "ToolTipAppear");
        }
    },
    flag: null,
    ToolTipShow: function(text, nodelay, nohide, width, xoffset, yoffset)
    {
        $E.flag = Math.random();
        var a = $E.flag;
        setTimeout(function()
        {
            if (this.a == $E.flag)
                $E.ToolTip(this.text, this.nohide, this.width, this.x, this.y);
        } .bind({ a: a, text: text, nohide: nohide, width: width, x: xoffset, y: yoffset }), (nodelay ? 1 : 500));
    },
    inToolTip: false,
    ToolTipHide: function(e)
    {
        $E.flag = Math.random();
        setTimeout(function()
        {
            if (!$E.inToolTip)
            {
                var menu = $G.Get.ById("ToolTip");
                if (menu)
                    menu.parentNode.removeChild(menu);
            }
        }, 500);
    },
    ShowDateTime: function(obj, x, y)
    {
        if (obj)
        {
            var calendar = new A2.UI.Calendar(function(date)
            {
                var input = $G.GET(this, "prev:1");
                if (input)
                {
                    var dates = (input.value || "").split(" ");
                    if (dates.length == 1)
                        dates.push("00:00");
                    dates[0] = $G.Date.ShortDate(date);
                    input.value = dates.join(" ");
                    if ($G.Browser.Detect.ie && !$G.Browser.Detect.ie7)
                        $G.Get.ById("ToolTip").parentNode.removeChild($G.Get.ById("ToolTip"));
                    else
                        $FX.Time(300).FadeOut($G.Get.ById("ToolTip"), function() { $G.Get.ById("ToolTip").parentNode.removeChild($G.Get.ById("ToolTip")) }, "ToolTipDisappear");
                }
            } .bind(obj));
            $E.ToolTipShow(calendar, true, true, 150, x, y);
            document.body.onclick = $E.BodyClick.bind(calendar);
        }
    },
    BodyClick: function(e)
    {
        var targ = $G.Event.Target(e);
        if (!this.ContainsElement(targ))
        {
            if ($G.Browser.Detect.ie && !$G.Browser.Detect.ie7)
            { var tip = $G.Get.ById("ToolTip"); if (tip) { tip.parentNode.removeChild($G.Get.ById("ToolTip")) }; }
            else
                $FX.Time(300).FadeOut($G.Get.ById("ToolTip"), function() { var tip = $G.Get.ById("ToolTip"); if (tip) { tip.parentNode.removeChild($G.Get.ById("ToolTip")) } }, "ToolTipDisappear");
        }
    },
    ShowCalendar: function(obj, x, y)
    {
        if (obj)
        {
            var calendar = new A2.UI.Calendar(function(date)
            {
                $G.GET(this, "prev:1").value = $G.Date.ShortDate(date);
                if ($G.Browser.Detect.ie && !$G.Browser.Detect.ie7)
                { var tip = $G.Get.ById("ToolTip"); if (tip) { tip.parentNode.removeChild($G.Get.ById("ToolTip")) }; }
                else
                    $FX.Time(300).FadeOut($G.Get.ById("ToolTip"), function() { var tip = $G.Get.ById("ToolTip"); if (tip) { tip.parentNode.removeChild($G.Get.ById("ToolTip")) } }, "ToolTipDisappear");
            } .bind(obj));
            $E.ToolTipShow(calendar, true, true, 150, x, y);
            document.body.onclick = $E.BodyClick.bind(calendar);
        }
    },
    _tmpLdr: null
}
$E._tmpLdr = window.onload;
window.onload = function(e)
{
    if ($E._tmpLdr && Function.prototype.isPrototypeOf($E._tmpLdr))
        $E._tmpLdr(e);
    $E.Init();
}