// Current year
var now = new Date();
var year = now.getYear();
if (year < 2000) {
    year += 1900;
}

// Functions to display scrolling messages defined in array msg
var height = 120;
var msgId = 0;
var pos1 = 0;
var pos2 = 0;

function scrollMsg() {
    var msgNum = msg.length;
    if (msgNum && document.getElementById) {
        msgId = (msgId < msgNum) ? msgId : 0;
        if (pos1 <= -(height - 2)) {
            pos1 = height;
            updateMsg('msg1');
        } else {
            pos1 -= 2;
        }
        if (pos2 <= -((height*2) - 2)) {
            pos2 = 0;
            updateMsg('msg2');
        } else {
            pos2 -= 2;
        }
        document.getElementById('frame1').style.top = pos1 + "px";
        document.getElementById('frame2').style.top = pos2 + "px";
        delay = (pos1%height) ? 25 : 2000;
        setTimeout("scrollMsg()", delay);
    }
}

function updateMsg(id) {
    document.getElementById(id).innerHTML = eval("msg["+msgId+"]");
    msgId++;
}

// Functions to rotate contents defined in array thumbs
var thumbId = 0;

function rotateThumbs(dir) {
    if ((thumbs.length >= 3) && document.getElementById) {
        var ids = new Array();
        thumbId = ids[0] = getValidId(eval(thumbId + dir + 1));
        ids[1] = getValidId(thumbId + 1);
        ids[2] = getValidId(thumbId + 2);
        for (x = 0; x < 3; x++) {
            replacement = eval("thumbs[" + ids[x] + "]");
            elementId = 'screenshot' + (x + 1);
            document.getElementById(elementId).innerHTML = replacement;
        }
    }
}

function getValidId(id) {
    if (id >= thumbs.length) {
        return id % thumbs.length;
    } else if (id < 0) {
        return id + thumbs.length;
    } else {
        return id;
    }
}

// Functions to manage dynamic menu objects

var menu = function(name, parent) {
    this.name = name;
    this.parent = parent;
    this.children = Array();
    this.status = 'closed';
    this.closingId = null;
    this.items = new Array();

    this.open = function(event) {
        if (element = document.getElementById(this.name)) {
            switch (this.status) {
                case 'closing':
                    if (this.closingId) {
                        this._updateStatus('open');
                        clearTimeout(this.closingId);
                        this.closingId = null;
                    }
                    break;
                case 'closed':
                    if (event) {
                        element.style.left = event.clientX + 'px';
                        element.style.top = event.clientY + 'px';
                    }
                    element.innerHTML = '';
                    if (this.items) {
                        this._updateStatus('opening');
                        element.style.visibility = 'visible';
                        var children = '';
                        var childCount = 0;
                        for (nb = 0; nb < this.items.length; nb++) {
                            delay = nb * 100;
                            setTimeout(this.name + '._showItem(' + nb + ')', delay);
                            if (child = this.items[nb][2]) {
                                children += '<DIV ID="' + child + '" ONMOUSEOVER="' + child + '.open()" ONMOUSEOUT="' + child + '.close()" STYLE="position:absolute; left:182px; top:' + (nb * 24) + 'px; visibility:hidden"></DIV>\r\n'
                                this.children[childCount] = child;
                                childCount++;
                            }
                        }
                        setTimeout(this.name + '._updateStatus(\'open\')', delay);
                        element.innerHTML += children;
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
        return false;
    }

    this._showItem = function(nb) {
        if (element = document.getElementById(this.name)) {
            element.innerHTML += '<A CLASS="menuItem" HREF="' + (this.items[nb][0] ? this.items[nb][0] : '#nolink') + '" ONMOUSEOVER="' + this.name + '.hoverItem(' + nb + ')"' + (this.items[nb][2] ? ('ONMOUSEOUT="' + this.items[nb][2] + '.close()"') : '') + '>' + this.items[nb][1] + '</A>\r\n';
            return true;
        }
        return false;
    }

    this.hoverItem = function(nb) {
        if (this.children) {
            for (child = 0; child < this.children.length; child++) {
                if (this.children[child] != this.items[nb][2]) {
                    eval(this.children[child] + '._hide()');
                }
            }
            if (childMenu = this.items[nb][2]) {
                eval(childMenu + '.open()');
            }
        }
    }

    this.close = function() {
        if (element = document.getElementById(this.name)) {
            switch (this.status) {
                case 'open':
                    this.closingId = setTimeout(this.name + '._hide()', 100);
                    this._updateStatus('closing');
                    if (this.parent) {
                        eval(this.parent + '.close()');
                    }
                    break;
                default:
                    break;
            }
            return true;
        }
        return false;
    }

    this._hide = function() {
        if (element = document.getElementById(this.name)) {
            element.style.visibility = 'hidden';
            element.innerHTML = '';
            this._updateStatus('closed');
            return true;
        }
        return false;
    }

    this._updateStatus = function(status) {
        if (this.status != status) {
            this.status = status;
        }
    }

}
