$(document).ready(function(){

	$('#suggestions_loader').hide();
	$('#hideThisDiv').hide();
	$('#subnav a.selected, #product-nav nav').prepend('<span></span>');

	$("#movie").click(function(event) { 
		if (this.paused) { 
			this.play(); 
		} else { 
			this.pause(); 
		} 
	});
	
	$("#tellAFriendError").click(function(){
		$('#tellAFriendError').slideUp(500);
	});
	
	
	$('#TAFLabel').click(function(){
		if($('#hideThisDiv').is(':visible')){
			$('#hideThisDiv').slideUp(500);
		}
		else{
			$('#hideThisDiv').slideDown(500);
		}
	});
	
	
	
	$("section.carousel").each(function(event){
		var carousel = $(this);
		var width = $(carousel.find("li").get(0)).width();
		var c_width = carousel.width();
		var nav = $(carousel.find("nav").get(0));
		var numChildren = carousel.find("li").length;
		
		nav.find("a.number").click(function(event){
			var index = nav.find("a.number").index(this);
			carousel.find("ul").animate({
				marginLeft:index*-width
			},500,function(){});
			nav.find('a.number').removeClass('selected');
			$(this).addClass('selected');
			event.preventDefault();
			return false;
		});
		
		nav.find("a.prev").click(function(event){
			var index = nav.find("a.number").index(nav.find('a.selected').get(0));
			if (index == 0) return;
			console.log(index);
			$(nav.find("a.number").get(index-1)).trigger('click');
			event.preventDefault();
			return false;
		});
		
		nav.find("a.next").click(function(event){
			var index = nav.find("a.number").index(nav.find('a.selected').get(0));
			nav.find("a.number").length;
			if (index == nav.find("a.number").length) return;
			$(nav.find("a.number").get(index+1)).trigger('click');
			event.preventDefault();
			return false;
		});
		
		$(carousel.parent()).find("a.right").click(function(event){
			var oldX = parseInt(carousel.find('ul').css('marginLeft').replace('px', '').replace('auto',0));
			var newX = oldX - c_width;
			if(newX < (c_width - width*numChildren)) newX = c_width - width*numChildren;
			carousel.find("ul").animate({
				marginLeft:newX
			},500,function(){});
		});
		
		$(carousel.parent()).find("a.left").click(function(event){
			var oldX = parseInt(carousel.find('ul').css('marginLeft').replace('px', '').replace('auto',0));
			var newX = oldX + c_width;
			if(newX > 0) newX = 0;
			carousel.find("ul").animate({
				marginLeft:newX
			},500,function(){});
		});
	});
	
	$("section.galery").each(function(event){
		var galery = $(this);
		var nav = $(galery.find("nav").get(0));
		
		nav.find("a").click(function(event){
			galery.find('.placeholder').attr('src', $(this).attr('href'));
			galery.find('.placeholderlink').attr('href', $(this).attr('href').replace('_370_240_cth_', '_1024_768_'));
			event.preventDefault();
			return false;
		});
	});
	
	//set subnav equal height
	
	var heightPlaf = $('#nav-plafonds').height();
	var heightWand = $('#nav-wanden').height();
	var heightAfb  = $('#nav-afbouw').height();
	var heightMeub = $('#nav-meubilair').height();
	
	var biggest = heightPlaf;
	
	if(heightWand > biggest){
		biggest = heightWand;
		if(heightAfb > biggest){
			biggest = heightAfb;
			if(heightMeub > biggest){
				biggest = heightMeub;
			}
		}
	}
	else if(heightAfb > biggest){
		biggest = heightAfb;
		if(heightMeub > biggest){
			biggest = heightMeub;
		}
	}
	else if(heightMeub > biggest){
		biggest = heightMeub;
	}
	
	$('#nav-wanden').css('min-height', biggest);
	$('#nav-plafonds').css('min-height', biggest);
	$('#nav-meubilair').css('min-height', biggest);
	$('#nav-afbouw').css('min-height', biggest);
	
	
/*	
	$('#nav-wanden').css('min-height', $('#nav-plafonds').height());
	$('#nav-plafonds').css('min-height', $('#nav-wanden').height());
	$('#nav-meubilair').css('min-height', $('#nav-afbouw').height());
	$('#nav-afbouw').css('min-height', $('#nav-meubilair').height());
*/	
	//set up fancybox
	$(".carousel li a.detail, a.placeholderlink").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600, 
		'speedOut'		:	200, 
		'overlayShow'	:	false
	});
	
	//searchField
	$('#search').keyup(function(){
		
		$('.suggestionBox').css('border', '0');	
		$('.suggestionBox').css('border', '0');
		$('#suggestions').css('display', 'none');
		$('.suggestions_loader').hide();
		
		//	Save search
		var input = $('#search').val();
		if(input.length > 2){
			$('#suggestions_loader').show();
			$.ajax({ 
				type: "POST",
				url: "nl/producten/ajaxSearch/",
				data: ( {
							'postback'		: true,
							'search'		: input
						}),
				success: function(response) {
					if(response!="false"){
						$('#suggestions_loader').hide();
						$('.suggestionBox').css('border', '1px solid #cbccc7');
						$('#suggestions').show();
						$('ul#suggestions-list li').remove();
						$('ul#suggestions-list').html(response);
					}else{	
						return false;
					}
				}
			});
		}
	});
	
	//click op searchresult
	$("#suggestions-list li").live('click', function(e) { 
	    e.preventDefault; 
	    this.blur(); 
		
		var postcode = '';
		var gemeente = '';
		
		$(this).children().each(function(){
				search = $(this).html();
		});
		
		
	    return updateNavigation(search); 
	});
	
	//search input in field & form submit
	function updateNavigation(search) {
	    $('#search').val(search);
		$('.suggestionBox').css('border', '0');	
		$('#suggestions').hide();
		$('#suggestions2').hide();
		$('#frmZoeken').submit();
	}
	

	$("#frmZoeken").keypress(function(e) {
		if (e.which == 13) {
			return false;
		}
	});
	
	// =======================
	// = ACCORDION BEHAVIOUR =
	// =======================
	$('h2.heading').nextAll().hide();
	$('h2.heading').nextAll().css('list-style-type', 'none');
	
	$('h2.heading').click(function(){
		if(!$(this).hasClass('active')){
			$('h2.heading').each(function(){
				//	If active, toggle slide and remove class
				if($(this).hasClass('active')){
					$(this).nextAll().slideUp(500);
					$(this).removeClass('active');
				}
			});
			//	Set new active item
			$(this).addClass('active');
			$(this).nextAll().slideDown(500);
		}else{
			$(this).nextAll().slideUp(500);
			$(this).removeClass('active');
		}
	});
	
	
	
});
