$(document).ready(function(){
	// PRODUCT OPTIONS ---------- /
	$("#form_choose_options .product_option").change(function(){
		var i = 0;
		var postData = {};

		$("#form_choose_options .product_option option:selected").each(function(){
			postData["option_" + i] = $(this).val();
			i++;
		});
		postData["product_id"]	= $("#form_choose_options #id").val();
		postData["qty"]			= $("#form_choose_options #qty").val();
		$.ajax({
			type:		"POST",
			url:		"/ajax/get_option_info.php",
			data:		(postData),
			async:		false,
			success:	function(xml){
							$(xml).find("element").each(function(){
								var dataId = $(this).find("id").text();
								var dataPriceChange = $(this).find("price_change").text();
								var dataOptionsTotal = $(this).find("options_total").text();
								var dataItemsTotal = $(this).find("items_total").text();
								var dataGrandTotal = $(this).find("grand_total").text();
								$("#form_choose_options #option_cost_" + dataId).html(dataPriceChange);
								$("#form_choose_options #options_total").html(dataOptionsTotal);
								$("#form_choose_options #items_total").html(dataItemsTotal);
								$("#form_choose_options #grand_total").html(dataGrandTotal);
							});
						}
		});
		return false;
	});
	
	// PRODUCT OPTIONS FOR DETAILS ---------- /
	$(".update_options .product_option").change(function(){
		var i = 0;
		var postData = {};
		var theID = $(this).parent().parent().find(".product_id").val();

		$("#form_choose_options_" + theID + " .product_option").find("option:selected").each(function(){
			postData["option_" + i] = $(this).val();
			i++;
		});
		postData["product_id"]	= $("#form_choose_options_" + theID + " .product_id").val();
		postData["qty"]			= $("#form_choose_options_" + theID + " .qty").val();
		$.ajax({
			type:		"POST",
			url:		"/ajax/get_option_info.php",
			data:		(postData),
			async:		false,
			success:	function(xml){
							$(xml).find("element").each(function(){
								var dataId = $(this).find("id").text();
								var dataPriceChange = $(this).find("price_change").text();
								var dataOptionsTotal = $(this).find("options_total").text();
								var dataItemsTotal = $(this).find("items_total").text();
								var dataGrandTotal = $(this).find("grand_total").text();
								$("#form_choose_options_" + theID + " .prod_cost").html(dataGrandTotal);
							});
						}
		});
		return false;
	});
	
	// SEARCH ---------- /
	$("#search_results_go").click(function(){
		location.href = "/products/keywords/" + $("#keywords_search").val() + "/";
		return false;
	});
	
	// INDEX IMG ROTATE ---------- /
	$('#img_rotation').innerfade({
		speed: 'slow',
		timeout: 5000,
		type: 'sequence',
		containerheight: '260px'
	});
	
	// VIEW CART ---------- /
	$("#form_cart #update_button").click(function(){
		$("#form_cart #action").val("update");
		$("#form_cart").submit();
	});
	$("#form_cart #continue_shopping_button").click(function(){
		location.href = "/continue_shopping.php";
	});
	$("#form_cart #update_button2").click(function(){
		$("#form_cart #action").val("update");
		$("#form_cart").submit();
	});
	$("#form_cart #checkout_button").click(function(){
		$("#form_cart #action").val("checkout");
		$("#form_cart").submit();
	});
	$(".removeItem").click(function(){
		return confirm("Are you sure you want to remove this product from your cart?");
	});
	
	// CHECKOUT ---------- /
	$(".update_shipping_method").change(function(){
		location.href = "/checkout.php?shipping_method_id=" + this.value;
	});
	$("#checkout_place_order_button").click(function(){
		$("#checkout_place_order").hide("fast");
		$("#checkout_processing").show("fast");
		$("#form_checkout").submit();
		return false;
	});
	$("#apply_promo_code").click(function(){
		$("#form_promo_code").submit();
		return false;
	});
	
	// NAV ---------- /
	$(".menu a").hover(function() {
		$(this).next("em").show();
	}, function() {
		$(this).next("em").hide();
	});
	
	$("em.sub01").hover(function() {
		$("em.sub01").show();
	}, function() {
		$(this).hide();
	});
	
	$("em.sub02").hover(function() {
		$("em.sub02").show();
	}, function() {
		$(this).hide();
	});
	
	$("em.sub03").hover(function() {
		$("em.sub03").show();
	}, function() {
		$(this).hide();
	});
	
	$("em.sub04").hover(function() {
		$("em.sub04").show();
	}, function() {
		$(this).hide();
	});
	//FORMS ---------- /
	$(".submit_recipe").click(function(){
		if($("#name").val() == "")
		{
			alert("You must enter your name in order to continue");
			$("#name").focus();
			return false;
		}
		if($("#email").val() == "")
		{
			alert("You must enter your email in order to continue");
			$("#email").focus();
			return false;
		}
		if($("#recipe_name").val() == "")
		{
			alert("You must enter your recipe_name in order to continue");
			$("#recipe_name").focus();
			return false;
		}
		if($("#ingredients").val() == "")
		{
			alert("You must enter your ingredients in order to continue");
			$("#ingredients").focus();
			return false;
		}
		if($("#instructions").val() == "")
		{
			alert("You must enter your instructions in order to continue");
			$("#instructions").focus();
			return false;
		}
		
		return true;
	});
	
	$(".submit_retailers").click(function(){
		if($("#name").val() == "")
		{
			alert("You must enter your name in order to continue");
			$("#name").focus();
			return false;
		}
		if($("#email").val() == "")
		{
			alert("You must enter your email in order to continue");
			$("#email").focus();
			return false;
		}
		if($("#company").val() == "")
		{
			alert("You must enter your company in order to continue");
			$("#company").focus();
			return false;
		}
		if($("#license").val() == "")
		{
			alert("You must enter your license in order to continue");
			$("#license").focus();
			return false;
		}
		if($("#address1").val() == "")
		{
			alert("You must enter your address in order to continue");
			$("#address1").focus();
			return false;
		}
		if($("#city").val() == "")
		{
			alert("You must enter your city in order to continue");
			$("#city").focus();
			return false;
		}
		if($("#state").val() == "")
		{
			alert("You must enter your state in order to continue");
			$("#state").focus();
			return false;
		}
		if($("#zip").val() == "")
		{
			alert("You must enter your zip in order to continue");
			$("#zip").focus();
			return false;
		}
		if($("#phone").val() == "")
		{
			alert("You must enter your phone in order to continue");
			$("#phone").focus();
			return false;
		}
		if($("#description").val() == "")
		{
			alert("You must enter your business description in order to continue");
			$("#description").focus();
			return false;
		}
		
		return true;
	});
	
	$(".submit_request").click(function(){
		if($("#name").val() == "")
		{
			alert("You must enter your name in order to continue");
			$("#name").focus();
			return false;
		}
		if($("#address1").val() == "")
		{
			alert("You must enter your address in order to continue");
			$("#address1").focus();
			return false;
		}
		if($("#city").val() == "")
		{
			alert("You must enter your city in order to continue");
			$("#city").focus();
			return false;
		}
		if($("#state").val() == "")
		{
			alert("You must enter your state in order to continue");
			$("#state").focus();
			return false;
		}
		if($("#zip").val() == "")
		{
			alert("You must enter your zip in order to continue");
			$("#zip").focus();
			return false;
		}
		
		return true;
	});
	
	$(".submit_comments").click(function(){
		if($("#name").val() == "")
		{
			alert("You must enter your name in order to continue");
			$("#name").focus();
			return false;
		}
		if($("#email").val() == "")
		{
			alert("You must enter your email in order to continue");
			$("#email").focus();
			return false;
		}
		
		return true;
	});
	
	$(".submit_community_service").click(function(){
		if($("#name").val() == "")
		{
			alert("You must enter your name in order to continue");
			$("#name").focus();
			return false;
		}
		if($("#email").val() == "")
		{
			alert("You must enter your email in order to continue");
			$("#email").focus();
			return false;
		}
		if($("#group").val() == "")
		{
			alert("You must enter your group name in order to continue");
			$("#group").focus();
			return false;
		}
		if($("#phone").val() == "")
		{
			alert("You must enter your phone number in order to continue");
			$("#phone").focus();
			return false;
		}
		if($("#function").val() == "")
		{
			alert("You must enter your function in order to continue");
			$("#function").focus();
			return false;
		}
		if($("#date").val() == "")
		{
			alert("You must enter your date in order to continue");
			$("#date").focus();
			return false;
		}
		if($("#item").val() == "")
		{
			alert("You must enter your item in order to continue");
			$("#item").focus();
			return false;
		}
		if($("#needed_by").val() == "")
		{
			alert("You must enter when you need yourr item by in order to continue");
			$("#needed_by").focus();
			return false;
		}
		if($("#comments").val() == "")
		{
			alert("You must enter your comments in order to continue");
			$("#comments").focus();
			return false;
		}
		
		return true;
	});
	
	
	
	//GENERAL -------- /
	$(".go_back").click(function(){
		history.go(-1);
		return false;
	});
	$("a.imgZoom").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'overlayShow':	true,
		'overlayOpacity': .8
	});
	$(".print_this").click(function(){
		window.print();
		return false;
	});
	$(".confirm_delete").click(function(){
		return confirm("Are you sure you want to delete this?");
	});
});