var Menu = {
  data: [
  {
    title: 'Shop',
    categories: [
    {
      title: 'dark chocolate', 
      key: '#dark-chocolate',
      type: 'category'
    }, 

    {
      title: 'milk chocolate', 
      key: '#milk-chocolate',
      type: 'category'
    }, 

    {
      title: 'chocolate with nuts', 
      key: '#nuts',
      type: 'category'
    }, 

{
      title: 'dragée', 
      key: '#dragee',
      type: 'category'
    }, 

    {
      title: 'bars', 
      key: '#bars',
      type: 'category'
    }

    ]
  },
  {
    title: 'Gifts',
    categories: [
    {
      title: 'for my wife', 
      key: '#wife',
      type: 'category'
    }, 

    {
      title: 'for my husband', 
      key: '#husband',
      type: 'category'
    }, 

    {
      title: 'for a friend', 
      key: '#bff',
      type: 'category'
    },  

    {
      title: 'for me', 
      key: '#me',
      type: 'category'
    },  

    {
      title: 'for dad', 
      key: '#dad',
      type: 'category'
    },  

    {
      title: 'for mom', 
      key: '#mom',
      type: 'category'
    },  

    {
      title: 'for a prospect, a business gift', 
      key: '#deals',
      type: 'category'
    },  

    {
      title: 'for expressing my sympathies', 
      key: '#sympathies',
      type: 'category'
    }
    ]
  },
  {
    title: 'Seasons',
    categories: [
    {
      title: 'For my Valentine', 
      key: '/valentine.html',
      type: 'category'
    }, 

    {
      title: 'Easter &amp; Passover', 
      key: '/spring.html',
      type: 'category'
    }, 

    {
      title: 'Thanksgiving', 
      key: '/thanksgiving.html',
      type: 'category'
    }, 

    {
      title: 'Christmas &amp; Chanukah', 
      key: '/winter.html',
      type: 'category'
    }
    ]
  },
  {
    title: 'Styles',
    categories: [
    {
      title: 'Avant-Garde', 
      key: '#avantgarde',
      type: 'category'
    }, 

    {
      title: 'Boardroom Treats', 
      key: '#boardroomtreats',
      type: 'category'
    }, 

    {
      title: 'Bombons', 
      key: '#bombons',
      type: 'category'
    }, 

    {
      title: 'ChocoChocolate', 
      key: '#chocochocolate',
      type: 'category'
    }
    ]
  },
  {
    title: 'Chocolate',
    categories: [
    {
      title: 'What are the ingredients', 
      key: '/ingredients.html',
      type: 'page'
    }, 

    {
      title: 'Food of the gods', 
      key: '/food-of-the-gods.html',
      type: 'page'
    }, 

    {
      title: 'Keeping your chocolate fresh', 
      key: '/storing-chocolate.html',
      type: 'page'
    }

    ]
  },
  {
    title: 'About Chocoville',
    categories: [
    {
      title: 'Who we are', 
      key: '/chocoville-store.html',
      type: 'page'
    }, 

    {
      title: 'Becoming a Trusted Tester', 
      key: '/trusted.html',
      type: 'page'
    }, 

    {
      title: '100% Satisfaction Guarantee', 
      key: '/satisfaction.html',
      type: 'page'
    }, 

    {
      title: 'Supporting 501-c-3 organizations', 
      key: '/charity.html',
      type: 'page'
    }
    ]
  },
  {
    title: 'Retailers',
    categories: [
    {
      title: 'How to become a Gaspare retailer', 
      key: '/retailers.html',
      type: 'page'
    }, 

    {
      title: 'Ordering Samples', 
      key: '/sample.html',
      type: 'page'
    }
    ]
  },
  {
    title: 'For Industry',
    categories: [
    {
      title: 'Hospitality Chocolate', 
      key: '/hospitality-chocolate.html',
      type: 'page'
    }, 

    {
      title: 'Trade Shows and Events', 
      key: '/event-chocolates.html',
      type: 'page'
    }, 

    {
      title: 'Meetings and Guests', 
      key: '/boardroom-pantry.html',
      type: 'page'
    }

    ]
  },
  {
    title: 'Events',
    categories: [
    {
      title: 'Our Tasting Events', 
      key: '/events.html',
      type: 'page'
    }, 

    {
      title: 'Photo Competitions', 
      key: '/photography.html',
      type: 'page'
    }, 

    {
      title: 'Your upcoming event', 
      key: '/event-chocolates.html',
      type: 'page'
    }
    ]
  }
  ],
  container: 'main_menu',

  init: function() {
    //render and display the menu
    this._renderTopLevel(this.data, $('#' + this.container));
  },

  getTitle: function(category) {
    var group = '';
    for(var key in this.data) {
      group = this.data[key].title;
      for (var key2 in this.data[key].categories) {
        if (this.data[key].categories[key2].key == category) {
          return group + " " + this.data[key].categories[key2].title;
        }
      }
    } 
  },

  _renderTopLevel: function(data, container) {
    for (var menu in data) {
      var menu_item = data[menu];
      var el = $('<li><a href="#"><span>' + menu_item.title + '</span></a></li>');
      var cats = this._renderCategorySecondLevel(menu_item.categories, el);
      el.append(cats);
      //el.attr('data', menu);
      container.append(el);
    }

  },

  _renderCategorySecondLevel: function(data) {
    var category_list = $('<ul></ul>');
    for (var categories in data) {
      var category = data[categories];
      var elCategory = $('<li><a href="' + category.key + '"><span>' + category.title + '</span></a></li>');
      elCategory.find("a").click(function(e) {
        e.preventDefault();
        var link = "";
        if ($(this).attr('href').indexOf("#") == 0) {
          link = $(this).attr('href').substring(1) + ".html";
        } else {
          link = $(this).attr('href');
        }
        if(category.type == "category") {
            GaspareStore._loadPopup(Category, link);
        } else if (category.type == "page") {
          GaspareStore._loadPopup(ExtraPage, link);          
        } else  if (category.type == "product") {
          GaspareStore._loadPopup(GsProduct, link);             
        }
        return false;
      });
      category_list.append(elCategory);
    } 
    return category_list;
  }
};

