﻿function PageMethod(fn, paramArray, successFn, errorFn) {
    var pagePath = '/RecipeService.asmx';
    var paramList = '';
    if (paramArray.length > 0) {
        for (var i = 0; i < paramArray.length; i += 2) {
            if (paramList.length > 0) paramList += ',';
            paramList += '"' + paramArray[i] + '":"' + paramArray[i + 1] + '"';
        }
    }
    paramList = '{' + paramList + '}';
    $.ajax({
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });

}

$(document).ready(function() {
    $('img.asyncPhoto').each(function(i) {
        var image = $(this);
        var recipeId = image.attr('rel');
        PageMethod("GetImageUrl", ["recipeId", recipeId],
                function(data) {
                    if (data == null) {
                        image.attr('src', '/images/recipe/empty_recipe.jpg');
                        return;
                    }
                    image.attr('src', data.d);
                },
                function(xhr, err) {
                    //alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                    //alert("responseText: " + xhr.responseText);
                    image.attr('src', '/images/recipe/empty_recipe.jpg');
                });
    });
});        
