﻿function amenu(cname, name)
{
    this.cobj = $(cname);
    this.obj = $(name);
    this.arr = Array(0);
    this.zi = 10;

    this.sel = null;

    this.timer_o = 0;

    this.typea = 1;

    this.timer_a = 0;

    this.timer_a_start = 0;

    this.add = function(src, oid)
    {
        var l = this.arr.length;
        this.arr[l] = new amenu_item(l, oid, src, this.obj, this.typea)
        return l;
    }

    this.init = function()
    {
        var i = 0;

        for (i = 0; i < this.arr.length; i++)
        {
            this.arr[i].init();
            this.arr[i].obj_bg.inject(this.cobj);

            if (i > 0)
                this.arr[i].hide();

            this.arr[i].obj.addEvent("mouseover", function(a, b, c)
            {
                return function()
                {
                    a.hide_all(1);
                    b.timer_over = setTimeout(function(a, b)
                    {
                        return function()
                        {
                            a.select(b);
                        }
                    } (a, c), 300);
                    //}

                    //показываем подменю
                    b.show_sub();
                }
            } (this, this.arr[i], i));
            this.arr[i].obj.addEvent("mouseout", function(a, b, c)
            {
                return function()
                {
                    if (b.is_show == 0)
                    {
                        b.hide();
                    }
                    //скрываем подменю
                    b.timer_sub = setTimeout(function(b)
                    {
                        return function()
                        {
                            b.hide_sub();
                        }
                    } (b), 10);
                }
            } (this, this.arr[i], i));
            this.arr[i].obj.addEvent("mousemove", function(a, b, c)
            {
                return function()
                {
                    clearTimeout(b.timer_sub);
                }
            } (this, this.arr[i], i));
        }

        this.obj.addEvent("mousemove", function(a)
        {
            return function()
            {
                a.MoveMenu();
            }
        } (this));
        this.obj.addEvent("mouseout", function(a)
        {
            return function()
            {
                a.timer_o = setTimeout(function(a)
                {
                    return function()
                    {
                        a.OutMenu();
                    }
                } (a), 150);
            }
        } (this));
    }

    this.MoveMenu = function()
    {
        clearTimeout(this.timer_o);

        this.StopAnimation();
    }

    this.OutMenu = function()
    {
        var i = 0;

        for (i = 0; i < this.arr.length; i++)
        {
            clearTimeout(this.arr[i].timer_over);
        }

        this.timer_a_start = setTimeout(function(a)
        {
            return function()
            {
                a.StartAnimation();
            }
        } (this), 3000);
    }

    this.MouseOverItem = function(id)
    {
        if (this.arr[id].timer_over == 0)
        {
            this.hide_all(1);
            this.arr[id].timer_over = setTimeout(function(a, b)
            {
                return function()
                {
                    a.select(b);
                }
            } (this, id), 600);
        }
    }

    this.hide_all = function() {
        var i = 0;
        for (i = 0; i < this.arr.length; i++) {
            if (this.arr[i].is_show == 1 || this.sel == i) {
                continue;
            }
            this.arr[i].hide();
            clearTimeout(this.arr[i].timer_over);
            this.arr[i].timer_over = 0;
            this.arr[i].hide_sub();
        }
    }

    this.select = function(id)
    {
        if (id == this.sel)
            return;

        if (this.sel != null)
        {
            if (this.typea == 0)
                this.arr[this.sel].obj_bg.tween("top", "-289px");
            else if (this.typea == 1)
                this.arr[this.sel].obj_bg.tween("opacity", "0");
        }

        var old = this.sel;

        this.arr[id].show();
        this.sel = id;
        if (this.typea == 0)
            this.arr[id].obj_bg.tween("top", "0px");
        else if (this.typea == 1)
        {
            this.arr[id].obj_bg.set("tween", { duration: 600, transition: Fx.Transitions.linear.easeIn });
            this.arr[id].obj_bg.tween("opacity", "1");
        }

        var i = 0;
        for (i = 0; i < this.arr.length; i++)
        {
            if (id == i)
            {
                continue;
            }
            if (old == i)
            {
                continue;
            }
            this.arr[i].hide();
            clearTimeout(this.arr[i].timer_over);
            this.arr[i].timer_over = 0;
        }
    }

    this.StartAnimation = function()
    {
        for (i = 0; i < this.arr.length; i++) {
            clearTimeout(this.arr[i].timer_over);
            this.arr[i].timer_over = 0;

            clearTimeout(this.arr[i].timer_sub);
            this.arr[i].timer_sub = 0;
        }
        
        this.timer_a = setTimeout(function(a)
        {
            return function()
            {
                a.Animation();
            }
        } (this), 3000);
    }

    this.StopAnimation = function()
    {
        clearTimeout(this.timer_a);
        clearTimeout(this.timer_a_start);

        var i = 0;

        for (i = 0; i < this.arr.length; i++)
        {
            this.arr[i].obj.removeClass("over");
        }
    }

    this.sela = null;
    this.Animation = function()
    {
        if (this.sela == null)
            this.sela = 0;
        else if (this.sela == (this.arr.length - 1))
        {
            this.arr[this.sela].obj.removeClass("over");
            this.sela = 0;
        }
        else
        {
            this.arr[this.sela].obj.removeClass("over");
            this.sela = this.sela + 1;
        }

        this.arr[this.sela].obj.addClass("over");

        this.hide_all(1);
        this.select(this.sela);

        this.StartAnimation();
    }
}

function amenu_item(id, oid, src, pobj, typea)
{
    this.id = id;
    this.oid = oid;
    this.src = src;
    this.pobj = pobj;
    this.typea = typea;

    this.obj = $("amenu_item_" + this.id);

    this.obj_sub = $("amenu_item_" + this.id + "_sub");
    //this.obj_sub_close = $("amenu_item_" + this.id + "_sub_close");

    this.obj_bg = null;

    this.timer_over = 0;
    this.timer_sub = 0;

    this.is_show = 0;

    this.f = null;

    this.init = function()
    {
        var myImage = new Asset.image(this.src);

        this.obj_bg = new Element('div', {
            'id': "amenu_item_" + this.id + "_bg",
            'class': 'bg'
        });
        this.obj_bg.setStyle("background", "url(" + this.src + ") no-repeat");
        this.obj_bg.setStyle("display", "none");

        if (this.obj_sub)
        {
            this.obj_sub.addEvent("mouseout", function(a)
            {
                return function()
                {
                    a.timer_sub = setTimeout(function(a)
                    {
                        return function()
                        {
                            a.hide_sub();
                            am.OutMenu();
                        }
                    } (a), 50);
                }
            } (this));

            this.obj_sub.addEvent("mousemove", function(a)
            {
                return function()
                {
                    clearTimeout(a.timer_sub);
                    am.StopAnimation();
                }
            } (this));

           /* this.obj_sub_close.addEvent("click", function(a)
            {
                return function()
                {
                    a.obj_sub.setStyle("display", "none");
                    return false;
                }
            } (this));*/
        }
    }

    this.show = function()
    {
        if (this.typea == 0)
        {
            this.obj_bg.setStyle("top", "289px");
            this.obj_bg.setStyle("display", "block");
        }
        else if (this.typea == 1)
        {
            this.obj_bg.setStyle("top", "0px");
            this.obj_bg.setStyle("opacity", "0");
            this.obj_bg.setStyle("display", "block");
        }
        this.is_show = 1;
    }

    this.hide = function()
    {
        this.obj_bg.setStyle("display", "none");
        //this.obj_sub.setStyle("display", "none");
        this.is_show = 0;
    }

    this.show_sub = function()
    {
        if (!this.obj_sub)
            return;
        this.obj_sub.setStyle("display", "block");

        var p2 = $("bg_div").getPosition();

        var p = this.obj.getPosition();
        this.obj_sub.setStyle("top", p.y - p2.y - 13);
    }

    this.hide_sub = function()
    {
        if (!this.obj_sub)
            return;
        this.obj_sub.setStyle("display", "none");
    }
}