/****************************************
 * This bit defines the Termcard object *
 ****************************************/
TermcardObject.prototype.InitMappings = _proto_InitMappings;
TermcardObject.prototype.WriteInDays = _proto_WriteInDays;
TermcardObject.prototype.UpdateMappings = _proto_UpdateMappings;
TermcardObject.prototype.Update = _proto_Update;
TermcardObject.prototype.PopulateEventCells = _proto_PopulateEventCells;
TermcardObject.prototype.GetMonthLength = _proto_GetMonthLength;
TermcardObject.prototype.IncMonth = _proto_IncMonth;
TermcardObject.prototype.DecMonth = _proto_DecMonth;
TermcardObject.prototype.SendAJAX = _proto_SendAJAX;

function TermcardObject()
{
        this.months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
        this.month_lengths = new Array('31', '28', '31', '30', '31', '30', '31', '31', '30', '31', '30', '31');
        this.events = new Array(); // will be populated via xmlhttp.js
        
        this.normal_day_class = 'termcard_day';
        this.other_month_class = 'termcard_day_other_month';
        this.edge_on_class = 'termcard_edge_on';
        this.edge_off_class = 'termcard_edge_off';
        
        var D = new Date();
        this.curYear  = D.getFullYear();
        this.curMonth = D.getMonth();
        this.selYear = this.curYear;
        this.selMonth = this.curMonth;
        
        this.cellMappings = new Array();
        this.prevMonthCells = new Array(); // list of all visible cells in prev month
        this.nextMonthCells = new Array(); // list of all visible cells in next month
        this.thisMonthCells = new Array(); // list of all visible cells in this month
        
        // initialise mappings
        this.InitMappings();
}

function _proto_Update()
{
        this.UpdateMappings();
        this.WriteInDays(); // write in this month
        this.PopulateEventCells(); // write in events
}

function _proto_SendAJAX()
{
        XMLHttp_Send('/termcard_events.php?month=' + this.selMonth + '&year=' + this.selYear, 'XMLHttp_CallBack()');
}

function _proto_InitMappings()
{
        for(var i = 1; i < 7; i++)
        {
                for (var j = 1; j < 7; j++)
                {
                        var cell = "r"+j+"c"+i;
                        this.cellMappings[cell] = "";
                }
        }
}

function _proto_UpdateMappings()
{
        var F = new Date();
        F.setYear(this.selYear);
        F.setMonth(this.selMonth);
        F.setDate(1);
        /*var first = F.getDay()-1; // this is the first "c", that has 1
        if(first < 0){ first = 6; }*/
        var first = F.getDay();
        
        var count = 0;
        var mlength = this.GetMonthLength(this.selMonth);
        var plength = this.GetMonthLength( (this.selMonth == 0)?(11):(this.selMonth-1) ); // length of previous month
        var next = 1; // first date of the following month
        var e; // an element
        this.nextMonthCells = Array();
        this.prevMonthCells = Array();
        this.thisMonthCells = Array();
        // first: the last few days of the previous month
        for(var i = first-1; i > -1; i--)
        {
                var cell = "r1c"+i;
                this.cellMappings[cell] = plength;
                this.prevMonthCells[this.prevMonthCells.length] = cell;
                plength--;
                e = document.getElementById(cell);
                e.className = this.other_month_class;
        }
        // second: the first few days of the current month
        for(i = first; i < 7; i++)
        {
                count++;
                var cell = "r1c"+i;
                this.cellMappings[cell] = count;
                this.thisMonthCells[this.thisMonthCells.length] = cell;
                e = document.getElementById(cell);
                e.className = this.normal_day_class;
        }
        // all the rest of the days of the current month + next month
        for(i = 2; i < 7; i++)
        {
                for (var j = 0; j < 7; j++)
                {
                        cell = "r"+i+"c"+j;
                        if(count < mlength)
                        {
                                // current month
                                count++;
                                this.cellMappings[cell] = count;
                                this.thisMonthCells[this.thisMonthCells.length] = cell;
                                e = document.getElementById(cell);
                                e.className = this.normal_day_class;
                        }
                        else
                        {
                                // next month
                                this.cellMappings[cell] = next;
                                this.nextMonthCells[this.nextMonthCells.length] = cell;
                                next++;
                                e = document.getElementById(cell);
                                e.className = this.other_month_class;
                        }
                }
        }
}

function _proto_PopulateEventCells()
{
        // the array 'events' is populated by the XMLHttp call.
        // it contains dates in this month on which there is an event.
        // the format is:
        // Termcard.events[0] = new Array('15', '114');
        // these elements are the day of the month, and the id of the event in the database
        // use the database-format date for the link, e.g. <a href="/events/?date=2006-10-15">15</a>
        
        // example array:
        //this.events[0] = new Array('15', '114');
        for(var i = 0; i < this.events.length; i++)
        {
                var e = document.getElementById(this.thisMonthCells[this.events[i][0]-1]);
                var d = this.selYear + '-';
                if(this.selMonth < 9)
                {
                        d += '0';
                }
                d += (this.selMonth+1) + '-';
                if(this.events[i][0].length < 2)
                {
                        d += '0';
                }
                d += this.events[i][0];
                e.innerHTML = '<a href="/events/?date=' + d + '">' + this.events[i][0] + '</a>';
        }
}

function _proto_WriteInDays()
{
        // Called after the table is updated by changing the month / year
        var m = document.getElementById('termcard_date');
        m.innerHTML = '<a href="/listevents/?month=">' + this.months[this.selMonth] + ' ' + this.selYear + '</a>';
        // populate cellMappings
        for(i = 0; i < 7; i++)
        {
                for (var j = 1; j < 7; j++)
                {
                        var cell = "r"+j+"c"+i;
                        var e = document.getElementById(cell);
                        e.innerHTML = this.cellMappings[cell];
                }
        }
}

function _proto_GetMonthLength(month)
{
        if(month == 1 && this.selYear%4 == 0)
        {
                return 29;
        }
        return this.month_lengths[month];
}

function _proto_IncMonth()
{
        this.selMonth = (this.selMonth + 1)%12;
        if(this.selMonth == 0)
        {
                this.selYear++;
        }
        this.events = new Array();
        this.SendAJAX();
        this.Update();
}

function _proto_DecMonth()
{
        (this.selMonth == 0)?(this.selMonth = 11):(this.selMonth--)
        if(this.selMonth == 11)
        {
                this.selYear--;
        }
        this.events = new Array();
        this.SendAJAX();
        this.Update();
}

/********************************************
 * This bit actually starts up the Termcard *
 ********************************************/
window.onload=function(){ SetupTermcard(); }
var Termcard = new Object();

function SetupTermcard()
{
        Termcard = new TermcardObject("Termcard");
        Termcard.SendAJAX();
}