$(function(){
	
	Cufon.replace('h2.script', {
		fontFamily: 'bickham',
		textShadow: '#826e6f 0px 1px 5px, #fce6e6 0px -1px'
	});
	Cufon.replace('h2.normal, h3',{
		fontFamily: 'futura'
	});
	// Cufon.replace('h2', { fontFamily: 'Bickham Script Pro' });
	// Cufon.replace('h2.normal', { fontFamily: 'Futura Book Normal' });
	
	/* Add 'focus' class to input wrappers
	----------------------------------------------*/
	$('input, textarea').focus(function(){
		$(this).parent().addClass('focus');
	});
	$('input, textarea').blur(function(){
		$(this).parent().removeClass('focus');
	});
	/* !end */
	
	
	$('#navigation li:last-child').addClass('last-child');
	$('#navigation li:first-child').addClass('first-child');
	$('#menu-footer li:last-child').addClass('last-child');
	$('table#cart-list td:first-child, table#cart-list th:first-child').addClass('first-child');
	
	/* Clear the search input box
	----------------------------------------------*/
	var search_initial = $('form#search input#searchterm').val();
	$('form#search input#searchterm').focus(function(){
		if( $(this).val() == search_initial ){
			$(this).val('');
		}
	});
	$('form#search input#searchterm').blur(function(){
		if( $(this).val() == '' ){
			$(this).val(search_initial);
		}
	});
	/* !end */
	
	var productOptions = false;
	var productIdSelect = false;
	$('#product #productId').each(function(){
		productSelectClone = this.cloneNode(true);
		productOptions = productSelectClone.getElementsByTagName("option");
		productIdSelect = this;
	});
	$("#colours li a").click(function(){
		// update the selected color
		$("#colours li.selected").removeClass("selected");
		$(this).parent().addClass("selected");

		// remove all options from the select, then append any that match class names
		while (productIdSelect.options.length) { productIdSelect.remove(0); }
		var pattern2 = new RegExp("( |^)(" + $(this).attr('class') + ")( |$)");
		for (var i = 0; i < productOptions.length; i++) {
			if (productOptions[i].className.match(pattern2)) {
				productIdSelect.appendChild(productOptions[i].cloneNode(true));
			}
		}
		productIdSelect.selectedIndex = 0;
		
		// update the selected color htmnl
		var colourDesc = $(this).parent().find("span").html();
		$("#colour-status").html("Your selected colour is "+colourDesc);
		return false;
	});
	$("#colours li.selected a:first-child").click();
	
	$('#print-page').click(function(){
		window.print();
		return false;
	});
	
	/* Sigh...
	----------------------------------------------*/
	$('.cart-checkout-login label[for=user_email]').html('Please provide your email address').addClass('user-email');
	$('.cart-shipping-estimate form').prepend('<p>Please select your delivery region.</p>');
	$('#cart-list .cart-item-code').prepend('Style number ');
	$('#different_billing_address').parent().addClass('different_billing_address');
	$('#checkout-payment-ccform').prepend('<h4>Credit Card</h4>');
	$('#checkout-payment-details p:first label:first').text('Please select one of the following payment options.');
	$('#member-delivery-details').append('<div class="clear">&nbsp;</div>');
	$('#member-delivery-details').prepend('<h3>Order Details</h3>');
	/*!end */
	
	//Hides the card type field
	$('#checkout-payment-ccform p strong').parent().remove(); 
	$('#checkout-payment-ccform h4').after('<input type="hidden" value="" name="cc_type" id="cc_type"><div id="cc-type"><span>Please select your credit card type.</span><input name="type" type="radio" value="1" /> <input name="type" type="radio" value="2" /><input name="type" type="radio" value="3" /></div>');
	
	
	// uncomment this when finalising zoom tool
	
	// Product Page - Image Scrolling
	var containerHeight = $('#image-zoomer').height();
	var containerWidth = $('#image-zoomer').width();
	
	var imageHeight = $('#originalImageHeight').val();
	var imageWidth = $('#originalImageWidth').val();


	$('#image-zoomer img').hide();

	$('#image-zoomer img').each(function(i){
		
		$(this).css('height', 'auto');
		$(this).css('width', 'auto');
		if (imageHeight >= imageWidth) {
			thumbHeight = containerHeight;
			thumbWidth = Math.round(containerWidth * (imageWidth/imageHeight));
		} else {
			thumbHeight = Math.round(containerHeight * (imageHeight/imageWidth));
			thumbWidth = containerWidth;
		}
		
		$(this).css('height', containerHeight+'px').fadeIn(250);
		
		$(this).css('top', '0px');
		$(this).css('left', '0px');

	});
	
	$('#image-zoomer img').click(function(e) {

		if ($(this).css('height') == imageHeight+'px') {
			$(this).animate({height: thumbHeight+'px', top: 0, left: 0}, 150);
			$('#zoom-text').html('Click image to zoom in');
		} else if ($(this).css('height') == thumbHeight+'px') {
			offset = $('#image-zoomer').offset();
			mouseX = (e.pageX - offset.left);
			mouseY = (e.pageY - offset.top);
	
			if (imageHeight >= containerHeight) {
				imageTop = (imageHeight - containerHeight) / 100 * (100 / containerHeight * mouseY) * -1;
			} else {
				imageTop = (containerHeight/2)-(imageHeight/2);
			}
			if (imageWidth >= containerWidth) {
				imageLeft = (imageWidth - containerWidth) / 100 * (100 / containerWidth * mouseX) * -1;
			} else {
				imageLeft = (containerWidth / 2) - (imageWidth / 2);
			}
	
			$(this).animate({
				height: imageHeight+'px', 
				top: imageTop + 'px',	
				left: imageLeft + 'px'
			}, 150);
			$('#zoom-text').html('Click image to zoom out');
		}

	});
	$('#image-zoomer img').mousemove(function(e) {
		
		offset = $('#image-zoomer').offset();
		mouseX = (e.pageX - offset.left);
		mouseY = (e.pageY - offset.top);
		
		
		if (imageHeight >= containerHeight) {
			imageTop = (imageHeight - containerHeight) / 100 * (100 / containerHeight * mouseY) * -1;
		} else {
			imageTop = (containerHeight/2)-(imageHeight/2);
		}
		if (imageWidth >= containerWidth) {
			imageLeft = (imageWidth - containerWidth) / 100 * (100 / containerWidth * mouseX) * -1;
		} else {
			imageLeft = (containerWidth / 2) - (imageWidth / 2);
		}
		
		if ($('#image-zoomer img').css('height') == imageHeight+'px') {

			$('#image-zoomer img').css('top', (imageTop) + 'px');
			$('#image-zoomer img').css('left', (imageLeft) + 'px');
		}
	});
	$('.photos a').click(function() {
		$('#image-zoomer img').animate({height: thumbHeight+'px', top: 0, left: 0}, 225);
		$('#image-zoomer img').attr('src', $(this).attr('href'));
		return false;
	});

	$("#payment_type_paypal").click(function(){
	
		$.scrollTo("#paypal-container");
		
	});
});

