// This is example code showing how to use the Calendar object.

function Location(a, b)
{
    this.x = a;
    this.y = b;
    if (arguments.length == 1)
    {
        if (a.offsetTop && a.offsetLeft)
        {
            this.x = function(el){ return el && el.offsetLeft + arguments.callee(el.offsetParent) || 0; }(a);
            this.y = function(el){ return el && el.offsetTop + arguments.callee(el.offsetParent) || 0; }(a);
        }
        else if (a.x || a.y)
        {
            this.x = a.x;
            this.y = a.y;
        }
    }
}
Location.prototype.set = function(x, y)
{
    this.x = x;
    this.y = y;
    return this;
};
Location.prototype.move = function(x, y)
{
    this.x += x;
    this.y += y;
    return this;
};
Location.prototype.positionElement = function(el, posType)
{
    if (el && el.style)
    {
        el.style.position = posType || "absolute";
        el.style.left = this.x + "px";
        el.style.top = this.y + "px";
    }
    return this;
};

addEvent(window, "load", function()
{
    var dateStr = function(date)
    {
        return date.getDate() + " " + Calendar.monthNames[date.getMonth()] + " " + date.getFullYear();
    }
    var now = new Date();
    var a = document.createElement("a");
    a.href = "#";
    a.appendChild(document.createTextNode(dateStr(new Date())));
    var p = document.createElement("p");
    p.appendChild(document.createTextNode("Change the date: "));
    p.appendChild(a);
    document.getElementsByTagName("body")[0].appendChild(p);
    var calendarObject = new Calendar();
    var calendar = calendarObject.getElement();
    calendar.setAttribute("id", "calendar");
    calendarObject.onSelect = function(date)
    {
        a.replaceChild(document.createTextNode(dateStr(date)), a.firstChild);
        calendar.style.display = "none";
    };
    a.parentNode.appendChild(calendar);
    calendar.style.display = "none";
    addEvent(a, "click", function(e)
    {
        if (calendar.style.display == "none")
        {
            calendarObject.gotoSelection();
            calendar.style.display = "";
            (new Location(a)).move(0, a.offsetHeight).positionElement(calendar);
        }
        else
            calendar.style.display = "none";
        cancelEvent(e);
        return false;
    });
});