$(document).ready(function() {
	$('#navigation').mouseenter(function() {
			$('#dropdown-wrapper').fadeTo(500, 0.9);
			$('#dropdown-menu').fadeTo(700, 1);
	});
	$('#navigation').mouseleave(function() {
		$('#dropdown-wrapper').stop().hide();
		$('#dropdown-menu').stop().hide();
	});
	
	addTransitions();
	
	var fullimage;
	
	$("a[rel^='prettyPhoto']").prettyPhoto({
		opacity: 0.45,
		overlay_gallery: false,
		gallery_markup : false,
		horizontal_padding: 50,
		changepicturecallback: function(){ /* Called everytime an item is shown/changed */
		
			// zorg dat het frame bij een wijziging altijd gesloten is. 
			$('#openPrettyContact').hide();
				
			// frame openen om iets te vragen over wat er zichtbaar is in de popup.
			$('.pp_social .info').toggle(function() {
				$('#openPrettyContact').show();
			}, function() {
				$('#openPrettyContact').hide();
			});
			
			// afbeelding waarbij de vraag wordt gestuurd meegeven. 
			fullimage = $('#fullResImage').attr('src');
			fullimage = fullimage.replace(baseUrl, '/');
			$('#openPrettyContact').attr('src', baseUrl+'get-info/?img='+fullimage);			
		}, 
		social_tools : '<div class="pp_social"><div class="info">Dit wil ik!</div><iframe scrolling="no" id="openPrettyContact" src=""></iframe><div class="twitter"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a></div><div class="facebookshare"><a name="fb_share" type="button">Delen</a><script src="http://static.ak.fbcdn.net/connect.php/js/FB.Share" type="text/javascript"></script></div><div class="facebook"><div class="fb-like" data-send="true" data-width="450" data-show-faces="false" data-layout="button_count"></div><div id="fb-root"></div><script>(function(d, s, id) {  var js, fjs = d.getElementsByTagName(s)[0];  if (d.getElementById(id)) {return;}  js = d.createElement(s); js.id = id;  js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";  fjs.parentNode.insertBefore(js, fjs);}(document, \'script\', \'facebook-jssdk\'));</script></div></div><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>'
		
	});
		
	// Zet autocomplete op het zoekvenster
	var options = { serviceUrl:'/ajax/search.php?random='+Math.random(), autoSubmit: true };
	var ac = $('#search').autocomplete(options);
	ac.enable();
});

$(window).load(function(){
	attachProjectHover();
});

function attachProjectHover() {
	$('.project-block').hover(
		function(){
			$(this).parent().find('.hover').stop().animate({ backgroundPosition: "-120px -120px" }, 1000, 'easeOutQuad');
			$(this).parent().find('.overlay').stop().fadeTo(500, 0);
			$(this).parent().find('.title').stop().delay(150).animate({ bottom: '0px'}, 500, 'easeOutQuad');
		}, 
		function() {
			$(this).find('.hover').stop().animate({ backgroundPosition: "50px 50px" }, 700, 'easeInOutQuad');
			$(this).parent().find('.overlay').stop().fadeTo(500, 0.7);
			$(this).parent().find('.title').stop().animate({ bottom: '-100px'}, 500, 'easeOutQuad');
		}
	);
}

function moveSliderViewportCanvas(type, totSlides) {
	$('#slider-holder .prev').removeClass('disable');
	$('#slider-holder .prev').attr('onClick', 'javascript: moveSliderViewportCanvas(\'prev\', \''+totSlides+'\');');
	$('#slider-holder .next').removeClass('disable');
	$('#slider-holder .next').attr('onClick', 'javascript: moveSliderViewportCanvas(\'next\', \''+totSlides+'\');');
	
	var viewportWidth = 800;
	
	// index bepalen
	var slideIndex = parseInt($('#sliderIndex').html());
	if(type == 'prev') {
		$('#sliderIndex').html(slideIndex - 1);
	}
	else {
		$('#sliderIndex').html(slideIndex + 1);
	}
	
	var newSlideIndex = parseInt($('#sliderIndex').html());
	
	// buttons de-activeren als de eerste of laatste slide actief is
	if (newSlideIndex == 1) {	// de-activate prev btn
		//alert('FIRST is active');
		$('#slider-holder .prev').addClass('disable');
		$('#slider-holder .prev').attr('onClick', '');
	}
	else if(newSlideIndex == totSlides) { // de-activate next btn
		//alert('LAST is active');
		$('#slider-holder .next').addClass('disable');
		$('#slider-holder .next').attr('onClick', '');
	}
	
	// positie bepalen
	var newPos = ((newSlideIndex * viewportWidth) - viewportWidth) / -1;
	$('#slider_viewport_canvas').animate({left : newPos}, 1000);		
}

function boxEffect(delay) {
	// First line of blocks
	$('.home-block:eq(1)').delay(500).animate({ backgroundPosition: "320px -320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(2)').delay(500).animate({ backgroundPosition: "320px 320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(3)').delay(500).animate({ backgroundPosition: "320px -320px" }, 1600, 'easeInOutQuad');
	// Second line of blocks
	$('.home-block:eq(7)').delay(500).animate({ backgroundPosition: "-320px 320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(6)').delay(500).animate({ backgroundPosition: "-320px -320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(5)').delay(500).animate({ backgroundPosition: "-320px 320px" }, 1600, 'easeInOutQuad');
	// Third line of blocks
	$('.home-block:eq(12)').delay(500).animate({ backgroundPosition: "320px 320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(13)').delay(500).animate({ backgroundPosition: "320px -320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(14)').delay(500).animate({ backgroundPosition: "320px 320px" }, 1600, 'easeInOutQuad');
	$('.home-block:eq(15)').delay(500).animate({ backgroundPosition: "320px -320px" }, 1600, 'easeInOutQuad');
	
	showProjects(delay);
}

function queueShowEffect(classname, number, effecttype, delay) {
	var len = $('.'+classname).length;
	
	$('.'+classname+':eq('+number+')').show(effecttype, delay, function() {
		if (number < len) {
			queueShowEffect(classname, number+1, effecttype, delay);
		}
	});
}

function showProjects(delay) {
	var amt = $('.project-block').length;
	
	for (var i = 0; i < amt; i++) {
		$('.project-block:eq('+i+')').delay(delay+((i+1) * 50)).show('fade');
		$('.project-block:eq('+i+') .overlay').delay(delay + 400 + ((i+1) * 100)).show('fade');
	}
	
	$('#home-wrapper-navigation').delay(3500).animate({ opacity: 0.99 }, 1000, function() {
		timeNavigation(0, 3000);
		
		$('#home-wrapper-navigation a').hover(
			function() {
				$('#home-wrapper-navigation').stop();
				$('#home-wrapper-navigation a.hover').removeClass('hover').animate({ color: '#636363'}, 200);
				$(this).stop().addClass('hover').animate({ color: '#ed6a00'}, 200);
				$('#home-block-holder a:not(.'+$(this).attr('class').replace(' hover', '')+') .overlay').stop().fadeTo(1000, 0.8);
				$('#home-block-holder a.'+$(this).attr('class').replace(' hover', '')+' .overlay').stop().fadeTo(1000, 0);
			},
			function() {
				timeNavigation($('#home-wrapper-navigation a').index($(this)), 4000);
			}
		);		
	});
}

function timeNavigation(number, delay) {
	var total = $('#home-wrapper-navigation a').length;
	$('#home-wrapper-navigation a.hover').stop().removeClass('hover').animate({ color: '#636363'}, 200);
	$('#home-wrapper-navigation a:eq('+number+')').stop().addClass('hover').animate({ color: '#ed6a00'}, 200);
	$('#home-block-holder a:not(.cat'+(number+1)+') .overlay').stop().fadeTo(1000, 0.8);
	$('#home-block-holder a.cat'+(number+1)+' .overlay').stop().fadeTo(1000, 0);
	
	number = (number == total-1) ? 0 : number + 1;
	
	$('#home-wrapper-navigation').animate({ height: $('#home-wrapper-navigation').height() }, delay, function() {
		if (homepagequeue != 'stop') {
			timeNavigation(number, delay);
		}
	});
}

function addTransitions() {
	$('#footer .leftcontent a').hover(function() {
		$(this).stop().fadeTo(500, 1);
	},
	function() {
		$(this).stop().fadeTo(500, 0.40);
	});
	
	$('#navigation li:not(.active) a').hover(function() {
		$(this).stop().animate({ 'color' : '#D16A19'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#ffffff'});
	});
	
	$('#type-navigation li:not(.active) a').hover(function() {
		$(this).stop().animate({ 'color' : '#D16A19'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#636363'});
	});
	
	$('#topcontent ul.breadcrumbs li a').hover(function() {
		$(this).stop().animate({ 'color' : '#ffffff'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#636363'});
	});
	
	$('.subsubmenu li:not(.active) a').hover(function() {
		$(this).stop().animate({ 'color' : '#D16A19'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#A4A4A4'});
	});
	
	$('#dropdown-menu ul:not(#type-navigation) li a').hover(function() {
		$(this).stop().animate({ 'color' : '#ffffff'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#9e9e9e'});
	});
	
	$('#home-footer .footer-block a:not(.meer) ').hover(function() {
		$(this).stop().animate({ 'color' : '#ffffff'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#636363'});
	});
	
	$('#topcontent .submenu li:not(.active) a, #topcontent .submenu-rightcontent li:not(.active) a').hover(function() {
		$(this).stop().animate({ 'color' : '#D16A19'});
	},
	function() {
		$(this).stop().animate({ 'color' : '#636363'});
	});
}

function showProjectInfo(type) {
	$('#innerSubmenu').find('li').removeClass();	
		
	if(type == 'toepassingen') { 
		$('#gerelateerde-producten').hide(); 
		$('#gerelateerde-toepassingen').show();
	}
	if(type == 'producten') { 
		$('#gerelateerde-toepassingen').hide(); 
		$('#gerelateerde-producten').show();
	}
}

function homeLoadCat(cat) {
	// Stop the navigation animation and enable the correct item
	homepagequeue = 'stop';
	$('#home-wrapper-navigation').stop(true, true);
	$('#home-wrapper-navigation a').unbind('mouseenter mouseleave');
	$('#home-wrapper-navigation a.hover').stop().removeClass('hover').animate({ color: '#636363'}, 200);
	$('#home-wrapper-navigation a:eq('+(cat-1)+')').stop().addClass('hover').animate({ color: '#ed6a00'}, 200);

	// Unload the current items shown
	var j = 1;
	for (var i = 16; i >= 0; i--) {
		$('#home-block-holder .home-block:eq('+i+')').delay(j * 50).hide('scale', 500, function() {
			$(this).remove();
		})
		j++;
	}
	$('#home-wrapper-navigation').animate({ height: $('#home-wrapper-navigation').height() }, 1500, function() {
		$.ajax({
			url		: "/ajax/homepage.php?ajax=ajax&type="+cat,
			cache	: false,
			async	: true,
			success	: function(data){
				$('#home-block-holder').append(data);
				for (var i = 0; i <= 16; i++) {
					$('#home-block-holder .home-block:eq('+i+')').delay(i * 50).show('fade', 300);
				}
				attachProjectHover();
			}
		});
	});
}

// Global variables
var homepagequeue = '';
