
( function( $, undefined ) {

	$.widget( "ui.calendar", {
		widgetEventPrefix : "calendar",
		
		options : {
			days : [ "Mo", "Di", "Mi", "Do", "Fr", "Sa", "So" ],
			months : [ "Januar", "Februar", "März", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember" ],
			data : []
		},
		
		_create : function () {
			this.monthOffset = 0;
			this.current = this._determineDate();
			this.active = $.extend({}, this.current );

			this._prepareElements();
			
			this._update();
			
			this.element
				.addClass( "ui-calendar" )
				.append( this.construct )
		},
		
		_determineDate : function ( monthOffset ) {
			var now = new Date();

			if( monthOffset ) {
				now.setMonth( now.getMonth() + monthOffset );
			}

			var	d = {
					date : now.getDate(),
					day : now.getDay() - 1,
					monthName : this.options.months[ now.getMonth() ],
					month : now.getMonth(),
					year : now.getFullYear()
				};

			if ( d.day < 0 ) d.day = 6;
			
			return d;
			
		},
		
		_prepareElements : function () {
			this.construct = $( "<div/>" );
			
			
			var that = this,
				left = $( "<div/>")
					.addClass( "ui-calendar-go-left" )
					.click( function () {
						that.active = that._determineDate( -- that.monthOffset );
						that._update();
					}),
				
				right = $( "<div/>")
					.addClass( "ui-calendar-go-right" )
					.click( function () {
						that.active = that._determineDate( ++ that.monthOffset );
						that._update();
					});
			
			
			this.header = $( "<div/>" )
				.addClass( "ui-calendar-header" )
				
				.append( left )
				.append( "<span></span>")
				.append( right )
				
				.appendTo( this.construct );
				
			this.table = $( "<table/>" )
				.append( "<tr/>" )
				.appendTo( this.construct );
			
			var that = this;
			
			$.each( this.options.days, function() {
				$( "tr:eq(0)", that.table ).append( "<th>" + this + "</th>" );
			});
		},
		
		_update : function () {
			this._updateHeader();
			this._updateDays();
		},
		
		_updateDays : function () {
		
			$( "tr", this.table ).not( ":eq(0)" ).remove();
		
			var now = new Date(),
				day = 1,
				row = $( "<tr/>" ).appendTo( this.table ),
				events = ( this.options.data[ this.active.year ] || [] )[ this.active.month ] || [];
				
			now.setMonth( this.active.month );
			now.setDate( 1 );
			now.setYear( this.active.year );
			
			var nowDay = now.getDay() || 7;

			while ( nowDay != day ) {
				day ++;
				
				$( "<td/>" )
					.addClass( "ui-calendar-day-empty" )
					.appendTo( row );
			}

			while ( now.getMonth() == this.active.month ) {


				var date = now.getDate(),
					curEvent = events[ date ],
					td = $( "<td>" + date + "</td>" ).appendTo( row );

				if ( this.active.month == this.current.month && this.active.year == this.current.year && date == this.current.date ) {
					td.addClass( "ui-calendar-day-today" );
				}

				if ( curEvent ) {
					td
						.addClass( "ui-calendar-day-event ")
						.attr( "title", curEvent.title )
						.data( "event", curEvent )
						.click( function () {
							window.location.href = $( this ).data( "event" ).link ;
						});
				}

				if ( now.getDay() == 0 ) {
					row = $( "<tr/>" ).appendTo( this.table );
				}
			
				now.setDate( date + 1 );				
			}

			while ( now.getDay() != 1 ) {
				now.setDate( now.getDate() + 1 );
				$( "<td/>" )
					.addClass( "ui-calendar-day-empty" )
					.appendTo( row );
			}
		},
		
		_updateHeader : function () {
			$( "span", this.header ).text( this.active.monthName + " " + this.active.year );
		}
	});

}( jQuery ) );

$( function () {
	if ( window.event_data === undefined ) {
		return;
	}
	
	var obj = $( "dl.ecal dd" );
	
	obj.calendar( {
		data : event_data
	});
	
} );
