

	if ($(".validate-me").length > 0) {
		$(".validate-me").validate();
	}
	
	if ($(".secure").length > 0) {
		$.get('http://www.chiselwood.co.uk/scripts/token.php',function(txt) {
			$('.secure').append('<input type="hidden" name="ts" value="'+txt+'" />');
		});
	}
	
	if ($("#contact").length > 0) {
		$("#contact_us").validate();
		$("#request_brochure").validate();
	}
	
	if ($("#register").length > 0) {
		$("#register").validate();
	}
	
	if ($("#checkout-form").length > 0) {
		$("#checkout-form").validate();
	}
	
	if ($("#portfolio").length > 0) {
		$("#portfolio .grid-3 ul li").hover(function(){
			$(this).find('img').css({'margin-right':'0'});
		},
		function(){
			$(this).find('img').css({'margin-right':'3px'});
		});
	}
	
	$(".large-link").hover(function(){
		$(this).find('img').css({'margin-left':'5px'});
	},
	function(){
		$(this).find('img').css({'margin-left':'3px'});
	});
	
	$("#home #wallpaper img").hide();
	
	$(window).load(
		function() {
			$("#wallpaper img").vAlign().fadeIn();
		}
	);
	
	$(window).resize(function () {
		$("#wallpaper img").vAlign();
	});
	
	$('#foo').minmax();
	
	if ($("#pfolio-images").length > 0) {
		$('.grid-3').append('<ul id="portfolio-tns">');
		$('#pfolio-images div p .zoomimg').before('<a href="#" class="next nextimg more"></a><a href="#" class="prev previmg more"></a>');
		$('#pfolio-images').cycle({ 
		    fx:     'fade', 
		    speed:  0, 
			timeout: 0, 
		    pager:  '#portfolio-tns', 
			prev:   '.previmg',
			next:   '.nextimg',

		    // callback fn that creates a thumbnail to use as pager anchor 
		    pagerAnchorBuilder: function(idx, slide) { 
		        var s = idx > 100 ? ' style="display:none"' : '';
				var src = $('img',slide).attr('src'); 
				return '<li'+s+'><a href="#"><img src="' + src + '" height="113" /></a></li>';
		    } 
		});
		$("#portfolio-tns").jcarousel({
			scroll: 1 
		});
		var carousel = $("#portfolio-tns").data('jcarousel');
		//carousel.scroll(jQuery.jcarousel.intval(2));
		$(".jcarousel-prev, .jcarousel-next").css({'display':'none'});
		$("#portfolio-tns, .jcarousel-prev, .jcarousel-next").hover(function(){
			$(".jcarousel-prev, .jcarousel-next").css({'display':'block'});
		}, function(){
			$(".jcarousel-prev, .jcarousel-next").css({'display':'none'});
		});
		$(".jcarousel-prev").hover(function(){
			$(this).css({'background-position':'0 50%'});
		}, function(){
			$(this).css({'background-position':'50% 50%'});
		});
		$(".jcarousel-next").hover(function(){
			$(this).css({'background-position':'100% 50%'});
		}, function(){
			$(this).css({'background-position':'50% 50%'});
		});
		$(".zoomimg").colorbox({ maxHeight: '90%', maxWidth: '90%', rel:'group1' });
		
		var swipeOptions = { swipeLeft:swipeLeft,swipeRight:swipeRight,threshold:0 }

		$(function(){ $("#portfolio-tns").swipe( swipeOptions ); });

		function swipeLeft(event){
			carousel.next();
		}
		
		function swipeRight(event){
			carousel.prev();
		}
	}
	
	if ($("#press").length > 0 || $("#contact").length > 0) {
		$('.collapse').hide().removeClass('collapse').addClass('closed');
		$('#directions').css({'height':'0','overflow':'hidden','padding-bottom':'0'});
		$('.grid-2 form:nth-child(4)').hide().removeClass('collapse').addClass('closed');
	}
	
	$(".formxp,.dirxp").click(function(){
		//reset styles for #directions 
		if ($(this).next().attr('id') == 'directions') {
			$("#directions:not(.closed)").addClass('closed');
			$('#directions').css({'height':'auto','overflow':'visible','padding-bottom':'24px'});
		}
		//get associated form
		asc_form = $(this).attr('href');
		//get form state
		form_state = $(asc_form).attr('class');
		if (form_state == "closed") {
			$(asc_form).slideDown('slow', function() {
				$(asc_form).removeClass('closed').addClass('open');
				$('a[href*=' + asc_form + ']').find('img').attr('src','http://www.chiselwood.co.uk/images/templates/down-arrow-grey.png');
			});
		} else {
			$(asc_form).slideUp('slow', function() {
				$(asc_form).removeClass('open').addClass('closed');
				$('a[href*=' + asc_form + ']').find('img').attr('src','http://www.chiselwood.co.uk/images/templates/right-arrow.png');
			});
		}
		return false;
	});
	
	if ($("#paint-nav").length > 0) {
		$("#paint-nav li div").css({'visibility':'hidden'});
		$("#paint-nav li").hover(function(){
			$(this).find('div').css({'visibility':'visible'});
		}, function(){
			$(this).find('div').css({'visibility':'hidden'});
		});
	}
	
	// call colobox function for nav
	$('.call-modal').each(function(){
		page_req = $(this).attr('href');
		$(this).colorbox({ href: page_req + " #content", opacity: 0 });
	});
	
	function calculate_total() { 
		current_total = $('.total').html();
		total = 0;
		objValue = 0;
		selected_sizes = [];
		$('.size').each(function(){
			price = $(this).find('option:selected').val();
			price = price.replace('£',''); 
			selected_sizes.push( price * $(this).parent().parent().find('#quantity').val() );
		})
		$.each(
			selected_sizes,
		 		function( intIndex, objValue ){
					objValue = parseFloat(objValue);
					total = total + objValue;
				}
		);
		total = total.toFixed(2);
		$('.total > span').html(total);
		$('#total').val(total);
	}
	
	function calculate_delivery() {
		delivery_total = 0;
		/*if ($('.size option[title="60ml"]:selected').length > 2 && $('.size option[title!="60ml"]:selected').not('[value=""]').length == 0) {
			delivery_total = 5;
		} else if ($('.size option[title="60ml"]:selected').length > 0 && $('.size option[title!="60ml"]:selected').not('[value=""]').length == 0) {
			total_testers = $('.size option[title="60ml"]:selected').length;
			tester_delivery = $('.size option[title="60ml"]').attr('rel');
			delivery_total = delivery_total + (total_testers * tester_delivery);
			delivery_total = parseFloat(delivery_total);
		} else {
			if ($('.size option[value!=""]:selected').length > 0) {
				delivery_total = $('.size option[value!=""]:selected').not('[title="60ml"]').attr('rel');
				delivery_total = parseFloat(delivery_total);
			}
		}*/
		if ($('.size option[title="60ml"]:selected').length > 0 && $('.size option[title!="60ml"]:selected').not('[value=""]').length == 0) { // if only testers selected
			testers_selected = [];
			i = 0;
			$('.size option[title="60ml"]:selected').each(function(){
				qty = $(this).parent().parent().parent().find('#quantity').val(); 
				qty = parseFloat(qty);
				i += qty;
			});
			if (i < 3) {
				per_tester = $('#per_tester').val();
				delivery_total = per_tester * i;
				delivery_total = parseFloat(delivery_total);
			} else {
				delivery_total = $('#three_testers').val();
				delivery_total = parseFloat(delivery_total); 
			}  
		} else {
			if ($('.size option[value!=""]:selected').length > 0) {
				delivery_total = $('#one_litre').val();
				delivery_total = parseFloat(delivery_total);
			}
		} 
		delivery_total = delivery_total.toFixed(2);
		$('.total em span').html(delivery_total); 
	} 
	
	$(".add").click(function() { 
		 row_nums = [];
		 $('#select_paint tr:not(:first-child)').each(function(){
			row_num = $(this).attr('id');
			row_num = row_num.replace('row_','');
			row_nums.push(row_num);
		 });
		 Array.max = function( array ){
		    return Math.max.apply( Math, array );
		 }; 
		 max_num = Array.max(row_nums);
		 max_num++;
	     $('#select_paint tbody>tr:last').clone(true).insertAfter('#select_paint tbody>tr:last');
		 num_items = $('#num_items').val();
		 num_items++;
		 $('#num_items').val(num_items);
		 $('#select_paint tbody>tr:last').attr('id','row_' + num_items); 
		 $('#select_paint tbody>tr:last #finish').attr('name','finish' + num_items)
		 $('#select_paint tbody>tr:last #colour').attr('name','colour' + num_items).addClass('disabled').attr('disabled','disabled'); 
		 $('#select_paint tbody>tr:last #quantity').val(1).attr('name','quantity[' + num_items + ']');
		 $('#select_paint tbody>tr:last #size').html("<option selected='' value=''>Select size</option>").attr('name','size' + num_items).addClass('disabled').attr('disabled','disabled');
		 $('#paint-data').append('<input type="hidden" name="entry_id[' + max_num + ']" value=""><input type="hidden" name="price[' + max_num + ']" value=""><input type="hidden" name="item_options[' + max_num + '][cf_paint_colour]" value=""><input type="hidden" name="item_options[' + max_num + '][cf_paint_size]" value="">');
	     return false;
	});
	
	$(".remove").click(function() {
		product_row = $(this).parent().parent().attr('id');
		row_num = product_row.replace('row_','');
		if ($('#select_paint tbody>tr').length > 2) {
	     	$(this).parent().parent().remove();
			$('#paint-data input[name="entry_id[' + row_num + ']"]').remove(); 
			$('#paint-data input[name="price[' + row_num + ']"]').remove();
			$('#paint-data input[name="item_options[' + row_num + '][cf_paint_colour]"]').remove();
			$('#paint-data input[name="item_options[' + row_num + '][cf_paint_size]"]').remove();   
			num_items = $('#num_items').val();
			num_items--;
			$('#num_items').val(num_items);
		}
		calculate_total();
		calculate_delivery();
	    return false;
	});
	
	$("#finish").change(function() {
		product_id = $(this).val();
		product_row = $(this).parent().parent().attr('id');
		row_num = product_row.replace('row_','');
		if (product_id == "") {
			$('#' + product_row).find('#colour').attr('disabled','disabled').addClass('disabled').html("<option selected='' value=''>Select colour</option>");
			$('#' + product_row).find('#size').attr('disabled','disabled').addClass('disabled').html("<option selected='' value=''>Select size</option>");
			calculate_total();
			calculate_delivery();
			$('input[name="price[' + row_num + ']"]').val(0.00); 
		} else { 
			$('input[name="entry_id[' + row_num + ']"]').val(product_id);
			$.ajax({
				url: '/paint/get-colours/' + product_id,
				dataType: 'html',
	      		type: 'post',
				success: function(data){ 
					$('#' + product_row).find('#colour').removeAttr('disabled').removeClass('disabled').html(data);
				}
			});
			$.ajax({
				url: '/paint/get-sizes/' + product_id,
				dataType: 'html',
	      		type: 'post',
				success: function(data){ 
					$('#' + product_row).find('#size').removeAttr('disabled').removeClass('disabled').html(data);
					calculate_total();
					calculate_delivery();
				}
			});
			if ($(this).next().length > 0) {
				$(this).parent().find('.error').remove();
			}
		}
	})
	
	$("#colour").change(function() {
		colour = $(this).val();
		product_row = $(this).parent().parent().attr('id');
		row_num = product_row.replace('row_','');
		if (colour == "") {
			 $('input[name="item_options[' + row_num + '][cf_paint_colour]"]').val('');
		} else {
			 $('input[name="item_options[' + row_num + '][cf_paint_colour]"]').val(colour);
			 if ($(this).next().length > 0) {
				$(this).parent().find('.error').remove();
			 } 
		}
	});
	
	$("#size").change(function() {
		calculate_total();
		calculate_delivery(); 
		price = $(this).val();
		product_row = $(this).parent().parent().attr('id');
		row_num = product_row.replace('row_','');
		if (price != "") { 
			price = price.replace('£','');
			$('input[name="price[' + row_num + ']"]').val(price);
			size = $(this).find('option:selected').attr('title');
			$('input[name="item_options[' + row_num + '][cf_paint_size]"]').val(size);
			if ($(this).next().length > 0) {
				$(this).parent().find('.error').remove();
			 } 
		} else {
			$('input[name="item_options[' + row_num + '][cf_paint_size]"]').val('');
		}
	});
	
	$("#quantity").change(function() {
		/*current_total = $('.total').html();
			total = 0;
			objValue = 0;
			selected_sizes = [];
			$('.size').each(function(){ 
				price = $(this).find('option:selected').val();
				price = price.replace('£','');
				selected_sizes.push( price * $(this).parent().parent().find('#quantity').val() );
			})
			$.each(
				selected_sizes,
					function( intIndex, objValue ){
						objValue = parseFloat(objValue);
						total = total + objValue;
					}
			);
			total = total.toFixed(2);
			$('.total > span').html(total);
			$('#total').val(total);*/
		calculate_total();
		calculate_delivery();
	});
	
	$('#order-paint').submit(function(event) { 
		$('#select_paint tr:not(:first-child)').each(function(){
			if ($(this).find('#finish').val() == "") {
				if ($(this).find('#finish').next().length < 1) {
					$(this).find('#finish').after('<p class="error">Please select a finish</p>');
				}
				event.preventDefault();
			}
			if ($(this).find('#colour').val() == "") {
				if ($(this).find('#colour').next().length < 1) { 
					$(this).find('#colour').after('<p class="error">Please select a colour</p>');
				}
				event.preventDefault();
			}
			if ($(this).find('#size').val() == "") { 
				if ($(this).find('#size').next().length < 1) { 
					$(this).find('#size').after('<p class="error">Please select a size</p>');
				}
				event.preventDefault();
			}
		});
	});
	
	if ($("#checkout-form").length > 0) {
		$("input#same_as_billing").click(
			function(){
				if($(this).is(':checked')) {
					$("#shipping_first_name").attr('value', $("#first_name").attr('value'));
					$("#shipping_last_name").attr('value', $("#last_name").attr('value'));; 
					$("#shipping_address").attr('value', $("#address").attr('value'));
					$("#shipping_address2").attr('value', $("#address2").attr('value'));
					$("#shipping_city").attr('value', $("#city").attr('value'));
					$("#shipping_state").attr('value', $("#state").attr('value'));
					$("#shipping_zip").attr('value', $("#zip").attr('value'));
					var ccode = $("#country_code").val();
					$("#shipping_country_code").val(ccode);
				} else {
					$("#shipping_first_name").attr('value', '');
					$("#shipping_last_name").attr('value', '');
					$("#shipping_address").attr('value', '');
					$("#shipping_address2").attr('value', '');
					$("#shipping_city").attr('value', '');
					$("#shipping_state").attr('value', '');
					$("#shipping_zip").attr('value', '');
				}
			}
		);
	}
	
	if ($("#about").length > 0) {
		$( "#tabs" ).tabs();
	}
		
	
