$(function() {
	var package_checkbox = $('input[@name=package_checkbox]');
	var type_usb = $('#product_item_list input[@name="type_usb[]"]');
	var logo_type = $('#product_logo_list input[@name="logo_type[]"]');
	var type_usb_index = 0;
	var single_package_price = 19.99;
	var double_package_price = 24.99;
	var triple_package_price = 29.99;
	var usb_price = 12;
	var shipping_price = 4.95;
	var items_price = single_package_price;
	var total_price = 0;
	var package_index = 1;
	package_checkbox.click(function() {
		if ($(this).val() == 3) {
			type_usb.each(function() {
				$(this).attr('checked',true);
				$(this).attr('disabled',true);
			});
		} else if ($(this).val() == 2) {
			if ( $('#product_item_list input[@name="type_usb[]"]:checked').length  == 2 ) {
				type_usb.each(function() {
					if ( !$(this).attr('checked') ) {
						$(this).attr('disabled',true);
					}
				});
			} else if ( $('#product_item_list input[@name="type_usb[]"]:checked').length  > 2 ) {
				type_usb.each(function() {
					$(this).attr('checked',false)
					$(this).attr('disabled',false);
				});
			} else {
				type_usb.each(function() {
					if ( !$(this).attr('checked') ) {
						$(this).attr('disabled',false);
					}
				});
			}
			
			
		} else {
			type_usb.each(function() {
				$(this).attr('checked',false);
				$(this).attr('disabled',false);
			});
			$(type_usb[type_usb_index]).attr('checked',true);
		}
		calculatePrice();
	});
	
	type_usb.click(function() {
		package_index = getPackageIndex();
		if ( package_index == 1 ) {
			type_usb_index = type_usb.index(this);
			type_usb.each(function() {
				$(this).attr('checked',false);
			});
			$(this).attr('checked',true);
		} else if ( package_index == 2 ) {
			type_usb_index = type_usb.index(this);
			if ( $('#product_item_list input[@name="type_usb[]"]:checked').length  == 2 ) {
				type_usb.each(function() {
					if ( !$(this).attr('checked') ) {
						$(this).attr('disabled',true);
					}
				});
			} else if ( $('#product_item_list input[@name="type_usb[]"]:checked').length  > 2 ) {
				type_usb.each(function() {
					$(this).attr('checked',false)
					$(this).attr('disabled',false);
				});
			} else {
				type_usb.each(function() {
					if ( !$(this).attr('checked') ) {
						$(this).attr('disabled',false);
					}
				});
			}
		}
	});
	
	logo_type.click(function() {
		package_index = getPackageIndex();
		logo_type.each(function() {
			$(this).attr('checked',false);
		});
		$(this).attr('checked',true);
	});
	
	$('#quantity').keyup(calculatePrice);
	
	calculatePrice();
	
	$('#checkout_button').click(function() {
		//check required attributes
		var type_usb_checked = $('#product_item_list input[@name="type_usb[]"]:checked');
		var logo_type_checked = $('#product_logo_list input[@name="logo_type[]"]:checked');
	
		package_index = getPackageIndex();
		if ( logo_type_checked.length != 1 ) {
			alert ('You must choose one Logo');
			location.hash = 'logo';
			return false;
		}
		if ( type_usb_checked.length < package_index ) {
			alert ('You must choose '+ package_index +' of Memory Traveldrive covers');
			location.hash = 'item';
			return false;
		}
		//send form
		var form = $('#checkout_form');
		form.empty();
		type_usb_checked.each(function() {
			form.append('<input type="hidden" name="type_item[]" value="' + $(this).val() + '" />');
		});
		form.append('<input type="hidden" name="package_items" value="' + package_index + '" />');
		logo_type_checked.each(function() {
			form.append('<input type="hidden" name="logo_type[]" value="' + $(this).val() + '" />');
		});
		form.append('<input type="hidden" name="quantity" value="' + $("#quantity").val() + '" />');
		form.append('<input type="hidden" name="action" value="checkoutProcess" />');
		
		form.submit();
	});
	
	function calculatePrice() {
		var quantity = parseInt($("#quantity").val());
		if ( isNaN(quantity) ) {
			quantity = 0;
		}
		$("#quantity").attr('value',quantity);
		
		package_index = getPackageIndex();
		if ( package_index == 3 ) {
			items_price = triple_package_price;
		} else if ( package_index == 2 ) {
			items_price = double_package_price;
		} else {
			items_price = single_package_price;
		}
		
		total_price = items_price + (quantity * usb_price) + shipping_price;
		
		$('#total_price span').html('$' + total_price.toFixed(2));
	}
	
	function getPackageIndex() {
		var flag = false;
		package_checkbox.each(function(){
			if ($(this).attr('checked') == true ) {
				package_index = $(this).val();
				flag = true;
			}
		});
		if ( !flag ) {
			$(package_checkbox[0]).attr('checked',true);
			package_index = 1;
		}
		
		return package_index;
	}
});
