
/* - result_list.js - */
/*
#  Origami http://plone.org/products/origami/
#  Publishing tools based on Archetypes and AT Content Types
#  Copyright (c) 2007 Academic Technologies Northwestern University
#
#  This program is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#
"""JavaScript helper functions that implement the TopicFolder
functionality.
"""
__author__  = 'Origami Team <http://nuamps.at.northwestern.edu/origami>'
__docformat__ = 'restructuredtext'
*/

// TODO: rewrite this using jQuery libraries

var xmlrpc = importModule("xmlrpc");
var Kw = importModule("pythonkw").PythonKw;

var currItemTracker = {};
var sortTracker = {};
var bServer;

serviceURL = "."
try
{
    bServer = new xmlrpc.ServiceProxy(serviceURL, ['getResults', 'getTypeCount']);
}
catch(e) {alert(e);}

function gen_snippet(elem, columns, column_classes) {
    var snippet = document.createElement('div');
    snippet.className = "list_items";

    for (var i=0; i<columns.length; i++)
    {
        var field = document.createElement('div');
        field.innerHTML = elem[columns[i]];
        field.className = column_classes[i];
        snippet.appendChild(field);
    }
    
    return snippet
}

function getResults(type, direction, b_size, sort_on, Id, labels, columns, column_classes)
{
    if (type[0] in currItemTracker && direction != 'resort' && direction != 'same')
    {
        if (direction == 'forward')
        {
            currItemTracker[type[0]] += b_size;
        }
        else if (direction == 'back')
        {
            currItemTracker[type[0]] -= b_size;
        }
    }
    else if (direction != 'resort' && direction != 'same')
    {
         currItemTracker[type[0]] = b_size;
    }
    else
    {
         currItemTracker[type[0]] = 0;
    }

    itemNum = currItemTracker[type[0]];
    var sort_reverse;

    // set direction of sorting
    if (typeof sortTracker[type[0]] != 'undefined' && sort_on != '')
    {
        if (direction != 'same')
        {
            sortTracker[type[0]][0] = sort_on;
            sortTracker[type[0]][1] = !sortTracker[type[0]][1];
        }
    }
    else if (typeof sortTracker[type[0]] == 'undefined')
    {
        sortTracker[type[0]] = new Array(2);
        sortTracker[type[0]][0] = sort_on == "" ? "date" : sort_on;
        // reverse the sort?
        sortTracker[type[0]][1] = columns[columns.length-1] == 'title' ? false : true;
    }

    results = bServer.getResults(batch=true, start=itemNum, b_size=b_size, sort_on=sortTracker[type[0]][0], type=type, sort_reverse=sortTracker[type[0]][1]);

    appendId = "appendChild" + Id;
    node = document.getElementById(appendId);
    // remove the old nodes
    children = node.childNodes;
    while(children.length > 0) {
        node.removeChild(children[0]);
    }
    // add the new nodes
    for(var i=0; i<results.length; i++) {
        elem = results[i];
        snippet = gen_snippet(elem, columns, column_classes);
        node.appendChild(snippet);
    }

    totalCount = bServer.getTypeCount(type=type);
    current = document.getElementById("current" + Id);
    first = itemNum + 1
    last = itemNum + b_size > totalCount ? totalCount : itemNum + b_size;
    
    if (totalCount > 1)
    {
        current.innerHTML = "items " + first + "-" + last + " of " + totalCount;
    }
    else if (totalCount == 1)
    {
    	current.innerHTML = "1 item"
    }
    else if (totalCount == 0)
    {
        current.innerHTML = "no items found"
    }

    previous = itemNum;
    more = previous >= b_size ? b_size : previous;
    linkText = previous > 0 ? "&lt; &lt; previous " + more + " |" : "";
    obj = getElement('goback' + Id);

    if (obj)
    {
        obj.innerHTML = linkText;
    }

    remaining = totalCount - (itemNum + b_size);
    more = remaining >= b_size ? b_size : remaining;
    linkText = remaining > 0 ? "| next " + more + " &gt; &gt;" : "";
    obj = getElement('goahead' + Id);

    if (obj)
    {
        obj.innerHTML = linkText;
    }

    updateColumnHeaders(columns, column_classes, Id, sort_on, labels, sortTracker[type[0]][1]);
}

function updateColumnHeaders(columns, column_classes, Id, sort_on, labels, isReversed)
{
    // update column header labels
    for (var i=0; i < columns.length; i++)
    {
        var label = getElement(column_classes[i] + Id);
        var child = label.childNodes[1];

        if (columns[i] != sort_on)
        {
            child.innerHTML = labels[i]
            child.className = '';
        }
        else
        {
            var iconTag = isReversed ? ' <img src="arrow_up.gif" />' : ' <img src="arrow_down.gif" />';
            child.innerHTML = labels[i] + iconTag;
            child.className = 'list_order';
        }
    }
}
