var menuAnimated = 
{
 init: function ()
 {
 menuAnimated.frameRate = 35;
 menuAnimated.duration = 0.5;
 var menuMain = document.getElementById("menuMain");
 var menuItems = menuMain.getElementsByTagName("ul");
 
     for (var i = 0; i < menuItems.length; i++)
    {
      var folds = menuItems[i].childNodes;
       //document.write(folds[j].nextSibling);
    // var foldsIn = menuItems[i].childNodes;
      for (var j = 0; j < folds.length; j++)
      {
      
        if (folds[j].nodeType == 1)
        {
     

          var accordionContent = document.createElement("div");
          accordionContent.className = "accordionContent";
          
          for (var k = 0; k < folds[j].childNodes.length; k++)
          {
            if (folds[j].childNodes[k].nodeName.toLowerCase() != "a")
            {
              accordionContent.appendChild(folds[j].childNodes[k]);
              k--;
            }
          }
          
          folds[j].appendChild(accordionContent);
          folds[j]._accordionContent = accordionContent;        
        
        
      
          menuAnimated.collapse(folds[j]);
          var foldLinks = folds[j].getElementsByTagName("a");
          var foldTitleLink = foldLinks[0];
          Core.addEventListener(foldTitleLink, "click", menuAnimated.clickListener);
          
          for (var k = 1; k < foldLinks.length; k++)
          {
            Core.addEventListener(foldLinks[k], "focus", menuAnimated.focusListener);
          }
        }
      }
      
      if (location.hash.length > 1)
      {
        var activeFold = document.getElementById(location.hash.substring(1));
        if (activeFold && activeFold.parentNode == menuItems[i])  // asi misto menuItem dát ul
        {
          menuAnimated.expand(activeFold);
        }
      }
    }
	if(document.getElementById('active-item')&&document.getElementById('active-item').childNodes[1].firstChild)
		document.getElementById('active-item').childNodes[1].firstChild.style.display = "block";
 },

 collapse: function(fold)
  {

    var content = fold._accordionContent;
    
    if(!isNaN(parseInt(content.style.height, 10))){
		content._height = parseInt(content.style.height, 10);
		content._increment = content._height / (menuAnimated.frameRate * menuAnimated.duration);
	} else
		Core.addClass(fold, "hidden");
		
    if (Core.hasClass(fold, "expanded"))
    {
      clearTimeout(content._timer);
      menuAnimated.collapseAnimate(content);
    }
    else
    {
      Core.addClass(fold, "collapsed");
    }
  },
  
  collapseAnimate: function(content)
  {
    var newHeight = content._height - content._increment;
    
    if (newHeight < 0)
    {
      newHeight = 0;
      Core.removeClass(content.parentNode, "expanded");
      Core.addClass(content.parentNode, "collapsed");
    }
    else
    {    
      content._timer = setTimeout(function()
        {
          menuAnimated.collapseAnimate(content);
        }, 1000 / menuAnimated.frameRate);
    }

    if(!isNaN(newHeight)){
		content._height = newHeight;
		content.style.height = Math.round(newHeight) + "px";    
    }
//    
  },

  collapseAll: function(menu)
  {
    var folds = menu.childNodes;
    for (var i = 0; i < folds.length; i++)
    {
      if (folds[i].nodeType == 1)
      {
        menuAnimated.collapse(folds[i]);
      }
    }
  },

  expand: function(fold)
  {

 var content = fold._accordionContent;

    menuAnimated.collapseAll(fold.parentNode);

   
    
    if (!Core.hasClass(fold, "expanded"))
    {
      content.style.height = "0";
      content._height = 0;
      Core.removeClass(fold, "collapsed");
      Core.addClass(fold, "expanded");
      content._increment = content.scrollHeight / (menuAnimated.frameRate * menuAnimated.duration);
      menuAnimated.expandAnimate(content);
    }
  },
  
  expandAnimate: function(content)
  {
    var newHeight = content._height + content._increment;
    
    if (newHeight > content.scrollHeight)
    {
      newHeight = content.scrollHeight;
    }
    else
    {
      content._timer = setTimeout(function()
          {
            menuAnimated.expandAnimate(content);
          }, 1000 / menuAnimated.frameRate);
    }
    
    content._height = newHeight;
    content.style.height = Math.round(newHeight) + "px";
    content.scrollTop = 0;

    

  },
  
  redirectPage: function(url){
	window.location = url;
  },
  
  clickListener: function(event)
  {
    var fold = this.parentNode;
	
	
    if (Core.hasClass(fold, "collapsed"))
    {
      menuAnimated.expand(fold);
	  
    }
    else
    {
      menuAnimated.collapse(fold);
	  
    }
    Core.preventDefault(event);
	var url = this.href;
	if(document.getElementById('active-item')){
		if(document.getElementById('active-item')!=fold&&fold.parentNode.parentNode.parentNode!=document.getElementById('active-item')){
			if(document.getElementById('active-item').childNodes[1].firstChild){
				child = document.getElementById('active-item').childNodes[1].firstChild;
				Effect.BlindUp(child ,{ duration: 0.8 });
			}
		}
	}
	
	setTimeout(function()
          {
            window.location = url;
          }, 1000);
  },
  
  focusListener: function(event)
  {
    var element = this;
    while (element.parentNode)
    {
      if (element.parentNode.className == "menu")
      {
        menuAnimated.expand(element);
        return;
      }
      element = element.parentNode;
    }
  }
  
  

};
Core.start(menuAnimated);
