﻿$(document).ready(NewsList_Initialize);
var listActionButton;

function NewsList_Initialize() {
    var list;

    list = $(".hlTitle");
    for (i = 0; i < list.length; i++) {
    	$("#" + list[i].id).bind("click", NewsList_PostClickRun);
    }
    
    list = $(".imgBtnVote");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", NewsList_VoteRun);
    }

    list = $(".imgBtnEmail");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", NewsList_EmailRun);
    }

    list = $(".imgBtnFavorite");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", NewsList_FavoriteRun);
    }

    list = $(".imgBtnSaved");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", NewsList_SavedRun);
    }

    list = $(".imgBtnHide");
    for (i = 0; i < list.length; i++) {
        $("#" + list[i].id).bind("click", NewsList_HideRun);
    }

    list = $(".imgBtnTweet");
    for (i = 0; i < list.length; i++) {
		$("#" + list[i].id).bind("click", NewsList_TweetRun);
	}
}


// Post Click Section
function NewsList_PostClickRun(e) {
	//e.preventDefault();
	//e.stopPropagation();

	listActionButton = $("#" + this.id);

	if (listActionButton.attr("task") == "add") {
		listActionButton.attr("task", "remove");
		var voteCount = $("span[postid='" + listActionButton.attr("postid") + "']");
		voteCount.text(parseInt(voteCount.text()) + 1);
	}
	else {

	}
}


// Vote Section
function NewsList_VoteRun(e) {
    e.preventDefault();
    e.stopPropagation();

    listActionButton = $("#" + this.id);

    if (isMember == "True") {
        if (listActionButton.attr("task") == "remove") return;
        var data = "{postId:" + listActionButton.attr("postid")
            + ",task:'" + listActionButton.attr("task")
            + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/services/postlist.asmx/ChangeVoteStatus",
            data: data,
            dataType: "json",
            success: NewsList_VoteSuccess,
            error: NewsList_VoteError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_VoteSuccess(responseJSON) {
    if (listActionButton.attr("task") == "add") {
        listActionButton.attr("src", "/images/btn_voted.png");
        listActionButton.attr("task", "remove");
        var voteCount = $("span[postid='" + listActionButton.attr("postid") + "']");
        voteCount.text(parseInt(voteCount.text()) + 1);
    }
    else {
        
    }
}

function NewsList_VoteError(responseJSON) {
    //error code
}


// Email Section
function NewsList_EmailRun(e) {
    e.preventDefault();
    e.stopPropagation();

    if (isMember == "True") {
        document.location = "/tell-a-friend.aspx?id=" + $("#" + this.id).attr("postid");
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

// Twitter Section
function NewsList_TweetRun(e) {
   	e.preventDefault();
   	e.stopPropagation();

   	//if (isMember == "True")
   	//{
   		window.open("http://twitter.com/home?status=" + encodeURIComponent($("#" + this.id).attr("postname") + " - " + "http://" + "FoodBlogs.com" + "?t=" + $("#" + this.id).attr("postid")));
   	//}
   	//else {
   		//DisplayModal("usernotloggedin", '350');
   	//}
}


// Favorite Section
function NewsList_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 want to remove this feed from your favorites?  (you can always add it back to your favorites at any time)");
        }
        $("#btnConfirmYes").bind("click", NewsList_FavoriteSend);
        $("#btnConfirmNo").bind("click", HideModal);
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_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: NewsList_FavoriteSuccess,
            error: NewsList_FavoriteError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_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 NewsList_FavoriteError(responseJSON) {
    //error code
    HideModal();
}


// Saved Section
function NewsList_SavedRun(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 post to your Saved Posts?  (you can always remove it at any time)");
        }
        else {
            $("#confirmmessage").text("Are you sure you want to remove this post from your Saved Posts?  (you can always add it back at any time)");
        }
        $("#btnConfirmYes").bind("click", NewsList_SavedSend);
        $("#btnConfirmNo").bind("click", HideModal);
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_SavedSend(e) {
    e.preventDefault();
    e.stopPropagation();

    if (isMember == "True") {
        var data = "{postId:" + listActionButton.attr("postid")
            + ",task:'" + listActionButton.attr("task")
            + "'}";

        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "/services/postlist.asmx/ChangeSavedStatus",
            data: data,
            dataType: "json",
            success: NewsList_SavedSuccess,
            error: NewsList_SavedError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_SavedSuccess(responseJSON) {
    if (listActionButton.attr("task") == "add") {
        listActionButton.attr("src", "/images/btn_remove_saved.jpg");
        listActionButton.attr("task", "remove");
    }
    else {
        listActionButton.attr("src", "/images/btn_option_save.png");
        listActionButton.attr("task", "add");
    }
    HideModal();
}

function NewsList_SavedError(responseJSON) {
    //error code
    HideModal();
}


// Hide Section
function NewsList_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?  Posts from this feed will no longer be displayed on the News page.  (you can always unhide it at any time)");
        }
        else {
            $("#confirmmessage").text("Are you sure you want to unhide this feed?  Posts from this feed will no longer be displayed on the News page.  (you can always hide it at any time)");
        }
        $("#btnConfirmYes").bind("click", NewsList_HideSend);
        $("#btnConfirmNo").bind("click", HideModal);
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_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: NewsList_HideSuccess,
            error: NewsList_HideError
        });
    }
    else {
        DisplayModal("usernotloggedin", '350');
    }
}

function NewsList_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 NewsList_HideError(responseJSON) {
    //error code
    HideModal();
}