
// LightBox interface
$('a.lightbox').lightBox();

// Thickbox pages:
$('a.popup-info').each(function() {
	// Prepare the new URL:
	var u = $(this).attr('href');
	u = u.substr(0, u.lastIndexOf('/')) + '/info/detail-ajax' + u.substr(u.lastIndexOf('/'), u.length);
	
	// Size of window:
	var w = $(window).width() - 300;
	var h = $(window).height() - 250;
	
	if(w > 500) {
		w = 500;
	}
	
	if(h > 500) {
		h = 500;
	}
	
	// Set the new URL, and prepare for thickbox:
	$(this).attr('href', u + '?keepThis=true&TB_iframe=true&height='+ h +'&width=' + w);
});

// Initiate the thickbox:
tb_init('a.popup-info');

// Define Animation Easing Equations:
// Exponential easing in/out - accelerating until halfway, then decelerating
jQuery.easing['M_InOutExponential'] = function(p, t, b, c, d) {
	if(t==0) {
		return b;
	}
	if (t==d) {
		return b+c;
	}
	if ((t/=d/2) < 1) {
		return c/2 * Math.pow(2, 10 * (t - 1)) + b;
	}
	return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
};

// jCarousel
$('.jCarousel').hide().children().each(function() {
	$(this).removeClass('hidden');
});

// Additional interaction with jCarousel:
function carouselInitCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});
	
	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

$('.jCarousel').fadeIn(400).jcarousel({
    // Scroll every x seconds
    auto: 5,
    // Make the carousel wrap around the selection of products
    wrap: 'last',
    // Do some additional interaction:
    initCallback: carouselInitCallback,
    // Animation speed:
    animation : 400,
    // Animation Easing effect:
    easing : "M_InOutExponential"
});


// ----- Thumbnails

// Fade in thumbnail images, when loaded
$('.product-list a.image, #product-list-source a.img').children('img').each(function() {
	// Hide the image
	$(this).hide().stop();

	// Create the image resource
	var $t  = $(this);
	var img = new Image();
	img.onload = function() {
		$t.fadeIn();
	}

	// Load the image
	img.src = $(this).attr('src');
});


// ----- Order form

// Add ucfirst to String Prototype:
String.prototype.ucFirst = function () {
	return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
};

// When changed country in shipping address
$('#country').change(function() {
	// If the client is NOT picking up the order himself/herself
	if(! $('#donotship').attr('checked')) {
		// If the country is left empty:
		if($('#country').val() == '') {
			$('#price-transport').html('-');
			$('#price-total').html($('#price-subtotal').html());
		}
		// If country not empty, update transport costs via AJAX
		$.ajax({
			url : JQueryBaseHrefWithPrefix + '/winkelmandje/transportCostAjax',
			data : {
				country : $('#country').val()
			},
			dataType : 'xml',
			success : function(data) {
				$('#price-transport').html($('transport', data).text());
				$('#price-total').html($('total', data).text());
			}
		});
	}
	// If the client is picking up the order himself/herself
	else {
		$('#price-transport').html('zelf ophalen');
		$('#price-total').html($('#price-subtotal').html());
	}
});

// Also react on check/uncheck of picking up order:
$('#donotship').click(function() {
	$('#country').change();
});

// Log changes in billing address:
$('#billing-address-wrapper input, #billing-address-wrapper select').each(function() {
	if($(this).val() == '') {
		$(this).change(function() {
			$(this).data('changeValue', 1);
		});
	} else {
		$(this).data('changeValue', 1);
	}
});

// Copy the billing address from shipping address, by default:
$('#shipping-address-wrapper input, #shipping-address-wrapper select').change(function() {
	// When a new value is introduced in the shipping address, we look up the
	// corresponding billing-address field:
	var $f = $('#billing' + $(this).attr('id').ucFirst());

	// If the corresponding field has been found:
	if($f.length == 1) {
		// We make sure to update only if no other value has been inserted already:
		if(! $f.data('changeValue')) {
			// Copy the value:
			$f.val($(this).val());
		}
	}
});
