var friendExplorerOrderBy = "activity";
var friendExplorerPageSize = 9;
var friendExplorerPageOffset = 0;
var friendExplorerPrefetch = friendExplorerPageSize * 5;
var friendExplorerResetOnNextFetch = false;

function friendsByAlphabet() {
    if (!Element.hasClassName("friendsAlpha", "disabled") &&
        !Element.hasClassName("friendsAlpha", "loading") &&
        !Element.hasClassName("friendsAlpha", "selected")) {

        $("friendsAlpha").className = "loading";
        $("friendsActivity").className = "disabled";

        friendExplorerOrderBy = "alpha";
        reset();
    }
}

function friendsByActivity() {
    if (!Element.hasClassName("friendsActivity", "disabled") &&
        !Element.hasClassName("friendsActivity", "loading") &&
        !Element.hasClassName("friendsActivity", "selected")) {

        $("friendsActivity").className = "loading";
        $("friendsAlpha").className = "disabled";

        friendExplorerOrderBy = "activity";
        reset();
    }
}

function reset() {
    friendExplorerResetOnNextFetch = true;
    friendExplorerPageOffset = 0;
    scrollFriends(0);
}

function nextFriends() {
    if (!Element.hasClassName("nextFriends", "disabled") &&
        !Element.hasClassName("nextFriends", "loading")) {
        scrollFriends(friendExplorerPageSize);
    }
}

function previousFriends() {
    if (!Element.hasClassName("previousFriends", "disabled") &&
        !Element.hasClassName("previousFriends", "loading")) {
        scrollFriends(friendExplorerPageSize * -1);
    }
}

function scrollFriends(direction) {

    friendExplorerPageOffset += direction;
    if (friendExplorerPageOffset < 0) {
        friendExplorerPageOffset = 0;
    }

    if (friendExplorerPageOffset == 0) {
        Element.addClassName("previousFriends", "disabled");
    } else {
        Element.removeClassName("previousFriends", "disabled");
    }

    var friendList = getFriendList();

    var offset =
        friendExplorerResetOnNextFetch ? 0 : friendList.getElementsByTagName("li").length;

    var targetCount =
        Math.min(friendExplorerPageOffset + friendExplorerPageSize,
                 friendExplorerMax);
    if (friendExplorerResetOnNextFetch || targetCount > offset) {
        if (friendExplorerResetOnNextFetch) {
            Element.addClassName("previousFriends", "disabled");
            Element.addClassName("nextFriends", "disabled");
        } else {
            Element.addClassName("nextFriends", "loading");
        }

        new Ajax.Request("FriendExplorer.ashx" +
                         "?login=" + encodeURIComponent(friendExplorerLogin) +
                         "&offset=" + offset +
                         "&num=" + friendExplorerPrefetch +
                         "&order=" + friendExplorerOrderBy,
                         { method: "GET",
                           onSuccess: friendListRequestComplete });
    } else {
        updateFriendDisplay();
    }

}

function friendListRequestComplete(request) {

    Element.removeClassName("nextFriends", "loading");

    var newFriendsList = document.createElement("div");
    newFriendsList.innerHTML = getElementText(request.responseXML);

    document.body.appendChild(newFriendsList);

    var friendList = getFriendList();

    if (friendExplorerResetOnNextFetch) {
        var listItems = friendList.getElementsByTagName("li");
        for (var i = listItems.length - 1; i >= 0; i--) {
            friendList.removeChild(listItems.item(i));
        }
        if (friendExplorerOrderBy == "alpha") {
            $("friendsAlpha").className = "selected";
            $("friendsActivity").className = "";
        } else {
            $("friendsActivity").className = "selected";
            $("friendsAlpha").className = "";
        }
        friendExplorerResetOnNextFetch = false;
    }

    $A(newFriendsList.getElementsByTagName("li")).each((function(c) {
        friendList.appendChild(c);
    }));

    updateFriendDisplay();

}

function updateFriendDisplay() {

    var listItems = getFriendList().getElementsByTagName("li");
    var upperBound =
        Math.min(listItems.length,
                 friendExplorerPageOffset + friendExplorerPageSize);

    $A(listItems).each((function(e, i) {
        (i < friendExplorerPageOffset || i >= upperBound ? Element.hide : Element.show)(e);
    }));

    $("friends").getElementsByTagName("span").item(0).firstChild.nodeValue =
        (friendExplorerPageOffset + 1) + " - " + upperBound;

    // Update the next button here, so "disabled" doesn't stomp
    // "loading".
    if ((friendExplorerPageOffset + friendExplorerPageSize) >= friendExplorerMax) {
        Element.addClassName("nextFriends", "disabled");
    } else {
        Element.removeClassName("nextFriends", "disabled");
    }
}

function getFriendList() {
    return $("friends").getElementsByTagName("ul").item(0);
}
