$(document).ready(function(){
						   
	$('#alert, #alert-left').fadeIn('slow').animate({opacity: 1.0}, 5000).fadeOut('slow', function() { $(this).remove(); });

	$("input[name^='login_'], .join-form").focus(function(){
		if($(this).val()==$(this).attr('title')){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val()==''){
			$(this).val($(this).attr('title'));
		}
	});
	
	//required forms
	$('#check-form').submit(function(){
		var error = false;							 	
		$('.required input, .email input').each(function(i){
			if($(this).val()==''){
				$(this).css("border", "1px solid #cc0000");
				error = true;
			}
		});
		if(error==true){
			alert("Please complete all the required fields");
			return false;
		}else{
			return true;
		}
	});
	
	$('.email input').change(function(){
		$.post("/ajax/validate.php", { type: "email", email: $(this).val() },
		  function(data){
			if(data.error)
			{
				$('#account_email').val('');
				$('.email').next(".validate-error").remove();
				$('.email').after("<p class='validate-error'>"+ data.message +"</p>");
			}else{
				$('.email').next(".validate-error").remove();
				$('.autocomplete').val($('.email').val());
			}
		}, "json");
	});
	
	$('#delivery_same').click(function(){
		if($(this).is(':checked')){
			$('#delivery_name').val($('#billing_fname').val() +" "+ $('#billing_surname').val());
			$('#delivery_add1').val($('#billing_add1').val());
			$('#delivery_add2').val($('#billing_add2').val());
			$('#delivery_add3').val($('#billing_add3').val());
			$('#delivery_add4').val($('#billing_add4').val());
			$('#delivery_add5').val($('#billing_add5').val());
			$('#delivery_postcode').val($('#billing_postcode').val());
		}else{
			$('#delivery_name').val('');
			$('#delivery_add1').val('');
			$('#delivery_add2').val('');
			$('#delivery_add3').val('');
			$('#delivery_add4').val('');
			$('#delivery_add5').val('');
			$('#delivery_postcode').val('');
		}
	});
				   
	//vehicle selector
	$("#selector-make").change(function(){
		$("#selector-model").html("<option value='0'>Loading models...</option>");		
		$.getJSON("/ajax/load_selector.php", {make: $(this).val(), category: $('#selector-category').val(), type: "1"}, function(j){													   
			var options = "";
			for(var i=0; i<j.length; i++){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#selector-model").html(options).attr('disabled', '').focus();
		});
		$("#selector-engine").html("<option value='0'>Engine</option>").attr('disabled', 'disabled');
		$("#selector-year").html("<option value='0'>Year</option>").attr('disabled', 'disabled');
	});
	$("#selector-model").change(function(){
		//reset the current selection
		$("#selector-engine").html("<option value='0'>Loading...</option>");
		$.getJSON("/ajax/load_selector.php", {make: $("#selector-make").val(), model: $(this).val(), category: $('#selector-category').val(), type: "2"}, function(j){
			var options = "";
			for( var i=0; i<j.length; i++ ){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#selector-engine").html(options).attr('disabled', '').focus();
		});
		$("#selector-year").html("<option value='0'>Year</option>").attr('disabled', 'disabled');
	});
	$("#selector-engine").change(function(){
		//reset the current selection
		$("#selector-year").html("<option value='0'>Loading...</option>");
		
		$.getJSON("/ajax/load_selector.php", {make: $("#selector-make").val(), model: $("#selector-model").val(), engine: $(this).val(), category: $("#selector-category").val(), type: "3"}, function(j){
			var options = "";
			for( var i=0; i<j.length; i++ ){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#selector-year").html(options).attr('disabled', '').focus();
		});

	});		
	//tool selector
	$("#tools-category1").change(function(){
		$("#tools-category2").html("<option value='0'>Loading...</option>");		
		$.getJSON("/ajax/load_selector.php", {category_one: $(this).val(), category: $('#selector-category').val(), type: "4"}, function(j){													
			var options = "";
			for(var i=0; i<j.length; i++){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#tools-category2").html(options).attr('disabled', '').focus();
		});
		$("#tools-category3").html("<option value='0'>NA</option>").attr('disabled', 'disabled');
	});
	$("#tools-category2").change(function(){
		//reset the current selection
		$("#tools-category3").html("<option value='0'>Loading...</option>");
		$.getJSON("/ajax/load_selector.php", {category_one : $('#tools-category1').val(), category_two: $(this).val(), category: $('#selector-category').val(), type: "5"}, function(j){
			var options = "";
			for( var i=0; i<j.length; i++ ){
				options += '<option value="'+ j[i].optionValue +'">' + j[i].optionDisplay +'</option>';
			}
			$("#tools-category3").html(options).attr('disabled', '').focus();
		});
	});
	$('#selector-select').click(function(){
		$(this).hide();
		$(this).after("<img src='/images/filter_loading.gif' alt='Loading...' width='16' height='16' style='position: absolute;' />");
	});
	
	//ajax add to basket
	$("form[id^='add_basket_']").submit(function(e){
		e.preventDefault();										 
		var form = this;
		var button = $(".add-to-basket", form);
		button.attr('src', '/images/button_add_loading.gif');
		$.post('/add_basket.php', 
			  { qty: $("input[name='qty']", form).val(),
			    product_id: $("input[name='product_id']", form).val(),
				category_id: $("input[name='category_id']", form).val(),
				sub_category_id: $("input[name='sub_category_id']", form).val(),
				ajax: true
			  }, function(data){
					button.attr('src', data.src);
					if(data.disabled=='disabled'){
						button.hide();
						button.after("<a href='/basket.php' class='add-to-basket'><img src='/images/button_added_basket.png' alt='view basket' /></a>");
					}
					//update totals
					$("#bt-items").html(data.items);
					$("#bt-total").html(data.total);
			  }, "json");
	});
	
	//selected items counter
	$("#selected-total").html("You have selected a total of 0 part(s)");
	$("select[id^='qty_']").change(function(){
		var total = 0;
		$("select[id^='qty_']").each(function(){
			total = total + parseInt($(this).val());
		});
		$("#selected-total").html("You have selected a total of "+ total +" part(s)");
	});
});