﻿$(document).ready(FeedList_Initialize);
var listActionButton;

function FeedList_Initialize() {
    var list;

    list = $(".imgBtnFavorite");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", FeedList_FavoriteRun);
    }

    list = $(".imgBtnHide");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", FeedList_HideRun);
    }
}


// Favorite Section
function FeedList_FavoriteRun(e) {
    e.preventDefault();
    e.stopPropagation();

    listActionButton = $("#" + this.id);

    if (isMember == "True") {
        DisplayModal("confirmpopup", '350');
        if (listActionButton.attr("task") == "add") {
            $("#confirmmessage").text("Are you sure you want to add this feed to your favorites?  (you can always remove it from your Favorites at any time)");
        }
        else {
            $("#confirmmessage").text("Are you sure you wish to remove this feed from your favorites?");
        }
        $("#btnConfirmYes").bind("click", FeedList_FavoriteSend);
        $("#btnConfirmNo").bind("click", HideModal);
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function FeedList_FavoriteSend(e) {
    e.preventDefault();
    e.stopPropagation();

    if (isMember == "True") {
        var data = "{feedId:" + listActionButton.attr("feedid")
            + ",task:'" + listActionButton.attr("task")
            + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/services/postlist.asmx/ChangeFavoriteStatus",
            data: data,
            dataType: "json",
            success: FeedList_FavoriteSuccess,
            error: FeedList_FavoriteError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function FeedList_FavoriteSuccess(responseJSON) {
    if (listActionButton.attr("task") == "add") {
        var list = $(".imgBtnFavorite[feedid='" + listActionButton.attr("feedid") + "']");
        for (i = 0; i < list.length; i++) {
            $("#" + list[i].id).attr("src", "/images/btn_remove_fav.jpg");
            $("#" + list[i].id).attr("task", "remove");
        }
    }
    else {
        var list = $(".imgBtnFavorite[feedid='" + listActionButton.attr("feedid") + "']");
        for (i = 0; i < list.length; i++) {
            $("#" + list[i].id).attr("src", "/images/btn_option_favorite.png");
            $("#" + list[i].id).attr("task", "add");
        }

    }
    HideModal();
}

function FeedList_FavoriteError(responseJSON) {
    //error code
    HideModal();
}


// Hide Section
function FeedList_HideRun(e) {
    e.preventDefault();
    e.stopPropagation();

    listActionButton = $("#" + this.id);

    if (isMember == "True") {
        DisplayModal("confirmpopup", '350');
        if (listActionButton.attr("task") == "add") {
            $("#confirmmessage").text("Are you sure you want to hide this feed?");
        }
        else {
            $("#confirmmessage").text("Are you sure you want to unhide this feed?");
        }
        $("#btnConfirmYes").bind("click", FeedList_HideSend);
        $("#btnConfirmNo").bind("click", HideModal);
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function FeedList_HideSend(e) {
    e.preventDefault();
    e.stopPropagation();

    if (isMember == "True") {
        var data = "{feedId:" + listActionButton.attr("feedid")
            + ",task:'" + listActionButton.attr("task")
            + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/services/postlist.asmx/ChangeHiddenStatus",
            data: data,
            dataType: "json",
            success: FeedList_HideSuccess,
            error: FeedList_HideError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function FeedList_HideSuccess(responseJSON) {
    if (listActionButton.attr("task") == "add") {
        var list = $(".imgBtnHide[feedid='" + listActionButton.attr("feedid") + "']");
        for (i = 0; i < list.length; i++) {
            $("#" + list[i].id).attr("src", "/images/btn_remove_hid.jpg");
            $("#" + list[i].id).attr("task", "remove");
        }
    }
    else {
        var list = $(".imgBtnHide[feedid='" + listActionButton.attr("feedid") + "']");
        for (i = 0; i < list.length; i++) {
            $("#" + list[i].id).attr("src", "/images/btn_option_hide.png");
            $("#" + list[i].id).attr("task", "add");
        }

    }
    HideModal();
}

function FeedList_HideError(responseJSON) {
    //error code
    HideModal();
}