// Update tag list.
// Dependencies : Prototype

var currentOffset = 0;
var maxTagsPerList = 30;

function getPage(offset) {
 
    currentOffset = offset;

    var url = "/ajax?get-tag-page&tag-page-offset="+offset;

    new Ajax.Request(url, {
       method: 'get',
       onSuccess: function callUpdatePage(response) {
           updatePage(response)
       }
    });

}


function updatePage(response) {

    var page  = eval('(' + response.responseText + ')');
   
    var numTags = page.tags.length;

    // Update list of tag links
    var i = 0;
    for (;i < numTags;i++) {
        var tag = page.tags[i];
        // tag element identities are 1..maxTagsPerList range
        var elementId = 'tag'+(i+1);
        var tagElement = document.getElementById(elementId);
        tagElement.innerHTML = '<a href="'+tag.uri+'">'+tag.name+'</a>';     
    }
    while (i < maxTagsPerList) {
        var tagElement = document.getElementById('tag'+(i+1));
        tagElement.innerHTML = '';
        i++;
    }

    // Update numbering
    $('tag-page-from-index').update(page.displayTagPageFromIndex);
    $('tag-page-to-index').update(page.displayTagPageToIndex);

    if (page.hasNextPage == true) {

        var n = document.getElementById('top-next-link');
      //  alert("true n="+n);
        $('top-next-link').show();
        $('top-next-link-text').hide();
        $('bottom-next-link').show();
        $('bottom-next-link-text').hide();

        var nextPageTopElement = document.getElementById('top-next-link');
        var nextPageBottomElement = document.getElementById('bottom-next-link');
        
        // IE requires we use this method of updating the onclick attribute
        nextPageTopElement.onclick = function() { getPage(page.nextPageOffset);return false; }
        nextPageBottomElement.onclick = function() { getPage(page.nextPageOffset);return false; }

    } else if (page.hasNextPage == false) {
         
        $('top-next-link').hide();
        $('top-next-link-text').show();
        $('bottom-next-link').hide();
        $('bottom-next-link-text').show();

    }


    if (page.hasPrevPage == true) {

        var n = document.getElementById('top-prev-link');
      //  alert("true n="+n);
        $('top-prev-link').show();
        $('top-prev-link-text').hide();
        $('bottom-prev-link').show();
        $('bottom-prev-link-text').hide();

        var nextPageTopElement = document.getElementById('top-prev-link');
        var nextPageBottomElement = document.getElementById('bottom-prev-link');
        
        // IE requires we use this method of updating the onclick attribute
        nextPageTopElement.onclick = function() { getPage(page.prevPageOffset);return false; }
        nextPageBottomElement.onclick = function() { getPage(page.prevPageOffset);return false; }

    } else if (page.hasPrevPage == false) {

        $('top-prev-link').hide();
        $('top-prev-link-text').show();
        $('bottom-prev-link').hide();
        $('bottom-prev-link-text').show();

    }



}
 
