$(function(){
	var prefetchImageSrcs = [
		'images/product_th_i_on.gif',
		'images/product_th_i_on.gif',
		'images/product_th_fi_on.gif',
		'images/product_th_fi_on.gif',
		'images/product_th_ga_on.gif',
		'images/product_th_ga_on.gif',
		'images/product_shot_link_1_on.gif',
		'images/product_shot_link_1_off.gif',
		'images/product_shot_link_2_on.gif',
		'images/product_shot_link_2_off.gif',
		'images/product_shot_link_3_on.gif',
		'images/product_shot_link_3_off.gif',
		'images/product_shot_mini.jpg',
		'images/product_shot_small.jpg',
		'images/product_shot_large.jpg',
		'images/product_shot_an_mini.jpg',
		'images/product_shot_an_small.jpg'
	];
	
	$('#Product_Ingredients_Header').bind(		'click',			function() { openProductInfo('Ingredients');		});
	$('#Product_Feeding_Header').bind(				'click',			function() { openProductInfo('Feeding');				});
	$('#Product_Analysis_Header').bind(				'click',			function() { openProductInfo('Analysis');				});
	$('#Production_Description_Link').bind(		'click',			function() { openProductInfo('Description');		});
	
	$('#Product_Shot_Link_Mini').bind(				'mouseover',	function() { swapPackageShot('Mini');						});
	$('#Product_Shot_Link_Small').bind(				'mouseover',	function() { swapPackageShot('Small');					});
	if (!allNatural) {
		$('#Product_Shot_Link_Large').bind(			'mouseover',	function() { swapPackageShot('Large');					});
	}
	
	$('#Feeding_Link_Mini').bind(							'click',			function() { swapFeedingInstructions('Mini');		});
	$('#Feeding_Link_Small').bind(						'click',			function() { swapFeedingInstructions('Small');	});
	if (!allNatural) {
		$('#Feeding_Link_Large').bind(					'click',			function() { swapFeedingInstructions('Large');	});
	}
	
	prefetchImages(prefetchImageSrcs);
});

var openProductInfo = function(panelName) {
	toggleImageStatus($('#Product_Ingredients_Header'),'off');
	toggleImageStatus($('#Product_Feeding_Header'),'off');
	toggleImageStatus($('#Product_Analysis_Header'),'off');
	
	$('.Product_Information_Panel').hide();
		
	if (panelName!='Description') {
		toggleImageStatus($('#Product_'+panelName+'_Header'),'on');
		$('#Production_Description_Link').show();
	} else {
		$('#Production_Description_Link').hide();
	}
	
	$('#Product_'+panelName).fadeIn('fast');
}

var swapPackageShot = function(packageName) {
	toggleImageStatus($('#Product_Shot_Link_Mini'),'off');
	toggleImageStatus($('#Product_Shot_Link_Small'),'off');
	if (!allNatural) {
		toggleImageStatus($('#Product_Shot_Link_Large'),'off');
	}
	
	toggleImageStatus($('#Product_Shot_Link_'+packageName),'on');
	
	if (allNatural) {
		$('#Package_Shot').attr('src','images/product_shot_an_'+packageName+'.jpg');
	} else {
		$('#Package_Shot').attr('src','images/product_shot_'+packageName+'.jpg');
	}
}

var swapFeedingInstructions = function(productSize) {
	toggleImageStatus($('#Feeding_Link_Mini'),'off');
	toggleImageStatus($('#Feeding_Link_Small'),'off');
	if (!allNatural) { toggleImageStatus($('#Feeding_Link_Large'),'off'); }
	
	toggleImageStatus($('#Feeding_Link_'+productSize),'on');
	
	$('.Product_Feeding_Panel').hide();
	$('#Product_Feeding_'+productSize).fadeIn('fast');
}

var prefetchImages = function(prefetchImageSrcs) {
	var allImages = [];
	for (var i=0;i<prefetchImageSrcs.length;i++) {
		allImages[i] = new Image();
		allImages[i].src = prefetchImageSrcs[i];
	}
}

var toggleImageStatus = function(imageObject,status) {
	if (status=='on') {
		imageObject.attr('src',imageObject.attr('src').replace('_off','_on'));
	} else {
		imageObject.attr('src',imageObject.attr('src').replace('_on','_off'));
	}
}