window.addEvent('domready', function() {

  $$('ul.sub li').each(function(li) {
    
    var sub = li.getChildren('ul');

    if(sub[0]) {
      var a = li.getChildren('a')[0];
      a.addEvent('click', function(e) {
         e.stop();
         sub[0].toggleClass('active');
         li.toggleClass('open');
      });
      if(li.hasClass('active')) li.addClass('open');
    }      
  });
  
});

var active = false;  
var submenu = [];    
window.addEvent('domready', function() {

  
  var step = 40;
  var speed = 0.0001;
  var periodical;
  var scrolls = [];  
  
  
  var actives = $$('#menu a.active').length;
  var submenus = new Array();
  var i = 0;
  $$('#menu a').each(function(a) {
 
    
    
    i++;
    
    submenu[i] = new Fx.Slide($(a.get('rel')),{

      onComplete: function() {

        if(this.open == false) {
          
          //a.removeClass('active');           
           
        }
      }   
      
    });
    
    submenu[i].hide();
    submenus.include(submenu[i]);


    if(a.hasClass('active') || $$('#'+a.get('rel')+' li.active').length) {
      a.addClass('active');
      active = submenus.length - 1;
    }
    
    
    a.addEvent('click', function(e) {
      e.stop();

      if(a.hasClass('normal')) {
        window.location = a.get('href');
        return true;
      }
        //if(!home && a.hasClass('active')) return true;


        if(actives && a.hasClass('active')) return true;
      
      
        $$('#menu a').each(function(foo) {
        
          if(foo != a)
            foo.removeClass('active');
        
        });
        
        a.toggleClass('active');
        
        var id = a.get('id').toInt() - 2;
        
        submenus[id].toggle();
        
        submenus.each(function(sub) {
          sub.hide();
        })
     

    });
    
 
   if(active !== false) submenus[active].show();  
    

    /* scroller
    var scroller = $('scroller'+a.get('id').toInt());

      if(scroller) {
        scroll[a.get('id').toInt()] = new Scroller(scroller, {area: 20, velocity: 0.1}).start();
        $('up'+a.get('id').toInt()).addEvent('mouseover', scroll[a.get('id').toInt()].start.bind(scroller));
        $('up'+a.get('id').toInt()).addEvent('mouseout', scroll[a.get('id').toInt()].start.bind(scroller));
      }
    */

    var id = a.get('id').toInt();
    var scroller = $('scroller'+id);
    if(scroller) 
    {
       scrolls[id] = new Fx.Scroll(scroller);
       var active_child = $$('#scroller'+id+' li.active');
      
       if(active_child.length > 0) scrolls[id].toElement(active_child[active_child.length - 1]);
       else scrolls[id].toTop();
       
       $('up'+id).addEvent('mouseenter', function() {up(id); periodical = up.periodical(speed, this, id)});
       $('up'+id).addEvent('mouseout', function() { $clear(periodical); });

       $('down'+id).addEvent('mouseenter', function() {down(id); periodical = down.periodical(speed, this, id)});
       $('down'+id).addEvent('mouseout', function() { $clear(periodical); });
                                                    
    }
      
      
      
  });  


  var down = function(id) {

    scrolls[id].start(0, $('scroller'+id).getScroll().y + step);
  }

  var up = function(id) {

    scrolls[id].start(0, $('scroller'+id).getScroll().y - step);
  }    



  // scrollspy
  /*var ss = new ScrollSpy({
    min: 152,
    onEnter: function(position,state,enters) {
       if($('sidebar')) $('sidebar').addClass('fixed');
    },
    onLeave: function(position,state,leaves) {
       if($('sidebar')) $('sidebar').removeClass('fixed');
    },
    container: window
  }); */                          
                     
});   

