$(document).ready( function() {
	
	// Clearing up form fields sample values:
	$(function() {
		var searchSuggestion = 'Trefwoord of bestelnummer';
		var newsletterSuggestion = 'Vul je e-mailadres in';
		$('#txtSearch').focus( function() {
			if (this.value == searchSuggestion) {
				this.value = '';
			}
		});
		$('#txtSearch').blur( function() {
			if (this.value == '') {
				this.value = searchSuggestion;
			}
		});
		$('#nb_email').focus( function() {
			if (this.value == newsletterSuggestion) {
				this.value = '';
			}
		});
		$('#nb_email').blur( function() {
			if (this.value == '') {
				this.value = newsletterSuggestion;
			}
		});
	});
	
	// Hide/show newsletter form:
	$(function () {
		$('#newsletter #nb-abort').hide();
		$('#switch-nb-abort').click( function() {
			$('#newsletter #nb-abort').slideToggle('slow');
			return false;
		});
	});
	
	// Hide/show shopping cart:
	$(function () {
		$('#minicart #cart-content').hide();
		$('#switch-cart').click( function() {
			$('#minicart #cart-content').slideToggle('slow');
			return false;
		});
		$('#minicart #cart-content a').click( function() {
			$('#minicart #cart-content').slideUp('slow');
		});
	});
	
	// Homepage: How-to jcarousel:
	$(function () {
		$('#howto-slideshow').jcarousel({
			scroll: 1,
			auto: 3,
			animation: 1200,
			wrap: "circular"
		});
	});
	
	// Login from secondary menu:
	$(function () {
		$('#trigger-login').click( function() {
			$('#pop-ups').fadeIn('slow');
			return false;
		});
		$('#pop-ups .closer').click( function() {
			$('#pop-ups').fadeOut('slow');
			return false;
		});
	});
	
	// BOF Mattia
	
	// Submit order from product lists:
	$(function () {
		if ($('form.cart_quantity').length > 0) {
			$('form.cart_quantity').each( function() {
				var form = $(this);
				var submitter = $('a.cta', this);
				submitter.click( function() {
					form.submit();
					return false;
				});
			});
		}
	});
	
	// EOF Mattia
	// Style-switch for search results/product lists:
	$(function () {
		if ($('#plvm-list').length > 0) {
			$('#plvm-list').click(function () {
				var listState = $('#product-list').attr('class');
				var switchState = $('#plvm-list').attr('class');
				if (listState != 'list deals') {
					$('#product-list').addClass('list');
					if (switchState != 'selected') {
						$('#plvm-list').addClass('selected');
						$('#plvm-tiles').removeClass('selected');
					}
				}
				// Evt. cookie plaatsen...
				return false;
			});
		}
		if ($('#plvm-tiles').length > 0) {
			$('#plvm-tiles').click(function () {
				var listState = $('#product-list').attr('class');
				var switchState = $('#plvm-tiles').attr('class');
				if (listState == 'list deals' || listState == 'deals list') {
					$('#product-list').removeClass('list');
					if (switchState != 'selected') {
						$('#plvm-tiles').addClass('selected');
						$('#plvm-list').removeClass('selected');
					}
				}
				// Evt. cookie plaatsen...
				return false;
			});	
		}	
	});
		
	// Extended search minimum and maximum price:
	$(function() {
		if ($('#slider-range').length > 0) {
			$( "#slider-range" ).slider({
				range: true,
				min: 1,
				max: 4500,
				values: [ 275, 2500 ],
				slide: function( event, ui ) {
				
					// Set values when sliding the slider:
					$( "#amount" ).html( "&euro;" + ui.values[ 0 ] + " t/m &euro;" + ui.values[ 1 ] );
					$('#se-pfrom').val(ui.values[ 0 ]);
					$('#se-pto').val(ui.values[ 1 ]);
				}
			});
			// Set initial values (dit evt. aanpassen voor zoekresultaten-pagina. Waarden worden namelijk standaard gereset op elke pagina...):
			$( "#amount" ).html( "&euro;" + $( "#slider-range" ).slider( "values", 0 ) + " t/m &euro;" + $( "#slider-range" ).slider( "values", 1 ) );
		}
	});
	
	// Product information tabs:
	if ($('.nav-tabs').length > 0) {
		$('.nav-tabs li').click( function() {
		
			var tabTarget = $("a", this).attr("href");
			tabTarget = tabTarget.substr( 1 );
		
			$('.nav-tabs li').each( function() {
				if ( $(this).hasClass("selected")) {
					$(this).removeClass("selected");
				}
			});
		
			if ( ! $(this).hasClass("selected")) {
				$(this).addClass("selected");
			}
		
			$('.tab').hide();
			$('#' + tabTarget + '.tab').show();
		
			if ( ! $('#' + tabTarget + '.tab').hasClass("selected")) {
				$('#' + tabTarget + '.tab').addClass("selected");
			}
			return false;
		});
	
		// Clicking the first product information tab (Overzicht):
		$('.nav-tabs li:first-child').click( function() {
			$(".tab").each( function() {
				$(this).show();
			})
			return false;
		});
	}
	
	// Setting up FAQs:
	$(function() {
		if ($('ol.faq').length > 0) {
			$("ol.faq li.item").each( function() {
				var theAnswer = $("div", this);
				theAnswer.hide();
				$("h2:first-child", this).click( function() {
					$(this).toggleClass("active");
					theAnswer.slideToggle("slow");
				});
			});
			var hash = window.location.hash;
			if (hash.length > 0) {
				$('li'+hash+' div').show();
			}
		}
	});
	
	// Masonry on the Sitemap:
	$(function(){
		if ($('#intro.sitemap-container').length > 0) {
			$('#intro.sitemap-container').masonry({
				itemSelector : '.sitemap'
			});
		}
	});
	
});

// Seperate functions:
function resetProductTabs() {
	$(".tab").each( function() {
		$(this).show();
	})
}
