/**
 * Bootstrap.js - JavaScript bootstrapper
 * 
 * @author Webstores <info at webstores dot nl>
 *         Copyright (c) Webstores internet totaalbureau <http://www.webstores.nl/>
 */
$(document).ready(function() {

	// Validation
	$('form').each(function(i, el) {
		$(el).validate();
	});
	
	// External links
	$('a[rel="external"]').each(function() {
		this.target = '_blank'
	});
	
	// Toggle input values
	if(typeof $('<input>').attr('placeholder') == 'undefined') {
		$('[placeholder]').focus(function() {
			var input = $(this);
			
			if(input.val() == input.attr('placeholder')) {
				input.val('');
			}
			
			input.addClass('focus');
		}).blur(function() {
			var input = $(this);
			
			if(input.val() == '') {
				input.removeClass('focus');
				input.val(input.attr('placeholder'));
			}
		}).blur().parents('form').submit(function() {
			$(this).find('[placeholder]').each(function(i, el) {
				if(el.val() == el.attr('placeholder')) {
					el.val('');
				}
			});
		});
	}
	
	// IE6 hover
	if(/msie 6/i.test(navigator.userAgent)) {
		$('#navigation li').each(function() {
			$(this).hover(function() {
				$(this).addClass('hover');
			},
			function () {
				$(this).removeClass('hover');
			});
		});
	}
	
	//Cufon navigation hover fix
	$('#navigation > li > ul').mouseleave(function() {
		setTimeout(function() {
			Cufon.replace('#navigation > li:not(li.selected) > a', { fontWeight: 600, hover: true });
		}, 10);
	});
	
	//Carousel	
	if($('.carousel #carousel-items').length) {
		if($('#assortiment-carousel').length) {
			var carouselInterval = 0;
		} else {
			var carouselInterval = 5;
		}
		$('.carousel #carousel-items').jcarousel({
			scroll: 1,
			animation: 'slow',
			auto: carouselInterval,
			wrap: 'both',
			buttonNextHTML: null,
			buttonPrevHTML: null,
			initCallback: function(carousel) {
				$('#carousel-next').bind('click', function(e) {
				    carousel.next();
				    e.preventDefault();
				});

				$('#carousel-prev').bind('click', function(e) {
				    carousel.prev();
				    e.preventDefault();
				});
			
				$('#carousel-controls li').each(function(i) {
					$(this).bind('click', function(e) {
						/*
						e.preventDefault();
						carousel.stopAuto();
						carousel.scroll(i + 1);
						carousel.startAuto();
						*/
					});
				});
			
				carousel.clip.hover(
					function() {
						carousel.stopAuto();
					},
					function() {
						carousel.startAuto();
					}
				);
			},
			itemVisibleInCallback: function(carousel, slide, index, state) {
				var slideContent = $(slide).children().first();
				$("#carousel-caption").text(slideContent.attr('alt'));
				Cufon.replace('#carousel-caption', { 
					fontFamily: 'DIN-Engschrift',
					fontWeight: 400,
					hover: false
				});

				$('#carousel-controls li:nth-child(' + index + ')').addClass('selected');
			},
			itemVisibleOutCallback:function(carousel, slide, index, state) {
				$('#carousel-controls li:nth-child(' + index + ')').removeClass('selected');
			}
		});
	}
	
	// Product quantity
	$('.quantity-controls .quantity-increase').click(function(e) {
			e.preventDefault();
			$(e.currentTarget.hash).val(parseInt($(e.currentTarget.hash).val()) + 1);
			var input = $(e.currentTarget.hash);
			setQuantityTimeout(input);
	});

	$('.quantity-controls .quantity-decrease').click(function(e) {
			e.preventDefault();
			if(parseInt($(e.currentTarget.hash).val()) > 1) {
					$(e.currentTarget.hash).val(parseInt($(e.currentTarget.hash).val()) - 1);
					var input = $(e.currentTarget.hash);
					setQuantityTimeout(input);
			}
	});

	$('.quantity').keyup(function(e) {
			e.preventDefault();
			var input = $(e.currentTarget);
			if(parseInt($(input).val()) < 1) {
					$(input).val(1);
			}
			setQuantityTimeout(input);
	});

	var setQuantityTimeout = function(input) {
		var self = this;
		self.input = input;
		clearTimeout(self.quantityTimeout);
		self.quantityTimeout = setTimeout(function() {
			if($(self.input).val() == '') {
				$(self.input).val(1);
			}
			quantityChange(parseInt($(self.input).attr('id').replace('quantity-', '')), parseInt($(self.input).val()));
		}, 300);
	}

	var quantityChange = function(id, quantity) {
		//response = {product_total: '709,35', total: '1.156,06'};
		$.getJSON('/products/getprice/'+id+'/'+quantity, function(data) {
			$('#total-'+id).html("Totaal : &euro; "+data.total);
		});
	}
});

// Shadowbox
Shadowbox.init({
	overlayOpacity: 0.8,
	troubleElements: ['select']
});

//Cufon regular
Cufon.replace('.spotlight-content .sub-title, .sub-title-home', { 
	fontFamily: 'eurostile',
	fontWeight: 600,
	hover: false
});

//Cufon regular hover
Cufon.replace('#navigation > li > a, #clubkas .sub-title', { 
	fontFamily: 'eurostile',
	fontWeight: 600,
	hover: true
});

//cufon bold
Cufon.replace('.button .button-inner, .spotlight-content .page-title, #content-title, #clubs-title, #clubkas .character', { 
	fontFamily: 'eurostile',
	fontWeight: 700,
	hover: false
});

//cufon bold hover
Cufon.replace('#clubkas .title-wrapper', { 
	fontFamily: 'eurostile',
	fontWeight: 700,
	hover: true
});


