$(function(){
	$('.scroll-pane', $('[id^=menu-block-]')).html('')
	$('#menu-block-1').show();
	$('[id^=submenu-0]').each(function(){
		parts = $(this).attr('id').split('-')
		level = parts.length - 1
		$('.scroll-pane', $('#menu-block-' + level)).append($(this))
		$('a', $(this)).click(function(){
			// simply follow link if item already active
			if ($(this).hasClass('active')) {
				if (!$(this).hasClass('clc-disabled-menu')) {
					return true;
				}
				else
				{
					return false;
				}
			}
			 
			 
			$('a', $(this).parents('ul:first')).removeClass('active')
			mlid = $(this).addClass('active').attr('id').split('-')[1]
			child = $('ul[id^=submenu-0][id$=' + mlid + ']:first')
			if (child.size()) {
				$('a', child).removeClass('active')
				child_level = child.attr('id').split('-').length -	1
				for (i = 4; i > child_level; i--) {
					$('ul.submenu-block:visible', $('#menu-block-' + i)).hide('normal', function(){
						$(this).parents('[id^=menu-block-]:first').removeClass('scroll-border')
					})
				}
				to_hide = $('ul.submenu-block:visible', $('#menu-block-' + child_level))
				if (to_hide.size()) {
					to_hide.hide('normal', function(){
						submenu_show(child)
					})
				} else {
					submenu_show(child)
				}
				return false
			}
			return true
		})
	});
	$('#submenu-0').show();
	$('.scroll-pane').jScrollPane();
	$('a.active[id^=mlid-]').removeClass('active');
	$(document).ready(function(){
		highlight_path();
	});
	
	setTimeout(function(){
		$('.active[id^=mlid-]').each(function(){
			scroll_to_active($(this));
		});
	}, 500);
	
	
	function submenu_show(submenu){
		var menu_block = submenu.parents('[id^=menu-block-]:first')
		menu_block.addClass('scroll-border')
		submenu.show('normal', function(){
			$('.scroll-pane').jScrollPane();
		})
	}
	
	function scroll_to_active(active_path){
		if (active_path.size()) {
			var container = active_path.parents('[id^=menu-block-]:first');
			var sp = container.find('.scroll-pane');
			var pd = container.find('.jScrollPaneDrag');
			var itm_pos = active_path.position().top;
			var visbHeight = parseInt(container.innerHeight());
			var active_h = active_path.attr('scrollHeight');
			var last_itm = active_path.parents('ul').find('li:last');
			var target_pos = visbHeight / 2 -
			active_path.attr('scrollHeight') /
			2;
			if (itm_pos > target_pos) {
				var real_height = last_itm.position().top +
				last_itm.attr('scrollHeight');
				
				var offset = itm_pos - target_pos;
				if (offset > (real_height - visbHeight)) {
					offset = real_height - visbHeight;
				}
				if (offset > 0) {
					sp.animate({
						'top': '-' + offset + 'px'
					});
					
					var dragH = visbHeight - parseInt(pd.css('height')) - 4 * 2;
					var mul = offset / (real_height - visbHeight);
					pd.animate({
						'top': String(dragH * mul) + 'px'
					});
				}
			}
		}
	}
	
	function highlight_path(){
	
		var current_path = window.location.pathname;
		var active_path = $('#block-menu-primary-links').find('a[href=' + current_path + ']').eq(0);
		if (active_path.size()) {
			var active_container = active_path.parent().parent();
			var active_itm_id = active_path.attr('id').split('-');
			var trail = active_container.attr('id').split('-');
			var trace = trail[0];
			trail[trail.length] = active_itm_id[1];
			for (var i = 1; i < trail.length; i++) {
				trace += ('-' + trail[i]);
				submenu_show($('#' + trace));
				$('#mlid-' + trail[i]).addClass('active');
				// scroll_to_active($('#mlid-' + trail[i]));
			}
		}
	}
})
