﻿
function loadRecipes() {

    if (location.pathname.toLowerCase().indexOf("recipes") == -1) {
        return;
    }

    try {
        // Firefox, Opera 8.0+, Safari...
        xmlHttpObject = new XMLHttpRequest();
    }
    catch (ex) {
        // Internet Explorer...
        try {
            xmlHttpObject = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (ex) {
            xmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }

    xmlHttpObject.open("GET", "../xml/recipes.xml", true);
    xmlHttpObject.onreadystatechange = function() { processStateChangeRecipes();  };
    xmlHttpObject.send(null);
}

function open_recipe(recipe) {
    window.open('recipe_popup.aspx?r=' + recipe, 'Recipe', 'menubar=no,width=430,height=360,toolbar=no');
}

function processStateChangeRecipes() {

    if (xmlHttpObject.readyState == 4) { // Complete
        if (xmlHttpObject.status == 200) { // OK response
            xmlDoc = xmlHttpObject.responseXML;
            var tempHTML = "";
            var recipeHTML = "";
            var rowcount = 0;
            var rowgood = "";
            var x = xmlDoc.documentElement.childNodes;

            for (i = 0; i < x.length; i++) {
                if (x[i].nodeType == 1) {//Process only element nodes (type 1)

                    recipeHTML += "<div class=\"recipecard\">";
                    recipeHTML += "<div id=\"recipecard_top\"></div>";
                    recipeHTML += "<div id=\"recipecard_content\">";
                    recipeHTML += "<div id=\"card_" + x[i].nodeName + "\">";
                    recipeHTML += "<a target=\"blank\" href=\"recipe_popup.aspx?r=" + x[i].nodeName + "\"><img class=\"recipeimage\" src=\"/images/buttons/" + x[i].nodeName.toLowerCase() + "_small.jpg\"></a><div class=\"recipelink\"><a target=\"blank\" href=\"recipe_popup.aspx?r=" + x[i].nodeName + "\">" + x[i].getElementsByTagName("PRODUCT_NAME")[0].childNodes[0].nodeValue + "</a></div>";
                    recipeHTML += "<div class='hrdiv'></div>";
                    recipeHTML += "<p>";
                    recipeHTML += x[i].getElementsByTagName("DESCRIPTION")[0].childNodes[0].nodeValue;
					recipeHTML += "</p>";
                    recipeHTML += "</div>";
					recipeHTML += "</div>";
                    recipeHTML += "<div id=\"recipecard_bottom\"></div>";
                    recipeHTML += "</div>";

                    document.getElementById('recipes_main').innerHTML = recipeHTML;
                    
                }
            }
            for (i = 0; i < x.length+1; i++) {
                sIFR.replace(interstate, {
                    selector: 'div.recipelink',
                    wmode: 'transparent',
                    antiAliasType: 'advanced',
                    offsetTop: '0px',
                    offsetleft: '0px',
                    css: ['.sIFR-root {color: #734a37; font-size:13px; text-transform:uppercase; width:150px; cursor:pointer; postion:relative;}',
                    ' a { color: #734a37; text-decoration:none;} a:hover { color: #db9010; text-decoration:none;}']
                });

            }
        }
    }
}

function display_recipe(recipe_title) {

    if (location.pathname.toLowerCase().indexOf("recipe_popup") == -1) {
        return;
    }

    try {
        // Firefox, Opera 8.0+, Safari...
        xmlHttpObject = new XMLHttpRequest();
    }
    catch (ex) {
        // Internet Explorer...
        try {
            xmlHttpObject = new ActiveXObject('Msxml2.XMLHTTP');
        }
        catch (ex) {
            xmlHttpObject = new ActiveXObject('Microsoft.XMLHTTP');
        }
    }


    xmlHttpObject.open("GET", "../xml/recipes.xml", true);
    xmlHttpObject.onreadystatechange = function() { display_recipe_code(recipe_title); };
    xmlHttpObject.send(null);

}

function display_recipe_code(recipe_title) {
    if (xmlHttpObject.readyState == 4) { // Complete
        if (xmlHttpObject.status == 200) { // OK response
            xmlDoc = xmlHttpObject.responseXML;
            var tempHTML = "";
            var recipeHTML = "";
            var rowcount = 0;
            var rowgood = "";
            var x = xmlDoc.getElementsByTagName(recipe_title);
            for (i = 0; i < x.length; i++) {
                if (x[i].nodeType == 1) {//Process only element nodes (type 1)

                    r = xmlDoc.getElementsByTagName(x[i].nodeName)[0].childNodes;
                    recipeHTML += "<div id=\"" + x[i].nodeName + "\">";
					recipeHTML += "<div id='header_recipe'>";
					recipeHTML += "<div id='recipe_image'><img src='../images/buttons/";
                    recipeHTML += x[i].getElementsByTagName("IMAGE")[0].childNodes[0].nodeValue;
					recipeHTML += "' /></div>";
					recipeHTML += "<div id='recipe_image_products'><img src='../images/buttons/";
                    recipeHTML += x[i].getElementsByTagName("IMAGE_PRODUCTS")[0].childNodes[0].nodeValue;
					recipeHTML += "' /></div>";
					recipeHTML += "<h3>" + x[i].getElementsByTagName("PRODUCT_NAME")[0].childNodes[0].nodeValue + '</h3>';
					recipeHTML += "<p>" + x[i].getElementsByTagName("DESCRIPTION_PRINT")[0].childNodes[0].nodeValue + '</p>';
					recipeHTML += "</div>";
                    recipeHTML += "<ul class='horizontal_list right'>";
					recipeHTML += "<li><span class='orange'>Prep:</span> ";
                    recipeHTML += x[i].getElementsByTagName("PREP_TIME")[0].childNodes[0].nodeValue;
                    recipeHTML += "</li>";		
					recipeHTML += "<li><span class='orange'>Total:</span> ";
                    recipeHTML += x[i].getElementsByTagName("TOTAL_TIME")[0].childNodes[0].nodeValue;
                    recipeHTML += "</li>";							
					recipeHTML += "</ul>";
					recipeHTML += "<h4 id='ingredients'>Ingredients</h4>";
					recipeHTML += "<h6>Serves ";
					recipeHTML += x[i].getElementsByTagName("SERVES")[0].childNodes[0].nodeValue;
					recipeHTML += ".</h6>";
					var ingList = x[i].getElementsByTagName('INGREDIENT');
					
					if (ingList != null) {
					    recipeHTML += "<ul class='vertical_list'>";
					    for (ingI = 0; ingI < ingList.length; ingI++) {
					        if (ingList[ingI].nodeType == 1) {
					            recipeHTML += "<li class='vertical_list'><span>" + ingList[ingI].childNodes[0].nodeValue + "</span></li>";
					        }
					    }
					    recipeHTML += "</ul>";
					}

					recipeHTML += "<h4>Directions</h4>";
					var dirList = x[i].getElementsByTagName('DIRECTION');
					if (dirList != null) {
					    recipeHTML += "<ol>";
					    for (dirI = 0; dirI < dirList.length; dirI++) {
					        if (dirList[dirI].nodeType == 1) {
					            recipeHTML += "<li><span>" + dirList[dirI].childNodes[0].nodeValue + "</span></li>";
					        }
					    }
					    recipeHTML += "</ol>";
					}	
					recipeHTML += "<h4>Cook's Notes</h4>";
					var noteList = x[i].getElementsByTagName('COOKS_NOTE');
					if (noteList != null) {
					    recipeHTML += "<ol>";
					    for (noteI = 0; noteI < noteList.length; noteI++) {
					        if (noteList[noteI].nodeType == 1) {
					            recipeHTML += "<li><span>" + noteList[noteI].childNodes[0].nodeValue + "</span></li>";
					        }
					    }
					    recipeHTML += "</ol>";
					}					

                    document.getElementById('full_recipe').innerHTML = recipeHTML;

                }
            }
        }
    }
}

