$(document).ready(function(){

 
	$('#buy-now-form-box').find('h2:eq(0)').addClass('cufon-noshadow');


	//style picker *****
	$('#style-picker p').html('Click on a style for a preview.');

	$('a', '#style-picker-palette').click(function() {

		var targetImage = $('#style-picker-image');

 		//show loading background
		targetImage.attr('src', '/images/blank.gif');

		//get title
		var title =  $(this).data('tooltip') || $(this).attr('title'); //incase hoverTip steals the title!

		$('#style-picker-label').html('Style shown: <strong>'+title+'</strong>');
		targetImage.attr('src', this.href);

		return false;
	});



	//coupon code? ******
	$('#coupon-code-button').click(function(event) {
		product_totals($("input[name='product_id']").val(), event); //product id
	});


	//product totals - only for certain pages *****
	if($('select.options').length) {
		init_product_totals($("input[name='product_id']").val()); //product id
	}


	//initiate key bindings and call the product_totals function *****
	function init_product_totals(product_id) {
		$('select.options').live('change keyup', function() { product_totals(product_id); });
		$('select.options-extras').live('change keyup', function() { product_totals(product_id); });
		product_totals(product_id);
	}
	
	
	//total price/total qty for product page ************************
	function product_totals(product_id, event) {
		var qty = 0;
		var extras = '';
		var coupon_code = '';
	
		//get qty of all select boxes
		$('select.options').each(function() { qty += parseFloat($('option:selected', this).val()); });


		//add extras?
		$('select.options-extras').each(function() {
			if($('option:selected', this).val() == 'yes') {
				extras = $(this).attr('title');
			}
		});


		//check coupon?
		if($('#coupon-code').length) {
			coupon_code = $('#coupon-code').val();
		}

		//check coupon - without a qty selected?
		if(typeof event != 'undefined') {
			if(event.target.id == 'coupon-code-button' && qty < 1) {
				$('#coupon-code-response').html("<strong class='red'>Select a Quantity first.</strong>");
				//alert('event target is:' + event.target.id);
				return false;
			}
		}

	
		//load price and insert html?
		if(qty > 0) {
			$('.total-price', '#buy-now-form').html('<em>loading...</em>');  //preloading status

			//load prices
			$('.total-price', '#buy-now-form').load('/shop_prices.inc.php?ajax=1&qty='+qty+'&product_id='+product_id+'&extras='+extras+'&coupon_code='+coupon_code, function(response, status, xhr) {

				//coupon code - did we receive one in the ajax request? (ajax response includes a div element with an ID for identification here, and also contains the data)
				if($('#coupon-code-response-text').length) {
					$('#coupon-code-response').html($('#coupon-code-response-text').html());
					$('#coupon-code-response-text').remove();
				} else {
					$('#coupon-code-response').html('');
				}

			});

			$('.total-price-label', '#buy-now-form').html('Total Price:');
			$('.total-qty-label', '#buy-now-form').html('Total Qty:');
			$('.total-qty', '#buy-now-form').html(qty);
		} else {
			$('#coupon-code-response').html('');
			$('.total-price', '#buy-now-form').html('');
			$('.total-price-label', '#buy-now-form').html('');
			$('.total-qty-label', '#buy-now-form').html('Total Qty:');
			$('.total-qty', '#buy-now-form').html('0');
		}
	
	}

});



