var productListURL;

var servingSize;
var openTab;
var postId;

function initPage (){

	setTabNav();
	setBackButton();
	
	if (document.getElementById("servingSize") == null)
	{
		var firstDiv = document.getElementById("prodInfoContainer").getElementsByTagName("div")[1].id;
		showInfo(firstDiv,'');
	}
}

function showInfo (selectedVal,clickedLink) {

	if (selectedVal){
		servingSize = selectedVal;
	}
	
	if (clickedLink){
		openTab = clickedLink;
	}
	
	if (servingSize){
	
		// first, hide everything
		hideContent();
		
		// now open the selected serving div
		var elm = document.getElementById(servingSize);
		elm.style.display = "block";
		
		// set the appropriate tab backgound image and display the  
		// nutrition facts OR the heating instructions 
		var prodInfoContainer = document.getElementById("prodInfoContainer");
		
		if (openTab == 'hInstruc'){
			// show heating instructions info
			elm.getElementsByTagName("div")[1].style.display = "block";
						
			// set the bg tab
			prodInfoContainer.className = "heatingOn";
		}	
		else {
			// show nutrition facts info
			elm.getElementsByTagName("div")[0].style.display = "block";
			
			// set the bg tab
			prodInfoContainer.className = "heatingOff";
		}
		
		setPrintBtn();
	}
	else {
		alert('Please select a serving size.')
		openTab = "";
	}
}

function setPrintBtn (){

	// first remove any previously existing print buttons
	if (document.getElementById("printBtn")){
		document.getElementById("tabLinkContainer").removeChild(document.getElementById("printBtn"));
	}
	
	var span = document.createElement("span");
	span.appendChild(document.createTextNode("PRINT"));
	span.id='printBtn';
	span.onclick = launchPrint;
	document.getElementById("tabLinkContainer").appendChild(span)
}

function launchPrint(){

	var printURL = appPath + '/MyStuff/printProduct.aspx?postId=' + postId + '&size=' + servingSize;

	superPopup({url:printURL,width:630,height:650,scrollbars:'yes',toolbar:'no'})
}

function setTabNav (){

	var tabLinks = '<a onclick="showInfo(\'\',\'nFacts\');" id="nLink">Nutrition Facts</a>' + 
		'<a onclick="showInfo(\'\',\'hInstruc\');" id="hLink">Heating Instructions</a>';
		
	document.getElementById("tabLinkContainer").innerHTML = tabLinks;
}

function setBackButton () {

	if (productListURL == ""){
		productListURL = appPath + "/products/default.aspx?PostId=35";
	}
	
	var a = document.createElement("a");
	a.appendChild(document.createTextNode("Back to Products"));
	a.id='BackToProductList';
	a.href = productListURL;
	document.getElementById("tabLinkContainer").appendChild(a)
}

function hideContent(){
	var divList = document.getElementById("prodInfoContainer").getElementsByTagName('div');
	for (var i=0;i<divList.length;i++)
	{
		if (divList[i].className == "sizeContainer")
		{
			// hide the serving size div
			divList[i].style.display = "none";	

			// hide the nutrition facts & heating instructions 
			divList[i].getElementsByTagName("div")[1].style.display = "none";
			divList[i].getElementsByTagName("div")[0].style.display = "none";
		}	
	}
}



window.onload = initPage;

