package it.mice.voila.runtime.springmvc.taglibs;

import it.mice.voila.runtime.springmvc.bean.PathStackElement;
import it.mice.voila.runtime.springmvc.util.SpringMvcUtil;
import it.mice.voila.runtime.web.bean.MenuDefinition;
import it.mice.voila.runtime.web.bean.MenuItemDefinition;
import it.mice.voila.runtime.web.menu.MenuDefinitionFactory;
import it.mice.voila.runtime.web.menu.MenuFilterManager;

import java.util.Enumeration;
import java.util.StringTokenizer;

import javax.servlet.jsp.JspException;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.tags.form.AbstractFormTag;
import org.springframework.web.servlet.tags.form.TagWriter;

public class MenuTag extends AbstractFormTag {
    /**
	 * serial version UID
	 */
	private static final long serialVersionUID = 1365950626443575870L;

	private static final Log logger = LogFactory.getLog(MenuTag.class);

    private static final String MENU_TYPE_CSS_MAIN = "css_main";
    private static final String MENU_TYPE_EXTJS = "extjs";
    private static final String MENU_TYPE_CSS_LEFT = "css_left";
    private static final String MENU_TYPE_BS_LEFT = "bs_left";
    private static final String MENU_TYPE_BREADCRUMB = "breadCrumb";
    private static final String MENU_TYPE_BS_BREADCRUMB = "bs_breadCrumb";
    private static final String MENU_TYPE_MENUPAGE = "menuPage";
    public static final String SAVED_MENU_DEFINITION = MenuTag.class.getName() + ".";

    private String menuId;
    private String menuName;
    private String menuType = MENU_TYPE_CSS_MAIN;
    private String menuFilterManagerBeanName = "menuFilterManager";
    private int minLevel = 0;
    private int maxLevel = 9;
    private boolean cacheMenu = true;
    private boolean appendStack = false;
    private String currentHeaderKey = null;

    private String hrefCurrentElement = "true";
    private String renderLastBreadcrumbSymbol = "true";

    private TagWriter tagWriter;
    
	public boolean isCacheMenu() {
		return cacheMenu;
	}

	/**
	 * @return the appendStack
	 */
	public boolean isAppendStack() {
		return appendStack;
	}

	/**
	 * @param appendStack the appendStack to set
	 */
	public void setAppendStack(boolean appendStack) {
		this.appendStack = appendStack;
	}

	public String getCurrentHeaderKey() {
		return currentHeaderKey;
	}

	public void setCurrentHeaderKey(String currentHeaderKey) {
		this.currentHeaderKey = currentHeaderKey;
	}

	/**
	 * @param menuId the menuId to set
	 */
	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public void setCacheMenu(boolean cacheMenu) {
		this.cacheMenu = cacheMenu;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public String getMenuType() {
		return menuType;
	}

	public void setMenuType(String menuType) {
		this.menuType = menuType;
	}

	public String getMenuFilterManagerBeanName() {
		return menuFilterManagerBeanName;
	}

	public void setMenuFilterManagerBeanName(String menuFilterManagerBeanName) {
		this.menuFilterManagerBeanName = menuFilterManagerBeanName;
	}

	public int getMinLevel() {
		return minLevel;
	}

	public void setMinLevel(int minLevel) {
		this.minLevel = minLevel;
	}

	public int getMaxLevel() {
		return maxLevel;
	}

	public void setMaxLevel(int maxLevel) {
		this.maxLevel = maxLevel;
	}

	public String getHrefCurrentElement() {
		return hrefCurrentElement;
	}

	public void setHrefCurrentElement(String hrefCurrentElement) {
		this.hrefCurrentElement = hrefCurrentElement;
	}

	public String getRenderLastBreadcrumbSymbol() {
		return renderLastBreadcrumbSymbol;
	}

	public void setRenderLastBreadcrumbSymbol(String renderLastBreadcrumbSymbol) {
		this.renderLastBreadcrumbSymbol = renderLastBreadcrumbSymbol;
	}

	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		this.tagWriter = tagWriter;
		
        if (logger.isDebugEnabled())
            logger.debug("Start building menu type: " + menuType + "...");

        MenuFilterManager menuFilterManager =
        	(MenuFilterManager)WebApplicationContextUtils
        	.getRequiredWebApplicationContext(
        			this.pageContext.getServletContext())
        			.getBean(getMenuFilterManagerBeanName());
        
        MenuDefinition filteredMenuDefinition;
        MenuDefinition menuDefinition = menuFilterManager.getMenu(getMenuName());
        
        filteredMenuDefinition = getMenuDefinitionFromCache();
        if (filteredMenuDefinition == null) {
            filteredMenuDefinition = menuFilterManager.getFilteredMenu(getMenuName());
            setMenuDefinitionToCache(filteredMenuDefinition);
        }

        if (menuType.equals(MENU_TYPE_CSS_MAIN)) {
            drawCssMenuDefinitionTree(filteredMenuDefinition, 0, getSelectedLevelTree(), true);
        } else if (menuType.equals(MENU_TYPE_EXTJS)) {
        } else if (menuType.equals(MENU_TYPE_CSS_LEFT)) {
            drawCssMenuDefinitionTree(filteredMenuDefinition, 0, getSelectedLevelTree(), true);
        } else if (menuType.equals(MENU_TYPE_BS_LEFT)) {
            drawBsLeftMenuDefinitionTree(filteredMenuDefinition, 0, getSelectedLevelTree(), true);
        } else if (menuType.equals(MENU_TYPE_BREADCRUMB)) {
            drawBreadCrumb(menuDefinition, 0, getSelectedLevelTree(), true);
            if (appendStack) {
                drawPathStack();
            }
        } else if (menuType.equals(MENU_TYPE_BS_BREADCRUMB)) {
            drawBsBreadCrumb(menuDefinition, 0, getSelectedLevelTree(), true);
            if (appendStack) {
                drawBsPathStack();
            }
        } else if (menuType.equals(MENU_TYPE_MENUPAGE)) {
            drawMainMenuPage(filteredMenuDefinition);
        } else {
            JspException e =
                    new JspException("Invalid menuType value: " + menuType + ". Must be one of the following values: dhtml, css_main, css_left.");
            throw e;
        }
        return EVAL_BODY_INCLUDE;
	}

	private MenuDefinition getMenuDefinitionFromCache() {
		MenuDefinition result = null; 
		if (isCacheMenu()) {
			result = (MenuDefinition)pageContext.getSession().getAttribute(SAVED_MENU_DEFINITION + "_" + getMenuName());
//		} else {
//			result = (MenuDefinition)pageContext.getRequest().getAttribute(SAVED_MENU_DEFINITION + "_" + getMenuName());
		}
		return result;
	}

    private void setMenuDefinitionToCache(MenuDefinition filteredMenuDefinition) {
		if (isCacheMenu()) {
			pageContext.getSession().setAttribute(SAVED_MENU_DEFINITION + "_" + getMenuName(), filteredMenuDefinition);
//		} else {
//			pageContext.getRequest().setAttribute(SAVED_MENU_DEFINITION + "_" + getMenuName(), filteredMenuDefinition);
		}
	}

	private void drawMainMenuPage(MenuDefinition filteredMenuDefinition) throws JspException {
		MenuItemDefinition foundMenuItemDefinition = getMenuItemDefinitionById(filteredMenuDefinition, getMenuId());
		if (foundMenuItemDefinition != null) {
			tagWriter.startTag("h3");
			if (foundMenuItemDefinition.getTitle() != null || foundMenuItemDefinition.getTitleKey() != null) {
				tagWriter.appendValue(TagUtils.getMessage(getRequestContext(), foundMenuItemDefinition.getTitleKey(), foundMenuItemDefinition.getTitle()));
			} else {
				tagWriter.appendValue(TagUtils.getMessage(getRequestContext(), foundMenuItemDefinition.getContentKey(), foundMenuItemDefinition.getContent()));
			}
			tagWriter.endTag();
			tagWriter.startTag("p");
			tagWriter.writeAttribute("class", "menuLevel0");
			addDescriptionToParagraph(foundMenuItemDefinition);
			tagWriter.endTag();
			drawMenuPage(foundMenuItemDefinition.getMenuToShow(), 0);
		} else {
			drawMenuPage(filteredMenuDefinition, 0);
		}
	}

    private void drawMenuPage(MenuDefinition menuDefinition, int levelDeep) throws JspException {
        if (levelDeep > maxLevel) {
            return;
        }
        tagWriter.startTag("ul");
        for (int i = 0; i < menuDefinition.getMenuItems().length; i++) {
            MenuItemDefinition menuItemDefinition = menuDefinition.getMenuItems()[i];
            String caption = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent());
            tagWriter.startTag("li");
            StringBuffer classContent = new StringBuffer("menuLevel" + (levelDeep + 1));
            if (menuItemDefinition.isFlagDisabled()) {
            	classContent.append(" disabled");
            }
            if (menuItemDefinition.isFlagRestricted()) {
            	classContent.append(" restricted");
            }
            tagWriter.writeAttribute("class", classContent.toString());
            
            if (menuItemDefinition.isFlagDisabled()) {
                tagWriter.startTag("span");
            } else {
                tagWriter.startTag("a");
            	tagWriter.writeAttribute("href", prepareLink(menuItemDefinition));
                addTargetToAnchor(menuItemDefinition);
            }
            addTitleToAnchor(menuItemDefinition);
            addOnClickToAnchor(menuItemDefinition);
            tagWriter.appendValue(caption);
            tagWriter.endTag();

            if (levelDeep < maxLevel) {
            	tagWriter.startTag("p");
            	tagWriter.writeAttribute("class", "menuLevel" + (levelDeep + 1));
                addDescriptionToParagraph(menuItemDefinition);
                tagWriter.endTag();
            }
            if (menuItemDefinition.getMenuToShow() != null) {
                drawMenuPage(menuItemDefinition.getMenuToShow(), (levelDeep + 1));
            }
            tagWriter.endTag();
        }
        tagWriter.endTag();
    }

    private void addDescriptionToParagraph(MenuItemDefinition menuItemDefinition) throws JspException {
        String description = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getDescriptionKey(), menuItemDefinition.getDescription());
        if (description != null) {
        	tagWriter.appendValue(description);
        }
    }

    private MenuItemDefinition getMenuItemDefinitionById(MenuDefinition menuDefinition, String menuId) {
        MenuDefinition currentMenuDefinition = menuDefinition;
        MenuItemDefinition selectedMenuItemDefinition = null;
        for (int i=0; i<currentMenuDefinition.getMenuItems().length; i++) {
        	if (currentMenuDefinition.getMenuItems()[i].getItemId().equals(menuId)) {
        		return currentMenuDefinition.getMenuItems()[i];
        	} else if (currentMenuDefinition.getMenuItems()[i].getMenuToShow() != null) {
        		selectedMenuItemDefinition = getMenuItemDefinitionById(
        					currentMenuDefinition.getMenuItems()[i].getMenuToShow(), menuId);
        		if (selectedMenuItemDefinition != null) {
        			return selectedMenuItemDefinition;
        		}
        	}
        }
        return selectedMenuItemDefinition;
    }

	private void drawCssMenuDefinitionTree(MenuDefinition filteredMenuDefinition,
    									   int levelDeep,
    									   int[] selectedLevelTree,
    									   boolean isParentSelected)
    							throws JspException
    {
        if (levelDeep > maxLevel) {
            return;
        }
        tagWriter.startTag("ul");
        if (levelDeep == 0) {
        	tagWriter.writeAttribute("id", menuType);
        }
        for (int i = 0; i < filteredMenuDefinition.getMenuItems().length; i++) {
            boolean isElementSelected = false;
            MenuItemDefinition menuItemDefinition = filteredMenuDefinition.getMenuItems()[i];
            int[] currentMenuItemId = convertToLevelTree(menuItemDefinition.getItemId());
            if (selectedLevelTree[levelDeep] == currentMenuItemId[levelDeep] && isParentSelected) {
            	if ((menuType.equals(MENU_TYPE_CSS_LEFT)) && (menuItemDefinition.getMenuToShow() == null)) {
            		// per il left menu se il menu item e' una foglia, va indicato come selezionato 
            		// solo se la voce risulta effettivamente selezionata
            		if (menuItemDefinition.getItemId().equals(getMenuId())) {
            			isElementSelected = true;
            		}
            	} else {
            		isElementSelected = true;
            	}
            }
            if (levelDeep >= minLevel || (levelDeep < minLevel && isElementSelected)) {
                String caption = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent());
                StringBuffer classContent = new StringBuffer();
                if (isElementSelected) {
                	classContent.append(" selected");
                }
                if (menuItemDefinition.isFlagDisabled()) {
                	classContent.append(" disabled");
                }
                if (menuItemDefinition.isFlagRestricted()) {
                	classContent.append(" restricted");
                }
                tagWriter.startTag("li");
                if (classContent.length() > 0) {
                	tagWriter.writeAttribute("class", classContent.toString().trim());
                }
                if (menuItemDefinition.isFlagDisabled()) {
                    tagWriter.startTag("span");
                } else {
                    tagWriter.startTag("a");
                	tagWriter.writeAttribute("href", prepareLink(menuItemDefinition));
                    addTargetToAnchor(menuItemDefinition);
                }
                addTitleToAnchor(menuItemDefinition);
                addOnClickToAnchor(menuItemDefinition);
                tagWriter.appendValue(caption);
                tagWriter.endTag();
                if (menuItemDefinition.getMenuToShow() != null) {
                    drawCssMenuDefinitionTree(menuItemDefinition.getMenuToShow(),
                    						  (levelDeep + 1),
                    						  selectedLevelTree,
                    						  isElementSelected);
                }
                tagWriter.endTag();
            }
        }
        tagWriter.endTag();
    }

	private void drawBsLeftMenuDefinitionTree(MenuDefinition filteredMenuDefinition,
    									   int levelDeep,
    									   int[] selectedLevelTree,
    									   boolean isParentSelected)
    							throws JspException
    {
        if (levelDeep > maxLevel) {
            return;
        }
        for (int i = 0; i < filteredMenuDefinition.getMenuItems().length; i++) {
            boolean isElementSelected = false;
            MenuItemDefinition menuItemDefinition = filteredMenuDefinition.getMenuItems()[i];
            int[] currentMenuItemId = convertToLevelTree(menuItemDefinition.getItemId());
            if (selectedLevelTree[levelDeep] == currentMenuItemId[levelDeep] && isParentSelected) {
            	if (menuItemDefinition.getMenuToShow() == null) {
            		// per il left menu se il menu item e' una foglia, va indicato come selezionato 
            		// solo se la voce risulta effettivamente selezionata
            		if (menuItemDefinition.getItemId().equals(getMenuId())) {
            			isElementSelected = true;
            		}
            	} else {
            		isElementSelected = true;
            	}
            }
            if (levelDeep >= minLevel || (levelDeep < minLevel && isElementSelected)) {
                String caption = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent());
                StringBuffer classContent = new StringBuffer();
                if (menuItemDefinition.getMenuToShow() != null) {
                	classContent.append("treeview");
                }
                if (isElementSelected) {
                	classContent.append(" active");
                }
                if (menuItemDefinition.isFlagDisabled()) {
                	classContent.append(" disabled");
                }
                if (menuItemDefinition.isFlagRestricted()) {
                	classContent.append(" restricted");
                }
                tagWriter.startTag("li");
                if (classContent.length() > 0) {
                	tagWriter.writeAttribute("class", classContent.toString().trim());
                }
                if (menuItemDefinition.isFlagDisabled()) {
                    tagWriter.startTag("span");
                } else {
                    tagWriter.startTag("a");
                	tagWriter.writeAttribute("href", prepareLink(menuItemDefinition));
                    if (menuItemDefinition.getMenuToShow() != null) {
                    	tagWriter.writeAttribute("class", "noWaitPopup");
                    }
                    addTargetToAnchor(menuItemDefinition);
                }
                addTitleToAnchor(menuItemDefinition);
                addOnClickToAnchor(menuItemDefinition);

                tagWriter.startTag("i");
            	tagWriter.writeAttribute("class", "fa " + (menuItemDefinition.getMenuToShow() == null ? "fa-angle-double-right" : "fa-circle-o"));
                tagWriter.endTag(true); // </i>
                tagWriter.startTag("span");
                
            	tagWriter.appendValue(caption);
                
            	tagWriter.endTag(); // </span>

                if (menuItemDefinition.getMenuToShow() != null) {
                	//<i class="fa fa-angle-left pull-right"></i>
                    tagWriter.startTag("i");
                	tagWriter.writeAttribute("class", "fa fa-angle-left pull-right");
                    tagWriter.endTag(true); // </i>

                    tagWriter.endTag(); // </span> or </a>

                    //<ul class="treeview-menu">
                    tagWriter.startTag("ul");
                	tagWriter.writeAttribute("class", "treeview-menu");
                    drawBsLeftMenuDefinitionTree(menuItemDefinition.getMenuToShow(),
                    						  (levelDeep + 1),
                    						  selectedLevelTree,
                    						  isElementSelected);
                    tagWriter.endTag(); // </ul>
                } else {
                    tagWriter.endTag(); // </span> or </a>
                }
                tagWriter.endTag(); // </li>
            }
        }
    }

    private void drawBreadCrumb(MenuDefinition menuDefinition, int levelDeep, int[] selectedLevelTree, boolean isParentSelected) throws JspException {
        if (levelDeep > maxLevel) {
            return;
        }
        for (int i = 0; i < menuDefinition.getMenuItems().length; i++) {
            boolean isElementSelected;
            if (selectedLevelTree[levelDeep] == (i + 1) && isParentSelected) {
                MenuItemDefinition menuItemDefinition = menuDefinition.getMenuItems()[i];
                String caption = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent());
                isElementSelected = true;
                tagWriter.startTag("a");
                tagWriter.writeAttribute("href", prepareLink(menuItemDefinition));
                addTargetToAnchor(menuItemDefinition);
                addTitleToAnchor(menuItemDefinition);
                addOnClickToAnchor(menuItemDefinition);
                if (menuItemDefinition.getMenuToShow() == null) {
                    tagWriter.writeAttribute("class", "selected");
                }
                tagWriter.appendValue(caption);
                tagWriter.endTag();
                if (menuItemDefinition.getMenuToShow() != null || evaluateBoolean("renderLastBreadcrumbSymbol", getRenderLastBreadcrumbSymbol())) {
                	TagUtils.safeWrite(pageContext, "&nbsp;&gt;&nbsp;");
                }
                if (menuItemDefinition.getMenuToShow() != null) {
                    drawBreadCrumb(menuItemDefinition.getMenuToShow(), (levelDeep + 1), selectedLevelTree, isElementSelected);
                }
            }
        }
    }

    private void drawPathStack() throws JspException {
    	Enumeration<PathStackElement> ePathStack = SpringMvcUtil.getStack().elements();
    	while (ePathStack.hasMoreElements()) {
    		PathStackElement aPathStackElement = ePathStack.nextElement();
			
            String caption = TagUtils.getMessage(getRequestContext(), aPathStackElement.getHeaderKey(), aPathStackElement.getHeaderKey());
            tagWriter.startTag("a");
            tagWriter.writeAttribute("href", SpringMvcUtil.getServerBaseUrl() + aPathStackElement.getPath());
            tagWriter.appendValue(caption);
            tagWriter.endTag();
           	TagUtils.safeWrite(pageContext, "&nbsp;&gt;&nbsp;");
        }
    }


    private void drawBsBreadCrumb(MenuDefinition menuDefinition, int levelDeep, int[] selectedLevelTree, boolean isParentSelected) throws JspException {
        if (levelDeep > maxLevel) {
            return;
        }
        for (int i = 0; i < menuDefinition.getMenuItems().length; i++) {
            if (selectedLevelTree[levelDeep] == (i + 1) && isParentSelected) {
                MenuItemDefinition menuItemDefinition = menuDefinition.getMenuItems()[i];
                String caption = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent());
                tagWriter.startTag("li");
                tagWriter.startTag("a");
                tagWriter.writeAttribute("href", prepareLink(menuItemDefinition));
                addTargetToAnchor(menuItemDefinition);
                addTitleToAnchor(menuItemDefinition);
                addOnClickToAnchor(menuItemDefinition);
                tagWriter.appendValue(caption);
                tagWriter.endTag();
                tagWriter.endTag();
            }
        }
    }

    private void drawBsPathStack() throws JspException {
    	Enumeration<PathStackElement> ePathStack = SpringMvcUtil.getStack().elements();
    	while (ePathStack.hasMoreElements()) {
    		PathStackElement aPathStackElement = ePathStack.nextElement();
			
    		if (getCurrentHeaderKey() != null && !getCurrentHeaderKey().equals(aPathStackElement.getHeaderKey())) {
                String caption = TagUtils.getMessage(getRequestContext(), aPathStackElement.getHeaderKey(), aPathStackElement.getHeaderKey());
                tagWriter.startTag("li");
                tagWriter.startTag("a");
                tagWriter.writeAttribute("href", aPathStackElement.getPath());
                tagWriter.appendValue(caption);
                tagWriter.endTag();
                tagWriter.endTag();
    		}
        }
    }

    private String prepareLink(MenuItemDefinition menuItemDefinition) {
        String baseUrl = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getBaseUrlKey(), menuItemDefinition.getBaseUrl());
    	String link = menuItemDefinition.getLink();
    	if (!StringUtils.isEmpty(baseUrl)) {
    		return baseUrl + link;
    	}
    	if (!StringUtils.isEmpty(link) && link.startsWith("http")) {
    		return link;
    	}
    	return getRequestContext().getContextUrl(link);
    }
    
    private int[] getSelectedLevelTree() {
        String menuId = getMenuId();
        int[] selectedLevelTree = convertToLevelTree(menuId);
        return selectedLevelTree;
    }

    private String getMenuId() {
    	if (this.menuId != null) {
    		return this.menuId;
    	}
        String menuId = pageContext.getRequest().getParameter(MenuDefinitionFactory.SELECTED_MENU_ID);
        if (menuId == null) {
            menuId = (String) pageContext.getSession().getAttribute(MenuDefinitionFactory.SELECTED_MENU_ID);
        } else {
            pageContext.getSession().setAttribute(MenuDefinitionFactory.SELECTED_MENU_ID, menuId);
        }
        return menuId;
    }

    private int[] convertToLevelTree(String menuId) {
        int[] returnLevelTree = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
        if (menuId != null) {
            int counter = 0;
            StringTokenizer st = new StringTokenizer(menuId, ".");
            while (st.hasMoreTokens()) {
                returnLevelTree[counter] = Integer.parseInt(st.nextToken());
                counter++;
            }
        }
        return returnLevelTree;
    }

	private void addTargetToAnchor(MenuItemDefinition menuItemDefinition) throws JspException {
		if (menuItemDefinition.getTarget() != null && menuItemDefinition.getTarget().length() > 0) {
           	tagWriter.writeAttribute("target", menuItemDefinition.getTarget());
		}
	}

    private void addTitleToAnchor(MenuItemDefinition menuItemDefinition) throws JspException {
        String title = TagUtils.getMessage(getRequestContext(), menuItemDefinition.getTitleKey(), menuItemDefinition.getTitle());
        if (title != null) {
           	tagWriter.writeAttribute("title", title);
        }
    }

	private void addOnClickToAnchor(MenuItemDefinition menuItemDefinition) throws JspException {
		if (menuItemDefinition.getOnClick() != null && menuItemDefinition.getOnClick().length() > 0) {
			StringBuffer onclickValue = new StringBuffer();
			onclickValue.append(" onclick=\"return ");
			onclickValue.append(menuItemDefinition.getOnClick()).append("(");
			onclickValue.append("'").append(menuItemDefinition.getBeanName()).append("'");
			onclickValue.append(", '").append(menuItemDefinition.getItemId()).append("'");
			onclickValue.append(", '").append(TagUtils.getMessage(getRequestContext(), menuItemDefinition.getContentKey(), menuItemDefinition.getContent())).append("'");
			onclickValue.append(", '").append(menuItemDefinition.getLink()).append("'");
			onclickValue.append(", ").append(menuItemDefinition.isFlagDisabled());
			onclickValue.append(", ").append(menuItemDefinition.isFlagRestricted());
			onclickValue.append(");");
			onclickValue.append("\"");
			tagWriter.writeAttribute("onclick", onclickValue.toString());
		}
	}

	/* (non-Javadoc)
	 * @see javax.servlet.jsp.tagext.TagSupport#release()
	 */
	@Override
	public void release() {
		this.menuId = null;
		this.currentHeaderKey = null;
		super.release();
	}

	
}
