// JavaScript Document
// Arrays in arrays.js


// Defining calculateFn in calculate.js... try doing it just for the hashtags first

jQuery(document).ready(function() {
$('#full-size').hide();
$('#pregnant .note, #pregnant .note-shadow, #cholesterol-note .note, #cholesterol-note .note-shadow, .salmon-info').hide();	
$('#pregnant a').click(function() {
	$('#pregnant .note, #pregnant .note-shadow').toggle();
	});	
$('#cholesterol-note a').click(function() {
	$('#cholesterol-note .note, #cholesterol-note .note-shadow').toggle();
	});
$('#farmed-wild a').click(function() {
	$('.nutrition').toggle();
	$('.salmon-info').toggle();
	});
	
$('#result').hide();
$('#yourWeight').focus();	
// Revision: Added this to change state of document based on hash (right now, only if hash is typed in the URL!)
var hash = window.location.hash;
if (hash == "#calamari") {
		calculateFn("calamari", 24, 150, 6);
} 
else if (hash == "#anchovies") {
		calculateFn("anchovies", 15, 150, 6);
} 
else if (hash == "#crab") {
		calculateFn("crab", 9, 150, 6);
}
else if (hash == "#tuna-sandwich") {
		calculateFn("tuna-sandwich", 2, 150, 6);
}
else if (hash == "#tuna-salad") {
		calculateFn("tuna-salad", 3, 150, 6);
}
else if (hash == "#catfish") {
		calculateFn("catfish", 8, 150, 6);
}
else if (hash == "#clams") {
		calculateFn("clams", 11, 150, 6);
}
else if (hash == "#cod") {
		calculateFn("cod", 10, 150, 6);
}
else if (hash == "#flounder") {
		calculateFn("flounder", 12, 150, 6);
}
else if (hash == "#grouper") {
		calculateFn("grouper", 25, 150, 6);
}
else if (hash == "#halibut") {
		calculateFn("halibut", 16, 150, 6);
}
else if (hash == "#lobster") {
		calculateFn("lobster", 14, 150, 6);
}
else if (hash == "#oysters") {
		calculateFn("oysters", 18, 150, 6);
}
else if (hash == "#mackerel") {
		calculateFn("mackerel", 20, 150, 6);
}
else if (hash == "#pollock") {
		calculateFn("pollock", 6, 150, 6);
}
else if (hash == "#farmed-salmon") {
		calculateFn("farmed-salmon", 5, 150, 6);
}
else if (hash == "#wild-salmon") {
		calculateFn("wild-salmon", 4, 150, 6);
}
else if (hash == "#sardines") {
		calculateFn("sardines", 19, 150, 6);
}
else if (hash == "#scallops") {
		calculateFn("scallops", 17, 150, 6);
}
else if (hash == "#shrimp") {
		calculateFn("shrimp", 1, 150, 6);
}
else if (hash == "#snapper") {
		calculateFn("snapper", 23, 150, 6);
}
else if (hash == "#swordfish") {
		calculateFn("swordfish", 21, 150, 6);
}
else if (hash == "#tilapia") {
		calculateFn("tilapia", 7, 150, 6);
		}
else if (hash == "#trout") {
		calculateFn("trout", 22, 150, 6);
		}
else if (hash == "#yellowfin-tuna") {
		calculateFn("yellowfin-tuna", 13, 150, 6);
		}
else {  // End Hash tags
	
	$('.fish-thumb a').click(function() {
			var image = $(this).attr("rel");
			var yourWeightLbs = parseInt($("input#yourWeight").val());
			var fishSizeOz = $("select#fishSize").val();
			var fishType = $(this).parent().attr("id")
			var	fishName = nutrArr[fishType][0];
			var Omega = ((parseFloat(nutrArr[fishType][1] * fishSizeOz)) + (parseFloat(nutrArr[fishType][2] * fishSizeOz))).toFixed(2);
			var OmegaRDI = (nutrArr[fishType][8] * fishSizeOz).toFixed(1);
			var Protein = (nutrArr[fishType][3] * fishSizeOz).toFixed(2);
			var ProteinRDI = (nutrArr[fishType][9] * fishSizeOz).toFixed(1);
			var B12 = (nutrArr[fishType][4] * fishSizeOz).toFixed(2);
			var B12RDI = (nutrArr[fishType][10] * fishSizeOz).toFixed(1);
			var Potassium = (nutrArr[fishType][5] * fishSizeOz).toFixed(2);
			var PotassiumRDI = (nutrArr[fishType][11] * fishSizeOz).toFixed(1);
			var Selenium = (nutrArr[fishType][6] * fishSizeOz).toFixed(2);
			var SeleniumRDI = (nutrArr[fishType][12] * fishSizeOz).toFixed(1);
			var Iron = (nutrArr[fishType][7] * fishSizeOz).toFixed(2);
			var IronRDI = (nutrArr[fishType][13] * fishSizeOz).toFixed(1);
			var Prep = (nutrArr[fishType][14]);
			var Calories = (nutrArr[fishType][15] * fishSizeOz).toFixed(0);
			var Fat = (nutrArr[fishType][16] * fishSizeOz).toFixed(2);
			var Sodium = (nutrArr[fishType][17] * fishSizeOz).toFixed(2);
			var RecipeURL =  (nutrArr[fishType][18]);
			var Merc = mercArr[fishType][1];
			if (Merc == 0) {
				var MercOz = 0;
				var MercNote = "The level of detectable mercury is too low to pose any hypothetical health risk, regardless of weekly intake. You may eat an unlimited amount without adverse health effects from mercury.";
			} else {
				var MercOz = parseInt((yourWeightLbs * 0.112 ) / Merc);
				var MercNote = "";
			}
			var MercLbs = (MercOz/16).toFixed(2);
			if (yourWeightLbs > 600 || yourWeightLbs < 30 ) {
				alert('Please enter a weight between 30 and 600 lbs');	
			} else {
				$('#fish-thumb-wrap').fadeOut(150);
				$('#full-photo').html('<img src="./images/full/' + image + '.jpg"/><a id="recipe" href="javascript;:" target="_blank">Recipes</a>');
				$('#recipe').attr("href", RecipeURL);
				$('#portionVar').html(portionText[fishSizeOz]);
				$('#OmegaVar').html(Omega);
				$('#OmegaRDIVar').html(OmegaRDI);
				$('#ProteinVar').html(Protein);
				$('#ProteinRDIVar').html(ProteinRDI);
				$('#B12Var').html(B12);
				$('#B12RDIVar').html(B12RDI);
				$('#PotassiumVar').html(Potassium);
				$('#PotassiumRDIVar').html(PotassiumRDI);
				$('#SeleniumVar').html(Selenium);
				$('#SeleniumRDIVar').html(SeleniumRDI);
				$('#IronVar').html(Iron);
				$('#IronRDIVar').html(IronRDI);
				$('#FatVar').html(Fat);
				$('#SodiumVar').html(Sodium);			
				$('#CaloriesVar').html(Calories);
				$('#PrepVar').html(Prep);
				$('#MercOz').html(MercOz);
				if ((fishName == "canned Albacore Tuna") || (fishName == "canned Light Tuna")) {
					$('#cans').html('' + (MercOz/5) + ' 5-oz. cans');
				} else {
					$('#cans').html('<span id="MercLbs"></span> pounds');
					$('#MercLbs').html(MercLbs);
				}
				$('#MercNote').html(MercNote);
				if (Merc == 0) {
					$('#mercOutput').hide();	
				} else {
				};
				
				if ((fishName == "Farmed Salmon") || (fishName == "Wild Salmon")) {
					$('#farmed-wild').html('(<a href="javascript:;">Confused about farmed vs. wild?</a>)');
					$('#farmed-wild a').click(function() {
						$('.nutrition').toggle();
						$('.salmon-info').toggle();
					});
				} 
				else {
					$('#farmed-wild').html('');	
				}
				
				$('.fishName').html(fishName);
				$('#full-size').fadeIn(500);
				$('#start').fadeOut(500);
				$('#result').fadeIn(500);
			}
			return false;
	});
	$('#reset').click(function() {
			$('.salmon-info').hide();
			$('#mercOutput, .nutrition').show();
			$('#full-size').fadeOut(150);
			$('#fish-thumb-wrap').fadeIn(500);
			$('#result').fadeOut(500);
			$('#start').fadeIn(500);
	});

}
});
