$(document).ready(function(){
	$("#products_box").children().hover(
		function(){
			$(this).addClass("hover");
		}, 
		function(){
			$(this).removeClass("hover");
		}
	).click(function(e){
		url = $(this).children("h2").children("a").attr("href");
		window.location = url;
		e.preventDefault();
	});
	
	$(".price_list tbody tr").hover(
		function(){
			$(this).addClass("hover");
		},function(){
			$(this).removeClass("hover");
		}
	);
	$("#prices .price_list tbody tr").click(function(e){
		if (e.target.type !== "checkbox") {
			$(':checkbox', this).attr('checked', function() {
				b = ! this.checked
				if (b) {
					$(this).closest('tr').addClass('selected');
				} else {
					$(this).closest('tr').removeClass('selected');
				}
				return b;
			});
		}
	});
	
	$("#quote_form .price_list tbody tr").click(function(e){
		if (e.target.type !== "text") {
			$(':text', this).focus();
		}
	});
	
	$("#quote_status").hover(function(){
		$(this).stop().animate({"height": "40px"}, 100);
		// $(this).children("#quote_status_detail").show();
	}, function() {
		$(this).stop().animate({"height": "20px"}, 500);
		// $(this).children("#quote_status_detail").hide();
	});
	setTimeout(function(){
		$(".success").slideUp(1000, function(){
			$(this).remove();
		});
	}, 5000);
	
	$("#quote_form .qty input").bind('change click keypress', function(){
		calculate_totals();
	});
	
	function calculate_totals(){
		total = 0;
		total_gst = '';
		rows = $('#quote_form tbody tr');
		$(rows).each(function(i){
			price = cur_to_float($(this).find('.price').text());
			gst = cur_to_float($(this).find('.gst').text());
			qty = parseInt($(this).find('.qty input').val());
			total += price * qty;
			total_gst += gst * qty;
		});
	
		tfoot = $('tfoot tr');
		tdprice = tfoot.children('.price');
		tdprice.text(float_to_cur(total));
		tdgst = tfoot.children('.gst');
		tdgst.text(float_to_cur(total_gst));
		// tdprice.add(tdgst)
		// 	// .css('background-color', '#9f9')
		// 	.animate( {backgroundColor: '#ffe'}, 300);
		$('#submit_buy').fadeOut(300);
		$('#print_quote').fadeOut(300);
		$('#submit_update').addClass('attention');
	}
	
	function cur_to_float(cur){
		return parseFloat(jQuery.trim(cur).substring(1).replace(',',''))
	}
	function float_to_cur(amount){
		var i = parseFloat(amount);
		if(isNaN(i)) { i = 0.00; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		i = parseInt((i + .005) * 100);
		i = i / 100;
		s = new String(i);
		if(s.indexOf('.') < 0) { s += '.00'; }
		if(s.indexOf('.') == (s.length - 2)) { s += '0'; }
		s = minus + s;
		amount = s;
		var delimiter = ","; // replace comma if desired
		var a = amount.split('.',2)
		var d = a[1];
		var i = parseInt(a[0]);
		if(isNaN(i)) { return ''; }
		var minus = '';
		if(i < 0) { minus = '-'; }
		i = Math.abs(i);
		var n = new String(i);
		var a = [];
		while(n.length > 3)
		{
			var nn = n.substr(n.length-3);
			a.unshift(nn);
			n = n.substr(0,n.length-3);
		}
		if(n.length > 0) { a.unshift(n); }
		n = a.join(delimiter);
		if(d.length < 1) { amount = n; }
		else { amount = n + '.' + d; }
		amount = minus + amount;
		return '$'+ amount;
	}
	
	$("#submit_update").after('<button type="button" id="print_quote" class="button">Print Quote</button>');
	$("#print_quote").click(function(){
		$('#content h1').text('Online Quote');
		window.print();
		$('#content h1').text('Cart');
	});

	$('#quote_form .remove').live('click', function(){
		row = $(this).closest('tr');
		row.children('.qty').children('input[name="qty[]"]').val(0);
		calculate_totals();
		row.css('background-color', '#f99').fadeOut(500, function(){
			$(this).remove();
		});
	});
	$('#quote_form .price_list tbody tr').hover(
		function(){
			$(this).children('td.qty').children('input[name="qty[]"]').after('<a href="#" class="remove">Remove</a>');
		},
		function(){
			$('.remove', this).remove();
		}
	); 
	
	// $('#submit_buy').click(function(e){
	// 	$('.quote_only').closest('tr').fadeOut(2000, function(){
	// 		$('#submit_buy').click();
	// 	});
	// 	e.preventDefault();
	// });
	
});
