﻿var loadingImage = '/Images/loading.gif';
function showFaceBoxCommonUsers() {

    $('#faceboxCommonUsers .alertcontent').empty()
    $('#faceboxCommonUsers .body').children().hide().end().
        append('<div class="loading"><img src="' + loadingImage + '"/></div>')
    $('#faceboxCommonUsers').css({
        top: getPageScroll()[1] + (getPageHeight() / 3.3),
        left: getPageScroll()[0] + (getPageWidth() / 3.3)
    }).show();
}
function showFaceBox() {
    if(navigator.appName == "Netscape")
        document.getElementById('dvContainer').style.position = "static";
    $('#facebox .alertcontent').empty()
    $('#facebox .body').children().hide().end().
        append('<div class="loading"><img src="' + loadingImage + '"/></div>')
    $('#facebox').css({
        top: getPageScroll()[1] + (getPageHeight() / 3.3),
        left: getPageScroll()[0] + (getPageWidth() / 3.3)
    }).show();
}
function SendUserMsg(SendMsgLink) {

    $('#facebox .alertcontent').empty();
    var UserIDVal = $(SendMsgLink).next()[0].value;
    var Fullname = $(SendMsgLink).next().next()[0].value;
    document.getElementById("hdnFullName").value = Fullname;
    document.getElementById("hdnUserID").value = UserIDVal;
    $("#divBody").children()[1].value = "";
    $("#divSubject").children()[1].value = "";
    showFaceBox();
    $('#facebox .alertcontent').append($("#dvSendMsg").html());
    $('#facebox .loading').remove();
    $('#facebox .body').children().fadeIn('normal');
}

var FriendLnk;
var UpdateSuggestedPath;
function AddToFriends(Friendlnk, UserID, IsSuggestion, AddFriendPath, ApprovedEnum, PendingEnum, FriendAddedEnum, UpdateSuggestionPath, lblCount) {
    showFaceBox();
    FriendLnk = Friendlnk;
    UpdateSuggestedPath = UpdateSuggestionPath;
    document.getElementById("hdnIsSuggestion").value = IsSuggestion;
    document.getElementById("hdTo").value = $(Friendlnk).next().next()[0].value;
    if(IsSuggestion == "recommendation" || IsSuggestion == "allRecommendation")
        document.getElementById("hdRecommendationID").value = $(Friendlnk).next().next().next()[0].value;
    document.getElementById("hdToName").value = $(Friendlnk).next()[0].value;
    var divMe = "div" + $(Friendlnk).next().next()[0].value;
    document.getElementById("hdDivID").value = divMe;
    document.getElementById('lblConfirmationMsgName').innerHTML = $(Friendlnk).next()[0].value;
    document.getElementById('lblHowDouKnowTitle').innerHTML = $(Friendlnk).next()[0].value + ":";
    if (document.getElementById("hdTo").value != UserID) {
        $.post(AddFriendPath,
                        { UserID: UserID, FriendID: document.getElementById('hdTo').value, Check: "Yes" },
                        function(data) {
                            if (data.indexOf("$$Wrong$$") != -1) {
                                switch (data.split("$$")[0]) {
                                    case ApprovedEnum:
                                        {
                                            $('#facebox .alertcontent').append("<div dir=\"rtl\"  class=\"done\"> انت و " + document.getElementById('lblConfirmationMsgName').innerHTML + " أصدقاء بالفعل </div>");
                                            break;
                                        }
                                    case PendingEnum:
                                        {
                                            $('#facebox .alertcontent').append("<div dir=\"rtl\" class=\"done\"> يوجد عندك طلب صداقة من " + document.getElementById('lblConfirmationMsgName').innerHTML + "</div>");
                                            break;
                                        }
                                    case FriendAddedEnum:
                                        {
                                            $('#facebox .alertcontent').append("<div dir=\"rtl\" class=\"done\">  هناك بالفعل طلب صداقة ل " + document.getElementById('lblConfirmationMsgName').innerHTML + " في انتظار قبوله </div>");
                                            break;
                                        }
                                }
                                $('#facebox .loading').remove();
                                $('#facebox .body').children().fadeIn('normal')

                                $("#facebox").animate({ Class: "fakeClass" }, 2000, function() {
                                    $("#facebox").fadeOut(300);
                                    HideSuggestion(lblCount);

                                });
                            }
                            else {
                                $('#facebox .alertcontent').append($("#divAddFriend").html());
                                $('#facebox .loading').remove();
                                $('#facebox .body').children().fadeIn('normal')
                            }


                        });
       

    }
    else {
        $('#facebox .loading').remove();
        $('#facebox .alertcontent').append("<div dir=\"rtl\" class=\"done\"> لا يمكنك اضافة نفسك كصديق </div>");
        $('#facebox .body').children().fadeIn('normal')
        $("#facebox").animate({ Class: "fakeClass" }, 2000, function() { $("#facebox").fadeOut(300); });
    }
}

function HideSuggestion(lblCount) {
    var IsSuggestion;
    if (document.getElementById("hdnIsSuggestion")!=null)
    IsSuggestion = document.getElementById("hdnIsSuggestion").value;
    if (IsSuggestion != null &&  IsSuggestion != "recommendation" &&  IsSuggestion != "allRecommendation") {
       
        var suggestionUserID = $(FriendLnk).next().next().next()[0].value;
        var SuggestionStatus = 'Accepted';
        var suggestedUserID = $(FriendLnk).next().next()[0].value;
        $.post(UpdateSuggestedPath, { SuggestionUserID: suggestionUserID, SuggestedUserID: suggestedUserID, Status: SuggestionStatus }, function() {
            $(FriendLnk).parents(".reqs-wraper").fadeOut();
            var dvHolder = $(FriendLnk).parents(".reqs-wraper").parent();
            $(FriendLnk).parents(".reqs-wraper").remove();
            if ($(dvHolder).children().length == 0) {
                $(dvHolder).parent().fadeOut();
                $(dvHolder).parent().remove();
                if ($("#dvRepeaterContainer").children().length == 0) {
                    $("#dvNoSuggestions").fadeIn();
                }
            } 
            if ($("#" + lblCount).length > 0)
                $("#" + lblCount)[0].innerHTML = "عدد الطلبات:" + $(".reqs-wraper").length;
            else
                $("#ctl00_ContentPlaceHolder1_lblResultCount")[0].innerHTML = "عدد الطلبات:" + $(".reqs-wraper").length;
        });
        
    }
    else if(IsSuggestion == "recommendation")
    {
        var RecommendationId = document.getElementById("hdRecommendationID").value;
        $.post("/Friends/UpdateRecommendation.aspx",
                {RecID:RecommendationId, IDs:$("#hdRecommendationsIDs")[0].value},
                function(data)
                {
                
                if(data.indexOf("$$NoMoreData$$") != -1)
                {
                   $("#divRecommendations").parents("#dvRecommendedUsersControl").fadeOut();
                    return;
                }
                $("#divRecommendations")[0].innerHTML = $(data).find("#divRecommendations")[0].innerHTML;
                });
    }
    else if(IsSuggestion == "allRecommendation")
    {
        var RecommendationId = document.getElementById("hdRecommendationID").value;
        RemoveRecommendation(RecommendationId);
    }
}
var DeleteSuggestionlnk;
var UpdateSuggestionLink;
function AlertDelete(deleteSuggestionlnk, updateSuggestionLink) {
    showFaceBox();
    DeleteSuggestionlnk = deleteSuggestionlnk;
    UpdateSuggestionLink = updateSuggestionLink;
    $('#facebox .alertcontent').append($("#dvAddConfirm").html());
    $('#facebox .loading').remove();
    $('#facebox .body').children().fadeIn('normal');
    
}
function DeleteSuggestedUser(lblCount) {
    CancelFaceBox();
    var SuggestionID = $(DeleteSuggestionlnk).next().next()[0].value;
    var suggestedUserID = $(DeleteSuggestionlnk).next()[0].value;
    var SuggestionStatus = 'Rejected';
    $.post(UpdateSuggestionLink, { SuggestionUserID: SuggestionID, SuggestedUserID: suggestedUserID, Status: SuggestionStatus }, function() {
        var dvHolder = $(DeleteSuggestionlnk).parents(".reqs-wraper").parent();

        $(DeleteSuggestionlnk).parents(".reqs-wraper").fadeOut();
        $(DeleteSuggestionlnk).parents(".reqs-wraper").remove();
        if ($(dvHolder).children().length == 0) {
            $(dvHolder).parent().fadeOut();
            $(dvHolder).parent().remove(); 
            if ($("#dvRepeaterContainer").children().length == 0) {
                $("#dvNoSuggestions").fadeIn();
            }
        }
        $("#" + lblCount)[0].innerHTML = "عدد الطلبات:" + $(".reqs-wraper").length;
    });
}
function ShowMutualFriends(MutualLnk, MutualPath,IsRecommendation) {
    var MutualIds = $(MutualLnk).next()[0].value;
    $.post(MutualPath, { MutualIds: MutualIds }, function(data) {
        showFaceBoxCommonUsers();
        $('#faceboxCommonUsers .alertcontent').append($(data).find("#dvMutualUsers").html());
      
        if (IsRecommendation == true) {
            if (MutualIds.toString().split(',').length > 8)
                ScrollingFn();
        }
        else {
            if (MutualIds.toString().split(',').length > 9)
                ScrollingFn();
        }

        $('#faceboxCommonUsers .loading').remove();
        $('#faceboxCommonUsers .body').children().fadeIn('normal');
    });
}
////////////////    Confirmation Started here ///////////////////////////////
var ActionCofirmation;
var FriendAction;
    
function CancelFaceBox() {
    $("#facebox").hide();
    $("#faceboxCommonUsers").hide();
}

    function ConfrimSuggest(SuggestFriendsPath, UpdateFriendsPath) {
        $.post(SuggestFriendsPath, { UserID: SuggestionUserID, FriendID: SuggestionFriendID }, function(SuggestedFriendsData) {
            if ($(SuggestedFriendsData).find("#dvSuggestedFriends").children().length > 0) {
                $('#facebox .alertcontent').empty()
                $('#facebox').css({
                    top: getPageScroll()[1] + (getPageHeight() / 3.3),
                    left: getPageScroll()[0] + (getPageWidth() / 3.3)
                }).show();
                if ($(SuggestedFriendsData).find("#dvSuggestedFriends").children().find("#dvUserData").length > 0) {
                    $('#facebox .alertcontent').append($(SuggestedFriendsData).find("#dvSuggestedFriends")[0].innerHTML);

                    if ($("#scroller").children(".sugg-item").length > 4) {
                        ScrollingFn();
                    }
                    $('#facebox .loading').remove();
                    $('#facebox .body').children().fadeIn();
                }
                else {
                    $('#facebox .alertcontent').append($("#dvNoSuggestedFriends").html());
                    $('#facebox .loading').remove();
                    $('#facebox .body').children().fadeIn('normal');
                    $('#facebox .alertcontent').animate({ Class: "Fake" }, 2000, function() {
                        ConfirmCancel(UpdateFriendsPath);
                    });
                }
            }
        });
    }
    
function RejectFriend(Friend, FriendsRequestsPath) {
    var UserID = $(Friend).next().next().next()[0].value
    var FriendID = $(Friend).next().next().next().next()[0].value
    var Status = "Reject";

    $.post(FriendsRequestsPath, { UserID: UserID, FriendID: FriendID, Status: Status }, function(data) {
        $( Friend).parents("#dvFriendRequests").remove();
        $("#ctl00_ContentPlaceHolder1_FriendRequests1_lblFriendsCount")[0].innerHTML = "عدد الطلبات:" + $("#dvRequestsData").children().length;
        if ($("#dvRequestsData").children().length == 0) {
            $("#dvRequestsData")[0].style.display = "none"; 
            $("#dvNoFriendsRequests")[0].style.display = "block";
        }
    });
}
var SuggestionUserID;
var SuggestionFriendID;
function ConfirmCancel(UpdateFriendsPath) {
    $('#facebox').fadeOut('fast');
    if ($("#dvFriendsList").length > 0) {
        $.post(UpdateFriendsPath, { UserID: SuggestionUserID }, function(FriendsData) {
            $("#dvFriendsList")[0].innerHTML = $(FriendsData).find("#dvFriendsList")[0].innerHTML;
            CollapseExpandItems('divForum0');
        });
    }
}
 
function AcceptFriend(Friend, FriendsRequestsPath,UpdateFriendsPath,HasFriends) {
    var friendscount;
    var UserID = $(Friend).next()[0].value;
    var FriendID = $( Friend).next().next()[0].value;
    var Status = "Approve";
    SuggestionFriendID = FriendID;
    SuggestionUserID = UserID;
    var f = $(Friend).prev()[0].innerHTML;
    $.post(FriendsRequestsPath, { UserID: UserID, FriendID: FriendID, Status: Status }, function(data) {
        
        $(Friend).parents("#dvFriendRequests").remove();
        $("#ctl00_ContentPlaceHolder1_FriendRequests1_lblFriendsCount")[0].innerHTML = "عدد الطلبات:" + $("#dvRequestsData").children().length;

        if ($("#dvRequestsData").children().length == 0) {
            $("#dvRequestsData")[0].style.display = "none";
            $("#dvNoFriendsRequests")[0].style.display = "block";
        }
        $('#facebox').animate({ Class: "clickme" }, 2000, function() {
            $('#facebox .alertcontent').empty()
            $('#facebox').css({
                top: getPageScroll()[1] + (getPageHeight() / 3.3),
                left: getPageScroll()[0] + (getPageWidth() / 3.3)
            }).show();

            $('#facebox .alertcontent').append("<div dir=\"rtl\" class=\"done\">انت و " + f + " اصبحتم اصدقاء الآن</div>");
            $('#facebox .loading').remove();
            $('#facebox .body').children().fadeIn();
            $('#facebox .body').children().animate({ Class: "clickme" }, 2000, function() {
                $('#facebox .alertcontent').empty();
                if (HasFriends == 'True') {

                    $("#dvSuggestAsk").children()[0].innerHTML = "هل تريد اقتراح بعض اصدقائك ل " + f;
                    $('#facebox .alertcontent').append($("#dvSuggestAsk").html());
                    $('#facebox .alertcontent').css('direction', 'rtl');
                    $('#facebox .body').children().fadeIn();
                }
                else {
                    ConfirmCancel(UpdateFriendsPath);
                }

            });

        });
    });
}
    function closeConfrim() {
        $("#facebox").hide();
    }
    function ConfirmReject() {
        $("#facebox").fadeOut();
    }
    function ConfrimAccept(FriendsRequestsPath,UpdateFriendsPath,HasFriends) {
        closeConfrim();
        if (ActionCofirmation == "Reject") {
            RejectFriend(FriendAction,FriendsRequestsPath);
        }
        else {
            AcceptFriend(FriendAction,FriendsRequestsPath,UpdateFriendsPath,HasFriends);
        }

    }



    function ConfirmFriendAction(Friend, Action, ConfirmLabelID, ConfirmLabelResource) {
        ActionCofirmation = Action;
        FriendAction = Friend;
        showFaceBox();
        $('#facebox .alertcontent').append($("#dvConfirm").html());
        $('#facebox .loading').remove();
        $('#facebox .body').children().fadeIn('normal');
        document.getElementById(ConfirmLabelID).innerHTML = ConfirmLabelResource;
        
    }
    ////////////////////////end confirmation here //////////////////////////////////////////////
  
