/*
 Datei: kalender.js
 Datum: 28.12.2004
 Version: 0.3

 jstruebig@web.de

 Benötigt die Module:

*/

/////////////////////////////////////////////////
// Globale Einstellungen:

var background    = '#fff';   // Farbe für den Hintergrund
var color         = '#000';   // Farbe für den vordergrund
var hover_color   = '#6ff';   // Farbe beim Hovern über die Tage
var markSunday    = '#f99';   // Sonntage färben
var markFeiertag  = '#f66';   // Feiertage färben
var markSaturday  = '#fcc';   // Samstage färben
var close         = true;     // Schliessen nach Klick
var c_grau        = '#D4D0C8';
var td_border     = '1px solid #000';
var font_size     = '10px';

/////////////////////////////////////////////////
var Kalender = function (w)
/////////////////////////////////////////////////
{
    this.ok = !!document.createTextNode;
    if(!this.ok) return alert('Der Browser ist leider zu alt');

    this.func   = null; // Funktion die bei einem Klick aufgerufen werden soll
    this.win    = w || window;
    this.heute  = new Date();
    this.id     = 'kalender' + Math.random();
    this.obj    = null;
    this.rows   = new Array();
    this.width  = 0;
    this.height = 0;
    this.isOpen = false;
    this.year   = this.heute.getFullYear();
    this.month  = this.heute.getMonth();
    var k = this;
    this.win.onload = function() { k.ini(); };
    return this;
}
/////////////////////////////////////////////////
Kalender.prototype.setFunc = function(func)
/////////////////////////////////////////////////
{
    if(typeof func != 'function') return alert('Keine Funktion!');
    this.func = func;
    return true;
}

/////////////////////////////////////////////////
Kalender.prototype.hide = function()
/////////////////////////////////////////////////
{
    this.isOpen = false;
    this.obj.style('display', 'none');
}
/////////////////////////////////////////////////
Kalender.prototype.next = function(e)
/////////////////////////////////////////////////
{
    this.month++;
    if(this.month > 11)
    {
         this.month = 0;
         this.year++;
    }
    this.showMonth();
}
/////////////////////////////////////////////////
Kalender.prototype.prev = function(e)
/////////////////////////////////////////////////
{
    this.month--;
    if(this.month < 0)
    {
         this.month = 11;
         this.year--;
    }
    this.showMonth();
}
/////////////////////////////////////////////////
Kalender.prototype.click = function(e)
/////////////////////////////////////////////////
{
    var d = new Date(this.parent.year, this.parent.month, this.firstChild.data, 0, 0, 0);
    if(this.parent.func) this.parent.func(d, this.parent.target);
}
/////////////////////////////////////////////////
Kalender.prototype.ini = function()
/////////////////////////////////////////////////
{
    if(!this.ok) return;
    var tmp,  row, t ;
    // Das Kalender DIV anlegen:
    addLayer(this.id);
    this.obj = getElementById(this.id);
    this.obj.style('position', 'absolute');

    // die Tabelle
    var tbl = createEl(this.win, 'table');
    var tbody = createEl(this.win, 'tbody');
    tbl.appendChild(tbody);
    tbl.style.margin = 0;
    tbl.style.borderCollapse = 'separate';

    row = createEl(this.win, 'tr');

    // Button Links
    this.btn(row, '\253', 'prev');
    // Titel
    this.btn_titel(row);
    // rechts
    this.btn(row, '\273', 'next');


    tbody.appendChild(row);

    // Die Header Zeile
    row = createEl(this.win, 'tr');
    var rTag = 0;
    var td;
    for(t = 0; t < 7; t++) // 7 Tage
    {
         rTag++;
         if(rTag == LOCALE.abday.length) rTag = 0;
         var tag = LOCALE.abday[rTag].substring(0, 1).toUpperCase() + LOCALE.abday[rTag].substring(1,2);
         td = createEl(this.win, 'th');
         var txt = this.win.document.createTextNode(tag);
         this.setTblStyle(td);
         td.style.backgroundColor = rTag == 6 ? markSaturday : rTag == 0 ? markSunday : c_grau;

         td.appendChild(txt);
         row.appendChild(td);
    }
    tbody.appendChild(row);

    for(var r = 0; r < 6; r++) // max. 6 Reihen
    {
         row = createEl(this.win, 'tr');
         this.rows[r] = new Array();
         for(t = 0; t < 7; t++) // á 7 Tage
         {
              td = createEl(this.win, 'td');
              txt = this.win.document.createTextNode('\240');
              td.appendChild(txt);
              this.setTblStyle(td);
              td.style.backgroundColor = t == 5 ? markSaturday : t == 6 ? markSunday : background;
              td.parent = this;

              row.appendChild(td);
              this.rows[r][t] = td;
         }
         tbody.appendChild(row);
    }
    // rechts
    row = createEl(this.win, 'tr');
    this.btn_close(row);
    tbody.appendChild(row);

    this.obj.obj.appendChild(tbl);


    this.obj.style('backgroundColor', '#fff');
    this.obj.style('border', '1px solid #000');

    this.show();
    this.width = this.obj.width();
    this.height = this.obj.height();
    this.hide();
    if(typeof this.old_onmousemove == 'undefined') this.old_onmousemove = this.win.document.onmousemove;
}
/////////////////////////////////////////////////
Kalender.prototype.setTblStyle = function(td)
/////////////////////////////////////////////////
{
    td.style.border     = td_border;

    td.style.textAlign = 'center';
    td.style.margin = '1px';
    td.style.fontSize = font_size;
    td.style.padding = '2px';
    td.style.fontFamily = 'Verdana';
    td.style.color = '#000';
}
/////////////////////////////////////////////////
Kalender.prototype.btn = function(row, text, func)
/////////////////////////////////////////////////
{
    var txt = this.win.document.createTextNode(text);
    var tmp = createEl(this.win, 'td');
    tmp.title = 'vorheriger Monat';
    tmp.parent           = this;
    tmp.onclick          = function(e) { this.parent[func](e);} ;

    tmp.onmousedown      = function(e) { this.style.paddingTop = '3px'; this.style.paddingBottom = '1px'; this.style.borderStyle = 'inset'; };
    tmp.onmouseup        = function(e) { this.style.paddingTop = '2px'; this.style.paddingBottom = '2px'; this.style.borderStyle = 'outset'; };
    tmp.onmouseover      = function(e) { this.style.backgroundColor = c_grau; this.style.cursor = 'pointer' ;} ;
    tmp.onmouseout       = function(e) { this.style.backgroundColor = ''; } ;

    tmp.style.padding = '2px';
    tmp.style.border     = '1px outset #fff';
    tmp.style.fontFamily = 'courier new, mono';
    tmp.style.fontSize = '14px';
    tmp.style.fontWeight = 'bold';
    tmp.style.textAlign = 'center';

    tmp.appendChild(txt);
    row.appendChild(tmp);
}
/////////////////////////////////////////////////
Kalender.prototype.btn_titel = function(row)
/////////////////////////////////////////////////
{
    var txt = this.win.document.createTextNode('Titel');
    var tmp = createEl(this.win, 'td');

    tmp.style.backgroundColor = c_grau;
    tmp.style['fontWeight'] = 'bold';
    tmp.style.textAlign = 'center';
    tmp.style.fontSize = '14px';
    tmp.style.fontFamily = 'Courier';
    tmp.appendChild(txt);
    tmp['colSpan'] = 5;
    row.appendChild(tmp);
    tmp.parent = this;
    tmp.onmousedown  = function(e) { this.parent.drag(e);} ;
    tmp.onmouseover      = function(e) { this.style.cursor = 'move' ;} ;
    this.titel = txt;

}
/////////////////////////////////////////////////
Kalender.prototype.btn_close = function(row)
/////////////////////////////////////////////////
{
    var txt = this.win.document.createTextNode('[Schliessen]');
    var tmp = createEl(this.win, 'td');
    tmp.style.backgroundColor = c_grau;
    tmp.style.border = td_border
    tmp.style.fontWeight = 'bold';
    tmp.style['fontFamily'] = 'courier new, mono';
    tmp.style['fontWeight'] = 'normal';
    tmp.style['backgroundColor'] = c_grau;
    tmp.style['textAlign'] = 'center';
    tmp.style.fontSize = '12px';
    tmp.appendChild(txt);
    tmp['colSpan'] = 7;
    row.appendChild(tmp);
    tmp.parent = this;
    tmp.onmouseover = this.hover;
    tmp.onmouseout = this.out;
    tmp.onclick = function () { this.onmouseout(); this.parent.hide();}
    tmp.appendChild(txt);
    row.appendChild(tmp);
}

/////////////////////////////////////////////////
Kalender.prototype.drag = function(e)
/////////////////////////////////////////////////
{
    var p = mouse_pos(e);
    this.obj.getRect();
    this.startPos = { top: this.obj.top() - p.scrollTop, left: this.obj.left() - p.scrollLeft};
    this.win.document.is_drag = this;
    this.win.document.onmousemove = function(e) { if(this.is_drag) this.is_drag.move(e); if(this.old_onmousemove ) this.old_onmousemove(e)};
    this.win.document.onmouseup = function(e) { this.is_drag = null; } ;
}
/////////////////////////////////////////////////
Kalender.prototype.move = function(e)
/////////////////////////////////////////////////
{
    var p = mouse_pos(e);
    var top  = p.scrollTop + this.startPos.top;
    var left = p.scrollLeft + this.startPos.left;

    this.obj.top( top);
    this.obj.left( left);
}

/////////////////////////////////////////////////
Kalender.prototype.hover = function()
/////////////////////////////////////////////////
{
    this.style.cursor = 'pointer';
    this.parent.oldBgClr = this.style.backgroundColor;
    this.style.backgroundColor = hover_color;
}
/////////////////////////////////////////////////
Kalender.prototype.out = function()
/////////////////////////////////////////////////
{
    this.style.backgroundColor = this.parent.oldBgClr;
}

/////////////////////////////////////////////////
Kalender.prototype.clear = function()
/////////////////////////////////////////////////
{
   for(var row = 0; row < this.rows.length; row++)
   {
         for(var i = 0; i < this.rows[row].length; i++)
         {
              this.rows[row][i].firstChild.data = '\240';
              this.rows[row][i].style.backgroundColor =
              (i % 7) == 5 ? markSaturday :
              (i % 7) == 6 ? markSunday :  background;

              this.rows[row][i].onmouseover = null;
              this.rows[row][i].onmouseout = null;
              this.rows[row][i].onclick = null;
              this.rows[row][i].style.cursor = '';
              this.rows[row][i].style.fontWeight =  'normal';
              this.rows[row][i].style.borderColor =  '#000';
         }
   }
}

/////////////////////////////////////////////////
Kalender.prototype.showMonth = function()
/////////////////////////////////////////////////
{
    this.clear();

    var today = new Date();
    var d     = new Date(this.year, this.month, 1);
    var row   = 0;
    var i     = d.getDay() - 1;

    if(i < 0) i = 6;

    d.setDate(1);

    for(var tag = 1; tag < 32; tag++)
    {
         var wd = (i % 7);
         d.setDate(tag);
         if(d.getMonth() > this.month) break;
         this.rows[row][wd].title  = tag + '.' + LOCALE.mon[this.month] + ' '+ this.year;

         this.rows[row][wd].firstChild.data = tag;
         this.rows[row][wd].onmouseover     = this.hover;
         this.rows[row][wd].onmouseout      = this.out;
         this.rows[row][wd].onclick         = this.click;

         if( tag == today.getDate() &&
             today.getMonth() == d.getMonth() &&
             today.getFullYear() == d.getFullYear()
         )
         {
          this.rows[row][wd].style.fontWeight =  'bold';
          this.rows[row][wd].style.borderColor =  '#f00';
         }

         if(isFeiertag(d))
         {
              this.rows[row][wd].style.backgroundColor = markFeiertag;
              this.rows[row][wd].title  = isFeiertag(d);
         }
         i++;
         if( !(i % 7) ) row++;
   }

   this.titel.data = LOCALE.mon[this.month] + '/' + d.getFullYear()
}
/////////////////////////////////////////////////
Kalender.prototype.show = function(e, string, obj)
/////////////////////////////////////////////////
{
    if(!this.ok || this.isOpen) return;
    this.target = obj;

    this.showMonth();
    this.obj.style('display', 'block');
    if(e)
    {
         var p = mouse_pos(e);
         var left = p.scrollLeft;
         var top = p.scrollTop;
         this.obj.top(top);
         this.obj.left(left);
    }
    this.isOpen = true;
}

/////////////////////////////////////////////////
/* Hilfsfunktion */
/////////////////////////////////////////////////
function mouse_pos(evt)
{
    if(!evt) evt = window.event;
    var pos = new Object();

    pos.left = evt.clientX;
    pos.top = evt.clientY;

    var b = getBody(window)

    if (b) {
        pos.scrollLeft= pos.left + b.scrollLeft;
        pos.scrollTop = pos.top + b.scrollTop;
    }
    else if(document.layers)
    {
        // Netscape 4.
        pos.scrollLeft = evt.pageX;
        pos.scrollTop = evt.pageY;
        pos.left = evt.pageX - window.pageXOffset;
        pos.top = evt.pageY - window.pageYOffset;

    }
    return pos;
}

function createText(w, text, style)
{
    // rechts
    var tmp = createEl(w, 'span');
    txt     = w.document.createTextNode(text);
    tmp.appendChild(txt);
    for(var a in style)
    {
       tmp.style[a] = style[a];
    }
    return tmp;
}
function createEl(w, e, style)
{
    if(!w) w = window;
    if(!e) return alert ('usage: createElement(\"element\")');
    var o = w.document.createElement(e);
    if(!o) return alert('Konnte ' + e + ' nicht erzeugen!');
    if(style)
    {
    for(var a in style)
    {
       o.style[a] = style[a];
    }
    }
    return o;
}
function addLayer(id, win)
{
    if(!win) win = window;

    if (win.document.body.appendChild)
    {
         var test = document.createElement('div');
         test.id = id;
         win.document.body.appendChild(test);
    }
    else if (document.body.insertAdjacentHTML)
    {
         win.document.body.insertAdjacentHTML("beforeEnd", '<div id="' + id + '"></div>');
    }
    else if (win.body.innerHTML) win.body.innerHTML += '<div id="' + id + '"></div>';
    else alert('Kann Objekt nicht einfügen');
}
