$(function() {
	// Open a tags in new window if appropriate
	$('a.newwin').click(function() {
		window.open(this.href);
		return false;
	});
	
	// Left Nav
	$('ul.listcars li.inactive ul').hide();
	$('ul.listcars li > span').click(function () {
		$(this).next().toggle();
		$(this).parent().toggleClass('active');
	});
	
	// Product Large Image
	$('#producthead').find('a.pi img').each(function() {
		var $this = $(this); // Cache $(this) to avoid having to look it up for each task
		var parent = $(this).parent();
		var largeimg = parent.attr('href');
	//	This line is for large image views (610x610)
	//	var largevw = '<div id="largeview"><img src="/themes/newton/front-end/gfx/build/view_product_detail2.png" alt="no alt text yest for large image view" style="background-image:url(' + largeimg + ');" /></div>';
	//	This line is for not so large image views (450x450)
		var largevw = '<div id="largeview"><img src="/themes/newton/front-end/gfx/build/image_cover_450.png" alt="no alt text yest for large image view" style="background-image:url(' + largeimg + ');" /></div>';
		$this.css('background-image', 'url(' + $(this).attr('src') + ')')
		.css('width', '305px')
		.css('height', '332px')
//		.attr('src', '/themes/newton/front-end/gfx/build/view_product_large.png')
// 		DISABLED UNTIL NEW IMAGES AVAILABLE AT 450x450, when ready, revert back to the line above
		.attr('src', '/themes/newton/front-end/gfx/build/view_product_large_white.png')
		.unwrap()
		$this.parent().append(largevw)
		.parent().find('#largeview').hide();
		$this.click(function () {
// 			DISABLED UNTIL NEW IMAGES AVAILABLE AT 450x450
//			$(this).parents().find('#largeview').toggle();
			return false;
		});
	});
	$('body').live("click", function(){
		$('#largeview').hide();
		$('#bigswatch').hide();
	});
	
	// Swatch dropdown selector
	$('#productselector').find('select').change(function () {
		$("#swatchwrap").removeClass().addClass('imgcss_' + $(this).val());
	})
	.change();
	
	// Swatch Large Image
	$('#swatch').each(function() {
		var $this = $(this); // Cache $(this) to avoid having to look it up for each task
		$this.parent().find('#bigswatch').hide();
		$this.parent().find('#bigswatch').addClass('bigswatch'); // This is to dynamically add the white border so that it's not visible if javascript is disabled or not supported
		$this.click(function () {
			$this.parents().find('#bigswatch').toggle();
			return false;
		});
	});
	
	// Auto update basket on select dropdown change
	$('#baskettblwrap').find('form').each(function() {
		$(this).find('input.update').hide();
		$('select').change(function() {
			$(this).closest("form").submit();
		});
	});
	
	// Auto fade out Notice messages in the basket
	$('body.one-column #primary').find('.notice').each(function() {
		$(this).addClass('dynamic');
		$(this).fadeOut(7000);
	});
	
	// Auto-complete the delivery address if requested
	$("#shop_destination-add_same .wysiwyg-false").click(function(){
		if ($("#shop_destination-add_same .wysiwyg-false").is(':checked'))
		{
			$("input#in_add_del1").val($("input#in_add_inv1").val());
			$("input#in_add_del2").val($("input#in_add_inv2").val());
			$("input#in_add_del3").val($("input#in_add_inv3").val());
			$("input#in_add_del4").val($("input#in_add_inv4").val());
			$("input#in_add_del5").val($("input#in_add_inv5").val());
			$("select#in_add_del6").val($("select#in_add_inv6").val());
			$("select#in_add_del7").val($("select#in_add_inv7").val());
		} else {
			$("input#in_add_del1").val("");
			$("input#in_add_del2").val("");
			$("input#in_add_del3").val("");
			$("input#in_add_del4").val("");
			$("input#in_add_del5").val("");
			$("select#in_add_del6").val("1"); // Default back to "1" (UK) as blank is not a valid country identifier
			$("select#in_add_del7").val("1");
		}
	});
	
	// Adding in-field labels to form fields where normal labels are inconvenient
	// And making sure that these default values are not passed on when the form is submitted
	$('input.default').each(function(){
		var defaultVal = $(this).attr('title');
		$(this).focus(function(){
			if ($(this).val() == defaultVal){
				// If prepopulated with defaultVal then remove defaultVal
				$(this).removeClass('jactive').val('');
			} else {
				// If prepopulated with other than defaultVal then keep content
				$(this).removeClass('jactive');
			}
		})
		.blur(function(){
			if ($(this).val() == ''){
				$(this).addClass('jactive').val(defaultVal);
			}
		})
		.blur().addClass('jactive');
	});
	$('form').submit(function(){
		$('.default').each(function(){
			var defaultVal = $(this).attr('title');
			if ($(this).val() == defaultVal){
				$(this).val('');
			}
		});
	});
  
  
});


