var CalendarJS = {
  now          : new Date(),
  eisprung     : null,
  fruchtbar1   : new Array(),
  fruchtbar2   : new Array(),
  dayname      : ["Mo","Di","Mi","Do","Fr","Sa","So"],
  monthname    : ["Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember"],
  dayspermonth : [31,28,31,30,31,30,31,31,30,31,30,31],
  tooltip      : ["",""],
  monthCell    : null,
	tableHead    : null,
	parEl        : null,
  cell_old		 : "1",
  
  init : function( id, size ) {
    this.monthCell         = document.createElement("th"),
  	this.date              = this.now.getDate();
   	this.month             = this.mm = this.now.getMonth();
   	this.year              = this.yy = this.now.getFullYear();

   	this.monthCell.colSpan = 5;
   	this.monthCell.appendChild(document.createTextNode( this.monthname[this.mm]+" "+this.yy ));
   	this.tableHead         = this.createTableHead( size );
    this.parEl             = document.getElementById( id );
   	this.show( size );
  },
    
  removeElements : function( Obj ) {
	  for (var i=0; i<Obj.childNodes.length; i++)
	    Obj.removeChild(Obj.childNodes[i]);
	  return Obj;

  },
  	
  show : function( size ) {
    this.parEl = this.removeElements( this.parEl );
    
  	this.monthCell.firstChild.replaceData(0, this.monthCell.firstChild.nodeValue.length, this.monthname[this.mm]+" "+this.yy);
	 	var table = document.createElement("table");
    //table.style.position = 'absolute';
    table.style.width = size + 'px';
    table.appendChild( this.createTableBody() );
  	table.appendChild( this.tableHead );
 		this.parEl.appendChild( table );
  },
  
  createTableHead : function( size ) {
    var thead = document.createElement("thead");
  	var tr    = document.createElement("tr");
   	var th    = document.createElement("th");
   	//th.appendChild(document.createTextNode( "\u00AB" ));
   	th.Instanz = this;
	  //th.onclick = function() { this.Instanz.switchMonth("prev", size); };
	  //th.title   = this.tooltip[0];
	  //try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
	  tr.appendChild( th );
	  tr.appendChild( this.monthCell );			
	  th = document.createElement("th");
	  //th.appendChild(document.createTextNode( "\u00BB" ));
	  th.Instanz = this;
	  //th.onclick = function() { this.Instanz.switchMonth("next", size); };
	  //th.title = this.tooltip[1];
	  //try { th.style.cursor = "pointer"; } catch(e){ th.style.cursor = "hand"; }
	  tr.appendChild( th );
	  thead.appendChild( tr );
	  tr = document.createElement('tr');
	  for (var i=0; i<this.dayname.length; i++)
	    tr.appendChild( this.getCell("th", this.dayname[i], "weekday" ) );
	  thead.appendChild( tr );
	  return thead;
  },
  
  createTableBody : function() {
	  var sevendaysaweek = 0;
	  var begin = new Date(this.yy, this.mm, 1);
	  var firstday = begin.getDay()-1;
	  if (firstday < 0) firstday = 6;
	  if ((this.yy%4==0) && ((this.yy%100!=0) || (this.yy%400==0)))
	    this.dayspermonth[1] = 29;
	
	  var tbody = document.createElement("tbody");
	  var tr = document.createElement('tr');

	  for (var i=0; i<firstday; i++, sevendaysaweek++)
	    tr.appendChild( this.getCell( "td", " ", null ) );

    for (var i=1; i<=this.dayspermonth[this.mm]; i++, sevendaysaweek++) {
  		if (this.dayname.length == sevendaysaweek){
    	  tbody.appendChild( tr );
   			tr = document.createElement('tr');
   			sevendaysaweek = 0;
  	  }
		
   		//this.date = day
   		//alert ( this.date );
   		
   		local_set = false;
     	if (this.eisprung==i && this.mm==this.month && this.yy==this.year) {
    	  tr.appendChild( this.getCell( "td", i, "eisprung", this.mm, this.year  ) );
     		local_set = true;
     	}
   		
   		if ( !local_set ) {
     		for (i2=0;i2<=this.fruchtbar1.length;i2++) {
     		  //alert (this.fruchtbar1[i2] + " ï¿½ " + i);
     		  if (this.fruchtbar1[i2]==i && this.mm==this.month && this.yy==this.year) {
    	      tr.appendChild( this.getCell( "td", i, "fruchtbar1", this.mm, this.year  ) );
     		    local_set = true;
     		  }
        }
      }

   		if ( !local_set ) {
     		for (i2=0;i2<=this.fruchtbar2.length;i2++) {
     		  //alert (this.fruchtbar1[i2] + " ï¿½ " + i);
     		  if (this.fruchtbar2[i2]==i && this.mm==this.month && this.yy==this.year) {
    	      tr.appendChild( this.getCell( "td", i, "fruchtbar2", this.mm, this.year  ) );
     		    local_set = true;
     		  }
        }
      }

   		if ( !local_set ) {
     		if (i==this.date && this.mm==this.month && this.yy==this.year && (sevendaysaweek == 5 || sevendaysaweek == 6))
     		  tr.appendChild( this.getCell( "td", i, "today weekend", this.mm, this.year ) );
     		else if (i==this.date && this.mm==this.month && this.yy==this.year)
     			tr.appendChild( this.getCell( "td", i, "today", this.mm, this.year  ) );
     		else if (sevendaysaweek == 5 || sevendaysaweek == 6)
  	      tr.appendChild( this.getCell( "td", i, "weekend", this.mm, this.year  ) );
      	else
    	    tr.appendChild( this.getCell( "td", i, null, this.mm, this.yy  ) );
	    }
	  }
	
		for (var i=sevendaysaweek; i<this.dayname.length; i++)
		  tr.appendChild( this.getCell( "td", " ", null  ) );
	
		tbody.appendChild( tr );
		return tbody;
  },
	
	getCell : function(tag, str, cssClass, month, year) {
    //Erzeugen der Wochennamen und Tagnamen
    //ï¿½bergebenes Tag wird geschrieben
    var El = document.createElement( tag );
  
	  //Bei Klick auf einen Wochentag, wird diese Funktion aufgerufen
	  if ( cssClass != "weekday" ) {
	  	//Auswahlcursor bei Wochentagen anzeigen
	  	//try { El.style.cursor = "pointer"; } catch(e){ El.style.cursor = "hand"; }
	  	
   	  El.Instanz = this;
	  	//El.onclick = function() { this.Instanz.selectCell( El, str, month + 1, year ); };

	    
	  }
	  
	  El.appendChild(document.createTextNode( str ));
		if (cssClass != null) El.className = cssClass;
   		return El;
  },
		
	switchMonth : function( s, size ){
		
  	switch (s) {
  		case "prev": 
	 			this.yy = (this.mm == 0)?this.yy-1:this.yy;
  			this.mm = (this.mm == 0)?11:this.mm-1;
  		break;
				
  		case "next":
  			this.yy = (this.mm == 11)?this.yy+1:this.yy;
  		  this.mm = (this.mm == 11)?0:this.mm+1;
	 		break;
    }
	  
	  this.show( size );
	},
	
	selectCell : function( cell, local_day, local_month, local_year ) {
		//Zelle die angeglickt wurde, farbig hinterlegen und zuletzt angeglickte Zelle entfï¿½rben

    if ( typeof(cell_old) != "undefined" ) cell_old.style.backgroundColor = "#F6F6F6";
    	
		cell_old = cell;
		cell.style.backgroundColor = "#FF3333";
	}
}
	
var DOMContentLoaded = false;

function addContentLoadListener (func) {
	if (document.addEventListener) {
		var DOMContentLoadFunction = function () {
			window.DOMContentLoaded = true;
			func();
		};
	  
	  document.addEventListener("DOMContentLoaded", DOMContentLoadFunction, false);
	}
	
	var oldfunc = (window.onload || new Function());
	
	window.onload = function () {
		if (!window.DOMContentLoaded) {
		  oldfunc();
			func();
		}
  };
}

//Funktion Kalendaraufruf aus Onload Event des Body entfernt	
//addContentLoadListener( function() { CalendarJS.init("calendar"); } );
