
function tiCal(id)
{
  this.id=id;
  this.monthNames=Array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");
  this.dayNames=Array("Montag","Dienstag","Mittwoch","Donnerstag","Freitag","Samstag","Sonntag");
  this.dayShortNames=Array("Mo","Di","Mi","Do","Fr","Sa","So");
  tiCal.prototype.setMonth=setMonth;
  tiCal.prototype.setYear=setYear;
  tiCal.prototype.render=render;
  tiCal.prototype.nextMonth=nextMonth;
  tiCal.prototype.prevMonth=prevMonth;
  tiCal.clicked=clicked;
  
  //highlighting
  this.markedDates={};
  tiCal.prototype.setMarked=setMarked;
  tiCal.prototype.setUnmarked=setUnmarked;
  
  //init to today
  var now=new Date();
  this.setYear(now.getFullYear());
  this.setMonth(now.getMonth());
  
  //create a referer from the div to tiCal object
  $(id).tiCal=this;
  
  function zero(i)
  {
    return i >9 ? i : '0' + i;
  }

  function clicked()
  {
    alert('Klick');
  }

  function nextMonth()
  {
    if (this.m==11) { this.y=this.y+1 }
    this.m=(this.m+1)%12;
    this.render();
  }

	function prevMonth()
	{
	  if (this.m==0) { this.y=this.y-1 }
	  this.m=(this.m-1+12)%12;
	  this.render();
	}
	
	function setMonth(m)
	{
	  this.m=m;
	}
	
	function setYear(y)
	{
	  this.y=y;
	}
	
  function setMarked(d)
  {
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    this.markedDates[d.getTime()]=d;
    this.render();
  }
  
  function setUnmarked(d)
  {
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    delete this.markedDates[d.getTime()];
    this.render();
  }
  
	function render()
	{
	  m=this.m;
	  y=this.y;
	  if ((m>11) || (m<0))
	  {
	    y=parseInt(y+(m/12));
	    m=m%12;
	    m=(m<0)? m+12 : m;
	  }
	  //alert(y + ' ' + m);
	  
	  var currentDate=new Date();
	  currentDate=new Date(currentDate.getFullYear(), currentDate.getMonth(), currentDate.getDate());
	  
	  var basicHTML='<table><thead><tr>';
	  if ((currentDate.getMonth()==m) &&(currentDate.getFullYear()==y))
	  {
	    basicHTML=basicHTML + '<th>&nbsp;</th>';
	  } else {
	    basicHTML=basicHTML + '<th class="calNav" onClick="$(\'' + this.id + '\').tiCal.prevMonth()">&lt;</th>';
	  }
	  basicHTML=basicHTML + '<th colspan="6">' + this.monthNames[m] + ' ' + y + '</th><th class="calNav" onClick="$(\'' + this.id + '\').tiCal.nextMonth()">&gt;</th></tr><tr><th>&nbsp;</th>';
	  for (var i=0;i<7;i++)
	  {
	    basicHTML=basicHTML + '<th>' + this.dayShortNames[i] + '</th>';
	  }
	  basicHTML=basicHTML + '</tr></thead>';
	  $(this.id).innerHTML=basicHTML + '<tbody id="calTable"></tbody></table>';
	  
	  //now create the inner table
	  //create the first day of month
	  var runDate=new Date(y,m,1);
	  //calc the Monday before, even in month before
	  runDate.setDate(1-(runDate.getDay()+6)%7);
	  var startDate=new Date(runDate);
	  startDate.setHours(0);
	  startDate.setMinutes(0);
	  
	  //create last day of month;
	  var endDate=new Date(y,m+1,1);
	  endDate.setDate(0);
	  //calc the Sunday after, even in month after
	  endDate.setDate(endDate.getDate()+(7-endDate.getDay())%7);

	  //TEST
	  //6 week calendar
	  //endDate.setDate((endDate.getDate()+(7-endDate.getDay())%7)+21);
	  
	  //while(!(twoMonthPassed) || (runDate.getDay()!=1))
	  while(runDate<=endDate)
	  {
	    //on Monday
	    if (runDate.getDay()==1)
	    {
	      var tr=new Element('tr');
	      var td=new Element('td');
	      td.innerHTML=kw(runDate);
	      td.addClassName('calweek');
	      tr.appendChild(td);
	    }
	    var td=new Element('td');
	    Element.extend(td);
	    td.innerHTML=runDate.getDate();
	    
	    //var dayId=runDate.getFullYear() + zero(runDate.getMonth()) + zero(runDate.getDate());
	    //var tdId="day" + dayId;
	    //td.id=tdId;
	    td.id='day' + runDate.getTime();
	    td.date=new Date(runDate);
	    if (runDate.getTime()<currentDate.getTime())
	    {
	      td.addClassName('past');
	    } else {
	      if (runDate.getTime()==currentDate.getTime()) {
		      td.addClassName('today');
	      }
	      
	      Event.observe(td,'click',optList.checkClick.bind(optList));
	      
		    //mark weekend
		    if ((runDate.getDay()==0) || (runDate.getDay()==6))
		    {
		      td.addClassName('weekend');
		    }
		    
		    if (runDate.getMonth()!=m)
		    {
		      td.addClassName('otherMonth');
		    }
	    }
	    
	    
	    tr.appendChild(td);
	    
	    //on Sunday
	    if (runDate.getDay()==0)
	    {
	      $('calTable').appendChild(tr);
	    }
	    
	    //inc
	    runDate.setDate(runDate.getDate()+1);
	  }
	  
	  
	  //add marked days
	  //for (var i=0;i<markedDates.length;i++)
	  for (var md in this.markedDates)
	  {
	    //alert(md);
      md=new Date(parseInt(md));
      if ((startDate<=md) && (md<=endDate))
      {
        //alert(md + ' anmalen');
        var selDate=new Date(md);
        selDate.setHours(0);
        selDate.setMinutes(0);
        selDate.setSeconds(0);
        var td=$('day' + selDate.getTime()).addClassName('daySelected');
      }
    }
  }
  
  function kw(Datum)
  {
    DoDat=donnerstag(Datum);
    kwjahr=DoDat.getFullYear();
    DoKW1=donnerstag(new Date(kwjahr,0,4)); // Anm. 2
    return Math.floor(1.5+(DoDat.getTime()-DoKW1.getTime())/86400000/7) // Anm. 3, 4
  }

  function donnerstag(datum) { // Anm. 5
    var Do=new Date();
    Do.setTime(datum.getTime() + (3-((datum.getDay()+6) % 7)) * 86400000); // Anm. 3
    return Do;
  }
  
  
}
