module ParserCommon
 
  def menu_level(page, current, menu, edit = false)
    res = ''
    if current <= menu.levels
      if current != 0
        res << "<div class='rc_dm'>"
        item = rand(100000)
        res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl' rel='#{item}'>#{page.name}<span class='down'></span></a>"
        if page.children.size > 0
          res << "<div id='#{item}' class='dm_list #{menu.values['class_#{current}']}'>"
          res << "<ul class='ini_list'>"
          i = nil
          i = 1 if menu.values["li_incremental_#{current}"]
          page.children.each do |child|
            res << menu_li(child, current, menu, i, edit)
            i += 1 if i
          end
          res << "</ul>"
        res << "</div>"
        end
        res << "</div>"
      else
        res << '<ul>'
        res << "<li>" + "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='dm_ctrl'>#{page.name}</a>" + "</li>"
        page.children.each do |child|
          res << "<li>" + menu_level(child, current + 1, menu, edit) + "</li>"
        end
        res << '</ul>'
      end
    end
  end
  
  def menu_li(page, current, menu, i, edit)
    res = "<li class="
    res << menu.values["li_class_#{current}"]
    res << "_#{i}" if i
    res << ">"
    if page.children.size > 0 
      res << menu_level(page, current + 1, menu, edit)
    else
      res << "<a href='#{edit ? admin_page_path(page.id) : page.full_name}' class='nav dm_ctrl'>#{page.name}</a>"
    end
    res << "</li>"
  end

end
