(function($){$.fn.autofill=function(options){var defaults={value:'First Name',defaultTextColor:"#868686",activeTextColor:"#333"};var options=$.extend(defaults,options);return this.each(function(){var obj=$(this);obj.css({color:options.defaultTextColor}).val(options.value).focus(function(){if(obj.val()==options.value){obj.val("").css({color:options.activeTextColor});}}).blur(function(){if(obj.val()==""){obj.css({color:options.defaultTextColor}).val(options.value);}});});};})(jQuery);

$(document).ready(function(){
	$('#keywords').autofill({
		value: 'Enter keywords or Product Code'
	});	

	$('#newsletter-email').autofill({
		value: 'Your Email Address'
	});	
	
	// Checkout functionality
	var sel_zone = $(".country-select select option[selected='selected']").parents('.country-select').attr("rel");
	
	$('.country-select').hide();
	if (sel_zone == 'uk') {
		$('#zone-int').hide();
		$('#VAT-no-entry').hide();
		$('#zone-int select').attr("disabled", "disabled");
		$('#zone-uk').show();
		$('#zone-uk select').removeAttr("disabled");
		$('.zone-choose-uk input').attr("checked", "checked");
		
		var country_val = $("#zone-uk select").val();
		var get_url = '/cgi-bin/trolleyed_public.cgi?action=get_postage_bands&country_id=' + country_val;
		get_postage_bands (get_url);
	} else {
		$('#zone-uk').hide();
		$('#zone-uk select').attr("disabled", "disabled");
		$('#zone-int').show();
		
		$('#zone-int select').removeAttr("disabled");
		$('.zone-choose-int input').attr("checked", "checked");
		
		var country_val = $("#zone-int select").val();
		var get_url = '/cgi-bin/trolleyed_public.cgi?action=get_postage_bands&country_id=' + country_val;
		get_postage_bands (get_url);
	}

	var zone_evt = $.browser.msie ? "click" : "change";

	$('.zone-choose input').bind(zone_evt, function () {
		var val = $(this).val();
		if (val == 'uk') {
			$('#zone-int').hide();
			$('#VAT-no-entry').hide();
			$('#zone-int select').attr("disabled", "disabled");
			$('#zone-uk').show();
			$('#zone-uk select').removeAttr("disabled").attr('selectedIndex', 0);
			
			var country_val = $("#zone-uk select").val();
			var get_url = '/cgi-bin/trolleyed_public.cgi?action=get_postage_bands&country_id=' + country_val;
			get_postage_bands (get_url);
		} else {
			$('#zone-uk').hide();
			$('#zone-uk select').attr("disabled", "disabled");
			$('#zone-int').show();
			$('#VAT-no-entry').show();
			$('#zone-int select').removeAttr("disabled").attr('selectedIndex', 0);
			
			var country_val = $("#zone-int select").val();
			var get_url = '/cgi-bin/trolleyed_public.cgi?action=get_postage_bands&country_id=' + country_val;
			get_postage_bands (get_url);
		}
	});
	
	$("select[name='country_id']").change(function() {
		var select_val = $(this).val();
		var select_text = $(this).find(':selected').text();
		var get_url = '/cgi-bin/trolleyed_public.cgi?action=get_postage_bands&country_id=' + select_val;
		get_postage_bands (get_url);
	});

	function get_postage_bands (get_url) {
		$.ajax({
			url: get_url,
			dataType: 'json',
			success: function(data) {
				if (!data) {
					$('#postage_band').empty();
					$('#postage_band').append(
						$('<option></option').val('0').html('Select Country before choosing postage')
					);
				} else {
					$('#postage_band').empty();
					$.each(data, function(key, val) {
						$('#postage_band').append(
							$('<option></option>').val(val.band_id).html(val.band_name)
						);
					});
					var postage_band_id = $('#postage_band_id').val();
					$("#postage_band").val(postage_band_id);
					$('#postage_band').trigger('change');
				}
			}
		});
	}
	
	$("#postage_band").live('change', function() {
		var base_currency_html = $("#base_currency_html").val();
		var postage_band = $(this).val();
		
		var country_val = $('.zone-choose input:checked').val();
		if (country_val == 'uk') {
			var country_id = $("#zone-uk select[name='country_id']").val();
		} else {
			var country_id = $("#zone-int select[name='country_id']").val();
		}
		
		var postage_update_url = '/cgi-bin/trolleyed_cart.cgi?action=postage_recalculation&country_id=' + country_id + '&postage_band=' + postage_band;
		$.ajax({
			url: postage_update_url,
			dataType: 'json',
			success: function(data) {
				if (!data) {
					console.log("Postage Invalid");
				} else {
					$('#postage_band_name').html(data.band_name);
					$('#postage_band_price').html(base_currency_html + data.postage_price);
					$('#tax_total_value').html(base_currency_html + data.tax_total);
					$('#grand_total_inc_tax').html(base_currency_html + data.grand_total_inc_tax);
					$('#discount_total').html(base_currency_html + data.discount_total);
				}
			}
		});
	});
	
	var scotland_option = $("#zone-uk select[name='country_id'] option[value='257']");
	scotland_option.html(scotland_option.html() + ' (Select your delivery postcode area below)');
});
