/*
 simple-calendar.js
 (c) 2008  C. Jasper Spaans <j@jasper.es>
 All rights reserved, contact me if you want to use this.
*/

var global = this;
 if (!global.es) {
     global.es = new function() {
     };
 };
 if (!global.es.jasper) {
     global.es.jasper = new function() {
     };
 };
 global.es.jasper.simpleCalendar = new function() {
 };

 var SC = global.es.jasper.simpleCalendar;
 SC.monthnames = [ "January", "February", "March", "April",
                   "May", "June", "July", "August",
                   "September", "October", "November", "December" ];

 SC.init = function(class_) {
     if (!class_) {
         class_ = "es-jasper-simpleCalendar";
     };
     SC.calendars = new Array();
     var calendarElements = document.getElementsByClassName(class_);
     for (var i=0; i < calendarElements.length; ++i) {
         SC.calendars.push(new SC.calendar(calendarElements[i]));
     };
 };

 SC.addcal = function(node) {
     SC.calendars.push(new SC.calendar(node));
 };

 SC.calendar = function(node) {
     this.node = node;
     this.year = new Date().getFullYear();
     this.month = new Date().getMonth() + 1;
     this.baseurl = "";
     for (var i=0; i < this.node.attributes.length; ++i) {
         var attr = this.node.attributes[i];
         if (attr.nodeType != 2) {
             continue;
         };
         if (attr.localName == "year") {
             this.year = parseInt(attr.nodeValue);
         } else if (attr.localName == "month") {
             this.month = parseInt(attr.nodeValue);
         } else if (attr.localName == "baseurl") {
             this.baseurl = attr.nodeValue;
         };
     };
     this.render();
 };

 SC.calendar.prototype.render = function() {
     /* render a calendar node! */
     var d1 = new Date();
     d1.setUTCHours(12);
     d1.setUTCFullYear(this.year);
     d1.setUTCMonth(this.month-1);
     d1.setUTCDate(1);

     var html =
         '<table><tr>' + // <td class="es-jasper-simpleCalendar-prev">prev</td>'
         '<td class="es-jasper-simpleCalendar-title" colspan="7">'+
         SC.monthnames[d1.getUTCMonth()]+' '+d1.getUTCFullYear()+'</td></tr>';
         // + '<td class="es-jasper-simpleCalendar-next">next</td></tr>';

     var ts = d1.getTime();
     ts = ts - (86400000 * ((d1.getDay() + 6) % 7));
     var d2 = new Date();
     d2.setTime(ts);
     // first, the fill in the previous month's data
     while (d2.getMonth()+1 != this.month) {
         if (d2.getDay() == 1) {
             html = html + "<tr>";
         };
         html = html + '<td class="es-jasper-simpleCalendar-prev">' +
                       d2.getDate() + "</td>";
         ts = ts + 86400000;
         d2.setTime(ts);
     };
     // then, do it for this month
     while (d2.getMonth()+1 == this.month) {
         if (d2.getDay() == 1) {
             html = html + "<tr>";
         };
         var url=this.baseurl+this.year+"/"+this.month+"/"+d2.getDate()+"/";
         html = html + '<td><a href="'+url+'">' + d2.getDate() + '</a></td>';
         if (d2.getDay() == 0) {
             html = html + "</tr>";
         };
         ts = ts + 86400000;
         d2.setTime(ts);
     };
     // finally, the last days of todays month
     while (d2.getDay() != 1) {
         html = html + '<td class="es-jasper-simpleCalendar-next">' +
                       d2.getDate() + "</td>";
         if (d2.getDay() == 0) {
             html = html + "</tr>";
         };
         ts = ts + 86400000;
         d2.setTime(ts);
     };

     html = html + "</table>";
     this.node.innerHTML = html;
     self = this;
     var prev = this.node.getElementsByClassName("es-jasper-simpleCalendar-prev");
     for (var i = 0; i < prev.length; ++i) {
         prev[i].addEventListener('click', function() { self.prev(); }, false);
     };
     var next = this.node.getElementsByClassName("es-jasper-simpleCalendar-next");
     for (var i = 0; i < next.length; ++i) {
         next[i].addEventListener('click', function() { self.next(); }, false);
     };
 };

 SC.calendar.prototype.prev = function() {
     this.month = this.month - 1;
     if (this.month < 1) {
         this.year = this.year - 1;
         this.month = 12;
     };
     this.render();
 };

 SC.calendar.prototype.next = function() {
     this.month = this.month + 1;
     if (this.month > 12) {
         this.year = this.year + 1;
         this.month = 1;
     };
     this.render();
 };