﻿
function MenuItem(title, href, menu,imageUrl,textAlign,imageAlign) {
    this.Title = title;
    this.Href = href;
    this.TextAlignment = textAlign;
    this.ImageAlignment = imageAlign;
    this.ImageUrl = imageUrl;
    if (menu) {
        if (menu != "GoBack") {
            this.GoesToMenu = true;
            menu.MenuItems.unshift(new MenuItem("<<", "#", "GoBack"));
        } else {
        }
        this.InnerMenu = menu;

    } else {
        this.InnerMenu = undefined;
    }
    this.Parent = null;

    this.GoBack = null;
}
function Menu(title, menuItems) {
    this.Title = title;
    this.MenuItems = new Array();
    var that = this;
    $.each(menuItems, function (i, v) {
        that.MenuItems.push(v);
        v.Parent = that;
    });
    this.AddItem = function (item) {
        that.MenuItems.push(item);
        item.Parent = that;
    }
    this.internalPointer = null;
    this.internalMenus = new Array();
    this.parent = null;
  
    this.Render = function (template, proxy) {
        var _that = this;
        var menu = null;
        if (proxy == 0) {
            menu = $(template).tmpl(this);
            $(menu).appendTo("body");
        } else {
            menu = $(proxy);
            $(menu).appendTo("body");
        }
        $(menu).find(".close-menu").click(function (e) {
            $(menu).hide().appendTo(_that.parent);
        });
        $(menu).find(".menu-item-list:first > .menu-item[data-goback]").click(function (e) {
            $(menu).hide().appendTo(_that.parent);
            var pop = $($(_that.parent).parents(".popup-menu:first"));
            $(pop).appendTo("body").show();
            e.stopPropagation();
        });
        $(menu).find(".menu-item").not(".menu-item[data-goback]").has(".popup-menu").click(function (e) {
            var that = this;
            $(menu).fadeOut(0, function () {

                var _innerMenu = $(that).find(".popup-menu:first");
                var _iM = new Menu("", []);
                _iM.parent = that;
                _that.internalMenus.push({ item: that, menu: _iM });
                _iM.Render("#popup-template", _innerMenu);
                $(_iM.internalPointer).fadeIn(0);
            });
            e.stopPropagation();
        });
        this.internalPointer = menu;
        return menu;
    }
    this.UpLevel = function () {
        this.FadeOut(0);
    }
    this.FadeOut = function (time) {
        $(this.internalPointer).fadeOut(time, function () {

        });
    }
    this.FadeIn = function (time) {
        var that = this.internalPointer;
        $(this.internalPointer).fadeIn(time);
        $.each(this.internalMenus, function (i, v) {
            $(v.menu.interalPointer).appendTo($(v.item));
        });
    }
}

