// JavaScript Document
$(function(){
	Shop = new function(){
		
		this.bind = function(){
			if($("input[name=price_type]").length >= 0){
				$(".price-select").click(function(){
					$(".price-select img").attr('src','/images/button-select.png');
					$("input[name=price_type]").val($(this).attr('rel'));
					$(this + " img", this).attr('src','/images/button-selected.png');
					return false;
				});
				$("#get-quote").click(function(){
					Shop.online_quote($(".shop-form"));
					return false;
				});
			}
			
			$(".search-input").focus(function(){
				if($(this).val() == 'Enter Search'){
					$(this).val('');
				}
			});
			$(".search-input").blur(function(){
				if($(this).val() == ''){
					$(this).val('Enter Search');
				}
			});
		}
		
		this.online_quote = function(data){
			$(".quote-errors").html('');
			var postcode = $("input[name=postcode]").val();
			var quantity = $("input[name=quantity]").val();
			var price_type = $("input[name=price_type]").val();
			
			var form_data = $(data).serialize();
			$.getJSON('/products/do_quote/',form_data, function(data){
				if(data.error_msg.length >= 1){
					$(".quote-errors").html(data.error_msg);
				}
				$(".quote-total").html('&pound;'+data.total_price);
				$("input[name=quantity]").val(data.quantity);
				$(".quote-delivery-total span").html(data.delivery_price)
				$(".quote-sub-total span").html(data.sub_total)
		 	});
		}
	}
	
	Shop.bind();

});
