
if (typeof(SM)=="undefined" || !SM || typeof(SM)!="object") SM = {};
SM.Menu = {};

SM.Menu.nutritionToolTip = function(tooltipClass){
    this.tooltipClass = tooltipClass;

    var tooltips = YAHOO.util.Dom.getElementsByClassName(tooltipClass);
    var items = []
    for (var i=0; i < tooltips.length; i++) {
        if (tooltipClass == "mealNutritionInfo") {
            items.push(YAHOO.util.Dom.getChildren(tooltips[i].parentNode)[0]);
        } else {
            items.push(tooltips[i].parentNode);
        }
    }
    YAHOO.util.Event.addListener(items, "mouseover", this.hover, this, true);
    YAHOO.util.Event.addListener(items, "mouseout", this.unhover, this, true);
    if (!SM.Menu.globalIETip) {
        SM.Menu.globalIETip = document.createElement('div');
        SM.Menu.globalIETip.style.display = 'none';
        SM.Menu.globalIETip.style.postion = 'absolute';
        SM.Menu.globalIETip.style.width = '130px';
        SM.Menu.globalIETip.style.color = '#f18021';
        SM.Menu.globalIETip.style.zIndex = '20';
        SM.Menu.globalIETip.style.border = '#f18021 1px solid';
        SM.Menu.globalIETip.style.padding = '10px';
        SM.Menu.globalIETip.style.fontSize = '11px';
        SM.Menu.globalIETip.style.backgroundColor = '#ffffff';
        SM.Menu.globalIETip.className = 'tooltip';
        SM.Menu.globalIETip = document.body.appendChild(SM.Menu.globalIETip);
    }
}

SM.Menu.nutritionToolTip.prototype.hover = function(e) {
    var el = YAHOO.util.Event.getTarget(e);
    if (!YAHOO.util.Dom.hasClass(el, "menu_property") && el.nodeName != "LI") el = el.parentNode;
    var tip = YAHOO.util.Dom.getElementsByClassName(this.tooltipClass, 'div', el)[0];
    if (tip && tip.style) {
        SM.Menu.globalIETip.innerHTML = tip.innerHTML;
        tip = SM.Menu.globalIETip;
        tip.style.display = 'block';
        var xy = YAHOO.util.Event.getXY(e);
        xy[0] = xy[0] + 10;
        xy[1] = xy[1] + 10;
        YAHOO.util.Dom.setXY(tip, xy);
    }
}
SM.Menu.nutritionToolTip.prototype.unhover = function(e) {
    var el = YAHOO.util.Event.getTarget(e);
    var tip = SM.Menu.globalIETip;
    if (tip && tip.style) tip.style.display = 'none';
}


SM.Menu.mealCollapse = (function(){
    var self = {};

    var menuGroup = function(link1, link2, link3) {
        this.open = 2;
        var offset = YAHOO.env.ua.ie > 0? 15:0;
        this.item1 = link1.nextSibling;
        while (this.item1.nodeType!=1 || !YAHOO.util.Dom.hasClass(this.item1, 'menu-content')) this.item1 = this.item1.nextSibling;
        this.item2 = link2.nextSibling;
        while (this.item2.nodeType!=1 || !YAHOO.util.Dom.hasClass(this.item2, 'menu-content')) this.item2 = this.item2.nextSibling;
        this.item3 = link3.nextSibling;
        while (this.item3.nodeType!=1 || !YAHOO.util.Dom.hasClass(this.item3, 'menu-content')) this.item3 = this.item3.nextSibling;
        this.show1 = new YAHOO.util.Anim(this.item1, { height: {from:1, to:this.item1.scrollHeight+offset}}, .7, YAHOO.util.Easing.easeIn);
        this.show2 = new YAHOO.util.Anim(this.item2, { height: {from:1, to:this.item2.scrollHeight+offset}}, .7, YAHOO.util.Easing.easeIn);
        this.show3 = new YAHOO.util.Anim(this.item3, { height: {from:1, to:this.item3.scrollHeight+offset}}, .7, YAHOO.util.Easing.easeIn);
        this.hide1 = new YAHOO.util.Anim(this.item1, { height: {to:1}}, .7, YAHOO.util.Easing.easeOut);
        this.hide2 = new YAHOO.util.Anim(this.item2, { height: {to:1}}, .7, YAHOO.util.Easing.easeOut);
        this.hide3 = new YAHOO.util.Anim(this.item3, { height: {to:1}}, .7, YAHOO.util.Easing.easeOut);
        this.link1 = link1;
        this.link2 = link2;
        this.link3 = link3;
    }
    menuGroup.prototype.init = function() {
        YAHOO.util.Event.addListener(this.link1, "click", this.choose1, this, true);
        YAHOO.util.Event.addListener(this.link2, "click", this.choose2, this, true);
        YAHOO.util.Event.addListener(this.link3, "click", this.choose3, this, true);
        this.show1.onComplete.subscribe(this.showComplete, this, true);
        this.show2.onComplete.subscribe(this.showComplete, this, true);
        this.show3.onComplete.subscribe(this.showComplete, this, true);
    }
    menuGroup.prototype.showComplete = function() {
        if (this.open == 1)
            this.item1.style.overflow = 'visible';
        else if (this.open == 2)
            this.item2.style.overflow = 'visible';
        else if (this.open == 3)
            this.item3.style.overflow = 'visible';
    }
    menuGroup.prototype.choose1 = function(e) {
        YAHOO.util.Event.preventDefault(e);
        if (this.open == 2) {
            this.hide2.animate();
            this.item2.style.overflow = 'hidden';
        } else if (this.open == 3) {
            this.hide3.animate();
            this.item3.style.overflow = 'hidden';
        } else {
            return;
        }
        this.show1.animate();
        this.open = 1;
    };
    menuGroup.prototype.choose2 = function(e) {
        YAHOO.util.Event.preventDefault(e);
        if (this.open == 1) {
            this.hide1.animate();
            this.item1.style.overflow = 'hidden';
        } else if (this.open == 3) {
            this.hide3.animate();
            this.item3.style.overflow = 'hidden';
        } else {
            return;
        }
        this.show2.animate();
        this.open = 2;
    };
    menuGroup.prototype.choose3 = function(e) {
        YAHOO.util.Event.preventDefault(e);
        if (this.open == 1) {
            this.hide1.animate();
            this.item1.style.overflow = 'hidden';
        } else if (this.open == 2) {
            this.hide2.animate();
            this.item2.style.overflow = 'hidden';
        } else {
            return;
        }
        this.show3.animate();
        this.open = 3;
    };

    self.init = function() {
        var lunch, after, vg;
        this.days = [];
        var breakfastLinks = YAHOO.util.Dom.getElementsBy(function(node){
                              if(YAHOO.util.Dom.hasClass(node,'breakfastLink'))return true; return false;}, 'a');
        for (var i=0; i<breakfastLinks.length; i++) {
            lunch = YAHOO.util.Dom.getElementsByClassName('lunchLink', 'a', breakfastLinks[i].parentNode)[0];
            after = YAHOO.util.Dom.getElementsByClassName('afterSchoolLink', 'a', breakfastLinks[i].parentNode)[0];
            mg = new menuGroup(breakfastLinks[i], lunch, after);
            mg.init();
            this.days.push(mg);
        }
    }

    return self;
})();

SM.Menu.tabView = new YAHOO.widget.TabView('submealTab');

SM.Menu.tabScrollLeft = function(){
    this.scrollAnim.attributes.marginLeft.to = 0;
    YAHOO.util.Dom.addClass(this.leftScroll, 'yui-hidden');
    YAHOO.util.Dom.removeClass(this.rightScroll, 'yui-hidden');
    this.scrollAnim.animate();
}

SM.Menu.tabScrollRight = function(){
    var width = this.tabView._tabParent.clientWidth - 413;
    this.scrollAnim.attributes.marginLeft.to = -width;
    YAHOO.util.Dom.removeClass(this.leftScroll, 'yui-hidden');
    YAHOO.util.Dom.addClass(this.rightScroll, 'yui-hidden');
    this.scrollAnim.animate();
}

YAHOO.util.Event.onDOMReady(function(){
    new SM.Menu.nutritionToolTip("mealNutritionInfo");
    new SM.Menu.nutritionToolTip("menu_property_title");

    var tabUl = SM.Menu.tabView._tabParent;
    if (tabUl.scrollHeight > 43) {
        var width = 500;
        tabUl.style.width = width + 'px';
        while (tabUl.scrollHeight > 43) {
            width += 10;
            tabUl.style.width = width + 'px';
            if (width > 1200) break;
        }
        tabUl.style.width = (width+10) + 'px';

        SM.Menu.leftScroll = YAHOO.util.Dom.get("submealTabLeft");
        SM.Menu.rightScroll = YAHOO.util.Dom.get("submealTabRight");
        YAHOO.util.Dom.removeClass(SM.Menu.rightScroll, "yui-hidden");
        SM.Menu.scrollAnim = new YAHOO.util.Anim(tabUl, { marginLeft: { to: -390, unit:'px'}}, 0.8);
        YAHOO.util.Event.addListener(SM.Menu.rightScroll, "mouseover", SM.Menu.tabScrollRight, SM.Menu, true);
        YAHOO.util.Event.addListener(SM.Menu.leftScroll, "mouseover", SM.Menu.tabScrollLeft, SM.Menu, true);
    }
});
YAHOO.util.Event.onDOMReady(SM.Menu.mealCollapse.init, null, SM.Menu.mealCollapse);
