var tagExplorerOrderBy = "popular";
var tagExplorerPageSize = 20;
var tagExplorerPageOffset = 0;
var tagExplorerPrefetch = tagExplorerPageSize * 5;

// the following are created by the inital request to the page.  don't
// uncomment these lines.
// var tagExplorerLogin = ?
// var tagExplorerMax = ?
// var tagExplorerMode = ?
// var tagExplorerSearchText = ?

function nextTags() {
    if (!Element.hasClassName("nextTags", "disabled") &&
        !Element.hasClassName("nextTags", "loading")) {
        scrollTags(tagExplorerPageSize);
    }
}

function previousTags() {
    if (!Element.hasClassName("previousTags", "disabled") &&
        !Element.hasClassName("previousTags", "loading")) {
        scrollTags(tagExplorerPageSize * -1);
    }
}

function scrollTags(distance) {
    tagExplorerPageOffset += distance;

    if (tagExplorerPageOffset < 0) {
        tagExplorerPageOffset = 0;
    }
    
    if (tagExplorerPageOffset == 0) {
        Element.addClassName("previousTags", "disabled");
    } else {
        Element.removeClassName("previousTags", "disabled");
    }

    var tagControl = getTagExplorerControl();

    var offset = tagControl.getElementsByTagName("li").length;

    var targetCount = 
        Math.min(tagExplorerPageOffset + Math.abs(distance), tagExplorerMax);

    if (targetCount > offset) {
        if (distance > 0) {
            Element.addClassName("nextTags", "loading");
        }

        new Ajax.Request("TagExplorer.ashx" + 
                         "?login=" + encodeURIComponent(tagExplorerLogin) +
                         "&offset=" + offset + 
                         "&pf=" + tagExplorerPrefetch + 
                         "&num=" + tagExplorerPageSize + 
                         "&st=" + tagExplorerSearchText + 
                         "&mode=" + tagExplorerMode + 
                         "&order=" + tagExplorerOrderBy,
                        { method: "GET", 
                          onSuccess: tagRequestComplete});

    } else {
        updateTagDisplay();
    }
}

function tagRequestComplete(request) {

    Element.removeClassName("nextTags", "loading");

    var newTagList = document.createElement("div");
    newTagList.innerHTML = getElementText(request.responseXML);
    
    document.body.appendChild(newTagList);

    var tagControl = getTagExplorerControl();
    
    $A(newTagList.getElementsByTagName("li")).each((function(c) {
        tagControl.appendChild(c);
    }));

    updateTagDisplay();
}

function updateTagDisplay() {
    var listItems = getTagExplorerControl().getElementsByTagName("li");

    var upperBound = 
        Math.min(listItems.length,
                 tagExplorerPageOffset + tagExplorerPageSize);

    $A(listItems).each((function(e, i) {
        (i < tagExplorerPageOffset || i >= upperBound ? Element.hide : Element.show)(e);
    }));
    
    $("tagz").getElementsByTagName("span").item(0).firstChild.nodeValue = 
        (tagExplorerPageOffset + 1) + " - " + upperBound;

    if ((tagExplorerPageOffset + tagExplorerPageSize) >= tagExplorerMax) {
        Element.addClassName("nextTags", "disabled");
    } else {
        Element.removeClassName("nextTags", "disabled");
    }
}

function getTagExplorerControl() {

    var index = 0;
    var tags = $("tagz").getElementsByTagName("ul");

    if ((tagExplorerMode == "Related") && (tags.length > 1)) {
        index = 1;
    }

    return tags.item(index);
}
