/*
 * AjaxObject is a hypothetical object that encapsulates the transaction
 *     request and callback logic.
 *
 * handleSuccess( ) provides success case logic
 * handleFailure( ) provides failure case logic
 * processResult( ) displays the results of the response from both the
 * success and failure handlers
 * call( ) calling this member starts the transaction request.
 */

var productObject = {

    handleSuccess: function(k) {

        this.removeLoaders('loaderdiv');
        this.processResult(k);
        prodinflight = false;
    },

    handleFailure: function(k) {
        //alert('fail');
        // Failure handler
    },

    processResult: function(k) {
        this.parseRecipe(k);

        // This member is called by handleSuccess
    },


    // parse and then display the data
    parseRecipe: function(k) {
        if (navigator.appName == "Microsoft Internet Explorer") {
            ie = true
            cname = 'className';
        } else {
            cname = 'class';
            ie = false;
        }
        var myXMLTags = ["ProductInfo", "NutritionInfo", "Ingredients", "Allergens", "FAQs", "DidYouKnow"];
        var starCSS = ["", "one-star", "two-stars", "three-stars", "four-stars", "five-stars"];
        var aQnotes = new Array();
        var xFragment = document.createDocumentFragment();
        //k.responseXML.normalize();

        for (var x = 0; x < myXMLTags.length; x++) {
            var myTag = k.responseXML.getElementsByTagName(myXMLTags[x]);


            /*********** start loop ****************************************/
            //loop throught each item in xml to diplay
            var sName;
            var sID;
            var sHeadline;
            var sPic;
            var sCounts;
            var sText;
            var sRecipeLink;
            var sRecipeLinkTitle;
            var iIngred;
            var iAllergens;
            var sUPC;
            var sQuick;


            for (var i = 0; i < myTag.length; i++) {

                switch (myXMLTags[x]) {

                    case "ProductInfo":

                        sID = myTag[i].getAttribute("ID");

                        //sPic = 'images/pro_pic/prod_01.jpg';
                        sPic = myTag[i].getAttribute("LargeImage");
                        sName = HtmlDecode(myTag[i].getElementsByTagName("Name")[0].firstChild.nodeValue);

                        //sHeadline = myTag[i].getElementsByTagName("Headline")[0].firstChild.nodeValue;

                        sCounts = myTag[i].getElementsByTagName("Counts")[0].firstChild.nodeValue;
                        sText = myTag[i].getElementsByTagName("Text")[0].firstChild.nodeValue;
                        sRecipeLinkTitle = myTag[i].getElementsByTagName("RecipeLink")[0].firstChild.nodeValue;
                        sRecipeLink = myTag[i].getElementsByTagName("RecipeLink")[0].getAttribute("Link");
                        sUPC = myTag[i].getElementsByTagName("UPC")[0].firstChild.nodeValue;
                        sQuick = myTag[i].getElementsByTagName("QuickCount")[0].firstChild.nodeValue;
                        //sText = 'coming soon';

                        // if there is a list of QuickItems, populate an array with them
                        if (sQuick > 0) {
                            var ele = new Array();
                            ele = myTag[i].getElementsByTagName("QuickItem");
                            for (var dd = 0; dd < sQuick; dd++) {
                                aQnotes[dd] = ele[dd].childNodes[0].nodeValue;
                                //alert(ele[dd].childNodes[0].nodeValue);
                            }
                        }

                        //                        for (var gg = 0; gg < aQnotes.length; gg++) {
                        //                            alert(aQnotes[gg]);
                        //                        }

                        break;
                    case "NutritionInfo":

                        var sni_image = myTag[i].getAttribute("Image");

                        //Line below is commented out to remove the the fetch to NLEAInfo in product.aspx
                        //var sni_html = String(myTag[i].getElementsByTagName("NLEAInfo")[0].firstChild.nodeValue)
                        var sni_html = "null"

                        //var sni_image = 'images/pro_pic/prod_nutr_01.jpg';
                        //alert(sni_image);
                        break;
                    case "Ingredients":

                        iIngred = myTag[i].firstChild.nodeValue;
                        break;

                    case "Allergens":
                        iAllergens = myTag[i].firstChild.nodeValue;
                        break;
                    case "FAQs":
                        //sCname = oCurrentChild.text;
                        break;
                    case "DidYouKnow":
                        var DYK = myTag[i].firstChild.nodeValue;
                        break;
                }

            }
        }



        //        for (var hh = 0; hh < aQnotes.length; hh++) {
        //            alert(aQnotes[hh]);
        //        }


        /*************************** Build Top Part of Recipe Box**********************************************/
        //create the container div
        var divContainer = document.createElement("div");
        var divContent = document.createElement("div");
        //give container div a css class
        divContainer.id = "thisrecipe";
        //create Rating and Lin
        var pTopLine = document.createElement("h2");
        pTopLine.appendChild(document.createTextNode(sName));


        divContent.appendChild(pTopLine);

        //append content to container
        divContainer.appendChild(divContent);
        //append container to doc object
        xFragment.appendChild(divContainer);

        //document.body.appendChild(oFragment);
        var theBody = document.getElementById("rTopHolder");

        theBody.appendChild(xFragment);
        /***********************end***************************************************************/

        /********************** build image box ****************************************************/
        var divContainer = document.createElement("div");
        var divContent = document.createElement("div");
        divContainer.id = "TOPBox";
        divContent.id = "thisimgBox";
        var pimg = document.createElement("img");
        pimg.className = 'recipepic';
        pimg.src = sPic;

        divContent.appendChild(pimg);


        if (sText != 'null') {
            var myText = document.createElement("p");
            myText.appendChild(document.createTextNode(sText));
            divContent.appendChild(myText);
        };

        //if there is only one val and it = null, don't add
        if (aQnotes.length > 0) {
            //(aQnotes.length = 1 && aQnotes[0] != 'null')
            var list = document.createElement("ul");
            for (var dd = 0; dd < aQnotes.length; dd++) {
                var listitem = document.createElement("li");
                listitem.appendChild(document.createTextNode(aQnotes[dd]));
                list.appendChild(listitem);
            }
            divContent.appendChild(list);
        }

        if (sUPC != 'null') {
            var myText = document.createElement("p");
            myText.className = 'UPCtext';
            myText.appendChild(document.createTextNode("UPC - "));
            myText.appendChild(document.createTextNode(sUPC));
            divContent.appendChild(myText);
        };
        //append content to container
        divContainer.appendChild(divContent);
        //append container to doc object
        xFragment.appendChild(divContainer);

        //document.body.appendChild(oFragment);
        var theBody = document.getElementById("tIMGBox");

        theBody.appendChild(xFragment);


        /*********************** end image box *********************************************************/

        /********************** bottom of image box ****************************************************/
        var divContainer = document.createElement("div");
        var divContent = document.createElement("div");
        divContainer.id = "btmBox";
        divContent.id = "thisimgBox";
        //        if (sCounts != "null") {
        //            var pTitle = document.createElement("p");
        //            pTitle.className = 'variety';
        //            //pTitle.appendChild(document.createTextNode(sCount));
        //            pTitle.appendChild(document.createTextNode(sCounts));

        //            divContent.appendChild(pTitle);
        //        };

        if (sRecipeLinkTitle != "null") {
            var sLink = document.createElement("p");
            sALink = document.createElement("a");
            sALink.href = sRecipeLink;
            sALink.appendChild(document.createTextNode(sRecipeLinkTitle));
            sLink.appendChild(sALink);
            var pimg = document.createElement("img");
            pimg.className = 'recRedArrow';
            //pimg.style.cssText = 'padding-left:5px;'
            pimg.src = 'images/hm_recipeBox/red_arrow.jpg';
            sLink.appendChild(pimg);
            divContent.appendChild(sLink);
        };

        //append content to container
        divContainer.appendChild(divContent);
        //append container to doc object
        xFragment.appendChild(divContainer);

        //document.body.appendChild(oFragment);
        var theBody = document.getElementById("tIMGBox2");

        theBody.appendChild(xFragment);


        /*********************** end image box *********************************************************/

        /*********************** NUTRITION Tab Boxes *************************************************************/
        //create the container div
        var divContainer = document.createElement("div");
        var divContent = document.createElement("div");

        //give container div a css class
        divContainer.id = "nutr";
        divContent.id = "recipeItemContentBox";
        if (sni_image != 'null') {
            var nutimg = document.createElement("img");
            nutimg.src = sni_image;
            nutimg.className = 'nutrPic';
            divContent.appendChild(nutimg);
        }
        //     else {
        //       var nutimg = document.createElement("img");
        //      nutimg.src = '/images/comingsoon.gif';
        //      divContent.appendChild(nutimg);

        //     }



        //append content to container
        divContainer.appendChild(divContent);
        //append container to doc object
        xFragment.appendChild(divContainer);

        //document.body.appendChild(oFragment);
        var theBody = document.getElementById("tab1");

        theBody.appendChild(xFragment);

        if (sni_html != 'null') {

            var container = document.getElementById("recipeItemContentBox");
            var nutdiv = document.createElement("div");
            container.innerHTML = sni_html;
            container.appendChild(document.createTextNode(sni_html));
        }

        /********************** end Nutritional tab boxes ***********************************************************/

        /*********************** DID YOU KNOW Tab Boxes *************************************************************/
        // only display if a did you know is passed
        if (DYK != 'null') {
            //create the container div
            var divContainer = document.createElement("div");
            var divContent = document.createElement("div");

            //give container div a css class
            divContainer.id = "dyu";
            divContent.id = "thisDidYouKnow";

            var pDYKTitle = document.createElement("p");
            pDYKTitle.className = 'didyouknow';
            pDYKTitle.appendChild(document.createTextNode('DID YOU KNOW...'));

            divContent.appendChild(pDYKTitle);

            var pDYKContent = document.createElement("p");
            pDYKContent.className = 'var';
            pDYKContent.appendChild(document.createTextNode(DYK));

            divContent.appendChild(pDYKContent);



            //append content to container
            divContainer.appendChild(divContent);
            //append container to doc object
            xFragment.appendChild(divContainer);

            //document.body.appendChild(oFragment);
            var theBody = document.getElementById("leftDidYouKnow");

            theBody.appendChild(xFragment);

        }
        /********************** end Nutritional tab boxes ***********************************************************/



        /*********************** Ingredients Tab Boxes *************************************************************/
        if (iIngred != 'null') {
            //create the container div
            var divContainer = document.createElement("div");
            var divContent = document.createElement("div");

            //give container div a css class
            divContainer.id = "ing";
            divContent.id = "recipeItemContentBox";

            var pIngredTitle = document.createElement("h2");
            pIngredTitle.appendChild(document.createTextNode('Ingredients:'));
            divContent.appendChild(pIngredTitle);

            var pIngred = document.createElement("p");
            pIngred.appendChild(document.createTextNode(iIngred));
            divContent.appendChild(pIngred);


            if (iAllergens != 'null') {

                var pAllergTitle = document.createElement("h2");
                pAllergTitle.appendChild(document.createTextNode('Allergens:'));
                divContent.appendChild(pAllergTitle);

                var pAllerg = document.createElement("p");

                pAllerg.appendChild(document.createTextNode(iAllergens));
                divContent.appendChild(pAllerg);
            }


            //append content to container
            divContainer.appendChild(divContent);
            //append container to doc object
            xFragment.appendChild(divContainer);

            //document.body.appendChild(oFragment);
            var theBody = document.getElementById("tab2");
            theBody.appendChild(xFragment);

        }
        //        else { 
        //                   var divContainer = document.createElement("div");
        //                   var divContent = document.createElement("div");
        //                   divContainer.id = "ing";
        //                   divContent.id = "recipeItemContentBox";
        //                   var nutimg2 = document.createElement("img");
        //                    nutimg2.src = '/images/comingsoon.gif';
        //                   divContent.appendChild(nutimg2);
        //                  
        //                    //append container to doc object
        //                    xFragment.appendChild(divContainer);

        //                    //document.body.appendChild(oFragment);
        //                   var theBody = document.getElementById("tab2");

        //                   theBody.appendChild(xFragment);
        //          }


        /********************** end Nutritional tab boxes ***********************************************************/

        /*********************** FAQ Tab Boxes *************************************************************/
        //create the container div
        var divContainer = document.createElement("div");
        var divContent = document.createElement("div");

        //give container div a css class
        divContainer.id = "mam";
        divContent.id = "recipeItemContentBox";

        var pFAQTitle = document.createElement("h2");
        pFAQTitle.appendChild(document.createTextNode('Product Detail: '));

        divContent.appendChild(pFAQTitle);

        var pFAQ = document.createElement("p");
        pFAQ.appendChild(document.createTextNode("Coming Soon"));
        divContent.appendChild(pFAQ);



        //append content to container
        divContainer.appendChild(divContent);
        //append container to doc object
        xFragment.appendChild(divContainer);

        //document.body.appendChild(oFragment);
        var theBody = document.getElementById("tab3");

        theBody.appendChild(xFragment);

        /********************** end Make a Meal tab boxes ***********************************************************/

        /*********** end loop ****************************************/

    },


    //loaders
    loader: function() {

        var loadFragment = document.createDocumentFragment('t');
        var xdivContainer = document.createElement("div");
        var xdivContent = document.createElement("div");
        //give container div a css class
        xdivContainer.id = "loaderdiv";
        var loadTxt = document.createElement("p");
        loadTxt.appendChild(document.createTextNode("Loading Product"));

        xdivContent.appendChild(loadTxt);

        var loadImg = document.createElement("img");
        loadImg.src = "images/common/loader.gif";
        xdivContent.appendChild(loadImg);
        //append content to container
        xdivContainer.appendChild(xdivContent);
        //append container to doc object
        loadFragment.appendChild(xdivContainer);
        var loadBody = document.getElementById('rTopHolder');
        //alert(loadBody);
        loadBody.appendChild(loadFragment);

    },

    // remove loading grpahuic from the current list
    removeLoaders: function(divNum) {

        var d = document.getElementById('rTopHolder');
        var olddiv = document.getElementById(divNum);
        d.removeChild(olddiv);

    },



    startRequest: function(myURL) {

        this.loader();
        YAHOO.util.Connect.asyncRequest("GET", myURL, callback2);

    }

};
var callback2 =
{
	success:productObject.handleSuccess,
	failure:productObject.handleFailure,
	scope: productObject
};

//remove recipe elements
// remove div elements from the current list
function removeProductElement() {
	
	var recipeTags = ["rTopHolder", "tIMGBox", "tIMGBox2", "tab1", "tab2", "tab3", "leftDidYouKnow"];
	var divNum = ["thisrecipe", "TOPBox", "btmBox", "nutr", "ing", "mam", "dyu"];
	for(i=0; i<=recipeTags.length; i++){
  		var d = document.getElementById(recipeTags[i]);
  		var olddiv = document.getElementById(divNum[i]);
		
		if(olddiv){
  			d.removeChild(olddiv);
		};
	}	
}

