﻿function FollowUserLink_FollowUser(followedUser, objLink, container, divLoading) {
    var obj= $_(objLink);
    if(obj.getAttribute("action") != "Disabled"){
        var params;
        params = "action="+obj.getAttribute("action")+"&";
        
        if(!isNaN(followedUser))
            params += "followedUserId=";
        else
            params += "followedUserName=";

        params += followedUser;
        FollowUserLink_ShowLoading(true, container, divLoading);
        new Ajax.Request('/AJAX/PostPages/Users/updatefollowuser.aspx', {method: 'post', parameters: params, onSuccess: function(AJAXResponse) {  FollowUserLink_onSuccess(AJAXResponse, objLink, container, divLoading); } });
    }else
        FollowUserLink_ShowLoading(false, container, divLoading)

}

function FollowUserLink_onSuccess(AJAXResponse, objLink, container, divLoading) {
   
    FollowUserLink_ShowLoading(false, container, divLoading);
    if (AJAXResponse.status == 200){
        var toFollow = AJAXResponse.responseText;
        
        var objIds = new Array();
        var multipleIds = $_(objLink).getAttribute("MultipleIds");
        
        if(multipleIds != null && multipleIds != "")
            objIds = multipleIds.split(";");

        if(objIds.length == 0)
            objIds.push(objLink);
        
        for(i=0; i<= objIds.length-1; i++){
            var obj= $_(objIds[i]);
            if (obj.id.indexOf('lblLink') >= 0)
            {
                if(toFollow == "True"){
                        obj.style.display = '';
                        obj.innerHTML = obj.getAttribute("FollowText");
                        obj.setAttribute("action", "Follow");
                }else{
                    if(obj.getAttribute("ShowIgnore") == "True"){
                       obj.innerHTML = obj.getAttribute("IgnoreText");
                       obj.setAttribute("action", "Ignore");
                    }else
                        obj.style.display = 'none';
                }
            }else
            {
                if(toFollow == "True"){
                        obj.title = obj.getAttribute("FollowText");
                        obj.setAttribute("action", "Follow");
                        obj.src = obj.getAttribute("FollowIcon");
                }else{
                    if(obj.getAttribute("ShowIgnore") == "True"){
                        obj.title = obj.getAttribute("IgnoreText");
                        obj.setAttribute("action", "Ignore");
                        obj.src = obj.getAttribute("IgnoreIcon");
                     }else{
                        //obj.style.display = 'none';
                        obj.title = obj.getAttribute("DisabledIgnoreText");
                        obj.setAttribute("action", "Disabled");
                        obj.src = obj.getAttribute("IgnoreIcon");
                        
                     }
                }
            }
        }
    }
    else
        alert("Error loading Options, please refresh your browser");
       
}

function FollowUserLink_ShowLoading(show, container, divLoading) {
    if (show) {
        $_(divLoading).style.display = 'block';
        $_(container).style.display = 'none';
    }
    else {
        $_(divLoading).style.display = 'none';
        $_(container).style.display = '';
    }
}
