﻿// JS file for Main.aspx
var gfrInProgress = false;
var gfrType = "";
var tempGfrType = "";
var tempPwd = "";
var moreInList = 0;
var loadingMore = false;
var moreIndex = 0;
var threadLimit = 5;
var threadCount = 0;
var idList;
var idListLoaded = new Array();
var userIdIndex = 0;
var currentUserIndex = 0;
var loadingInProgress = false;

function ProcessGFR(type) {
    gfrType = type;
    var pars = "action=userList&type=" + type;
    if (loadingMore) {
        pars += "&startIndex=" + moreIndex;
        loadingMore = false;
    }
    if (type == "flush") {
        $('lock').value = "Lock";
        $('FlushNlockButton').style.display = "block"
    }
    else {
        $('FlushNlockButton').style.display = "none"
    }
    if (type == "grow" || type == "reciprocate") {
        $('lock').value = "Block";
        $('FollowNlockButton').style.display = "block"
    }
    else {
        $('FollowNlockButton').style.display = "none"
    }
    if (type == "followers") {
        $('FollowersNFriendsTitle').style.display = "block";
        $('FollowersNFriendsTitle').innerHTML = "Followers";
    }
    else if (type == "followings") {
    $('FollowersNFriendsTitle').style.display = "block";
    $('FollowersNFriendsTitle').innerHTML = "Followings";
    }
    else if (type == "friends") {
    $('FollowersNFriendsTitle').style.display = "block";
    $('FollowersNFriendsTitle').innerHTML = "Friends";
    }
    else {
        $('FollowersNFriendsTitle').display = "none";
    }
    if (type == "advGrow") {
        pars += "&keyword=" + UrlEncode($('KeyWordToGrow').value);
        if ($('CheckBio').checked) {
            pars += "&searchIn=bio";
        }
        else if ($('CheckTwit').checked) {
            pars += "&searchIn=twit";
        }
    }
    if (type == "crossFollow") {
        var dispType = $('CheckCrossFollowers').checked ? "followers" : "followings";
        pars = "action=CrossFollowList&operation=" + dispType + "&CrossUserId=" + $('CrossTwitterId').value;
    }
    loadingInProgress = true;
    new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: LoadUserList });
    $('UserListMessage').innerHTML = "<img src=\"images/progress.gif\" alt=\"\" style=\"width: 25px; height: 25px;\" /> " + JsmUserList["fu"];
    $('UserListTitle').style.display = "none";
    $('LoadMoreLink').style.display = "none";
    $('ShortMsg').innerHTML = "";
    $('GfrLink').value = TitleCase(type);
}

function LoadMore(type) {
    loadingMore = true;
    GFR(type);
}

function LoadUserList(resp) {
    if (resp.responseText != "false") {
        var result = resp.responseText.split("##");
        idList = result[0].split(',');
        idListLoaded = new Array();
        SendUserRequest();
        // Show Loading
        $('UserListDisplay').style.display = "block";
        $('UserListTitle').style.display = "block";
        $('UserListDisplay').innerHTML = loadingFormat.replace('#%uid%#', idList[currentUserIndex]);
        $('UserListMessage').innerHTML = "";
        var moreCounts = result[1].split('|');
        moreInList = eval(moreCounts[0]);
        moreIndex = eval(moreCounts[1]);
        if (moreInList > 1 && (gfrType == "reciprocate" || gfrType == "flush" || gfrType == "advGrow" || gfrType == "followers" || gfrType == "followings" || gfrType == "friends")) {
            $('LoadMoreLink').style.display = "block";
        }
        if (gfrType == "followers" || gfrType == "followings"||gfrType=="friends")
        {
            $('GfrLink').style.display = "none";
            $('lock').style.display = "none";
        }
        else {
            $('GfrLink').style.display = "block";
            $('lock').style.display = "block";
        } 
    }
    else {
        $('UserListMessage').innerHTML = JsmUserList["noProfile"];
        $('UserListDisplay').style.display = "none";
        loadingInProgress = false;
        gfrInProgress = false;
        gfrType = "";
    }
}
function SendUserRequest() {
    for (; threadCount < threadLimit && userIdIndex < idList.length; threadCount++) {
        var pars = "action=showUser&id=" + idList[userIdIndex++];
        if (gfrType == 'ffs' || gfrType == 'advGrow') {
            pars += "&from=db";
        }
        new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: ShowUser });
    }
}

function ShowUser(resp) {
    var result = resp.responseText.split('|#|');
    var myIndex = currentUserIndex++;
    threadCount--;

    //show user
    if (result[0] == "true") {
        var user = eval("(" + result[1] + ")");
        var uHtml = showUserFormat.replace(/#%uid%#/g, user.Id);
        uHtml = uHtml.replace(/#%image%#/g, user.ImageUrl);
        uHtml = uHtml.replace(/#%name%#/g, user.Name);
        uHtml = uHtml.replace(/#%follower%#/g, user.FollowersCount);
        uHtml = uHtml.replace(/#%follower%#/g, user.FollowersCount);
        uHtml = uHtml.replace(/#%following%#/g, user.FollowingsCount);
        uHtml = uHtml.replace(/#%sname%#/g, user.ScreenName);
        uHtml = uHtml.replace(/#%updates%#/g, user.StatusesCount);
        uHtml = uHtml.replace(/#%location%#/g, user.Location);
        uHtml = uHtml.replace(/#%website%#/g, user.Url);
        uHtml = uHtml.replace(/#%webtext%#/g, user.Url.length < 28 ? user.Url : user.Url.substr(0, 28) + '...');
        if (user.Status) {
            uHtml = uHtml.replace(/#%status%#/g, user.Status.Text);
        }
        else {
            uHtml = uHtml.replace(/#%status%#/g, "");
        }
        uHtml = uHtml.replace(/#%bio%#/g, user.Description);
        $('uList-' + idList[myIndex]).innerHTML = uHtml;
        idListLoaded[idListLoaded.length] = user.Id;
    }
    else {
        $('uList-' + idList[myIndex]).style.display = "none";
        moreIndex--;
    }
    if (currentUserIndex < idList.length) {
        var listItem = document.createElement('div');
        listItem.setAttribute('id', 'uList-' + idList[currentUserIndex]);
        listItem.className = 'userList';
        listItem.innerHTML = '<img src=\"images/progress.gif\" alt=\"\" /> ' + JsmUserList["lu"];
        $('UserListDisplay').appendChild(listItem);
        SendUserRequest();
    }
    else {
        threadCount = 0;
        userIdIndex = 0;
        currentUserIndex = 0;
        loadingInProgress = false;
        if (idListLoaded.length == 0) {
            $('UserListMessage').innerHTML = JsmUserList["noProfile"]; //"Failed to load.";
            $('UserListTitle').style.display = "none";
            $('LoadMoreLink').style.display = "none";
        }
    }
}

function ToggleUserDetail(id) {
    if ($('uListDetail-' + id).style.display == "none") {
        $('uListDetail-' + id).style.display = "block";
        $('uListToggleBtn-' + id).innerHTML = $('DtlUp').innerHTML;
    }
    else {
        $('uListDetail-' + id).style.display = "none";
        $('uListToggleBtn-' + id).innerHTML = $('DtlDn').innerHTML;
    }
    $('Check-' + id).focus();
}

function TitleCase(type) {
    if (type == "grow")
        return JsmUserList["grow"];
    if (type == "flush")
        return JsmUserList["flush"];
    if (type == "reciprocate")
        return JsmUserList["reciprocate"];
    if (type == "ffs")
        return JsmUserList["follow"];
    if (type == "advGrow")
        return JsmUserList["follow"];
    if (type == "crossFollow")
        return JsmUserList["follow"];
    return type;
}

function GfrInProgressAlert(inprogressType, type) {
    if (loadingInProgress == true) {
        $(type + '-message').innerHTML = JsmUserList["luw"];
        //$('UserListMessage').innerHTML = JsmUserList["luw"];
        
    }
    else if (lockInProgress) {
        $(type + '-message').innerHTML = JsmUserList["lockPrg"];
    }
    else {
        $(type + '-message').innerHTML = TitleCase(gfrType) + " " + JsmUserList["alreadyPrg"];
    }
    $(type + '-button').style.display = "none";
    $(type + '-message').style.display = "block";
    var act = "$('" + type + "-message').style.display='none';$('" + type + "-button').style.display='block';";
    setTimeout(act, 5000);
}

function GFR(type) {
    if (isLn == false) {
        AjaxLogin(GFR, type);
    }
    else {
        if (gfrInProgress == false && loadingInProgress == false && lockInProgress == false) {
            ProcessGFR(type);
        }
        else {
            GfrInProgressAlert(gfrType, type);
        }
    }
}
var actionLock = false;
var lockInProgress = false;
var actionFlushAndLock = false;
var actionFollowAndLock = false;
function Lock() {
    actionLock = true;
    GfrFfsSwitch();
}
function FlushAndLock() {
    actionFlushAndLock = true;
    GfrFfsSwitch();
}
function FollowAndLock() {
    actionFollowAndLock = true;
    GfrFfsSwitch();
}   
function GfrFfsSwitch() {
    if (isLn == false) {
        AjaxLogin(GfrFfsSwitch);
    }
    else {
        GfrRequest();
    }
}
function GfrRequest()
 {
    $('ShortMsg').innerHTML = "";
    if (idListLoaded.length > 0) {
        var localIdList = "";
        var noSelected = 0;
        for (var i = 0; i < idListLoaded.length; i++) {
            if ($('Check-' + idListLoaded[i]).checked == true && !$('Check-' + idListLoaded[i]).disabled) {
                $('Check-' + idListLoaded[i]).disabled = true;
                localIdList += ',' + idListLoaded[i];
                noSelected++;
            }
        }
        if (noSelected == 0) {
            $('ShortMsg').innerHTML = "0 selected";
        }
        else {
            $('UserListMessage').innerHTML = "<img src=\"images/progress.gif\" alt=\"\" style=\"width: 25px; height: 25px;\" /> " + JsmUserList["prcReq"];
            if (!actionLock) {
                var tempAction = (gfrType == 'ffs') || (gfrType == 'advGrow') || (gfrType == 'crossFollow') ? "grow" : gfrType;
                var pars = "action=" + tempAction + "&userId=" + localIdList.substring(1);
                if (actionFlushAndLock) {
                    pars += "&flushNLock=true";
                    actionFlushAndLock = false;
                }
                if (actionFollowAndLock) {
                    pars += "&followNLock=true";
                    actionFollowAndLock = false;
                }
                gfrInProgress = true;
                new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: GfrRequestComplete });
            }
            else {
                actionLock = false;
                var tempType = gfrType == "flush" ? "unfollow" : "follow";
                var pars = "action=lock&type=" + tempType + "&userId=" + localIdList.substring(1);
                lockInProgress = true;
                new Ajax.Request("AjaxController.aspx", { method: 'post', parameters: pars, onComplete: LockComplete });
            }
        }
    }
}

function LockComplete(resp) {
    lockInProgress = false;
    $('UserListMessage').innerHTML = resp.responseText;
}

function GfrRequestComplete(resp) {

    gfrInProgress = false;
    var response = resp.responseText.split('##');
    var result = response[0].split('|');
    if (result.length == 3) {
        $('UserListMessage').innerHTML = result[0];
        if (!isNaN(result[2])) {
            var gfrCount = eval(result[2]);
            var folg = eval($('FollowingCount').innerHTML);
            if (gfrType == "flush") {
                var stars = eval($('StarsCount').innerHTML);
                folg -= gfrCount;
                stars -= gfrCount;
                $('FollowingCount').innerHTML = folg;
                $('StarsCount').innerHTML = stars;
            }
            else if (gfrType == "grow") {
                var stars = eval($('StarsCount').innerHTML);
                folg += gfrCount;
                stars += gfrCount;
                $('FollowingCount').innerHTML = folg;
                $('StarsCount').innerHTML = stars;
            }
            else if (gfrType == "reciprocate") {
                var fans = eval($('FansCount').innerHTML);
                var friends = eval($('FriendsCount').innerHTML);
                fans -= gfrCount;
                fans = fans < 0 ? 0 : fans;
                friends += gfrCount;
                folg += gfrCount;
                $('FollowingCount').innerHTML = folg;
                $('FansCount').innerHTML = fans;
                $('FriendsCount').innerHTML = friends;
            }
        }
    }
    result = response[1].split('#');
    for (var i = 0; i < result.length; i++) {
        var bResult = result[i].split('|');
        if (Trim(bResult[1]) == "1") {
            $('uListMessage-' + bResult[0]).style.color = "green";
            $('uListMessage-' + bResult[0]).innerHTML = gfrType == "flush" ? JsmUserList["removed"] : JsmUserList["added"];
        }
        else {
            $('uListMessage-' + bResult[0]).style.color = "red";
            $('uListMessage-' + bResult[0]).innerHTML = bResult[1];
        }
    }
}

function SelectAll() {
    for (var i = 0; i < idListLoaded.length; i++) {
        $('Check-' + idListLoaded[i]).checked = true;
    }
}

function SelectNone() {
    for (var i = 0; i < idListLoaded.length; i++) {
        $('Check-' + idListLoaded[i]).checked = false;
    }
}
