$(document).ready(function(){
	
	$('#intro').show();
	$('#intro1').delay(1000).fadeOut(2000, function() {
		$('#intro2').fadeOut(2000, function() {
			$('#intro').hide();
		});
	});
	$('#intro').click(function() {
		$(this).fadeOut(200);
	});
	
	/* Opacityhovers: gewoon 1, hover 0.75 */
	
	$('.aanbod li a').hover(function (e) {
		$(this).css('opacity', '1');
		$(this).stop(true, true).animate({
			opacity: 0.75
		}, 300)
	},
	function (e){
		$(this).stop(true, true).animate({
			opacity: 1
		}, 500)
	});
	
	$('#slider .viewport').css('overflow-x', 'hidden'); // Scrollbar weghalen
	
	$('#slider li, .productfotos li').hover(function (e) {
		/* Not hier moet je tussendoor checken omdat bij een klik de hier class wordt toegevoegd */
		$(this).not('.hier').css('opacity', '0.5');
		$(this).not('.hier').stop(true, true).animate({
			opacity: 1
		}, 300)
	},
	function (e){
		$(this).not('.hier').stop(true, true).animate({
			opacity: 0.5
		}, 500)
	});
	
	$('#slider a.buttons, .soorten li a img').not('.disable').hover(function (e) {
		$(this).not('.disable').css('opacity', '1');
		$(this).not('.disable').stop(true, true).animate({
			opacity: 0.5
		}, 300)
	},
	function (e){
		$(this).not('.disable').stop(true, true).animate({
			opacity: 1
		}, 500)
	});
	
	/* Productfotogallery */
	$('.productfotos li a').click(function (e) {
		e.preventDefault();
		
		/* Prepare for showing images */
		$('.productbackground').fadeOut(500);
		$('.productimagecontainer').show();
		
		/* Nieuwe thumb accentueren */
		fadeThumbs($('.productfotos li'), $(this).parent());
		
		/* Vergrote afbeelding tonen */
		image = $('#' + $(this).attr('id') + '_large');
		hideimages = $('.productimagecontainer img');
		fadeImages(image, hideimages);
		
		resizeBackgroundImage(); // Nodig om footer weer op de goede plaats te zetten
	});
	
	/* Fotogallery */
	var sliderIntID;
	
	$('body.fotogallery .fotogallery li a').click(function (e) {
		e.preventDefault();
		clearInterval(sliderIntID);
		
		/* Nieuwe thumb accentueren */
		fadeThumbs($('body.fotogallery .fotogallery li'), $(this).parent());
		
		/* Vergrote afbeelding tonen */
		image = $('#' + $(this).parent().attr('id') + '_large');
		hideimages = $('.background img.background');
		fadeImages(image, hideimages);
		
		resizeBackgroundImage(); // Nodig om footer weer op de goede plaats te zetten
	});
	
	if($('body').hasClass('fotogallery'))
	{
		sliderIntID = setInterval(nextPhoto, 8000);
	}
	
	/* Video's */
	vidlink = $('.videogallery .image').attr('href', '');
	$('body.videogallery .fotogallery li a').click(function (e) {
		e.preventDefault();
		
		id = $(this).parent().attr('id');
		$('.ytlink').attr('href', 'http://youtube.com/watch?v=' + id);
		loadAndPlay(id);
		currentid = id;
		
		// Nieuwe thumb accentueren
		fadeThumbs($('body.videogallery .fotogallery li'), $(this).parent());
		
		// Youtube link maken
		$('#videocontainer').html('<a href="http://www.macromedia.com/go/getflashplayer">Download Flashplayer</a> om video af te kunnen spelen.<br/>Of u kunt het <a class="ytlink" href="http://www.youtube.com/watch?v=' + id + '">filmpje in YouTube</a> afspelen.');
	});
	
});



function fadeThumbs(thumbs, hier)
{
	thumbs.removeClass('hier');
	hier.addClass('hier');
	hier.css('opacity', '1');
	
	/* Fade alle thumbs behalve de geklikte uit */
	thumbs.not('.hier').stop(true, true).animate({
		opacity: 0.5
	}, 300);
}

function fadeImages(showimage, hideimages)
{
	hideimages.not(showimage).css('z-index', '0');
	showimage.css('z-index', '1');
	showimage.fadeIn("slow", function () {
		hideimages.not(showimage).fadeOut(500);
	});
	
}

function nextPhoto()
{
	/* Nieuwe thumb accentueren */
	if($('.overview').children('li.hier').next().length > 0)
	{
		nextThumb = $('.overview').children('li.hier').next();
	}
	else
	{
		nextThumb = $('.overview').children('li').first();
	}
	fadeThumbs($('body.fotogallery .fotogallery li'), nextThumb);
	
	/* Vergrote afbeelding tonen */
	nextImage = $('#' + nextThumb.attr('id') + '_large');
	hideimages = $('.background img.background');
	fadeImages(nextImage, hideimages);
}

function resizeBackgroundImage(){
		
	$('div#container').height($(window).height()); // IE7 fix
	
	img = $('img.background');
	
    introdiv1 = $('#intro1');
    introdiv2 = $('#intro2');
	
    windowwidth = $(window).width();
    windowheight = $(window).height();
    contentheight = $('div#container').outerHeight();
    contentwidth = $('div#container').outerWidth();
	if(contentheight <= windowheight)
	{
		containerheight = "100%";
	}
	else
	{
		containerheight = contentheight;
	}
	if(contentwidth < windowwidth)
	{
		containerwidthvalue = "100%";
		containerwidth = windowwidth;
	}
	else
	{
		containerwidthvalue = contentwidth;
		containerwidth = contentwidth;
	}
	
    heightratio = img.height() / containerheight;
    widthratio = img.width() / containerwidth;
    
	// Set size of backgroundcontainer 
	$('div.background').css("width", containerwidthvalue);
	$('div.background').css("height", containerheight);
	
	// steekt aan de zijkant uit
    if (heightratio < widthratio) {
		img.css("height", "100%");
        img.css("width", "auto");
		introdiv1.css("height", "100%");
        introdiv1.css("width", "auto");
		introdiv2.css("height", "100%");
        introdiv2.css("width", "auto");
    }
	// steekt aan de onderkant uit
    else {
        img.css("height", "auto");
        img.css("width", "100%");
        introdiv1.css("height", "auto");
        introdiv1.css("width", "100%");
        introdiv2.css("height", "auto");
        introdiv2.css("width", "100%");
    }
	
	// Set size of videocontainer
	if ($('div.videocontainer').length) {
		$('div.videocontainer').css("width", containerwidth);
		$('div.videocontainer').css("height", containerwidth * aspectratios[currentid]);
	}
	
}

function onYouTubePlayerReady(playerId){
	$('.ytlink').attr('href', 'http://youtube.com/watch?v=' + currentid);
	loadAndPlay(currentid);
}

function loadAndPlay(id)
{
	ytplayer = document.getElementById("videoplayer");
	ytplayer.loadVideoById(id);
	
}

function externalLinks() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	var forms = document.getElementsByTagName("form");
	for (var i=0; i<anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("rel") != null) 
		{
			if (anchor.getAttribute("href") && anchor.getAttribute("rel").indexOf("externaltop") != -1)
			{
				anchor.target = "_top";
			}
			else if (anchor.getAttribute("href") && anchor.getAttribute("rel").indexOf("external") != -1)
			{
				anchor.target = "_blank";
			}
		}
	}
	for (var i=0; i<forms.length; i++) {
		var form = forms[i];
		if (form.getAttribute("rel") != null) 
		{
			if (form.getAttribute("action") && form.getAttribute("rel").indexOf("externaltop") != -1)
			{
				anchor.target = "_top";
			}
			if (form.getAttribute("action") && form.getAttribute("rel").indexOf("external") != -1)
			{
				form.target = "_blank";
			}
		}
	}
}
window.onload = externalLinks;


