$(document).ready(function() {
	
	$('#newsletter').click(function(){
		$(this).attr('value', '');
	});
	
	$('#discount_opener').click(function() {
		$('#discount_code').slideDown();
		$(this).slideUp();
		return false;
	});
	
	var clicked = false;
	
	$('#submit_discount').click(function() {
		// Get the user supplied code & make lowercase
		var $code = $('#discount').attr('value').toLowerCase();
		
		// Declare our discount variable
		var $discount;
		// Here the code gives us our discount array, so far, just enter the % off of the price
		//CAMT code
		if ($code == 'camt') {
			$discount = ['20'];
		}
		else if ($code == 'disc20') {
			$discount = ['20'];
		}
		else if ($code == 'teacher') {
			$discount = ['10'];
		}
		else if ($code == 'bc20off') {
			$discount = ['20'];
		}
		
		// Coupon code for CAMT
		if ($discount !== undefined) {
			// Disable Submit Button after it works
			$('#submit_discount').attr('disabled',true);
			// Get each of the input fields
			$('.pp_price.disc').each(function() {
				// Get the price of the item
				var $price = $(this).attr('value');
				// Turn our discount into a decimal named percentOFf
				$percentOff = (100 - $discount[0])/100;
				// Multiply the price with the discount
				$price = $price * $percentOff;
				// Make a Number object so we can round proper
				var p = new Number($price);
				// Round the number
				p = p.toFixed(2);
				// Change value in the PayPal button
				$(this).attr('value', p);
				// Change the value on the site
				$(this).parents('div.product_bubble.disc').find('#worst').html('$'+p);
				// Remove discount box
				$('#discount_code').fadeOut();
			});
		} else	{
			// If no code is good, alert and do nothing
			alert('Discount code is not valid.');
		}		
	return false;
	});
	
	// Drop down for type of product selection
	$('.type').each(function() {
		$(this).change(function() {
			var type = $(this).attr('value');
			div = $(this).parents('div').attr('id');
			if (div == 'fact_flip') {
				temp_name = "Math Fact Flip - Single Unit";
			}
			else if (div == 'deluxe_fact_flip') {
				temp_name = "Deluxe Fact Flip - Single Unit"
			}
			else if (div == 'double_fact_flip') {
				temp_name = "Fact Flip - Double Unit"
			}
			else if (div == 'class_fact_flip') {
				temp_name = 'Math Fact Flip - Class Set';
			}
			
			name = temp_name + ' - ' + type;
			$('#' + div + ' #item_name').attr('value', name);
		});
	});
})