var heatingInstructions = "<b>This Product Does Not Have Recommended Heating Instructions</b>";
var nutritionInfo = "<b>No Nutrition Information is Available.</b>";
var initialView = "nutrition";  //
var IsInShoppingList;

function initProducts(){

    if(document.getElementById("servingSize")) {   
        //if serving size, display the drop down menu
        var servingSizeContainer = document.getElementById("servingSize").innerHTML;
        document.getElementById("servingSizeContainer").innerHTML = servingSizeContainer;
        heatingInstructions = ""
        nutritionInfo = "";
        document.getElementById("servingSizeContainer").style.display = "block"; 
        //set the onchange event for the select
        var servingSizeForm = document.getElementById("servingSizeContainer").getElementsByTagName("select")[0]; 
         servingSizeForm.onchange = function() { showContent(this.options[this.selectedIndex].value); }
        initialView = "heating";
     }else {
        //we have only 1 serving size, so get the nutriton & heating info
        nutritionInfo = document.getElementById("nutritionFacts").innerHTML;
        //if heating instruction, default to that view, otherwise use generic copy
        if(document.getElementById("heatingInstructions")){
            initialView = "heating";
            heatingInstructions = document.getElementById("heatingInstructions").innerHTML;
        }
     }
     showContent(initialView);
     
     if (IsInShoppingList != undefined && IsInShoppingList == true){
		if (isAuthenticated == true){
			// product previously added to shopping list
			// print msg and set class accordingly
			document.getElementById("addShoppingListBtn").className = "previouslyAdded";
		}
	}
	else {
		document.getElementById("addShoppingListBtn").onclick = setProduct;
	}
}

function showContent(view) {
    if(view == "heating") {                 
        //turn off nutrition
        document.getElementById("nutritionContainer").style.display = "none"; 
        //turn on heating        
        document.getElementById("productContainer").className = "heatingOn";
        document.getElementById("heatingContainer").innerHTML = heatingInstructions;
        document.getElementById("heatingContainer").style.display = "block";  
        
        urchinTracker('/products/suggestions/' + postName);
        
    }else if(view == "nutrition") {              
        //turn off heating
        document.getElementById("heatingContainer").style.display = "none"; 
        //turn on nutrition
        document.getElementById("productContainer").className = "heatingOff";
        document.getElementById("nutritionContainer").innerHTML = nutritionInfo;
        document.getElementById("nutritionContainer").style.display = "block"; 
        }else{ //we must be serving size
            if(document.getElementById("servingSize")) {
       //get the heating/nutrition based on servingSize value ex: 16ozNutritionFacts
              heatingDivName  = view + "HeatingInstructions";
              nutritionDivName = view + "NutritionFacts";
              //update text on page with servingSize info
              heatingInstructions = document.getElementById(heatingDivName).innerHTML;
              nutritionInfo = document.getElementById(nutritionDivName).innerHTML;
              document.getElementById("heatingContainer").innerHTML = heatingInstructions;  
              document.getElementById("nutritionContainer").innerHTML = nutritionInfo;  
        }
        urchinTracker('/products/nutrition/' + postName);
     }
}


function setProduct () {

	var uri = appPath + '/MyStuff/setProduct.aspx?Action=add&postId=' + postId;
	
	var request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");
	request.open("GET", uri, false);

	document.getElementById("addShoppingListBtn").className = "newlyAdded";
	
	request.send(null);
				
	if(!request.getResponseHeader("Date"))
	{
		var cached = request;

		request = (typeof(XMLHttpRequest) != "undefined") ? new XMLHttpRequest() : new ActiveXObject("Msxml2.XMLHTTP");

		var ifModifiedSince = cached.getResponseHeader("Last-Modified");
		ifModifiedSince = (ifModifiedSince) ? ifModifiedSince : new Date(0); // January 1, 1970

		request.open("GET", uri, false);

		request.setRequestHeader("If-Modified-Since", ifModifiedSince);

		request.send("");
		if (request.status == 304)
		{
			request = cached;
		}
	}
	urchinTracker('/products/shoppingbag/' + postName);
}

function swapImg(imgSrc){
    var stage = document.getElementById("beautyShot");
    //alert(stage.src);
    stage.src = imgSrc;
}

function printImg(imgPath, imgName){
    var image = imgName;
    image = image.substring(0, image.indexOf(".jpg"));
    document.write( "<img src='" + imgPath + image + "_1.jpg' />" );
}