// JavaScript Document
var backoffice;
var referenceOffset;

var headerTimeout;
var headerSliding = false;

$(document).ready(function()
{
	// Show body background ---------------------------------------------------------------
	$('body').removeClass('preload');
	
	if ($('div.controlBorder').length > 0)
	{
		backoffice = true;
	}
	
	if ($('div#Header div.model').length > 0)
	{
		$('div#Header div.model div.image:first').addClass('activeImage');
		
	}
	$('div#Header div.info div.overlay').each(function()
	{
		$(this).attr('mywidth', $(this).outerWidth());
		$(this).css('width', 0);
		$(this).css('height', 0);
		$(this).hide();
	});
	
	if ($('div.topArea div.accordeon').length > 0)
	{
		if (backoffice != true)
		{
			headerTimeout = setTimeout(function(){ headerToggle($('div.topArea div.accordeon div.activeElement').next()); }, 7000);
		}
	}
	
	// Cufon --------------------------------------------------------------------------------------
	Cufon.now();
	
	Cufon.replace('.customFont');
	Cufon.replace('h1')('h2')('h3');
	Cufon.replace('div#Menu > div > ul > li > a');
	Cufon.replace('div.accordeon div.title');
	
	
	// Language ------------------------------------------------------------------
	$('div#Languages a:last').hover(function()
	{
		$(this).parent().find('div.languagePopup').addClass('show');
	}, function()
	{
		$(this).parent().find('div.languagePopup').removeClass('show');
	});
	
	
	// Menu ------------------------------------------------------------------
	var hoverout;
	var activeHover;
	var dropoutWidth = $('div#Menu ul li ul').width();
	var dropoutLeft;
	
	var menuWidth = 0;
	$('div#Menu > div > ul > li').each(function()
	{
		menuWidth += $(this).outerWidth();
	});
	
	$('div#Menu > div > ul > li:last').addClass('rightAlign');
	
	$('div#Menu > div > ul > li').mouseenter(function()
	{
		dropoutLeft = $(this).position().left;
		
		var newWidth = dropoutWidth;
		if ($(this).outerWidth() > dropoutWidth)
		{
			newWidth = $(this).outerWidth();
		}
		else if ($(this).hasClass('rightAlign') != true)
		{
			newWidth = $(this).outerWidth() + $(this).next().outerWidth();
		}
		else if ($(this).hasClass('rightAlign') == true)
		{
			newWidth = $(this).outerWidth() + $(this).prev().outerWidth();
		}
		$(this).find('> ul').width(newWidth);
		
		if (hoverout != null)
			clearTimeout(hoverout);
			
		if (activeHover != null)
			closeHover();
			
		if ((dropoutLeft + newWidth + dropoutWidth) > menuWidth)
		{
			$(this).find('ul').addClass('alignRight')
		}
		
		$(this).addClass('hover');
		if ($(this).find('ul').length > 0)
		{
			$(this).addClass('subHover');
			$(this).prev().addClass('prevHover');
			$(this).next().addClass('nextHover');
		}
		Cufon.refresh('div#Menu > div > ul > li > a');
		
		activeHover = $(this).find('> ul');
	});
	$('div#Menu > div > ul > li').mouseleave(function()
	{
		hoverout = setTimeout(closeHover, 1000);
	});
	
	$('div#Menu div ul li ul li').mouseenter(function()
	{
		$(this).addClass('hover');
	});
	$('div#Menu div ul li ul li').mouseleave(function()
	{
		$(this).removeClass('hover');
	});
	
	function closeHover()
	{
		$('div#Menu ul li.hover').removeClass('hover');
		$('div#Menu ul li.subHover').removeClass('subHover');
		$('div#Menu ul li.prevHover').removeClass('prevHover');
		$('div#Menu ul li.nextHover').removeClass('nextHover');
		Cufon.refresh('div#Menu > div > ul > li > a');
		hoverout = null;
		activeHover = null;
	}
	
	
	// Referentie blocks link ------------------------------------------------------------------
	$('div.redirectBlock').click(function()
	{
		if (backoffice == true)
		{
			return true;
		}
		if ($(this).find('a').length > 0)
		{
			var href = $(this).find('a:first').attr('href');
			var target = $(this).find('a:first').attr('target');
			
			if (target == '_blank')
				target = '_blank';
			else
				target = '_parent';
			
			if (backoffice != true)
				window.open(href, target);
		}
		
		return false;
	});
	
	
	// Model ------------------------------------------------------------------
	$('div.model div.info').mouseenter(function()
	{
		$(this).find('div.overlay').stop();
		$(this).addClass('activeInfo');
		
		var overlay = $(this).find('div.overlay');
		
		//overlay.animate({width: 200, height: '38px'}, 300);
		overlay.animate({width: overlay.attr('mywidth'), height: '38px'}, 300);
	});
	$('div.model div.info').mouseleave(function()
	{
		$(this).find('div.overlay').stop();
		$(this).find('div.overlay').animate({width: 0, height: 0}, 300, function()
		{
			$('div.model div.activeInfo').removeClass('activeInfo');
		});
	});
	
	
	// Accordeon Homepage ------------------------------------------------------------------
	if ($('div.topArea div.accordeon').length > 0)
	{
		$('div.topArea div.accordeon div.activeElement div.info').slideDown(500);
	}
	$('div.topArea div.accordeon div.element div.title').click(function()
	{
		if ($(this).parents('div.element').hasClass('activeElement'))
		{
			return;
		}
		clearTimeout(headerTimeout);
		headerToggle($(this).parents('div.element'));
	});
	$('div.topArea div.accordeon div.element div.sideBg').click(function()
	{
		if ($(this).parents('div.element').hasClass('activeElement'))
		{
			return;
		}
		clearTimeout(headerTimeout);
		headerToggle($(this).parents('div.element'));
	});
	
	$('div#Header div.model div.image').mouseenter(function()
	{
		if ($(this).find('div.info').length > 0)
		{
			clearTimeout(headerTimeout);
		}
	});
	$('div#Header div.model div.image').mouseleave(function()
	{
		if ($(this).find('div.info').length > 0)
		{
			headerTimeout = setTimeout(function(){ headerToggle($('div.topArea div.accordeon div.activeElement').next()); }, 5000);
		}
	});
	
	function headerToggle(element)
	{
		if (headerSliding == true)
		{
			return false;
		}
		
		headerSliding = true;
		var info = element.find('div.info');
		var active = $('div.topArea div.accordeon div.activeElement');
		var next = element.index() + 1;
		
		if (next == $('div.topArea div.accordeon div.element').length)
		{
			next = 0;
		}
		
		active.find('div.info').slideUp(500);
		active.removeClass('activeElement');
		
		if (element.html() != active.html())
		{
			info.slideDown(500, function(){headerSliding = false;});
			element.addClass('activeElement');
		}
		
		Cufon.refresh('div.accordeon div.title');
		
		changeHeaderImage(element);
		
		if (backoffice != true)
		{
			headerTimeout = setTimeout(function(){ headerToggle($('div.topArea div.accordeon div.element:eq(' + next + ')')) }, 7000);
		}
	}
	
	function changeHeaderImage(element)
	{
		var header = $('div#Header');
		var index = element.index();
		
		header.find('div.activeImage').stop();
		header.find('div.activeImage').animate({opacity: 1}, 0);
		
		header.find('div.activeImage').fadeOut(0, function()
		{
			$(this).removeClass('activeImage');
			header.find('div.image:eq(' + index + ')').addClass('activeImage');
			header.find('div.image:eq(' + index + ')').fadeIn(1000);
		});
	}
	
	
	// Accordeon list ------------------------------------------------------------------
	if ($('div.accordeonList').length > 0)
	{
		$('div.accordeonList h4').wrap('<div class="element"></div>');
		$('div.accordeonList p').each(function()
		{
			var index = $(this).index();
			$(this).appendTo($('div.accordeonList div.element:eq(' + (index - 1) + ')'));
		});
		
		$('div.accordeonList div.element h4').toggle(function()
		{
			$(this).parent().find('p').slideDown(300);
			$(this).parent().addClass('activeElement');
		}, function()
		{
			$(this).parent().find('p').slideUp(300);
			$(this).parent().removeClass('activeElement');
		});
	}
	
	
	// Klanten slider ------------------------------------------------------------------
	if ($('div.referenceSlider').length > 0)
	{
		$('div.referenceSlider').each(function()
		{
			makeReferenceSlider($(this));
		});
	}
		
	$('div.referenceSlider').find('div.referenceNav').mouseenter(function()
	{
		var slides = $(this).parents('div.referenceSlider').find('div.slides');
		var container = $(this).parents('div.referenceSlider').find('div.slideContainer');
		referenceOffset = container.width() - container.find('div.slides').outerWidth();
		
		if ($(this).hasClass('navPrev') == true)
		{
			if (slides.position().left >= 0)
			{
				return;
			}
			slideReference(slides, 1)
		}
		else if ($(this).hasClass('navNext') == true)
		{
			if (slides.position().left <= referenceOffset)
			{
				return;
			}
			slideReference(slides, -1)
		}
	});
	$('div.referenceSlider').find('div.referenceNav').mouseleave(function()
	{
		var slides = $(this).parents('div.referenceSlider').find('div.slides');
		slides.stop();
	});
	
	
	// Quotes ------------------------------------------------------------------
	if ($('div.topArea div.quote').length > 0)
	{
		var quote = $('div.topArea div.quote');
		
		quote.find('img.leftQuote').prependTo(quote.find('span'));
		quote.find('img.rightQuote').appendTo(quote.find('span'));
	}
});
	
function makeReferenceSlider(slider)
{
	var container = slider.find('div.slideContainer');
	var slides = slider.find('div.slides');
	var width = 0;
	
	if (slider.find('a').length > 0)
	{
		slides.find('a').not('div.popupMenu a').each(function()
		{
			width += $(this).outerWidth();
		});
	}
	else if (slider.find('img').length > 0)
	{
		slides.find('img').not('div.popupMenu img').each(function()
		{
			width += $(this).outerWidth();
		});
	}
	
	slides.width(width + 2);
	referenceOffset = container.width() - container.find('div.slides').outerWidth();
}

function slideReference(slides, direction)
{
	var distance = 2 * direction;
	var slideLeft = slides.position().left;
	
	if (slideLeft <= referenceOffset || slideLeft >= 0)
	{
		slides.stop();
	}
	
	slides.animate({left: '+='+ distance +''}, 10, function(){slideReference(slides, direction)});
}
