
function getLocation (id) {
    var location = new String(window.location.toString());
   // alert('location is ' + location);
    if (location.indexOf("myRecipes") > -1) {
        return '/diabetes/diabetic-recipes/recipeDetail.html?id=' + id;
    }
    if (location.indexOf('myFoods') > -1) {
        return '/diabetes/diabetic-recipes/foodDetail.html?id=' + id;
    }
    var ind = location.indexOf('/diabetes');
    if (ind < 0) {
        return '/diabetes/diabetic-recipes/recipeDetail.html?id=' +id;
    }
   // alert('sub is ' + location.substring(ind)); // indexB)
    return location.substring(ind);
}

function emailFavorite(id) {
    var loc = getLocation(id);
    	  var url = "/diabetes/jsp/sendAPage.jsp?URL=http://www.dlife.com" + loc;
	  var options = "menubar=0,toolbar=0,scrollbars=1,resizable=0,status=0,location=0,width=520,height=600";
	  var popupz = window.open(url, "EmailPage", options);
	  popupz.focus();
   // alert('emailFavorite called with id ' + id + ' loc is ' + loc);
}

function printFavorite(id) {

    var loc = getLocation(id);
    var detail = '/diabetes/diabetic-recipes/recipeDetail.html?id=' + id;
    if (loc == detail) {
        loc = '/diabetes/diabetic-recipes/recipeDetailPrint.html?id=' + id;
      
    }
    else {
        loc = getLocation(id, title) + '&printTemplate=true';
      
    }
    window.open(loc, "Print Page");
   // alert('printfavorite called with id ' + id + ' loc is ' + loc);
}

function addJonas(email) {
    var url='/diabetes/streams/addInterest.html?email=' + email;
    genericAjax(url, jonasCallback, '0');
}
function updateFood() {
    var url='/diabetes/streams/myFoods.html';
    genericAjax(url, foodCallback, '0');

}
function passToJoin() {
    var url = "/diabetes/member/whyJoin.html?refer=myrecipe&origin_url=" + window.location;
   window.location=url;
}
function updateRecipe() {
    var url = '/diabetes/streams/myRecipes.html';
    genericAjax(url, recipeCallback, '0');
}

function addRecipe(recipeId) {
    if (this.isLoggedIn()) {
    var url = '/diabetes/streams/addRecipe.html?servingSize=1&instructions=none&id=' + recipeId;
    // update to spinning wheel
    $('arf' + recipeId).update('updating');
    genericAjax(url, addRecipeCallback, recipeId);
    }
    else {
        this.passToJoin();
    }
}
function addFood(foodId) {
    if (this.isLoggedIn()) {
        var url = '/diabetes/streams/addFood.html?preparations=none&id=' + foodId;
        // update to spinning wheel
        $('arf' + foodId).update('updating');
        genericAjax(url, addFoodCallback, foodId);
    }
    else {
        this.passToJoin();
    }
}
function removeRecipe(recipeId) {
    var url = '/diabetes/streams/deleteRecipe.html?id=' + recipeId;
    // update to spinning wheel
    genericAjax(url, deleteRecipeCallback, recipeId);
}
function removeFood(foodId) {
    var url = '/diabetes/streams/deleteFood.html?id=' + foodId;
    // update to spinning wheel
    genericAjax(url, deleteFoodCallback, foodId);
}
/*
 * Performs an ajax call on the url.  On success (got a response) invokes the method specified by
 * the parameter callbackMethod along with a JSONResponse object and the id that's passed in (mostly for tracking).
 *
 *
 *
 */
function genericAjax(url, callbackMethod, id) {
    new Ajax.Request(url,
    {
        method:'get',
        onSuccess: function(transport){
            var response = transport.responseText || "no response text";
            callbackMethod(response, id);
        },
        onFailure: function(){ // silently fail
        }
    });
}
// callback from get favorite list .. creat a list of urls for the recipe and add it to the recipe box div
function recipeCallback(response, id) {                  
    recipes = response.evalJSON();
    var recipeList='';
    if (recipes.length > 0)   {
        for (var i=0, len = recipes.length; i < len; i++) {
            var recipeUrl = '/diabetes/diabetic-recipes/' + recipes[i].seoTitle + '/r' + recipes[i].recipeId + '.html';
            //var ele = new Element('a', { 'class': 'recipeLink', href: url }).update(recipes[i].title);
            //recipeElements[i] = ele;
            recipeList+= '<a href="' + recipeUrl + '"> ' +recipes[i].title + '</a> <br />';
        }
    }
    $('recipeBox').update(recipeList);
}
// callback from get favorite list .. creat a list of urls for the food and add it to the food box div
function foodCallback(response, id) {
    foods = response.evalJSON();
    var foodList='';
    if (foods.length > 0)   {
        for (var i=0, len = foods.length; i < len; i++) {
            var foodUrl = '/diabetes/diabetic-recipes/'+ foods[i].seoTitle + '/f' + foods[i].foodId +'.html';
            foodList+= '<a href="' + foodUrl + '> ' +foods[i].title + '</a> <br />';
        }
    }
    $('foodBox').update(foodList);
}

function addRecipeCallback(response, recipeId) {
    updateRecipe();
    // favorite star box:
    $('fs' + recipeId).update(' <img src="/diabetes/img/recipe/myFav.jpg" hspace="10" vspace="5" alt="gold star"/>');
    // add remove box.
    $('arf' + recipeId).update('<img hspace="5" vspace="5" src="/diabetes/img/recipe/removeRecipe.jpg" onclick="removeRecipe(' + recipeId + ');" />');
}

function deleteRecipeCallback(response, recipeId) {
    updateRecipe();
    // favorite star box:
    $('fs' + recipeId).update('');
    // add remove box.
    $('arf' + recipeId).update('<img hspace="5" vspace="5" src="/diabetes/img/recipe/addRecipe.jpg" onclick="addRecipe(' + recipeId + ');" />');
    $('myRecipe' + recipeId).update('');
}
function addFoodCallback(response, foodId) {
    updateFood();
    // favorite star box:
    $('fs' + foodId).update(' <img src="/diabetes/img/recipe/myFav.jpg" hspace="10" vspace="5" alt="gold star"/>');
    // add remove box.
    $('arf' + foodId).update('<img hspace="5" vspace="5" src="/diabetes/img/recipe/removeRecipe.jpg" onclick="removeFood(' + foodId + ');" />');
}

function deleteFoodCallback(response, foodId) {
    updateFood();
    // favorite star box:
    $('fs' + foodId).update('');
    // add remove box.
    $('arf' + foodId).update('<img hspace="5" vspace="5" src="/diabetes/img/recipe/addRecipe.jpg" onclick="addFood(' + foodId + ');" />');
    $('myFood' + foodId).update('');
}
function jonasCallback(response, id) {
    outcome = response.evalJSON();
    if (outcome.success) {
        $('jonasForm').update('You will be notified via email when the interview is available');
    }
    else {
        $('jonasError').update('There was a problem sending your information.  Please verify that your email is proper or use a different email');
    }

}



