﻿function PageMethod(pagePath, fn, paramArray, successFn, errorFn) {
    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({
        cache: false,
        type: "POST",
        url: pagePath + "/" + fn,
        contentType: "application/json; charset=utf-8",
        data: paramList,
        dataType: "json",
        success: successFn,
        error: errorFn
    });
}

function getUniqueQueryString() {
    return '?r=' + Math.random() * 99999;
}
$(document).ready(function () {

    $(".poll_results_button").click(function () {
        $(this).removeClass('inline').addClass('none');
        $('div.poll_results').removeClass('none').addClass('inline');
    });

    //    $(".poll_submit_button").click(function () {
    //        var poll = $(this).attr('rel');
    //        PageMethod("/recipes/default.aspx", "LikeRecipe", ["recipeId", recipeId],
    //                function (data) {
    //                    if (data == null) {
    //                        return;
    //                    }
    //                    if ($('.RecipeLikeToUpdate').length) {
    //                        $('.RecipeLikeToUpdate').load('/recipes/recipelike/' + recipeId, function () {
    //                        });
    //                    }

    //                },
    //                function (xhr, err) {
    //                    //alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
    //                    //alert("responseText: "+xhr.responseText);
    //                });
    //    });

    $(".userLikeRecipe").click(function () {
        var recipeId = $(this).attr('rel');
        PageMethod("/recipes/default.aspx", "LikeRecipe", ["recipeId", recipeId],
                function (data) {
                    if (data == null) {
                        return;
                    }
                    if ($('.RecipeLikeToUpdate').length) {
                        $('.RecipeLikeToUpdate').load('/recipes/recipelike/' + recipeId + getUniqueQueryString(), function () {
                        });
                    }

                },
                function (xhr, err) {
                    //alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
                    //alert("responseText: "+xhr.responseText);
                });
    });

    $('img.asyncPhoto').each(function (i) {
        var image = $(this);
        var recipeId = image.attr('rel');
        //WebMethod("GetRecipeImage", recipeId,
        PageMethod("/recipes/default.aspx", "GetRecipeImage", ["recipeId", recipeId],
                function (data) {
                    if (data == null) {
                        image.attr('src', '/images/empty_recipe_lg.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/empty_recipe_lg.jpg');
                });
    });


    $('img.asyncUrl').each(function (i) {
        var image = $(this);
        var url = image.attr('rel');
        image.attr('src', url.replace("~", ""));
    });

});

function loadReviews(recipeId, recipeName) {
    if ($(window.parent.document).find("div.RecipeReviewsToUpdate").length) {
        $(window.parent.document).find("div.RecipeReviewsToUpdate").load('/recipes/userreview/' + recipeName + getUniqueQueryString(), function () {
        });
    };
    PageMethod("/recipes/default.aspx", "GetReviewsCount", ["recipeId", recipeId],
                function (data) {
                    $(window.parent.document).find(".RecipeReviewNumberToUpdate").html(data.d);
                },
                function (xhr, err) {
                    //alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status);
                    //alert("responseText: "+xhr.responseText);
                });
}
