﻿/*
   Calendar Control
   version 1.0
   (C) Dexter, 2008
   www.dexter.hu
   All rights reserved.
   Requires: prototypejs 1.6+
*/

var DCalendar = Class.create({
    
    initialize: function(parent, current, data, onclick)
    {
        this.parent = $(parent);
        this.current = current ? current : new Date();
        this.month = this.getYM();
        this.data = data;
        this.days = ['H','K','Sze','Cs','P','Szo','V'];
        this.months = ['Január','Február','Március','Április','Május','Június','Július','Augusztus','Szeptember','Október','November','December'];
        this.onclick = onclick;
    },
    
    getDateStr: function(d)
    {
        if (typeof(d) == 'undefined')
            d = this.current;
        return d.getFullYear() + '. ' + this.months[d.getMonth()];
    },
    
    getDateStr2: function(d)
    {
        var m = d.getMonth() + 1;
        if (m < 10)
            m = '0' + m;
        var da = d.getDate();
        if (da < 10)
            da = '0' + da;
        return d.getFullYear() + '-' + m + '-' + da;
    },
    
    getYM: function(d)
    {
        if (typeof(d) == 'undefined')
            d = this.current;
        return new Date(d.getFullYear(), d.getMonth(), 1);
    },
    
    getDay: function(d)
    {
        if (typeof(d) == 'undefined')
            d = this.current;
	    return (d.getDay() + 6) % 7; // H:0, K:1, ..
    },
    
    move: function(offset)
    {
        this.month = new Date(this.month.getFullYear(), this.month.getMonth() + offset, 1);
        this.show();
    },
    
    show: function()
    {
        this.parent.innerHTML = '';
        
        var ohead = document.createElement('table');
        ohead.className = 'dcalendar_head';
        ohead.cellSpacing = 0;
        ohead.cellPadding = 0;
        var orow = ohead.insertRow(0);

        var ocell = orow.insertCell(0);
        ocell.align = 'left';       
        var tmp = document.createElement('a');
        tmp.className = 'button';
        ocell.appendChild(tmp);
        var diz = this;
        tmp.onclick = function() { diz.move(-1); }
        tmp.innerHTML = '&lt;';
        
        var ocell = orow.insertCell(1);
        ocell.align = 'center';
        var tmp = document.createElement('span');
        tmp.innerHTML = this.getDateStr(this.month);
        ocell.appendChild(tmp);
        
        var ocell = orow.insertCell(2);
        ocell.align = 'right';
        var tmp = document.createElement('a');
        tmp.className = 'button';
        ocell.appendChild(tmp);
        tmp.onclick = function() { diz.move(1); }
        tmp.innerHTML = '&gt;';
        
        this.parent.appendChild(ohead);
        
        var otable = document.createElement('table');
        otable.className = 'dcalendar_data';
        otable.cellSpacing = 0;
        otable.cellPadding = 0;
        var orhead = otable.createTHead().insertRow(0);
        for (var i = 0; i < 7; i++)
        {
            var odhead = document.createElement('th'); orhead.appendChild(odhead); //orhead.insertCell(i);
            odhead.className = 'dcalendar_day' + (i + 1);
            odhead.innerHTML = this.days[i];
        }
        var day = -this.getDay(this.month) + 1;
        for (var j = 0; j < 6; j++)
        {
            var orow = otable.insertRow(j + 1);
            for (var i = 0; i < 7; i++)
            {
                var d = new Date(this.month);
                d.setDate(day);
                day++;
                var tmp = orow.insertCell(i);
                tmp.innerHTML =  d.getDate();
                
                var key = this.getDateStr2(d);
                var list = this.data[key];
                if (typeof(list) != 'undefined')
                {
                    tmp.className = 'event';
                    if (this.onclick)
                    {
                        (function() {
                            var k = key;
                            Event.observe(tmp, 'click', function() { diz.onclick(k); });
                        })();
                    }
                }
                if (d.getMonth() != this.month.getMonth())
                    tmp.className = 'other';
            }
        }
        this.parent.appendChild(otable);
    }

});

