﻿//Favs.js file.
$(document).ready(function() {
    $("#myBlog li span").click(function() {
        var hidden = $(this).parents("li").children("ul").is(":hidden");

        $("#myBlog>ul>li>ul").hide();
        $("#myBlog>ul>li>a").removeClass();

        if (hidden) {
            $(this)
      .parents("li").children("ul").toggle()
      .parents("li").children("a").addClass("MyBlogSpan");
            $("#myBlog").addClass("MyBlogDivOpen");
        }
    });
    $("body").mouseup(function(oEvent) {
        //alert("mouseup");
        $("#myBlog>ul>li>ul").hide();
        $("#myBlog>ul>li>a").removeClass();
        $("#divSearchOptions").hide();
        $("#myBlog").removeClass();
        return true;
    });
});
var widgetIdToShowFunction = new Object();
var widgetIdToHideFunction = new Object();
function SetPanelVisibility(dpId, value, easing, duration) {
//debugger
    var dp = document.getElementById(dpId);
    if (value == 'toggle') {
        if (dp.style.visibility == 'hidden') {
            value = '';
        } else {
            value = 'hidden';
        }
    }
    if ((dp.style.visibility == 'hidden' && value == 'hidden') ||
        (dp.style.visibility == '' && value == '')) {
        return;
    }
    if (easing == 'none') {
        dp.style.display = '';
        dp.style.visibility = value;
    } else if (easing == 'fade') {
        if (value == 'hidden') {
            if (dp.style.visibility != 'hidden') {
                $('#' + dpId).fadeOut(duration, function() {
                    $('#' + dpId).css('visibility', 'hidden');
                });
            }
        } else {
            if (dp.style.visibility == 'hidden') {
                dp.style.display = 'none';
                dp.style.visibility = '';
                $('#' + dpId).fadeIn(duration, function() { });
            }
        }
    }
    if (value == 'hidden') {
        var hideFunction = widgetIdToHideFunction[dpId];
        if (hideFunction) {
            hideFunction();
        }
    } else {
        var showFunction = widgetIdToShowFunction[dpId];
        if (showFunction) {
            showFunction();
        }
    }
}
//Add fav
function addNewFav(favId) {
    var blogsArray = new Array();
    blogsArray[0] = favId;
    $.ajax({
        type: "POST",
        url: "../Edit/FollowingHandler.ashx",
        data: JSON.stringify({ Action: 5, FavBlog: blogsArray }),
        contentType: "application/jason",
        dataType: "json",
        success: function(data) {
            // alert(data);
            doGuiAddAction(data, favId);
        },
        error: function(xhr, status) {
            //alert("An error occurred: Go logout" );
            unspecifiedErr();
        }
    });
}
function doGuiAddAction(data, favId) {
    if (data.ActionSucceeded == true) {
        document.getElementById("divAddFav").style.display="none";
        document.getElementById("divDelFav").style.display = "block";
        SetPanelVisibility('divAddingSuccess', '', 'fade', 1000);
        SetPanelVisibility('divAddingSuccess', 'hidden', 'fade', 2000);
    }
}
//Delete a fav
function deleteFav(favId) {
    var blogsArray = new Array();
    blogsArray[0] = favId;
        $.ajax({
            type: "POST",
            url: "../Edit/FollowingHandler.ashx",
            data: JSON.stringify({ Action: 1, FavBlog: blogsArray }),
            contentType: "application/jason",
            dataType: "json",
            success: function(data) {
                doGuiDeleteAction(data, favId);
            },
            error: function(xhr, status) {
            //alert("An error occurred: Go logout" );
            unspecifiedErr();
            }
        });
}
function doGuiDeleteAction(data, favId) {
    if (data.ActionSucceeded == true) {
         document.getElementById("divAddFav").style.display="block";
         document.getElementById("divDelFav").style.display="none";
    }
}
function isFav(favId) {
  // alert("ISFAVVVVVVVVVVVV");
    var blogsArray = new Array();
    blogsArray[0] = favId;
    $.ajax({
        type: "POST",
        url: "../Edit/FollowingHandler.ashx",
        data: JSON.stringify({ Action: 6, FavBlog: blogsArray }),
        contentType: "application/jason",
        dataType: "json",
        success: function(data) {
       
            doGuiIsFavAction(data, favId);

        },
        error: function(xhr, status) {
        // alert("An error occurred: " + status);
        document.getElementById("divAddFav").style.display = "none";
        document.getElementById("divDelFav").style.display = "none";
        }
    });
}
function doGuiIsFavAction(data, favId) {
    //alert(data.ActionSucceeded);
    if (data.ActionSucceeded == true) {
        document.getElementById("divAddFav").style.display = "none";
        document.getElementById("divDelFav").style.display = "block";
    }
    else {
        document.getElementById("divAddFav").style.display = "block";
        document.getElementById("divDelFav").style.display = "none";
    }
}
function unspecifiedErr() {
  //  alert("Handler Err: go logout");
    window.location = "/HeaderActions.asp?logout=true"; 
}
