var nutrArr = new Array(
	Array("Raw nutrient data (1 oz. serving)","Long Chain Omega-3 EPA","Long Chain Omega-3 DHA","Protein","Vitamin B12","Potassium","Selenium","Iron","Omega-3 Fatty Acids","Protein","Vitamin B12","Potassium","Selenium","Iron"),
	Array("shrimp","0.048","0.041","5.9","0.4","52","11.2","0.88","17.8%","11.8%","7.1%","1.5%","20.4%","4.9%"),
	Array("canned albacore tuna","0.066","0.178","6.7","0.3","67","18.6","0.27","48.9%","13.4%","5.5%","1.9%","33.8%","1.5%"),
	Array("canned light tuna","0.008","0.029","8.3","0.6","59","21.5","0.39","7.3%","16.5%","10.4%","1.7%","39.2%","2.2%"),
	Array("wild salmon","0.116","0.405","7.2","0.9","178","13.3","0.29","104.3%","14.4%","14.4%","5.1%","24.1%","1.6%"),
	Array("farmed salmon","0.196","0.413","6.3","0.8","109","11.7","0.10","121.7%","12.5%","13.2%","3.1%","21.3%","0.5%"),
	Array("pollock","0.026","0.128","7.1","1.0","129","13.3","0.17","30.7%","14.1%","17.4%","3.7%","24.1%","0.9%"),
	Array("tilapia","0.001","0.037","7.4","0.5","108","15.4","0.20","7.7%","14.8%","8.8%","3.1%","28.0%","1.1%"),
	Array("catfish","0.014","0.036","5.3","0.8","91","4.1","0.23","10.1%","10.6%","13.2%","2.6%","7.5%","1.3%"),
	Array("blue crab","0.069","0.065","5.7","2.1","92","11.4","0.26","26.9%","11.4%","34.5%","2.6%","20.7%","1.4%"),
	Array("cod","0.001","0.044","6.5","0.3","69","10.7","0.14","8.9%","12.9%","4.9%","2.0%","19.4%","0.8%"),
	Array("clams","0.039","0.041","7.2","28.0","178","18.1","7.92","16.1%","14.5%","467.0%","5.1%","33.0%","44.0%"),
	Array("flounder/sole","0.069","0.073","6.8","0.7","97","16.5","0.10","28.4%","13.7%","11.8%","2.8%","30.0%","0.5%"),
	Array("","g","g","g","mcg","mg","mcg","mg","","","","","","")
);

var mercArr = new Array(
	Array("",0),
	Array("shrimp",0),
	Array("canned albacore tuna",0.353),
	Array("canned light tuna",0.118),
	Array("wild salmon",0.140),
	Array("farmed salmon",0.140),
	Array("pollock",0.041),
	Array("tilapia",0.010),
	Array("catfish",0.049),
	Array("blue crab",0.06),
	Array("cod",0.095),
	Array("clams",0),
	Array("flounder/sole",0.045)
);
var fishType = 0;
var fishSize = 0;
var perc = 0.0;
var amt = 0.0;
var isAllFishView = true; // Is the main panel showing all fish (false) or just one plus the slider (true)?
var yourWeight = 0;

function swapContent(fishOunces,fishName,userWeight) {
	$('#start').fadeOut(250, function() {
		$('#result .fishOunces').html(fishOunces + ' ');
		$('#result .fishSpecies').html(fishName + ' ');
		$('#result .yourWeight').html(userWeight + '');
		$('#result').fadeIn(250);
	});
}

function showVals() {
	var f = $('#calcOutput');
	f.html('');
	
	// fish name
	// $('#fishName').html(nutrArr[fishType][0]);
	
	// add omega 3, parse into floats, multiply by amount, compensate for rounding issues
	percStr = nutrArr[fishType][8];
	percStr = percStr.substring(0,percStr.length-1); // remove % symbol
	perc = Math.round( (parseFloat(percStr) * fishSize) *100) /100;
	amt = Math.round( ( (parseFloat(nutrArr[fishType][1]) + parseFloat(nutrArr[fishType][2])) * fishSize) *100) /100;
	var tmp0 = '';
	tmp0 +='<br /><strong>Proven health benefits of a '+fishSize+' oz. serving of '+nutrArr[fishType][0]+':</strong>';
	tmp0 +='<ul><li><small><span class="nutrient">Long Chain Omega-3:</span> ' + amt + 'g (' + perc + '% of RDI)</small></li>';
	
	// loop through all non omega 3
	for(x=3;x<8;x++) {
		
		percStr = nutrArr[fishType][x+6];
		percStr = percStr.substring(0,percStr.length-1); // remove % symbol
		perc = Math.round( (parseFloat(percStr) * fishSize)*100)/100;
		amt = Math.round( (parseFloat(nutrArr[fishType][x]) * fishSize)*100)/100;
		
		tmp0 +='<li><small><span class="nutrient">' + nutrArr[0][x] + ':</span> ' + amt + nutrArr[13][x] + ' (' + perc + '%)</small></li>';
	}
	tmp0 +='</ul>';
	f.append(tmp0);
}
function showMerc(weight) {
	m = $('#mercOutput');
	fishName = mercArr[fishType][0];
	div = mercArr[fishType][1];
	if (div > 0) {
		fishOunces = parseInt(((weight* 0.112 ) / div)*10)/10;
		fishPounds = parseInt((((weight* 0.112 ) / div) / 16)*10)/10;
		swapContent(fishOunces,fishName,weight);
		
		m.html('<p style="width:300px;"><strong>Hypothetical health risks:</strong><br />Your hypothetical health risk from the mercury in '+fishName+' begins at a weekly intake of:<div class="calcOuncesDiv"><span class="calcOunces">'+fishOunces+'</span> ounces ('+fishPounds+' pounds)</div></p>');
	} else {
		m.html('<p style="width:300px;">The level of detectable mercury in '+fishName+' is too low to pose any hypothetical health risk, regardless of weekly intake. You may eat an unlimited amount of '+fishName+' without adverse health effects from mercury.</p>');
	}
}

function checkIfReady() {
	// alert(window.fishType + ' ' + window.yourWeight + ' ' + window.fishSize);
	// check to see if we have good values
	if(window.fishType > 0 && Math.ceil(window.yourWeight) == Math.floor(window.yourWeight) && window.yourWeight > 0  && Math.ceil(window.fishSize) == Math.floor(window.fishSize) && window.fishSize > 0) {
		if ($("#submit").attr("disabled")) {
			$('#submit').removeAttr('disabled');
		} else {
			runCalc();
		}
	}
}

function runCalc() {
	$('#fishCalcDiv').fadeOut(250, function() {
		$('#back').show(0);
		$('#calcOutput').show(0);
		$('#mercOutput').show(0);
		showVals();
		showMerc(yourWeight);
	});
}

function resetCalc() {
	document.forms['fishCalc'].reset();
	$("#submit").attr("disabled", true);
	$('#back').hide(0);
	$('#feature_fish_one').hide(0);
	$('#feature_fish_all').fadeIn('fast');
	$('#calcOutput').hide(0);
	$('#mercOutput').hide(0);
	$('#result').hide(0);
	$('#fishCalcDiv').fadeIn(250);
	$('#start').fadeIn(250);
}

$(document).ready(function() {
	document.forms['fishCalc'].reset();
	$('#back').hide(0);
	$('#result').hide(0);
	$("#submit").attr("disabled", true);
	

	$('.fishSelect').click(function() {
									
		// get fish number from id
		fishType = parseInt($(this).attr('id').substring(6));
		if(window.isAllFishView) {
			// switch to single fish view
			$('#feature_fish_all').hide(0);
			$('#feature_fish_one').fadeIn('fast');
			window.isAllFishView = false;
		}
		// set the single fish image to the appropriate image
		$('#feature_fish_selected').attr('src','img/fish/big/'+fishType+'.png');
		$('#feature_fish_selected').attr('alt',window.nutrArr[fishType][0]);
		$('body').supersleight({shim: 'img/x.gif'});
		$('.fishTitle').html(window.nutrArr[fishType][0] + ' ');
		// remove any existing class
		for(x=1;x<13;x++) {
			if(window.fishType == x) {
				$('#calcFishName').addClass('fishTitle'+x);
			} else {
				$('#calcFishName').removeClass('fishTitle'+x);
			}
		}
		$('#calcFishName').addClass('fishTitle'+window.fishType);
		checkIfReady();
		pageTracker._trackPageview('/fish/'+mercArr[fishType][0]+'/');
		return false;
	});
	
	$('#yourWeight').change(function() {
		window.yourWeight = parseInt($('#yourWeight').val());
		checkIfReady();
	});
	$('#fishSize').change(function() {
		window.fishSize = parseInt($('.fishSizeCheck:selected').val());
		checkIfReady();
	});
	
	
	$('#submit').click(function() {
		runCalc();
		pageTracker._trackPageview('/submit/'+mercArr[fishType][0]+'/');
		return false;
	});
	
	$('#back').click(function() {
		resetCalc();
		return false;
	});
	
	//  $('#tweets').load('twitter.cfml');
	//	$("#feature_fish_slider").easySlider();

});