var mainMenuItems = [
    {
      "itemName": "Новости",
      "itemURI": "/news/",
      "subMenu": 
        [
          {"itemName": "Главные новости дня",
            "itemURI": "/"},
          {"itemName": "Новости недели",
            "itemURI": "/news/weekly/"},
          {"itemName": "Все новости",
            "itemURI": "/news/all/"}
        ]
    },
                
    {
      "itemName": "Каталог",
      "itemURI": "/catalog/",
      "subMenu": 
        [
          {"itemName": "Добавить организацию",
            "itemURI": "/catalog/addorg/"}
        ]
    },
                              
    {
      "itemName": "Фотобанк",
      "itemURI": "/photobank/"
    },
    
    {
      "itemName": "Энциклопедия",
      "itemURI": "/encyclopedia/"
    },
               
    {
      "itemName": "Форум",
      "itemURI": "http://forum.jnet.ru/"
    }];



function genMainMenu()
{
  var nestObj = window.document.getElementById("mMenu");
  var re;

  for (var i = 0; i < mainMenuItems.length; i++)
    {
      if (i == 0)
        {
          if (window.location.pathname == "/")
            {
              var span = window.document.createElement("span");
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              span.appendChild(textNode);
              nestObj.appendChild(span);
            }
          else if ((re = new RegExp("^" + mainMenuItems[i].itemURI)) 
              && re.test(window.location.pathname))
            {
              var anchor = window.document.createElement("a");
              anchor.href = "/";
              anchor.className="act";
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              anchor.appendChild(textNode);
              nestObj.appendChild(anchor);
            }
          else
            {
              var anchor = window.document.createElement("a");
              anchor.href = "/";
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              anchor.appendChild(textNode);
              nestObj.appendChild(anchor);
            }
        }
      else
        {
          if (window.location.pathname == mainMenuItems[i].itemURI)
            {
              var span = window.document.createElement("span");
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              span.appendChild(textNode);
              nestObj.appendChild(span);
            }
          else if ((re = new RegExp("^" + mainMenuItems[i].itemURI)) 
              && re.test(window.location.pathname))
            {
              var anchor = window.document.createElement("a");
              anchor.href = mainMenuItems[i].itemURI;
              anchor.className="act";
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              anchor.appendChild(textNode);
              nestObj.appendChild(anchor);
            }
          else
            {
              var anchor = window.document.createElement("a");
              anchor.href = mainMenuItems[i].itemURI;
              var textNode = window.document.createTextNode(mainMenuItems[i].itemName);
              anchor.appendChild(textNode);
              nestObj.appendChild(anchor);
            }
        }
    }
}



function genSubMenu()
{
  var nestObj = window.document.getElementById("sMenu");
  var re;
  
  if ((window.location.pathname == "/") 
      || ((re = new RegExp("^" + mainMenuItems[0].itemURI)) 
        && re.test(window.location.pathname)))
    {
      for (var i = 0; i < mainMenuItems[0].subMenu.length; i++)
        {
          if (window.location.pathname == mainMenuItems[0].subMenu[i].itemURI)
            {
              var span_item  = window.document.createElement("span");
              span_item.className = "sm-item";
              var span = window.document.createElement("span");
              var textNode = window.document.createTextNode(mainMenuItems[0].subMenu[i].itemName);
              span.appendChild(textNode);
              span_item.appendChild(span);
              nestObj.appendChild(span_item);
            }
          else if ((re = new RegExp("^" + mainMenuItems[0].subMenu[i].itemURI)) 
             && re.test(window.location.pathname) && (i > 0))
            {
              var span_item  = window.document.createElement("span");
              span_item.className = "sm-item";
              var anchor = window.document.createElement("a");
              anchor.href = mainMenuItems[0].subMenu[i].itemURI;
              anchor.className="act";
              var textNode = window.document.createTextNode(mainMenuItems[0].subMenu[i].itemName);
              anchor.appendChild(textNode);
              span_item.appendChild(anchor);
              nestObj.appendChild(span_item);
            }
          else
            {
              var span_item  = window.document.createElement("span");
              span_item.className = "sm-item";
              var anchor = window.document.createElement("a");
              anchor.href = mainMenuItems[0].subMenu[i].itemURI;
              var textNode = window.document.createTextNode(mainMenuItems[0].subMenu[i].itemName);
              anchor.appendChild(textNode);
              span_item.appendChild(anchor);
              nestObj.appendChild(span_item);
            }
        }
    } 
  else
    {
      for (var j = 0; j < mainMenuItems.length; j++)
        {
          if (mainMenuItems[j].subMenu 
                && (re = new RegExp("^" + mainMenuItems[j].itemURI)) 
                && re.test(window.location.pathname))
            {
              for (var i = 0; i < mainMenuItems[j].subMenu.length; i++)
                {
                  if (window.location.pathname == mainMenuItems[j].subMenu[i].itemURI)
                    {
                      var span_item  = window.document.createElement("span");
                      span_item.className = "sm-item";
                      var span = window.document.createElement("span");
                      var textNode = window.document.createTextNode(mainMenuItems[j].subMenu[i].itemName);
                      span.appendChild(textNode);
                      span_item.appendChild(span);
                      nestObj.appendChild(span_item);
                    }
                  else if ((re = new RegExp("^" + mainMenuItems[j].subMenu[i].itemURI)) 
                     && re.test(window.location.pathname) && i > 0)
                    {
                      var span_item  = window.document.createElement("span");
                      span_item.className = "sm-item";
                      var anchor = window.document.createElement("a");
                      anchor.href = mainMenuItems[j].subMenu[i].itemURI;
                      anchor.className="act";
                      var textNode = window.document.createTextNode(mainMenuItems[j].subMenu[i].itemName);
                      anchor.appendChild(textNode);
                      span_item.appendChild(anchor);
                      nestObj.appendChild(span_item);
                    }
                  else
                    {
                      var span_item  = window.document.createElement("span");
                      span_item.className = "sm-item";
                      var anchor = window.document.createElement("a");
                      anchor.href = mainMenuItems[j].subMenu[i].itemURI;
                      var textNode = window.document.createTextNode(mainMenuItems[j].subMenu[i].itemName);
                      anchor.appendChild(textNode);
                      span_item.appendChild(anchor);
                      nestObj.appendChild(span_item);
                    }
                }
            } 
        }
    }
}
