/*
CALENDAR OBJECT for JS

Creates a nice little calendar for use in JS.
Also contains appendices to Date object.
*/

Date.prototype.getWeek = function (dowOffset) {
    /*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */
    dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
    var newYear = new Date(this.getFullYear(),0,1);
    var day = newYear.getDay() - dowOffset; //the day of week the year begins on
    day = (day >= 0 ? day : day + 7);
    var daynum = Math.floor((this.getTime() - newYear.getTime() -
        (this.getTimezoneOffset()-newYear.getTimezoneOffset())*60000)/86400000) + 1;
    var weeknum;
    //if the year starts before the middle of a week
    if (day < 4) {
        weeknum = Math.floor((daynum+day-1)/7) + 1;
        if (weeknum > 52) {
            nYear = new Date(this.getFullYear() + 1,0,1);
            nday = nYear.getDay() - dowOffset;
            nday = nday >= 0 ? nday : nday + 7;
            /*if the next year starts before the middle of
            the week, it is week #1 of that year*/
            weeknum = nday < 4 ? 1 : 53;
        }
    } else {
        weeknum = Math.floor((daynum+day-1)/7);
    }
    return weeknum;
};

Date.prototype.getDays = function() {
    var days = new Date(this.getYear(), this.getMonth(), 32).getDate();
    days = 32-days;
    return days;
}

var Calendar = function(month) {
    // configuration
    var dn = new Array();
    dn[0] = "Mandag"; dn[1] = "Tirsdag"; dn[2] = "Onsdag"; dn[3] = "Torsdag";
    dn[4] = "Fredag"; dn[5] = "Lørdag"; dn[6] = "Søndag"; 
    var mn = new Array();
    mn[0] = "Januar"; mn[1] = "Februar"; mn[2] = "Marts";
    mn[3] = "April"; mn[4] = "Maj"; mn[5] = "Juni";
    mn[6] = "Juli"; mn[7] = "August"; mn[8] = "September";
    mn[9] = "Oktober"; mn[10] = "November"; mn[11] = "December";
    this.day_names = dn;
    this.month_names = mn;
    this.month = null;
    this.year = null;
    this.days_in_month = null;
    this.date_start = null;
    this.date_end = null;
    this.curmonth = null;
    this.date = null;
    this.icon_path = "/lib/icons/default/";
    
    // DOM node
    this.node = document.createElement("table");
    this.node.setAttribute("class", "calendar");
    
    // initialization, if arg passed
    if (month) {
        this.set_month(month);
    } else {
        this.set_month(new Date());
    }
}

Calendar.prototype.getNode = function() {
    var node = document.createElement('div');
    node.appendChild(this.node);
    return node; // disables rest, box handled by PHP
    try {
        var box = new Box(this.month_names[this.month]+' '+this.year, this.node, null, "calendar.png");
        return box.getNode();
    } catch (e) {
        return this.node;
    }
}

Calendar.prototype.set_month = function(month) {
    if (month instanceof Date) {
        this.date = month;
        this.date.setUTCDate(1);
    } else {
        var sep = month.indexOf('-');
        var year = month.substring(0, sep);
        var month = month.substring(sep+1);
        this.date = new Date();
        this.date.setMonth(month-1);
        this.date.setYear(year);
        this.date.setDate(1);
    }
    this.year = this.date.getUTCFullYear();
    this.month = this.date.getUTCMonth();
    this.date_start = this.date.getDay() == 0 ? 7 : this.date.getDay();
    this.days_in_month = this.date.getDays();
    this.render();
}

Calendar.prototype.navigation = function() {
    // calculations...
    var month = this.month+1;
    var prevmonth = month-1 < 1 ? 12 : month-1;
    var nextmonth = month+1 > 12 ? 1 : month+1;
    prevmonth = prevmonth < 10 ? '0'+prevmonth : prevmonth;
    nextmonth = nextmonth < 10 ? '0'+nextmonth : nextmonth;
    var prevyear = month-1 < 1 ? this.year-1 : this.year;
    var nextyear = month+1 > 12 ? this.year+1 : this.year;
    this.curmonth = this.month_names[this.month]+" "+this.year;
    
    // rendering navigation nodes: BACK
    var navtr = this.node.appendChild(document.createElement("tr"));
    navtr.setAttribute("class", "browse");
    var td = navtr.appendChild(document.createElement("td"));
    //td.setAttribute("colspan", 3);
    td.setAttribute('style', 'padding: 3px;');
    var prevlink = td.appendChild(document.createElement("a"));
    var img = prevlink.appendChild(document.createElement("img"));
    img.setAttribute("src", this.icon_path+"date_previous.png");
    img.setAttribute("class", "icon16");
    //prevlink.appendChild(document.createTextNode("Forrige"));
    prevlink.setAttribute("onclick", "calendar('"+prevyear+"-"+prevmonth+"');");
    prevlink.setAttribute("title", prevyear+'-'+prevmonth);
    prevlink.setAttribute("href", "javascript:;");
    
    var td = navtr.appendChild(document.createElement("td"));
    td.setAttribute("colspan", 5);
    td.setAttribute('style', 'text-align: center; font-weight: bold; padding: 5px;');
    td.innerHTML = this.month_names[this.month]+" "+this.year;
    
    // rendering navigation nodes: FORWARD
    var td = navtr.appendChild(document.createElement("td"));
    //td.setAttribute("colspan", 3);
    td.setAttribute("class", "rightalign");
    td.setAttribute('style', 'padding: 3px;');
    var nextlink = td.appendChild(document.createElement("a"));
    //nextlink.appendChild(document.createTextNode("Næste "));
    nextlink.setAttribute("onclick", "calendar('"+nextyear+"-"+nextmonth+"');");
    nextlink.setAttribute("title", nextyear+'-'+nextmonth);
    nextlink.setAttribute("href", "javascript:;");
    var img = nextlink.appendChild(document.createElement("img"));
    img.setAttribute("src", this.icon_path+"date_next.png");
    img.setAttribute("class", "icon16");
}

Calendar.prototype.header = function() {
    var tr = this.node.appendChild(document.createElement("tr"));
    //var empty = tr.appendChild(document.createElement("td"));
    //empty.appendChild(document.createTextNode("Uge"));
    //empty.setAttribute("class", "bold");
    //empty.setAttribute('style', 'text-align: center; font-weight: bold; padding: 3px;');
    
    for (num in this.day_names) {
        var short_day = this.day_names[num];
        var short_day = short_day.substring(0,1);
        var td = tr.appendChild(document.createElement("td"));
        td.setAttribute("class", "centeralign bold");
        td.setAttribute('style', 'text-align: center; font-weight: bold; padding: 3px; background-color: #e2e2db;');
        td.appendChild(document.createTextNode(short_day));
    }
}

Calendar.prototype.render = function() {
    // rendering navigation: LINKS
    this.navigation();
    
    // rendering navigation nodes: HEADERS
    this.header();
    
    // rendering calendar: MAIN BODY
    var month = this.month+1 < 10 ? '0'+(this.month+1) : this.month+1;
    var day = 1;
    var row = day;
    var week = this.date.getWeek();
    var year = this.date.getFullYear();
    var today = new Date();
    while (day <= this.days_in_month) {
        var tr = document.createElement("tr");
        this.node.appendChild(tr);
        if (row%2 == 0) tr.setAttribute("class", "a");
        for (i=1; i<=7; i++) {
            var td = tr.appendChild(document.createElement("td"));
            td.setAttribute("class", "centeralign");
            if ((i == this.date_start || day > 1) && day <= this.days_in_month) {
                var daybreak = day < 10 ? '0'+day : day;
                var day_href = this.year+'-'+month+'-'+daybreak;
                td.appendChild(document.createTextNode(day));
                td.setAttribute('onclick', "bigcalendar("+year+","+(week)+", "+(i)+");");
                td.setAttribute('style', 'cursor: hand; cursor: pointer; padding: 4px; background-color: white;');
                td.setAttribute('onmouseover', "this.style.backgroundColor = '#d9e06e';");
                td.setAttribute('onmouseout', "this.style.backgroundColor = 'white';");
                td.setAttribute('title', "Uge "+(week)+", dag "+(i));
                if (today.getUTCDate() == day && this.date.getMonth() == today.getMonth()) {
                    td.setAttribute('class', 'centeralign today');
                }
                day++;
            } else {
                td.appendChild(document.createTextNode(" "));
            }
        }
        
        week++;
        row++;
    }
}














