/* Development logs */
window.log = function(){
	log.history = log.history || [];
	log.history.push(arguments);
	if(this.console){
		console.log( Array.prototype.slice.call(arguments) );
	}
};

/**
 * Funcie kruimelpad
 */
function shortenBreadcrumb(options){

    var breadcrumb = $('div#breadcrumbs > ul');
    var maxWidth = $('div#breadcrumbs').innerWidth();

    if (options != undefined){
        if (options.breadcrumb != null) { breadcrumb = options.breadcrumb; }
        if (options.maxWidth != null) { maxWidth = options.maxWidth; }
    }

    var levelCount = breadcrumb.find('li').size();
    var shortEnough = false;
    var totalWidth;
    while (shortEnough == false) {
        totalWidth = 0;
		
		$(breadcrumb.children('li')).each(function() { 
				totalWidth += $(this).outerWidth(true);
		});
		
        if (totalWidth > maxWidth){
            var li = breadcrumb.children('li').not('.short').eq(1);
			//var li = breadcrumb.children('li');
			$(breadcrumb.children('li')).each(function() { 
				var subcount = 0;
				//Check of niet short heeft
				if( $(this).not('.short').eq(1) )
				{
					//Alleen eerste
					if(subcount === 0)
					{
						$(this).addClass('short');
						$(this).children('a').attr('title', $(this).children('a').html());
						//$(this).children('a').html(li.children('a').attr('fulltxt').substr(0, 2)+'..');
						$(this).children('a').html($(this).children('a').attr('fulltxt').substr(0, 5)+'..');
						$(this).children('a').addClass('nav_ttip');	
						
						//Updaten lengte
						totalWidth = 0;
						$(breadcrumb.children('li')).each(function() { 
								totalWidth += $(this).outerWidth(true);
						});		
						if (totalWidth < maxWidth){
							shortEnough = true;	
							return false;
						}
					}
	
					subcount = subcount +1;
				}
			});
        }
        else {
            shortEnough = true;
        }
		
		//            li.addClass('short');
		//            li.children('a').attr('title', li.children('a').html());
		//            li.children('a').html(li.children('a').attr('fulltxt').substr(0, 2)+'..');
		//            li.children('a').addClass('nav_ttip');		
		
		//		$(breadcrumb.children('li').get().reverse()).each(function() { 
		//				totalWidth += $(this).outerWidth(true);
		//		});		
    }
}

$(function(){
	//Kruimelpad
	shortenBreadcrumb({
			breadcrumb : $('ul#breadcrumbs'),
			maxWidth : 480,
			outerWidth: 480
		});	
	
	if ( $('#menu li li').hasClass('activesub-bg')) {
		$('#menu li').removeClass('active-bg')
	}
	if ( $('#menu li li li').hasClass('activesubsub-bg')) {
		$('#menu li li').removeClass('activesub-bg')
	}
	if ( $('#menu li li li li').hasClass('active-sub')) {
		$('#menu li li li').removeClass('activesubsub-bg')
	}
	
	//Bij laden van de pagina kijken welk element de active class heeft
	//en voor ieder onderliggende ul+li show aanzetten
	$('.active').addClass('hover').addClass('hover'); 
	$('.active-sub').addClass('actived').addClass('hover');
	
	//Kleuren wat gekleurd moet worden
	$('#menu a').bind({
		mouseover: function() {
			$(this).siblings('.menuitempijltje').addClass('hover');
		},
		
		mouseout: function() {
			$(this).siblings('.menuitempijltje').removeClass('hover');
		}
	});	
	
	//Uitklappen van menu
	$('.menuitempijltje').bind({
		click: function() {
			
			var liParent = $(this).closest("li");

			if ( liParent.hasClass('hover') )
			{
				$('#menu li a').removeClass('hover');
				liParent.removeClass('hover');
			//liParent.('> ul').hide();
			}
			else
			{
				$('#menu li').removeClass('hover');
				$('#menu li a').removeClass('hover');
				$(this).parentsUntil('#menu').filter('li').addClass('hover');
			}
		}
	});
	
	//Qtips initen
	$('.nav_ttip').qtip({
		show: 'mouseover',
		hide: 'mouseout',
		content: {
			text: function(api) {
				return $(this).attr('fulltxt');
			}
		},
		position: {
			target: 'mouse',
			adjust: {
				mouse: true
			}
		}
	});
});











