
$(document).ready( function() {

var busy = false;

$(".sidebar_menu>li:has(.sidebar_submenu)>a").click(function(){
  submenu = $(this).next('.sidebar_submenu');
  items = submenu.children('li:not(.scroll)');
  if(submenu.height()==0) {
    h = 0;
    for(i=0;i<items.length;i++) {
      h += $(items[i]).outerHeight();
    }
    submenu.children("li:has(.sidebar_submenu)").children('ul.sidebar_submenu').css({'height':0});
    if(h<300) {
      submenu.animate({'height':h,'margin-top':4});
    }
    else {
      submenu.prepend('<li class="scroll up"><a href="#" title="Poprzednie"><span>Poprzednie</span></a></li>');
      submenu.append('<li class="scroll down"><a href="#" title="Następne"><span>Następne</span></a></li>');
      
      submenu.find('.scroll.down a').click( function(){
        if( busy ) {
          return false;
        }
        items = $(this).parent('li').siblings('li:not(.scroll)');
        h = 0;
        for(i=0;i<items.length;i++) {
          h += $(items[i]).outerHeight();
        }
        offset = parseFloat($(items[0]).css('top').replace('px','')) - 0.666*$(this).parents('ul.sidebar_submenu').height();
        if(h+offset>0) {
          busy = true;
          items.animate({'top':offset},function(){ busy = false; });
        }

        return false;
      } );
      
      submenu.find('.scroll.up a').click( function(){
        if( busy ) {
          return false;
        }
        items = $(this).parent('li').siblings('li:not(.scroll)');

        offset = parseFloat($(items[0]).css('top').replace('px','')) + 0.666*$(this).parents('ul.sidebar_submenu').height();
        if(offset<0.5) {
          busy = true;
          items.animate({'top':(offset>0?0:offset)},function(){ busy = false; });
        }
        if(offset>0) {
          items.css({'top':0});
        }

        return false;
      } );
      
      submenu.animate({'height':300,'margin-top':4,'padding-top':16,'padding-bottom':16});
    }
    
  }
  else {
    submenu.animate({'height':0,'margin-top':0,'padding':0},function(){
      submenu.find('li.scroll').remove();
      submenu.children("li:has(.sidebar_submenu)").children('ul.sidebar_submenu').css({'height':'auto'});
    });

  }

  return false; });

$(".sidebar_menu>li>.sidebar_submenu>li:has(.sidebar_submenu)>a").click(function(){
  submenu = $(this).next('.sidebar_submenu');
  items = submenu.children('li:not(.scroll)');
  if(submenu.height()==0) {
    h = 0;
    for(i=0;i<items.length;i++) {
      h += $(items[i]).outerHeight();
    }
    submenu.animate({'height':h,'margin-top':5,'margin-bottom':5});
  }
  else {
    submenu.animate({'height':0,'margin-top':0,'margin-bottom':0});
  }
  return false; });

$('#toggle_profile_menu_btn').click(function(){
  if( $(this).hasClass('expand') ) {
    $(this).removeClass('expand');
    $('#toggle_profile_menu_btn').parent('li').removeClass('selected');    
    $.cookie('profile_menu_state','');
  }
  else {
    $(this).addClass('expand');
    $('#toggle_profile_menu_btn').parent('li').addClass('selected');
    $.cookie('profile_menu_state','expand');
  }
  return false;
});

$('.sidebar_menu .selected>a').click();

if($.cookie('profile_menu_state')) {
  $('#toggle_profile_menu_btn').parent('li').addClass('selected');
  $('#toggle_profile_menu_btn').addClass('expand');
  $('#toggle_profile_menu_btn').next('.sidebar_submenu').height('auto');
}

} );



