$(function() {
	//lightbox image popup
	if($('.lightbox').length){
		$(".lightbox").lightbox();
	}
	
	//ajax adding to basket
	
	$('form.product_form').submit(function(){
		var JsonVars=$(this).serialize();
		var thisFormId=$(this).attr("id");
		$.getJSON("/applications/shop/rpc/updatebasket.rm", JsonVars,
			function(data, thisForm){
				//update the "view basket" link at the top of the page
				$('#viewbasket span').show();
				$('#viewbasket span').html("("+data.totalproduct+" items: "+data.totalcost+")")
				$('#'+thisFormId).find('.addtobasket input').attr("src","/display_images/buttons/update_quantity.gif");
			}
		);
		 
		return false;
	});
	/**/
	
	//auto-population of billing address
	$('#asBillingAddress').click(function(){

		var fieldnames="title,first_name,second_name,tel,address1,address2,address3,town,county,postcode,country";
		var fieldarray=fieldnames.split(",");
	
		for(i=0;i<fieldarray.length;i++){
			if($('#asBillingAddress').attr("checked")==true) {
				$('#delivery_'+fieldarray[i]).attr("value", $('#personal_'+fieldarray[i]).val());
			}else {
				$('#delivery_'+fieldarray[i]).attr("value", "");
			}
		}
		
		if($('#asBillingAddress').attr("checked")==true) {
			$('fieldset.delivery input[@id!=asBillingAddress], fieldset.delivery select').fadeTo(500,0.4);
		}else {
			$('fieldset.delivery input, fieldset.delivery select').fadeTo(500,1);
		}
	});
	
	//swapping product variant images
	var tabContainers = $('div#protabs .productimage');
	$('#prolist ul a').click(function () {
		//hide them all
		tabContainers.hide();
		//but show the clicked on
		$('#div_' + $(this).attr("href").replace("#","")).show();
		
		$('div#protabs #prolist ul a').removeClass('selected');
		$(this).addClass('selected');
		
		return false;
	}).filter(':first').click();

});

