$(document).ready(function(){
	$('.submitReview').click(function() {
		if($("#reviewform").valid()){
			posturl = '/content/ajax/reviews.post.php';
			$.post(posturl, $("#reviewform").serialize(), function(data) {
				$('#review-left').html(data);
			});
		}
		return false;
	});
	
	$('#rateProduct').click(function() {
		toggleReview();
	});	
	
	$('#ShopCheckout #username, #createAccount-container').change(function() {
		$("#dummyusername-container #dummyusername").val($('#ShopCheckout #username').val());
	});	
	
	$('#review-left h2').click(function(){
		toggleReview();
	});
	
	if(!($.browser.msie && parseInt($.browser.version, 10) == 7)) {
		$('#rating .fbRadioButtons').allRating({
			theme: 'tinystar',
			input:'radio',
			showHover: false,
			onClickEvent: function() {
			}
		});
	}  
	
	function toggleReview(){
		$('#reviewFolded').animate({
			height: 'toggle'
		}, 300, function() {
			// Animation complete.
			});
		$('#review-left').animate({
			height: 'toggle'
		}, 300, function() {
			// Animation complete.
			});
	}
	
	$(".changeDeliveryAddressFull").fancybox({
		'width'	: 900,
		'height': 430,
		'autoDimensions': false,
		'onComplete' : applyCufon,
		ajax	: {
			type	: "POST", 
			data	: $("#ShopCheckout").serialize()
		}
	});
	
	$(".changeDeliveryAddressLite").fancybox({
		'width'	: 550,
		'height': 430,
		'autoDimensions': false,
		'onComplete' : applyCufon,
		ajax	: {
			type	: "POST", 
			data	: $("#ShopCheckout").serialize()
		}
	});	
	
	$("#addressCancel").live('click', function(){
		$("#fancybox-close").click();
	});

	// TOOLTIP
	$("#customerService a.label").tooltip({
		effect: 'slide',
		position: 'top center',
		offset: [20,0],
		predelay: 300
	});

	if ($.browser.msie) {
		$(".tooltipTrigger").tooltip({
			effect: 'slide',
			position: 'top left',
			offset: [-176,-55],
			predelay: 300
		});
	}else if ($.browser.webkit) {
		$(".tooltipTrigger").tooltip({
    			effect: 'slide',
			position: 'top center',
			offset: [-176,-209],
			predelay: 300
		});
	}else{
		$(".tooltipTrigger").tooltip({
			effect: 'slide',
			position: 'top center',
			offset: [-176,-135],
			predelay: 300
		});
	}

	// CUFON
	Cufon.replace('.cufon, #navigation > ul > li > a, h1, h2, #sitemap > li > a , .prodDetail h2 a, .prodDetailPage h3, #sidebarnav, #highlight h3, #topProducts .category',{
		hover: true,
		fontFamily: 'Museo'
	});
	function applyCufon(){
		Cufon.replace('.cufon, #navigation > ul > li > a, h1, h2, #sitemap > li > a , .prodDetail h2 a, .prodDetailPage h3, #sidebarnav, #highlight h3, #topProducts .category',{
			hover: true,
			fontFamily: 'Museo'
		});
	}

	// PRODUCT SLIDER
	jQuery('#slider').jcarousel({
		scroll: 5,
		animation: 'slow'
	});

	// TABS
	$(function() {
		$("ul.tabs").tabs("div.tabContent > div", {
			effect: 'fade',
			fx: {
				opacity: 'toggle'
			}
		});
	});

	$('#sort form select').change(function(action) {
		window.location.href = $('#sort form select :selected').val();
	//$('#sort form').submit();
	});

	/*$('#subcategorySelector select').change(function(action) {
		if($('#subcategorySelector select :selected').val() != '-1'){
			window.location.href = $('#subcategorySelector select :selected').val();
		}
	//   $('#subcategorySelector').submit();
	});*/

	$('#customPriceRange').submit(function(action) {
		url = $('#customPriceRange input.url').val();
		url = url.replace("{MINP}", $('#customPriceRange input.minPrice').val());
		url = url.replace("{MAXP}", $('#customPriceRange input.maxPrice').val());
		window.location.href = url;
		return false;
	});

	$(".cadeauverpakking .shoppingcartWrapFld").change(function(){
		updateShoppingcart();
	});

	$(".aantal .shoppingcartAmountFld").bind('change keyup', function(e){
		delay(function(){
			if(!isNumber($(e.currentTarget).val())){
				$(e.currentTarget).addClass('error');
			}else{
				$(e.currentTarget).removeClass('error');
				updateShoppingcart();
			}
		}, 600 );
	});
	
	function isNumber(n) {
		return !isNaN(parseFloat(n)) && isFinite(n);
	}

	$('#messageCheckbox').change(function(){
		if($('#messageCheckbox').attr('checked')){
			$("#messageOption #message").attr('disabled', false);
		}else{
			$("#messageOption #message").attr('disabled', true);
		}
		updateShoppingcart();
	});
	
	$("#messageOption #message").keyup(function(){
		delay(function(){
			updateShoppingcart();
		}, 600 );
	});

	var delay = (function(){
		var timer = 0;
		return function(callback, ms){
			clearTimeout (timer);
			timer = setTimeout(callback, ms);
		};
	})();

	function formatCurrency(num) {
		num = isNaN(num) || num === '' || num === null ? 0.00 : num;
		num = parseFloat(num).toFixed(2)
		num = num.replace('.', ',');
		return num;
	}

	function updateShoppingcart(){
		$.ajax({
			type: 'POST',
			url: '/content/ajax/shoppingcart.priceupdate.php',
			data: $("#shoppingCartForm").serialize(),
			dataType: 'json',
			success: function(data){
				$("#shpUpdTotal").html("&euro;&nbsp;" + formatCurrency(data.total));
				$("#shpUpdSubtotal").html("&euro;&nbsp;" + formatCurrency(data.subtotal));
				$("#shpUpdShipping").html("&euro;&nbsp;" + formatCurrency(data.shipping));
				
				if(data.message == false){
					$("#shpUpdMessage").hide();
				}else{
					$("#shpUpdMessage").show();
				}
				
				jQuery.each(data.products, function(i, val) {
					$(".product" + i + " .totaal span").html("&euro;&nbsp;" + formatCurrency(val.totalprice));
					if(val.amount > 1){
						$(".product" + i + " .wrapping-text").html(val.amount + " x &euro;&nbsp;" + formatCurrency("0.50"));
					}else{
						$(".product" + i + " .wrapping-text").html("&euro;&nbsp;" +formatCurrency("0.50"));
					}
				});
			}
		});	
	}

	/** Checkout*/
	$("#couponSubmit").click(function(){
		$("#ShopCheckout").submit();
	});
	
	$('#coupon_code').keypress(function(e){
		if(e.which == 13){
			$("#ShopCheckout").submit();
			e.preventDefault();
		}
	});

	
	$('#payment_method').change(function(){
		$("#ShopCheckout").valid();
	});



	$('#slideshow-1, #slideshow-2, #slideshow-3').cycle({
		fx: 'fade',
		timeout: 10000
	});



/*
	
	// set timeout
	var tid = setTimeout(mycode, 1000);
	
	var images = $('#highlight .colLeft figure');
	var thumbs = $('#highlight .colRight figure');
	var parent = $('#highlight .colRight');
	var thumbsRepo = thumbs;
	var index = 1;
	
	function mycode() {
		if(index == 3){
			index = 0;
		}
		$(images[index-1]).hide();
		$(images[index]).show();

		//$(thumbs[index]).parent().remove();
		//$(parent).append(thumbsRepo[index-1]);


		index++;
		// do some stuff...
		tid = setTimeout(mycode, 6000); // repeat myself
	}
	
	
	function abortTimer() { // to be called when you want to stop the timer
		clearTimeout(tid);
	}

	*/

	
});
