jQuery(document).ready(function(){
	
	// Shadowbox
	Shadowbox.init({players: ['img', 'html', 'iframe', 'qt', 'wmp', 'swf', 'flv']});
	
	// action folder fancybox
	$("a.iframe").fancybox({
 		
 		'frameWidth': 898,
 		'frameHeight': 650,
 		'overlayShow': false
 	});

	// Dialogs
 	$("a#dialog").fancybox({
 		
 		'frameWidth': 300,
 		'frameHeight': 80,
 		'overlayShow': false
 	});
	
	$("a#dialog").trigger('click');
	
	$('ul.products').cycle({
		fx: 'fade',
		speed: 2000
	});
	
	// Image slider
	var items = $('#wrapper #slider ul li').mouseenter (function () {
		
		// Clear interval
		clearInterval (slider);
		
		// Remove active
		$('#wrapper #slider ul li.active').removeClass('active');
		
		// Set active
		$(this).addClass('active');
		
		// Calculate the offset
		offset = items.index(this) + 1;
		offset = (offset * 235) - 235;
		
		// Set offset
		$('#slider-items').stop().animate({marginTop: '-' + offset}, 800, 'jswing');
	});
	
	// Home interval
	var slider = setInterval (function () {
				
		// Remove active
		active = $('#wrapper #slider ul li.active');
		
		// Calculate next
		next = items.index(active) + 1;
		
		// Check for offset
		if (next == items.length)
			next = 0;
		
		// Fetch next
		slide = $(items).get(next);
				
		// Remove active
		$(active).removeClass('active');
		$(slide).addClass('active');
		
		// Offset
		offset = (next * 235);
		
		// Set offset
		$('#slider-items').stop().animate({marginTop: '-' + offset}, 800, 'jswing');
		
	}, 5000);
		
	// Accordion settings
	var slide_speed = 800;
	var slide_easing = "jswing";
	var top_items = $('#wrapper #top5 .item');
	var item_amount = $(top_items).length;
	var start_height = 231;
	
	$(top_items).mouseenter (function () {
		
		// Check for active
		if ($(this).hasClass('active'))
			return false;
			
		// Current index
		var index = $(top_items).index(this) + 1;
		
		// Hide active accordion
		$('#wrapper #top5 .item').stop().animate({'height': '41px'}, slide_speed, slide_easing);
		$('#wrapper #top5 .item .accordion').stop().animate({'height': '0px'}, slide_speed, slide_easing);
		//$('#wrapper #top5 .item .accordion div').fadeOut(slide_speed - 500);
		$('#wrapper #top5 .item .accordion div').hide();
		
		// Show accordion
		$(this).stop().animate({'height': (51 + start_height) + 'px'}, (slide_speed - 10), slide_easing);
		$('.accordion', this).stop().animate({'height': start_height + 'px'}, slide_speed, slide_easing, function () {
			
			// Fade in
			//$('div', this).fadeIn(slide_speed);
			$('div', this).show();
			
			// Rerun the sifr
			sIFR.replace(themixbold, {
				selector: '#top5 .item .price span',
				transparent: true,
				forceSingleLine: true,
				css: [
					'.sIFR-root { font-size:40px; color:#e2001a; }'
				]
			});
		}).fadeIn(slide_speed - 200);
				
		// Remove active and set active
		$('#wrapper #top5 div.active').removeClass('active');
		$(this).addClass('active');
	});
	
	// Filter popup
	$('.filter span').click (function () {
		
		// Remove all chosen stuff if not submitted.
		$("#search_form").each (function (){ this.reset(); });
		
		
		// Toggle multiselect in this filter
		var option = $('.options', $(this).parent()).toggle();
		
		// Hide all other options
		$('.options').not($(option)[0]).hide();
		
		// Fetch position and lengths
		var position = $(this).parent().position();
		var width = parseInt($(option).css('width')) + parseInt($(option).css('paddingLeft')) + parseInt($(option).css('paddingRight'));
		
		var length = position.left + width;
		
		// Check if position
		if (length > 800) {
			
			// Set margin left
			$(option).css('marginLeft', '-' + ((length + 1) - 800) + 'px');
		}
	});
	
	// Register close filter button
	$('.filter input.button').click (function () { $('.options').hide(); });
	
	// Thumbs
	$('#thumbs li a').click(function () {
		
		// Fetch href
		var link = $(this).attr('href');
		
		// Set large image
		$('#large img').attr('src', link);
		
		// Remove and set active
		$('#thumbs li').removeClass('active');
		$(this).parent().parent().addClass('active');
		
		// Render offset
		var active = $('#thumbs ul li.active');
		var offset = $('#thumbs li').index (active);
		
		// Calculate offset
		offset = 0 - (offset * 90);
		
		// Margin top
		$('#thumb-container ul').stop().animate({'marginTop': offset}, 800, 'jswing');
		
		// Return false
		return false;
	});
	
	$('a#next-image').click(function () {
			
		// Remove active
		active = $('#thumbs ul li.active');
		
		// Calculate next
		next = $('#thumbs ul li').index(active) + 1;
		
		// Check for offset
		if (next == $('#thumbs ul li').length)
			next = 0;
			
		// Set image
		var next_image = $('#thumbs ul li').get(next);
				
		// Call click
		$('a', next_image).click();
	});
	
	$('a#previous-image').click(function () {
			
		// Remove active
		active = $('#thumbs ul li.active');
		
		// Calculate previous
		previous = $('#thumbs ul li').index(active) - 1;
		
		// Check for offset
		if (previous == -1)
			previous = $('#thumbs ul li').length - 1;
			
		// Set image
		var previous_image = $('#thumbs ul li').get(previous);
				
		// Call click
		$('a', previous_image).click();
	});
	
	$('a#next-page').click(function () {
		
		// Fetch active
		active = $('#thumbs ul li.active');
		
		// Calculate next
		next = $('#thumbs ul li').index(active) + 4;
		
		// Check for offset
		if (next >= $('#thumbs ul li').length)
			next = 0;
			
		// Set image
		var next_image = $('#thumbs ul li').get(next);
				
		// Call click
		$('a', next_image).click();
	});
	
	$('a#previous-page').click(function () {
			
		// Remove active
		active = $('#thumbs ul li.active');
		
		// Calculate previous
		previous = $('#thumbs ul li').index(active) - 4;
		
		// Check for offset
		if (previous <= -1) {
			
			// Calculate offset
			previous = $('#thumbs ul li').length % 4;
			previous = $('#thumbs ul li').length - previous;
		}
			
			
		// Set image
		var previous_image = $('#thumbs ul li').get(previous);
				
		// Call click
		$('a', previous_image).click();
	});
	
	$('input.sort').change(function () {
		
		// Check for active
		if (this.checked == true) {
			
			// Uncheck all
			$('input.sort').each (function () { this.checked = false; });
			
			// Check
			this.checked = true;
		}
	});
	
	// Check if catalogue checkbox exists
	if ($('#catalogue').length > 0) {
		
		if ($('#catalogue').is(':checked')) {
			
			setRequiredFields (true);
		}
		
		$('#catalogue').click( function () {
			
			setRequiredFields (this.checked);
		});
	}
})

// Update required fields in contactform based on catalogue checkbox
function setRequiredFields (catalogueValue) {
	
	if (catalogueValue === true){
		
		$('#address').html('Straat *');
		$('#hnumber').html('Huisnummer *');
		$('#postal').html('Postcode *');
		$('#city').html('Plaats *');
		
	} else if (catalogueValue === false) {
		
		$('#address').html('Straat');
		$('#hnumber').html('Huisnummer');
		$('#postal').html('Postcode');
		$('#city').html('Plaats');
	}
}