﻿

/*

Diese Klasse stellt die Funktionen für den Kalender zur Verfügung.

Members: 
this.currdate: Das aktuelle Datum.

*/
var resCalendar = new Class({
    Implements: Options,
    options: {
        id: null,
        month: null,                // der aktuell ausgewählte monat
        year: null,                 // das aktuell ausgewählte jahr
        eventlist: null,            // liste mit allen veranstaltungen
        datediv: null,              // das html-element welches mit dem aktuellen ausgewählten Tag gefüllt wird (zB.: DO, 22. Januar 2009)
        lastdate: null,             // das zuletzt ausgewählte datum
        lastdatecss: null,
        closed: false,
        elemdaystorage: null

    },
    initialize: function(options) {
        this.setOptions(options);
        this.currdate = new Date();

    },

    /* ladet alle events des aktuellen monat und jahres */
    loadEvents: function() {

        var jsonRequest = new Request.JSON({ url: "../pluginReservation/ajax.aspx?action=getEventsByMonth&year=" + this.options.year + "&month=" + this.options.month + "&x=" + new Date().getMilliseconds(), onComplete: function(events) {

            // alert(events);
        }
        }).get();
    },

    loadDate: function(date) {

        this.currdate = date;

        if (this.options.elemdaystorage != null) {
            this.options.elemdaystorage.value = this.currdate.getDate().toString(); 
        }

        plan.options.events = loader.getEventsByDay(date);
        plan.options.rooms = loader.options.rooms;
        plan.currdate = this.currdate;
        plan.positionizeEvents();


        // this.options.datediv.innerHTML = date.toLocaleString().substring(0, date.toLocaleString().length - 9);
        this.options.datediv.innerHTML = getWeekname(date.getDay()) + ", " + preZero(date.getDate()) + ". " + getMonthname(date.getMonth()) + " " + date.getFullYear();

        this.deselectDate(this.options.lastdate);
        this.selectDate(date);

        this.options.lastdate = new Date(date.getFullYear(), date.getMonth(), date.getDate());

        //loader.loadExternalEvents(date);
        loader.loadPublicEvents(date);
    },

    nextDay: function() {

        if (this.options.closed == true)
            return;

        this.currdate.setDate(this.currdate.getDate() + 1);

        if (this.currdate.getMonth() != this.options.month) {
            this.options.closed = true;
            location.href = "reservations.aspx?d=" + this.currdate.getDate() + "." + (this.currdate.getMonth() + 1) + "." + this.currdate.getFullYear();
        }

        this.loadDate(this.currdate);
    },

    prevDay: function() {

        if (this.options.closed == true)
            return;

        this.currdate.setDate(this.currdate.getDate() - 1);

        if (this.currdate.getMonth() != this.options.month) {
            this.options.closed = true;
            location.href = "reservations.aspx?d=" + this.currdate.getDate() + "." + (this.currdate.getMonth() + 1) + "." + this.currdate.getFullYear() + "&roomid=" + this.qs["roomid"];
        }

        this.loadDate(this.currdate)
    },

    selectDate: function(date) {

        var div = $("cal_day" + date.getDate() + "_" + (date.getMonth() + 1));

        this.options.lastdatecss = div.className;

        if (div != null)
            div.className = "day_today";



        if (date.getDay() > 0)
            $('weekdaydiv').childNodes[date.getDay() - 1].className = "weekday_sel";
        else
            $('weekdaydiv').childNodes[6].className = "weekday_sel";
    },

    deselectDate: function(date) {

        var div = $("cal_day" + date.getDate() + "_" + (date.getMonth() + 1));

        if (div != null) {
            if (this.options.lastdatecss != null)
                div.className = this.options.lastdatecss;
            else
                div.className = "day_normal";

            this.options.lastdatecss = null;
        }

        if (date.getDay() > 0)
            $('weekdaydiv').childNodes[date.getDay() - 1].className = "weekday";
        else
            $('weekdaydiv').childNodes[6].className = "weekday";

    },

    daysInMonth: function(month, year) {
        var m = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        if (month != 2) return m[month - 1];
        if (year % 4 != 0) return m[1];
        if (year % 100 == 0 && year % 400 != 0) return m[1];
        return m[1] + 1;
    }




});
