$.fn.widest = function(selector){
	var grootste_element = this;
	var grootste_waarde = 0;
	this.each(function(){
		if ($(this).outerWidth() > grootste_waarde) {
			grootste_element = $(this);
			grootste_waarde = $(this).outerWidth()
		}
	})
	return grootste_element;
}

var timeout_id;

function clearTimer() {
	if (timeout_id) {
		window.clearTimeout(timeout_id);
	}
}

function setTimer() {
	timeout_id = window.setTimeout(function(){
		$('.wrapping').hide();
		$('a.followed').removeClass('followed');
	}, 2250);
}


$(document).ready(function(){
	$('#content .links').css('height',($(document).height() - 234 - 35));
	$('.level-0 a').mouseover(function(){
		var id = $(this).attr('id').replace('item-','');
		$('.followed').removeClass('followed');
		$(this).addClass('followed');
		$('.wrapping').hide();
		$('#wrapping-'+id+':first').show();
		$('#wrapping-'+id+':first .submenu').hide();
		$('#wrapping-'+id+':first #menu-'+id+':first').show();
		clearTimer();
	});
	$('#landingtop-1').mouseover(function(){
		$(this).css('cursor','pointer')
	}).mouseout(function(){
		$(this).css('opacity','1')
	}).click(function(){
		location.href = '/en/1/spent\-catalyst/?chosen-area=1&language_id=1'
	})
	$('#landingtop-2').mouseover(function(){
		$(this).css('cursor','pointer')
	}).mouseout(function(){
		$(this).css('opacity','1')
	}).click(function(){
		location.href = '/en/64/products/?chosen-area=2&language_id=1'
	})
	$('.level-2 a').mouseover(function(){
		clearTimer();
	}).mouseout(function(){
		setTimer();
	});
	$('.level-1 a').mouseover(function(){
		var id = $(this).attr('id').replace('item-','');
		$('.level-2').hide();
			$('#menu-'+id+':first').fadeIn(200)
			$(this).css('width',$(this).find('a').widest().innerWidth());
		clearTimer();
	}).mouseout(function(){
		setTimer();
	});
	$('.level-0 a').each(function(){
		$(this).css('width',$(this).innerWidth());
		$(this).find('div').css('float','right');
		var id = $(this).attr('id').replace('item-','');
		var left = $(this).position().left;
		$('#wrapping-'+id).css('left',left);
	});
	$('.level-1').mouseover(function(){
		Cufon.set('.level-0 a', {
			'color': '#ffffff'
		});
	});
	$('.menu-piece-left').each(function(){
		$(this).css('height',$(this).parent().outerHeight());
	});
	$('.menu-piece-bottom').each(function(){
		$(this).css('height',$(this).parent().innerHeight());
		$(this).css('padding-bottom','12px');
	});
	$('.menu-piece-right').each(function(){
		$(this).css('height',($(this).parent().innerHeight() - 14));
	});
	$(window).bind('resize',function(){
		window.open(location.href,'_self');
		$('.wrapping').hide();
		$('a.followed').removeClass('followed');
		$('#content .links').css('height',($(document).height() - 234 - 35));
	});
	$('div#footer .footer_inside marquee').marquee('pointer').mouseover(function () {
		$(this).trigger('stop');
	}).mouseout(function () {
		$(this).trigger('start');
	}).mousemove(function (event) {
		if ($(this).data('drag') == true) {
			this.scrollLeft = $(this).data('scrollX') + ($(this).data('x') - event.clientX);
		}
	}).mousedown(function (event) {
		$(this).data('drag', true).data('x', event.clientX).data('scrollX', this.scrollLeft);
	}).mouseup(function () {
		$(this).data('drag', false);
	});
});
