﻿/* ------------------------------------------------------------ */
/* Seach By Ingredient Functionality                            */
/* ------------------------------------------------------------ */


var xmlhttpIngredient;
var searchIngredientsArray = new Array();


// Sets event handlers
function initSearchByIngredient() {
    document.getElementById("ingredientSearchBox").onfocus = sbi_setActive;
    document.getElementById("ingredientSearchBox").onblur = sbi_setInActive;
    document.getElementById("ingredientSearchBox").onkeyup = getRecipesFromXML;
}

// Changes css class when focused
function sbi_setActive() {
    this.className = "";
    this.select();
}

// Changes css class when blurred
function sbi_setInActive() {
    if (this.value == "Ingredient 1, Ingredient 2, Ingredient 3...") {
        this.className = "inactive";
        clearResults();
    }
    else if (this.value == "") {
        this.value = "Ingredient 1, Ingredient 2, Ingredient 3...";
        this.className = "inactive";
        clearResults();
    }
}


// Check XML for recipe matches
function getRecipesFromXML() {

    if (this.value != "," && this.value != "") {
        searchIngredientsArray = convertStringToArray(this.value.toLowerCase());
        xmlhttpIngredient = GetXmlHttpObject();
        xmlhttpIngredient.onreadystatechange=writeRecipesByIngredient;
        xmlhttpIngredient.open("GET","/xml/recipesXML.aspx",true);
        xmlhttpIngredient.send(null);
    }
    else {
        clearResults();
    }
    
}

// Convers a comma seperate string to an array
function convertStringToArray(str) {    
    var strArray = new Array();    
    strArray = str.split(',');
    return strArray;   
}


// Returns whether or not all search terms (in the array) were found in string
function allSearchTermsFound(str) {

    var isMatch = true; 
    str = str.toLowerCase(); // convert to lower case     
    
    for (j=0; j<searchIngredientsArray.length; j++) {
        if (str.indexOf(searchIngredientsArray[j]) == -1) {
            isMatch = false;
        }
    }    
    
    return isMatch;    
}


// Writes results to screen
function writeRecipesByIngredient()
{

    showLoading();
    
    if (xmlhttpIngredient.readyState==4)
    {   
        // 4 = "loaded"
        if (xmlhttpIngredient.status==200)
        {
            // 200 = OK
            // ...our code here...            
            xmlDoc = xmlhttpIngredient.responseXML;             

            var responseText = "<ul class='recipesByIngredientList'>";            
            
            // Loop through each item in xml doc (start at 1 to go to first post)
            for (x=0; x<xmlDoc.getElementsByTagName("recipe").length; x++) {
                                
                var ingredients = xmlDoc.getElementsByTagName("ingredients")[x].childNodes[0].nodeValue;                
                
                // Check all ingredients inputted to see if there's a match (note all search terms entered must match to return a result)
                if (allSearchTermsFound(ingredients)) {
                        responseText += "<li><a href='recipe.aspx?id=" + xmlDoc.getElementsByTagName("id")[x].childNodes[0].nodeValue + "'>" + xmlDoc.getElementsByTagName("name")[x].childNodes[0].nodeValue + "</a></li>";                                        
                }
                                
                
            }
            
            responseText += "</ul>";
            
            // Write response to div
            document.getElementById("recipesByIngredient_results").innerHTML = responseText;
            
        }
        else
        {
            //alert("Problem retrieving XML data");
        }
    }
}


// Clears results
function clearResults() {
    document.getElementById("recipesByIngredient_results").innerHTML = "";
}

// Display Loading Status
function showLoading() {

    var results = document.getElementById("recipesByIngredient_results");
    var output;
    
    output =  "<div class='loadingDiv'>";
    output += "  <img src='/images/ajax_loader_eaa864.gif' />";
    output += "</div>";
    
    results.innerHTML = output;

}









/* ------------------------------------------------------------ */
/* Recent Blog Posts                                            */
/* ------------------------------------------------------------ */


var xmlhttpBlogPosts;

// Used on homepage to retrieve blog entries
function initRecentBlogPosts() {
    xmlhttpBlogPosts = GetXmlHttpObject();
    xmlhttpBlogPosts.onreadystatechange=writeRecentBlogPosts;
    xmlhttpBlogPosts.open("GET","/rss/blog_proxy.aspx",true);
    xmlhttpBlogPosts.send(null);        
}




function writeRecentBlogPosts()
{
    if (xmlhttpBlogPosts.readyState==4)
    {   
        // 4 = "loaded"
        if (xmlhttpBlogPosts.status==200)
        {
            // 200 = OK
            // ...our code here...            
            xmlDoc = xmlhttpBlogPosts.responseXML;
            var responseText = "<ul>";
            var totalPosts = xmlDoc.getElementsByTagName("item").length;
            
            // Loop through each item in xml doc (start at 1 to go to first post)
            for (x=1; x<=3; x++) {
                responseText += "<li><a href=\""+ xmlDoc.getElementsByTagName("link")[x].childNodes[0].nodeValue +"\">" + xmlDoc.getElementsByTagName("title")[x].childNodes[0].nodeValue + "</a></li>";                
            }
            
            responseText += "</ul>";
            
            // Write response to div
            document.getElementById("recentBlogPostsContent").innerHTML = responseText;
            
        }
        else
        {
            // Hide widget it AJAX not supported or XML not found
            document.getElementById("recentBlogPosts").style.display = "none";
        }
    }
}

