function MenuHandler() {
    var self = this;
    var itemId = null;
    var itemUrl = null;
    var targetDiv = null;
    var itemParent = null;
    var prevParent = null;

    
    $('#menu a[href="#menuCalculator"]').click(function () {
        boxShift.scrollSection('#menuCalculator');
        return false;
    });
   
   

    //Attach event listener for each menu open/close
    $('#menu .accordian').click(function (e) {

        var menu = $(this);
        if (menu.hasClass('loading') == false) {
            menu.addClass('loading');
            var items = menu.find('div.items');
            if (items.length == 0) {
                var url = lang + '/menu/menu/' + menu.attr('data-id')
                menu.children('.openClosePrompt').text("loading menu");
                $.get(url, function (response) {
                    menu.append(response);
                    menu.find('div.innerBox').slideDown('slow', function () {
                        menu.children('.openClosePrompt').text("click to close");
                        menu.children('.openClosePrompt').addClass("closeAccordian");
                        menu.removeClass('loading');
                    });
                });
            } else {
            }
        }

    });
    // Attach event listeners to each menu item
    $('.menuItem').live('click', function(e) {
          self.openMenuItem($(this));
    });


    this.openMenuItem = function(elem) {
        // Make sure it's actually a menu item, not a menu.
        if(elem.hasClass('menuItem')){
            
            var element = elem;
            itemParent = element.parent().attr('id');
            
            // Adds any ajaxed menu item content into the taget div.
            function showContent(data) {
                $(targetDiv).empty().append(data);
                
                var closeButton = '<a href="" class="menuCloseBtn"><span>close</span></a>';
                
                $(targetDiv).append(closeButton);
                $(targetDiv).children('.menuCloseBtn').click(function() {
                    // when the close btn is clicked, let's close it all up
                    // return all menuItem boxes to grey
                    $('.menuItem').removeClass('redMenuItem');     // grey            
                    $('.menuItem').children('span').removeClass('redMenuText');
            
                    $('.menuExpand').children().hide();
                    $('.menuExpand').stop(true, true).animate({
                        height: '0'
                    }, 500);
                    
                    itemId = null;        // set the itemId to something silly so that
                                          // the same one can be opened again
                    return false;
                });

                // load in the background image from the hidden div in the template
                var imagePath = $(targetDiv).children('.bgImageURL').html();
                
                $(targetDiv).css('background-image', 'url(' + imagePath + ')');          
                $(targetDiv).children().show();
                $(targetDiv).children('.bgImageURL').hide();

                // Attach event handler to nutritional info section
                $('.nutritional_info a .nutri').click(function() {
                    $(this).parent().parent().find('.info').toggle();
                    return false;
                });

                $(targetDiv).stop(true, true).animate({
                    height: '440',
                    opacity: 100
                }, 750, function() {
                    var pos = $(this).offset();
                    pos.top -= 250;
                    $('html, body').animate({ scrollTop: pos.top }, 500);

                });        // end animate
            }


            // so, let's make sure it's no the same id,
            // unless it's in a different container
            // we don't want them to be able to re-open
            // the item that they already have open

            if((itemId !== element.attr('id')) || (itemParent !== prevParent)) {
                prevParent = itemParent;

                // return all menuItem boxes to grey, then set
                // the selected one to red, and fix text contrast
                $('.menuItem').removeClass('redMenuItem');     // grey            
                element.addClass('redMenuItem');           // red
                $('.menuItem').children('span').removeClass('redMenuText');
                element.children('span').addClass('redMenuText');
                
                // hide any open expanded divs and clear loaded data
                $('.menuExpand').children().hide();
                $('.menuExpand').stop(true, true).animate({
                    height: '0'
                }, 500);
            
                // load in the template for the menu items, then show the content
                itemId = element.attr('id');
                targetDiv = element.delay(600).nextAll('.menuExpand:first');
                itemUrl = $('#' + itemId).data('url');

                $.ajax({
                    url: itemUrl,
                    dataType: 'html',
                    error: function(data){
                        showContent('<h3>Item not Found</h3>');
                    },
                    success: function(data){
                        showContent(data);
                    }
                });
            }                  // end if itemId
            
        }
    };
}                          // end MenuHandler



// This gets called from the boxshifter whenever a specific menu page is requested.
showMenuContent = function (url) {
  
    menuHandler.openMenuItem($('#' + url));
    var activeMenuItem = url;
};
