var headline_count;
var headline_interval;
var old_headline = 0;
var current_headline=0;
var headlines = new Array(); // an array of jQuery objects

var startProduct;
var prevProduct = 0;
var nextProduct = 0;

var prevPosition;
var startPosition;
var nextPosition;

var rightPosition;

var returnFalse = 0;

ua = navigator.userAgent;
isMSIE = (navigator.appName == "Microsoft Internet Explorer");
isMSIE5 = this.isMSIE && (ua.indexOf('MSIE 5') != -1);
isMSIE5_0 = this.isMSIE && (ua.indexOf('MSIE 5.0') != -1);
isMSIE7 = this.isMSIE && (ua.indexOf('MSIE 7') != -1);
isGecko = ua.indexOf('Gecko') != -1;
isSafari = ua.indexOf('Safari') != -1;

function parseFloatX( obj ) {
	try
	{
		return parseFloat( obj.replace( /[^0-9\.]/g, '' ) );
	}
	catch (ex)
	{
	}
}

function null_promotions() {
	$('#checkout-promotional-code h4').hide();
	$('.shipping-discount').hide();
	$('#promotion_discount').val("0");
	$('#promotion_id').val("");
}

$(document).ready( function(){
	null_promotions();
});

$(window).resize(function(){
	// LOOPS PATCH..
	var haveLoop = $('#header .current').size();
	
	if(haveLoop > 0) {
		var loopPosition = $('#header .current').position();
		var loopClass = $('#header .current').attr('name');

		if(loopClass == 'loop-index') { loopPosition['top'] = (loopPosition['top'] - 20); loopPosition['left'] = (loopPosition['left'] - 40); }
		if(loopClass == 'loop-why-rude-health') { loopPosition['top'] = (loopPosition['top'] - 25); loopPosition['left'] = (loopPosition['left'] - 8); }
		if(loopClass == 'loop-our-products') { loopPosition['top'] = (loopPosition['top'] - 21); loopPosition['left'] = (loopPosition['left'] - 11); }
		if(loopClass == 'loop-whats-going-on') { loopPosition['top'] = (loopPosition['top'] - 15); loopPosition['left'] = (loopPosition['left'] - 15); }
		if(loopClass == 'loop-out-and-about') { loopPosition['top'] = (loopPosition['top'] - 20); loopPosition['left'] = (loopPosition['left'] - 20); }
		if(loopClass == 'loop-checkout') { loopPosition['top'] = (loopPosition['top'] - 20); loopPosition['left'] = (loopPosition['left'] - 35); }
		if(loopClass == 'loop-buy') { loopPosition['top'] = (loopPosition['top'] - 10); loopPosition['left'] = (loopPosition['left'] - 15); }

		$('.'+loopClass).css({'top': loopPosition['top'], 'left': loopPosition['left']});
		$('.'+loopClass).show();
	}
	// END LOOPS PATCH..
	
	var allImages = $('#all-images img').size();
	
	if(allImages > 0) {
		prevPosition = $('#prev-product').position();
		startPosition = $('#current-product').position();
		nextPosition = $('#next-product').position();
		rightPosition = $('.right').position();

		$('#all-images img').css({'top': startPosition['top'], 'left': startPosition['left']});

		if(allImages > 2)
			$('.image-'+prevProduct).css({'top': prevPosition['top'] + 50, 'left': prevPosition['left']});

		$('.image-'+startPosition).css({'top': startPosition['top'], 'left': startPosition['left']});
		$('.image-'+nextProduct).css({'top': nextPosition['top'] + 50, 'left': nextPosition['left']});

		$('.fullhidden').css({'top': rightPosition['top'], 'left': rightPosition['left']});
		$('.fullhidden-shadow').css({'opacity': 0.8, 'top': rightPosition['top'], 'left': rightPosition['left']});
	}

	var featuredImages = $('#featured-images img').size();

	if(featuredImages > 0) {
		prevPosition = $('#prev-featured').position();
		startPosition = $('#current-featured').position();
		nextPosition = $('#next-featured').position();

		startPosition['left'] = startPosition['left'] - 20;
		nextPosition['left'] = nextPosition['left'] - 10;

		$('#featured-images img').css({'top': startPosition['top'], 'left': startPosition['left']});

		if(featuredImages > 2)
			$('.image-'+prevProduct).css({'top': prevPosition['top'] + 50, 'left': prevPosition['left']});

		$('.image-'+startPosition).css({'top': startPosition['top'], 'left': startPosition['left']});
		$('.image-'+nextProduct).css({'top': nextPosition['top'] + 50, 'left': nextPosition['left']});
	}
});

$(document).ready(function(){
	$('a.lightbox').lightBox();
	
	headline_count = $("dd.headline").size();

	if(headline_count > 0) {
		for (var i = 0; i < headline_count; i++) {
			headlines[i] = $("dd.headline:eq("+i+")");
		}

		headlines[current_headline].show().animate({top: 5},"slow");

		headline_interval = setInterval(headline_rotate,7000);

		$('#scrollup').hover(function() {
			clearInterval(headline_interval);
		}, function() {
			headline_interval = setInterval(headline_rotate,7000);
		//headline_rotate();
		});
	}

	var allImages = $('#all-images img').size();
	
	if(allImages > 0) {
		nextProduct = ((startProduct + 1) < allImages) ? (startProduct + 1) : 0;
		//alert(startProduct);
		prevProduct = ((startProduct - 1) >= 0) ? (startProduct - 1) : (allImages - 1);
	
		prevPosition = $('#prev-product').position();
		startPosition = $('#current-product').position();
		nextPosition = $('#next-product').position();

		$('#all-images img').each( function(i) {
			$(this).addClass('image-'+i);

			//$(this).click( function() {
				//$('.fullhidden-shadow').fadeIn();
				//$('.'+$(this).attr('alt')).fadeIn();
			//});
		});
	
		$('#next').click( function() { 
			if(allImages > 2) {
				$('.image-'+prevProduct).css({'z-index': 5});
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 10});

				$('.image-'+nextProduct).animate( { 'width': '217', 'height': '403', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );
				$('.image-'+startProduct).animate( { 'width': '150', 'height': '280', 'top': prevPosition['top'] + 50, 'left': prevPosition['left'] }, 'slow' );
				$('.image-'+prevProduct).animate( { 'opacity': 0, 'width': '150', 'height': '280', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				prevProduct = startProduct;
				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (allImages - 1)) ? 0 : (startProduct + 1);

				$('.hidden').fadeOut();
				$('.info-'+startProduct).fadeIn();

				$('.image-'+nextProduct).css({'z-index': 5});
				$('.image-'+nextProduct).animate( { 'opacity': 1, 'width': '150', 'height': '280', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
			} else if(allImages == 2) {
				$('.image-'+startProduct).css({'z-index': 5});
				$('.image-'+nextProduct).css({'z-index': 10});

				$('.image-'+startProduct).animate( { 'width': '150', 'height': '280', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'width': '217', 'height': '403', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (allImages - 1)) ? 0 : (startProduct + 1);

				$('.hidden').fadeOut();
				$('.info-'+startProduct).fadeIn();
			}

			return false;
		});

		$('#prev').click( function() { 
			if(allImages > 2) {
				$('.image-'+prevProduct).css({'z-index': 10});
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 5});

				$('.image-'+prevProduct).animate( { 'width': '217', 'height': '403', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );
				$('.image-'+startProduct).animate( { 'opacity': 1, 'width': '150', 'height': '280', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'opacity': 0, 'width': '150', 'height': '280', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				nextProduct = startProduct;
				startProduct = prevProduct;
				prevProduct = ((startProduct - 1) >= 0) ? (startProduct - 1) : (allImages - 1);

				$('.hidden').fadeOut();
				$('.info-'+startProduct).fadeIn();

				$('.image-'+prevProduct).css({'z-index': 5});
				$('.image-'+prevProduct).animate( { 'opacity': 1, 'width': '150', 'height': '280', 'top': prevPosition['top'] + 50, 'left': prevPosition['left'] }, 'slow' );
			} else if(allImages == 2) {
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 5});

				$('.image-'+startProduct).animate( { 'width': '150', 'height': '280', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'width': '217', 'height': '403', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (allImages - 1)) ? 0 : (startProduct + 1);

				$('.hidden').fadeOut();
				$('.info-'+startProduct).fadeIn();
			}

			return false;
		});

		$('#all-images img').css({'opacity': 0, 'z-index': 1});
		$('#all-images img').css({'top': startPosition['top'], 'left': startPosition['left']});

		$('.image-'+nextProduct).css({'opacity': 1, 'top': nextPosition['top'] + 50, 'left': nextPosition['left']});
		$('.image-'+startProduct).attr({'width': '217', 'height': '403'});
		$('.image-'+nextProduct).attr({'width': '150', 'height': '280'});
		$('.image-'+prevProduct).attr({'width': '150', 'height': '280'});
		$('.image-'+startProduct).css({'opacity': 1, 'z-index': 10});

		if(allImages > 2)
			$('.image-'+prevProduct).css({'opacity': 1, 'width': '150', 'height': '280', 'top': prevPosition['top'] + 50, 'left': prevPosition['left']});

		if(allImages == 1) {
			$('#prev, #next').hide();

		$('.image-'+nextProduct).css({'opacity': 1, 'top': startPosition['top'], 'left': startPosition['left']});
		$('.image-'+nextProduct).attr({'width': '217', 'height': '403'});
		}

		$('.info-'+startProduct).show();

		$('.fullinfo').click( function() {
			$('.fullhidden-shadow').fadeIn();
			$('.'+$(this).get(0).name).fadeIn();

			return false;
		});

		$('.fullinfo-close').click( function() {
			$('.fullhidden').fadeOut();
			$('.fullhidden-shadow').fadeOut();

			return false;
		});

		$('#info').height($('#info').height() + $('#info .hidden').height());
	
		rightPosition = $('.right').position();

		$('.fullhidden').css({'top': rightPosition['top'], 'left': rightPosition['left']});
		$('.fullhidden-shadow').css({'opacity': 0.8, 'top': rightPosition['top'], 'left': rightPosition['left']});

		if(isSafari) {
			$('.fullhidden').height(($('.right').innerHeight() + 40 - 72));
			$('.fullhidden-shadow').height(($('.right').innerHeight() + 40 - 72));
		} else {
			$('.fullhidden').height(($('.right').innerHeight() - 72));
			$('.fullhidden-shadow').height(($('.right').innerHeight() - 72));
		}

		$('.buy-me').click( function() {
			var packageID = $(this).get(0).name;
			var quantity = $('.quantity-'+packageID).val();

			$.post(document.location.toString() , { action: 'add_to_basket', pack_id: packageID, quantity: quantity  }, function(data) {
				var intoText = (parseFloat(data) == 1) ? 'Item' : 'Items';
				var into = '<strong>'+ data +'</strong><br />'+intoText;

				$('#basket-items').html(into);

				$('#basket-items').fadeOut('fast', function() {
					$('#basket-items').fadeIn('fast');

					$('#basket-items').fadeOut('fast', function() {
						$('#basket-items').fadeIn('fast');

						$('#basket-items').fadeOut('fast', function() {
							$('#basket-items').fadeIn('fast');
						});
					});
				});

			} );

			return false;
		});

		$('.buy-me-inner').click( function() {
			var packageID = $(this).get(0).name;
			var quantity = $('.quantity-inner-'+packageID).val();

			$.post(document.location.toString() , { action: 'add_to_basket', pack_id: packageID, quantity: quantity }, function(data) { 
				var intoText = (parseFloat(data) == 1) ? 'Item' : 'Items';
				var into = '<strong>'+ data +'</strong><br />'+intoText;

				$('#basket-items').html(into);

				$('#basket-items').fadeOut('fast', function() {
					$('#basket-items').fadeIn('fast');

					$('#basket-items').fadeOut('fast', function() {
						$('#basket-items').fadeIn('fast');

						$('#basket-items').fadeOut('fast', function() {
							$('#basket-items').fadeIn('fast');
						});
					});
				});
			} );

			return false;
		});
	}
	
	var basketProducts = $('.checkout-box').size();
	if(basketProducts > 0) {
		Calculate.updateBasket();

		$('.location').html($('#billing_country').val());

		$('#billing_country').change( function() {
			$('.location').html($('#billing_country').val());

			Calculate.updateBasket();
		});

		$('.update').click( function() {
			var packageID = $(this).attr('alt');
			var quantity = $('.quantity-' + $(this).attr('alt')).val();

			$.post(document.location.toString() , { action: 'update_basket', pack_id: packageID, quantity: quantity }, function(data) { 
				Calculate.updateBasket();

				var intoText = (parseFloat(data) == 1) ? 'Item' : 'Items';
				var into = '<strong>'+ data +'</strong><br />'+intoText;

				$('#basket-items').html(into);

				$('#basket-items').fadeOut('fast', function() {
					$('#basket-items').fadeIn('fast');

					$('#basket-items').fadeOut('fast', function() {
						$('#basket-items').fadeIn('fast');

						$('#basket-items').fadeOut('fast', function() {
							$('#basket-items').fadeIn('fast');
						});
					});
				});
			});
		});

		$('#checkout-promotional-code .promo_update').click( function() {

			Calculate.updateBasket();
			
			var promotional_code = $('#promotional_code').val();

			var subtotal = parseFloatX( $('.shipping-subtotal').html() );

			if( promotional_code == "" ) {
				null_promotions();

				$('#checkout-promotional-code .quantity-box h4').html('');
				$('#checkout-promotional-code h4').hide();

				return;
			}

			$.post( document.location.toString(), { action : 'check_promotional_code', promotional_code : promotional_code, subtotal : subtotal }, function( data ) {
				
				eval( "var codes = " + data );


				switch( codes[0].type ) {
					// No promotion found
					case "0":
						null_promotions();

						$('#checkout-promotional-code .quantity-box h4').html( 'The code you entered is not correct, please try again or call 0845 4567833' );
						$('#checkout-promotional-code h4:eq(0)').hide();
						$('#checkout-promotional-code h4:eq(1)').show();
						break;

					// Postage
					case "-2":
						var postage = parseFloatX( $('.shipping-postage').html() );
						var total = parseFloatX( $('.shipping-total' ).html() );
						$('.shipping-postage').html( '&pound;0.00' );
						$('.shipping-total').html( '&pound;' + Transform.fixNum( total - postage ) );
						$('.shipping-discount').html( '&pound;-' + Transform.fixNum( postage ) );

						$('#promotion_discount').val( Transform.fixNum( postage ) );
						$('#promotion_type').val( codes[0].type );
						$('#promotion_id').val( codes[0].id );

						$('#checkout-promotional-code .quantity-box h4').html( codes[0].description );
						$('#checkout-promotional-code h4').show();

						//$('#shipping-totals h4:eq(1)').show();
						//$('.shipping-discount').show();
					break;

					// All products
					case "-1":
						var is_percent = parseFloat( codes[0].is_percent );
						var discount_percent = parseFloat( codes[0].discount );
						var subtotal = parseFloatX( $('.shipping-subtotal').html() );
						var total = parseFloatX( $('.shipping-total').html() );

						if(is_percent > 0) {
							var discount = ( discount_percent / 100 ) * subtotal;
						} else {
							var quantity = 0;
							$('input.quantity').each( function() {
								quantity += parseFloat($(this).val());
							});
							var discount = discount_percent * quantity;
						}

						//$('.shipping-subtotal').html( '&pound;' + Transform.fixNum( subtotal - discount ) );
						$('.shipping-total').html( '&pound;' + Transform.fixNum( total - discount ) );
						$('.shipping-discount').html( '&pound;-' + Transform.fixNum( discount ) );

						$('#promotion_discount').val( Transform.fixNum( discount ) );
						$('#promotion_type').val( codes[0].type );
						$('#promotion_id').val( codes[0].id );

						$('#checkout-promotional-code .quantity-box h4').html( codes[0].description );
						$('#checkout-promotional-code h4').show();

						$('#shipping-totals h4:eq(1)').show();
						$('.shipping-discount').show();
					break;

					default:
						var subtotal = parseFloatX( $('.shipping-subtotal').html() );
						var total = parseFloatX( $('.shipping-total').html() );
						var discount = 0;
						for( var i = 0; i < codes.length; i++ ) {

							var product_id = codes[i].product_id;

							if( $( '.price-' + product_id ).size() == 0 ) {
								continue;
							}

							var discount_percent = parseFloat( codes[i].discount );
							
							var product_price = parseFloatX( $('.price-' + product_id ).val() );
							var quantity = parseInt( $('.quantity-' + product_id ).val() );

							var price = quantity * product_price;

							if(parseFloat(codes[i].is_percent) > 0) {
								var d = ( discount_percent / 100 ) * price;
							} else {
								var d = quantity * discount_percent;
							}

							discount += d;
							
						}

						$('.shipping-subtotal').html( '&pound;' + Transform.fixNum( subtotal ) );
						$('.shipping-total').html( '&pound;' + Transform.fixNum( total - discount ) );
						$('.shipping-discount').html( '&pound;-' + Transform.fixNum( discount ) );

						$('#promotion_discount').val( Transform.fixNum( discount ) );
						$('#promotion_type').val( codes[0].type );
						$('#promotion_id').val( codes[0].id );

						$('#checkout-promotional-code .quantity-box h4').html( codes[0].description );
						$('#checkout-promotional-code h4').show();

						$('#shipping-totals h4:eq(1)').show();
						$('.shipping-discount').show();
				}
			});

		});

		$('.remove').click( function() {
			var packageID = $(this).attr('alt');

			$.post(document.location.toString() , { action: 'remove_from_basket', pack_id: packageID }, function(data) { 
				$('.box-'+packageID).remove();

				Calculate.updateBasket();

				var intoText = (parseFloat(data) == 1) ? 'Item' : 'Items';
				var into = '<strong>'+ data +'</strong><br />'+intoText;

				$('#basket-items').html(into);

				$('#basket-items').fadeOut('fast', function() {
					$('#basket-items').fadeIn('fast');

					$('#basket-items').fadeOut('fast', function() {
						$('#basket-items').fadeIn('fast');

						$('#basket-items').fadeOut('fast', function() {
							$('#basket-items').fadeIn('fast');
						});
					});
				});
			});
		});

		$('.place-order').click( function() {
			if(returnFalse > 0) {
				$('#global-error-box').fadeOut('fast', function() {
					$('#global-error-box').fadeIn('fast');

					$('#global-error-box').fadeOut('fast', function() {
						$('#global-error-box').fadeIn('fast');

						$('#global-error-box').fadeOut('fast', function() {
							$('#global-error-box').fadeIn('fast');
						});
					});
				});

				return false;
			}

			$.post(document.location.toString(), { 
					action: 'set_country',
					billing_country: $('#billing_country').val(),
					promotion_discount : $('#promotion_discount').val(),
					promotion_gift : $('#promotion_gift').val(),
					promotion_id : $('#promotion_id').val()
				}, function(data) { 
				document.location = '/checkout/step:1';
			} );
		});
	}

	$('.place-final-order').click( function() {
		if(returnFalse > 0) {
			$('#global-error-box').fadeOut('fast', function() {
				$('#global-error-box').fadeIn('fast');

				$('#global-error-box').fadeOut('fast', function() {
					$('#global-error-box').fadeIn('fast');

					$('#global-error-box').fadeOut('fast', function() {
						$('#global-error-box').fadeIn('fast');
					});
				});
			});

			return false;
		}

		document.location = '/checkout/do:payment';
	});

	$('.phone-order').click( function() {
		if(returnFalse > 0) {
			$('#global-error-box').fadeOut('fast', function() {
				$('#global-error-box').fadeIn('fast');

				$('#global-error-box').fadeOut('fast', function() {
					$('#global-error-box').fadeIn('fast');

					$('#global-error-box').fadeOut('fast', function() {
						$('#global-error-box').fadeIn('fast');
					});
				});
			});

			return false;
		}

		document.location = '/checkout/telephone-order';
	});
	
	var featuredImages = $('#featured-images img').size();
	
	if(featuredImages > 0) {
		nextProduct = ((startProduct + 1) < featuredImages) ? (startProduct + 1) : (startProduct - 1);
		prevProduct = ((startProduct - 1) >= 0) ? (startProduct - 1) : (featuredImages - 1);
	
		prevPosition = $('#prev-featured').position();
		startPosition = $('#current-featured').position();
		nextPosition = $('#next-featured').position();

		startPosition['left'] = startPosition['left'] - 20;
		nextPosition['left'] = nextPosition['left'] - 10;

		$('#featured-images img').each( function(i) {
			$(this).addClass('image-'+i);

			$(this).click( function() {
				document.location = $(this).attr('alt');
			});
		});
	
		$('#next').click( function() { 
			if(featuredImages > 2) {
				$('.image-'+prevProduct).css({'z-index': 5});
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 15});

				$('.image-'+nextProduct).animate( { 'width': '149', 'height': '281', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );
				$('.image-'+startProduct).animate( { 'width': '77', 'height': '142', 'top': prevPosition['top'] + 50, 'left': prevPosition['left'] }, 'slow' );
				$('.image-'+prevProduct).animate( { 'opacity': 0, 'width': '77', 'height': '142', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				prevProduct = startProduct;
				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (featuredImages - 1)) ? 0 : (startProduct + 1);

				$('#more-featured').get(0).href = $('.image-'+startProduct).attr('alt');

				$('.image-'+nextProduct).css({'z-index': 5});
				$('.image-'+nextProduct).animate( { 'opacity': 1, 'width': '77', 'height': '142', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
			} else if(featuredImages == 2) {
				$('.image-'+startProduct).css({'z-index': 5});
				$('.image-'+nextProduct).css({'z-index': 10});

				$('.image-'+startProduct).animate( { 'width': '77', 'height': '142', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'width': '149', 'height': '281', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (featuredImages - 1)) ? 0 : (startProduct + 1);

				$('#more-featured').get(0).href = $('.image-'+startProduct).attr('alt');
			}

			return false;
		});

		$('#prev').click( function() { 
			if(featuredImages > 2) {
				$('.image-'+prevProduct).css({'z-index': 15});
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 5});

				$('.image-'+prevProduct).animate( { 'width': '149', 'height': '281', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );
				$('.image-'+startProduct).animate( { 'opacity': 1, 'width': '77', 'height': '142', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'opacity': 0, 'width': '77', 'height': '142', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				nextProduct = startProduct;
				startProduct = prevProduct;
				prevProduct = ((startProduct - 1) >= 0) ? (startProduct - 1) : (featuredImages - 1);

				$('#more-featured').get(0).href = $('.image-'+startProduct).attr('alt');

				$('.image-'+prevProduct).css({'z-index': 5});
				$('.image-'+prevProduct).animate( { 'opacity': 1, 'width': '77', 'height': '142', 'top': prevPosition['top'] + 50, 'left': prevPosition['left'] }, 'slow' );
			} else if(featuredImages == 2) {
				$('.image-'+startProduct).css({'z-index': 10});
				$('.image-'+nextProduct).css({'z-index': 5});

				$('.image-'+startProduct).animate( { 'width': '77', 'height': '142', 'top': nextPosition['top'] + 50, 'left': nextPosition['left'] }, 'slow' );
				$('.image-'+nextProduct).animate( { 'width': '149', 'height': '281', 'top': startPosition['top'], 'left': startPosition['left'] }, 'slow' );

				startProduct = nextProduct;
				nextProduct = ((startProduct + 1) > (featuredImages - 1)) ? 0 : (startProduct + 1);

				$('#more-featured').get(0).href = $('.image-'+startProduct).attr('alt');
				$('.image-'+startProduct).click( function() {
					document.location = $('.image-'+startProduct).attr('alt');
				});
			}

			return false;
		});

		$('#featured-images img').css({'opacity': 0});
		$('#featured-images img').css({'top': startPosition['top'], 'left': startPosition['left']});

		$('.image-'+nextProduct).css({'opacity': 1, 'top': nextPosition['top'] + 50, 'left': nextPosition['left']});
		$('.image-'+nextProduct).attr({'width': '77', 'height': '142'});
		$('.image-'+prevProduct).attr({'width': '77', 'height': '142'});
		$('.image-'+startProduct).attr({'width': '149', 'height': '281'});
		$('.image-'+startProduct).css({'opacity': 1, 'z-index': 15});

		$('#more-featured').get(0).href = $('.image-'+startProduct).attr('alt');

		if(featuredImages > 2)
			$('.image-'+prevProduct).css({'opacity': 1, 'width': '77', 'height': '142', 'top': prevPosition['top'] + 50, 'left': prevPosition['left']});

		if(featuredImages == 1)
			$('#prev, #next').hide();
	}
	
	$('#do-register').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-register .text').each( function() {
			if($(this).val().trim() == '') {
				$('.info-console span').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.info-console').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#email').get(0).value.search(regEmail) == -1) {
			$('.info-console span').html('<font style="color: #b82f1d;">Please enter correct email address</font>');
			$('.info-console').show();

			$('#email').select();
			return false;
		}

		if ($('#password').val() != $('#password_confirm').val()) {
			$('.info-console span').html('<font style="color: #b82f1d;">Password you typed do not match</font>');
			$('.info-console').show();

			$('#password_confirm').select();
			return false;
		}

		$('.info-console').fadeOut();

		$('#form-register').submit();
	});	
	
	$('#do-login').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-login .text').each( function() {
			if($(this).val().trim() == '') {
				alert('Please complete all required fields');

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#_email').get(0).value.search(regEmail) == -1) {
			alert('Please enter correct email address');

			$('#_email').select();
			return false;
		}

		$('#form-login').submit();
	});
	
	$('#do-return').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-return .text').each( function() {
			if($(this).val().trim() == '') {
				alert('Please complete all required fields');

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#__email').get(0).value.search(regEmail) == -1) {
			alert('Please enter correct email address');

			$('#__email').select();
			return false;
		}

		$('#form-return').submit();
	});


	$('#do-step-2').click( function() {
		var error = 0;

		$('#form-step-2 .text').each( function() {
			if($(this).val().trim() == '' && $(this).get(0).id != 'email' && $(this).get(0).id != 'county') {
				$('.info-console span').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.info-console').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		$('.info-console').fadeOut();

		$('#form-step-2').submit();
	});

	$('#do-delivery').click( function() {
		var error = 0;

		$('#form-step-2 .text').each( function() {
			if($(this).val().trim() == '' && $(this).get(0).id != 'email' && $(this).get(0).id != 'county') {
				$('.info-console span').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.info-console').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		$('.info-console').fadeOut();

		$.post(document.location.toString() , { 
			action: 'step_2_delivery', 
			email: $('#email').val() || '',
			comments: $('#comments').val(),
			billing_title: $('#title').val(),
			billing_forename: $('#forename').val(),
			billing_surname: $('#surname').val(),
			billing_house_no: $('#house_no').val(),
			billing_street: $('#street').val(),
			billing_town: $('#town').val(),
			billing_county: $('#county').val(),
			billing_post: $('#post').val(),
			billing_country: $('#country').val(),
			billing_phone: $('#phone').val()
		}, function() { 

			document.location = '/checkout/step:2.1';
			return false;
		});

		return false;
	});
	
	$('#do-step-2-1').click( function() {
		var error = 0;

		$('#form-step-2-1 .text').each( function() {
			if($(this).val().trim() == '' && $(this).get(0).id != 'email' && $(this).get(0).id != 'county') {
				$('.info-console span').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.info-console').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		$('.info-console').fadeOut();

		$('#form-step-2-1').submit();
	});

	$('#do-forgotten').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-forgotten .text').each( function() {
			if($(this).val().trim() == '') {
				$('.error').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.error').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#email').get(0).value.search(regEmail) == -1) {
			$('.error').html('<font style="color: #b82f1d;">Please enter correct email address</font>');
			$('.error').show();

			$('#email').select();
			return false;
		}

		$('.error').fadeOut();

		$('#form-forgotten').submit();
	});
	
	$('#do-newsletter').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-newsletter .text').each( function() {
			if($(this).val().trim() == '') {
				$('.error').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.error').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#email').get(0).value.search(regEmail) == -1) {
			$('.error').html('<font style="color: #b82f1d;">Please enter correct email address</font>');
			$('.error').show();

			$('#email').select();
			return false;
		}

		$('.error').fadeOut();

		$('#form-newsletter').submit();
	});

	$('#do-faq').click( function() {
		var error = 0;

		$('#form-faq .text').each( function() {
			if($(this).val().trim() == '') {
				$('.error').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.error').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		$('.error').fadeOut();

		$('#form-faq').submit();
	});

	$('#do-reset').click( function() {
		var error = 0;
		var regEmail = /^[A-Za-z0-9\-_]+(\.[A-Za-z0-9\-_]+)*@([A-Za-z0-9\-_\.]+\.)+[A-Za-z]{2,8}$/i;

		$('#form-reset .text').each( function() {
			if($(this).val().trim() == '') {
				$('.error').html('<font style="color: #b82f1d;">Please complete all required fields</font>');
				$('.error').show();

				$(this).select();
				error = 1;

				return false;
			}
		});

		if(error > 0)
			return false;

		if ($('#email').get(0).value.search(regEmail) == -1) {
			$('.error').html('<font style="color: #b82f1d;">Please enter correct email address</font>');
			$('.error').show();

			$('#email').select();
			return false;
		}

		if ($('#password').val() != $('#password_confirm').val()) {
			$('.error').html('<font style="color: #b82f1d;">Password you typed do not match</font>');
			$('.error').show();

			$('#password_confirm').select();
			return false;
		}

		$('.error').fadeOut();

		$('#form-reset').submit();
	});
	
	$('.quantity').click( function() {
		$(this).select();
	});

	$('.quantity').keypress( function(int) {
		return Transform.numbers(int, false);
	});
	
	$('#input-postcode').focus( function() {
		if($(this).val() == 'enter postcode')
			$(this).val('');
	});

	$('#input-postcode').blur( function() {
		if($(this).val() == '')
			$(this).val('enter postcode');
	});
	
	$('#get-rude-submit').click( function() {
		if($('#input-postcode').val() == '' || $('#input-postcode').val() == 'enter postcode') {
			alert('Please enter postcode');

			return false;
		}
		
		$('#get-rude-form').submit();

		return false;
	});

	// LOOPS PATCH..
	var haveLoop = $('#header .current').size();
	
	if(haveLoop > 0) {
		var loopPosition = $('#header .current').position();
		var loopClass = $('#header .current').attr('name');
		var loopClassHref = $('#header .current').attr('href');
		var loopClassTitle = $('#header .current').attr('title');

		if(loopClass == 'loop-index') { loopPosition['top'] = (loopPosition['top'] - 20); loopPosition['left'] = (loopPosition['left'] - 40); }
		if(loopClass == 'loop-why-rude-health') { loopPosition['top'] = (loopPosition['top'] - 25); loopPosition['left'] = (loopPosition['left'] - 8); }
		if(loopClass == 'loop-our-food') { loopPosition['top'] = (loopPosition['top'] - 15); loopPosition['left'] = (loopPosition['left'] - 15); }
		if(loopClass == 'loop-news') { loopPosition['top'] = (loopPosition['top'] - 15); loopPosition['left'] = (loopPosition['left'] - 15); }
		if(loopClass == 'loop-rants') { loopPosition['top'] = (loopPosition['top'] - 21); loopPosition['left'] = (loopPosition['left'] - 9); }
		if(loopClass == 'loop-checkout') { loopPosition['top'] = (loopPosition['top'] - 20); loopPosition['left'] = (loopPosition['left'] - 35); }
		if(loopClass == 'loop-buy') { loopPosition['top'] = (loopPosition['top'] - 6); loopPosition['left'] = (loopPosition['left'] - 18); }
	
		$('.'+loopClass).css({'top': loopPosition['top'], 'left': loopPosition['left'], 'cursor': 'pointer'});
		$('.'+loopClass).attr('title', loopClassTitle);
		$('.'+loopClass).click( function() { document.location = loopClassHref });
		$('.'+loopClass).show();
	}
	// END LOOPS PATCH..
});

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function headline_rotate() {
	current_headline = (old_headline + 1) % headline_count;
	if(current_headline == 0) {}
	
	headlines[current_headline].hide();
	headlines[old_headline].fadeOut( function() { 
		headlines[old_headline].css('top', '200px');
	
		headlines[current_headline].fadeIn().show().animate({top: 5},"slow");
		old_headline = current_headline;
	});

}

Transform = {
	fixNum: function(amount, int) {
		return Number(amount).toFixed(2);
	},
	numbers: function(evt, negative) {
		evt = (evt) ? evt : window.event;
		var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	
		if (charCode > 31 && (charCode < 48 || charCode > 57) && charCode != 46 && charCode != 44 && charCode != 63234 && charCode != 63235 && !(negative && charCode == 45)) {
			return false
		}
		return true
	},
	weightToMoney: function(zone, weight) {
		$('#global-error-box').fadeOut();
		returnFalse = 0;

		if(zone == 'United Kingdom' || zone == 'UK Mainland' || zone == 'Channel Islands' || zone == 'Isle of Man' || zone == 'N Ireland') {
			if(weight >= 0 && weight < 600) return 2;
			if(weight >= 600 && weight < 1250) return 3.5;
			if(weight >= 1250 && weight <= 17500) return 5;

			//$('#global-error-box span').html('You can\'t order products with a total weight of over 17.50 kg');
			//$('#global-error-box').fadeIn();

			returnFalse = 0;

			return 5;
		} else {
			if(weight >= 0 && weight < 500) return 2.5;
			if(weight >= 500 && weight < 1250) return 5;
			if(weight >= 1250 && weight <= 1750) return 7;

			$('#global-error-box span').html('You can\'t order products with a total weight of over 1.75 kg');
			$('#global-error-box').fadeIn();

			returnFalse = 1;

			return 7;
		}
	}
};

Calculate = {
	updateBasket: function() {
		this.pID = 0;
		var subTotal = 0;
		var VAT = 0;
		var postage = 0;
		var postageWeight = 0;

		$('.h-id').each( function() {
			this.pID = $(this).val();
			this.pVAT = parseFloat($('.vat-' + this.pID).val());
			this.price = parseFloat($('.price-' + this.pID).val());
			this.weight = parseFloat($('.weight-' + this.pID).val());
			this.quantity = parseFloat($('.quantity-' + this.pID).val());
			
			subTotal = subTotal + this.price * this.quantity;
			VAT = VAT + this.quantity * this.price * this.pVAT / 100;
			postageWeight = postageWeight + this.quantity * this.weight;
		});

		postage = Transform.weightToMoney($('#billing_country').val(), postageWeight);

		$('.shipping-subtotal').html('&pound;' + Transform.fixNum(subTotal, 2));
		$('.shipping-vat').html((VAT > 0) ? '&pound;' + Transform.fixNum(VAT, 2) : 'N\\A');
		//$('.shipping-discount').html('');
		//$('h4.discount').hide();
		$('.shipping-postage').html('&pound;' + Transform.fixNum(postage));
		$('.shipping-total').html('&pound;' + Transform.fixNum(subTotal + ((VAT > 0) ? VAT : 0) + postage, 2));

		Promotions.checkForGiftPromotion(subTotal);
	}
};

String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/,"");
};