/*
 * Copyright (c) 2010 SciBet (support@scibet.com). All rights reserved.
 */

function setcookie(name, value, exdays)
{
    var exdate = new Date();

    exdate.setDate(exdate.getDate() + exdays);

    document.cookie = name + "=" + escape(value) + ((exdays == null) ?
        "" : ";expires=" + exdate.toUTCString()) + ";path=/";
}

function getcookie(name)
{
    if (document.cookie.length > 0)
    {
        var lindex, rindex;

        lindex = document.cookie.indexOf(name + "=");

        if (lindex != -1)
        {
            lindex = lindex + name.length + 1;
            rindex = document.cookie.indexOf(";", lindex);
            rindex = (rindex == -1) ? document.cookie.length : rindex;

            return unescape(document.cookie.substring(lindex, rindex));
        }
    }

    return null;
}


function selector(euid, dflt)
{
    var root;
    var head;
    var list;

    {
        var  save;

        root = document.getElementById(euid);
        list = root.getElementsByTagName("li");

        if ((list[0].childNodes.length) &&
            (list[0].childNodes[0].nodeName.toLowerCase() == "a"))
        {
            save = find(list, document.baseURI || document.URL);
        }
        else
        {
            save = getcookie(euid);
        }

        save = (save == null) ? dflt : save;

        for (var i = 0; i < list.length; i++)
        {
            if (list[i].id == save)
            {
                head = list[i].cloneNode(true);
                break;
            }
        }

        root.insertBefore(head, list[0]);
        hide();
    };

    function find(list, link)
    {
        for (var i = 0; i < list.length; i++)
        {
            if (list[i].childNodes[0].href == link)
            {
                return list[i].id;
            }
        }

        return null;
    }

    function show()
    {
        for (var i = 1; i < list.length; i++)
        {
            list[i].style.display = '';
        }

        head.onclick = function() { hide(); return false; };
    }

    function hide()
    {
        for (var i = 1; i < list.length; i++)
        {
            list[i].style.display = 'none';
            list[i].onclick = function() { open(this); return false; };
        }

        head.onclick = function() { show(); return false; };
    }

    function open(item)
    {
        hide();

        if ((item.childNodes.length) &&
            (item.childNodes[0].nodeName.toLowerCase() == "a"))
        {
            window.open(item.childNodes[0].href, '_self');
        }
        else
        {
            setcookie(root.id, item.id);
            window.location.reload();
        }
    };
}

function timezone(euid)
{
    var list;

    {
        var     root;
        
        root = document.getElementById(euid);
        list = root.getElementsByTagName("li");

        update();
        setInterval(function() { update(); }, 1000);
    }


    function update()
    {
        var date = new Date(), base = date.getTime();
        var hour, mins, zone, sign;

        for (var i = 0; i < list.length; i++)
        {
            sign = list[i].id.substring(3,4);
            sign = (sign == "p") ? 1 : -1;
            zone = sign * parseFloat(list[i].id.substring(4,6));

            date.setTime(base + zone * 3600000);

            hour = date.getUTCHours();
            mins = date.getUTCMinutes();

            hour = (hour < 10 ? "0" : "") + hour;
            mins = (mins < 10 ? "0" : "") + mins;
            zone = (zone >= 0 ? "+" : "-") + (Math.abs(zone) < 10 ? "0" : "") + Math.abs(zone);

            list[i].firstChild.nodeValue = hour + ":" + mins + " GMT" + zone;
        }
    }
}


function sortable(euid, size, fade)
{
    var     tless;
    var     tmore;
    var     tsize;
    var     tsort;
    var     tfade;
    var     table;
    var     tcont;

    {
        var     thead, tbody, trows, i;

        if (document.ELEMENT_NODE == null)
        {
            document.ELEMENT_NODE = 1;
            document.TEXT_NODE = 3;
        }

        table = document.getElementById(euid);
        thead = table.getElementsByTagName("th");

        for (i = 0; i < thead.length; i++)
        {
            if (thead[i].className != 'nosort')
            {
                thead[i].onclick = function() { sort(this.cellIndex); };
                thead[i].style.cursor = 'pointer';
            }
        }

        tbody = table.tBodies[0];
        trows = tbody.getElementsByTagName("tr");

        tless = (size <= -1) ? trows.length : size;
        tmore = trows.length;
        tsize = tless;
        tsort = -1;
        tfade = fade;

        for (i = 0; i < trows.length; i++)
        {
            trows[i].style.display = (i < tless) ? '' : 'none';
        }

        tcont = document.getElementById(euid + '_show');

        if (tless < tmore)
        {
            tcont.onclick = function() { show(tmore); };
            tcont.style.cursor = 'pointer';
            tcont.style.color = '#FFFFFF';
            tcont.style.borderBottom = 'thin dotted #FFFFFF';
            tcont.style.fontSize = '9px';
            tcont.style.letterSpacing = '1px';
            tcont.firstChild.nodeValue = 'more';
        }
    }


    function parse(cell, index)
    {
        var     numb, date, text = "";
        var     node;

        for (i = 0; i < cell.childNodes.length; i++)
        {
            node  = cell.childNodes[i];
            text += (node.nodeType == document.TEXT_NODE) ?
                node.nodeValue.toLowerCase() : parse(node);
        }

        text = text.replace(/(\$|\,)/g, "");

        if (!isNaN(date = Date.parse(text)))
        {
            return date;
        }

        return ((index == 0) || isNaN(numb = parseFloat(" " + text))) ? text : -numb;
    }


    function order(lval, rval)
    {
        return ((lval.value < rval.value) ? -1 : ((lval.value > rval.value) ? 1 : 0));
    }


    function sort(index)
    {
        var     tbody, cbody, trows, crows, array,  i, j;

        tbody = table.tBodies[0];
        trows = tbody.getElementsByTagName("tr");
        array = new Array(trows.length);

        for (i = 0; i < trows.length; i++)
        {
            array[i] = { index: i, value: parse(trows[i].cells[index], index) };
        }

        index != tsort ? array.sort(order) : array.reverse();
        cbody  = document.createElement('tbody');

        for (i = 0; i < trows.length; i++)
        {
            crows = trows[array[i].index].cloneNode(true);
            crows.style.display = (i < tsize) ? '' : 'none';

            for (j = 0; j < crows.cells.length; j++)
            {
                crows.cells[j].style.backgroundColor = (j == index) ? tfade : "transparent";
            }

            cbody.appendChild(crows);
        }

        table.replaceChild(cbody, tbody);
        tsort = index;

        return true;
    }


    function show(nrows)
    {
        var     tbody, trows, i;

        tbody = table.tBodies[0];
        trows = tbody.getElementsByTagName("tr");
        tsize = nrows;

        for (i = 0; i < trows.length; i++)
        {
            trows[i].style.display = (i < tsize) ? '' : 'none';
        }

        if (tless < tmore)
        {
            if (nrows == tless)
            {
                tcont.onclick = function() { show(tmore); };
                tcont.firstChild.nodeValue = 'more';
            }
            else
            {
                tcont.onclick = function() { show(tless); };
                tcont.firstChild.nodeValue = 'less';
            }
        }

        return true;
    }
};

