var Calendar = {
	field: null,
	year: null,
	month: null,
	days: null,
	day: null,
	today: null,
	daysBefore: null,
	startDay: null,
	weekNumber: null,
	itemCName: 'dateItem',
	itemOutCName: 'dateItemOut',
	itemWeCName: 'dateItemWe',
	itemTodayCName: 'dateItemToday',
	imgControlUp: './images/arrow_right.gif',
	imgControlDown: './images/arrow_left.gif',
	idDateCalendar: 'dateBlock',
	idDateField: 'dateField',
	idDateMonth: 'dateMonth',
	idDateMonthControl: 'dateMControl',
	idDateWeekNumber: 'dateWeekNumber',
	idDateYearControl: 'dateYControl',
	idDateYear: 'dateYear',
	//monthNames: [ 'Jan', 'Feb', 'M&auml;r', 'Apr', 'Mai', 'Jun', 'Jul', 'Aug', 'Sep', 'Okt', 'Nov', 'Dez' ],
	monthNames: [ 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec' ],
	exportFunction: null,
	exportInfo: null,
	active: false,
	handlerObject: null,
	ajaxRequest: 'daterequest.php',
	
	initialise: function( element ){
		element.onclick = this.display;
		if( !this.active ){
			
			if( (this.field = $(this.idDateField)) == null ){
					$$('body')[0].innerHTML += '<div id="'+this.idDateCalendar+'"><div id="'+this.idDateMonth+'"></div><div id="'+this.idDateMonthControl+'"></div><div id="'+this.idDateYearControl+'"></div><div id="'+this.idDateYear+'"></div>'+
					'<div id="dateIdentifier"><div>M</div><div>Tu</div><div>W</div><div>Th</div><div>F</div><div>Sa</div><div>Su</div></div>'+
					'<div id="'+this.idDateField+'"></div></div>';
					this.field = $(this.idDateField);
			}
	
			
			if( arguments[0] !== undefined )
				this.exportFunction = arguments[1];
			if( arguments[1] !== undefined )
				this.exportInfo = arguments[2];
			new Ajax.Request( this.ajaxRequest , {
				method: 'get',
				onSuccess: function(transport) { 
					Calendar.setValues(transport.responseText.evalJSON());
					Calendar.refreshView();
				}
			}); 
		} else {
			//
		}
	},
	
	display: function( eevent ){
		var eevent = window.event ? window.event : eevent;
		var element = (eevent.target ? eevent.target : eevent.srcElement);
		
		if( this != Calendar.handlerElement ){
			if(Calendar.active){
				Calendar.release();
				this.onmouseover();
			}
			
			Calendar.handlerElement = this;
			Calendar.active = true;
			var y = eevent.clientY +1+ (self.pageYOffset ? self.pageYOffset : (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)  );
			var x = eevent.clientX +1+ (self.pageXOffset ? self.pageXOffset : (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollLeft : document.body.scrollLeft)  );
	
			$(Calendar.idDateCalendar).setStyle({top:y+'px',left:x+ 'px',display:'block'});
			
		} else {
			Calendar.release();
		}
	},
	
	release: function () {
		$(Calendar.idDateCalendar).setStyle({display:'none'});
		Calendar.active = false;
		Calendar.handlerElement = null;
	},
	
	refreshDateInfo:  function(){
		new Ajax.Request( this.ajaxRequest , {
			method: 'get',
			parameters: {year: Calendar.year, month: Calendar.month },
			onSuccess: function(transport) {
				Calendar.setValues(transport.responseText.evalJSON());
				Calendar.refreshView();
			} 
		}); 
	},
	
	setValues: function( json ){
		this.year = json.year;
		this.month = json.month;
		this.today = {year : json.today.year, month : json.today.month,day : json.today.day};
		this.days = json.days;
		this.daysBefore = json.daysBefore;
		this.startDay = (json.startDay == 0 ? 7 : json.startDay);
		this.weekNumber = json.weekNumber;
	},
	
	refreshView: function(){
		this.clearItems();
		this.refreshControl();
		this.buildItems();
	},
	
	refreshControl: function(){
		$(this.idDateMonth).innerHTML = this.monthNames[(this.month-1)];
		$(this.idDateMonthControl).innerHTML =   '&nbsp;<img src="'+this.imgControlDown+'" onclick="Calendar.prevMonth()" style="cursor:pointer;">'+
										'&nbsp;<img src="'+this.imgControlUp+'" onclick="Calendar.nextMonth()" style="cursor:pointer;">';
		$(this.idDateYearControl).innerHTML =   '&nbsp;<img src="'+this.imgControlDown+'" onclick="Calendar.prevYear()" style="cursor:pointer;">'+
										'&nbsp;<img src="'+this.imgControlUp+'" onclick="Calendar.nextYear()" style="cursor:pointer;">';
		$(this.idDateYear).innerHTML = this.year;
	},
	
	clearItems: function(){
		this.field.immediateDescendants().each( function( element ) {
			element.remove();
		})
	},
	
	isToday: function ( day ) {
		var month = this.month;
		if( arguments[1] != undefined )
			 month -= -arguments[1];
		return (day == this.today.day && month == this.today.month && this.year == this.today.year ? true : false);
	},
	
	buildItems: function() {
		for( this.day = 1; this.day <= 42; this.day++ ){
			this.field.appendChild( this.buildItem(this.day) );
		}
	},
	
	buildItem: function( d ) {
		var day = null;
		var element = null;
		var className = null;
		var monthCorrection = 0;
		var yearCorrection = 0;
		var atr = null;
		if(d < this.startDay ){
			className = this.itemOutCName;
			day = this.daysBefore - (this.startDay - 1) + d ;
			if(this.month == 1){
				monthCorrection = 11;
				yearCorrection = -1;
			} else {
				monthCorrection = -1;
			}
		} else {
			day = d - (this.startDay - 1);
			if( day > this.days ){
				day = d - this.days - this.startDay -(-1);
				className = this.itemOutCName;
				monthCorrection = (this.month == 12 ? -11 : 1);
				if(this.month == 12){
					monthCorrection = -11;
					yearCorrection = 1;
				} else {
					monthCorrection = 1;
				}
			}else{
				className = ( this.isToday( day ) ? this.itemTodayCName : this.itemCName);	
			}
		}
		
		if( ((d-1) % 7 == 6 || (d-1) % 7 == 5 ) && (!this.isToday( day, monthCorrection )) ) // other color for weekend excepting today
			className = className + ' ' + this.itemWeCName;
		
		element = null;
		element = $(document.createElement("div")).addClassName( className );
		element.onclick = this.returnDate;
		element.id = 'date-'+(this.year-(-yearCorrection))+'-'+(this.month -(-monthCorrection))+'-'+day;
		if( (d -1) % 7 == 0 ) // next line for new week
			element.setStyle( {clear: 'left'} );
	
		element.innerHTML = day;
		return element;
	},
	
	nextYear: function () {
		this.year++;
		this.refreshDateInfo();
	},
	
	prevYear: function () {
		this.year--;
		this.refreshDateInfo();
	},
	
	nextMonth: function () {
		if(this.month >= 12){
			this.month = 1;
			this.year++;
		} else {
			this.month++;
		}
		this.refreshDateInfo();
	},
	
	prevMonth: function () {
		if(this.month <= 1){
			this.month = 12;
			this.year--;
		} else {
			this.month--;
		}
		this.refreshDateInfo();
	},
	
	set: function ( year, month, day ){
		this.year = year;
		this.month = month;
		this.day = day;
	},
	
	resetDate: function ( year , month, day ) {
		this.set( year , month, day );
		this.refreshDateInfo();
	},
	alertValues: function () {
		alert([this.days,this.month,this.year,this.startDay]);
	}, 
	
	returnDate: function(){
		var date = this.id.split('-');
		Calendar.exportFunction( {year:date[1], month:date[2], day:date[3]}, Calendar.exportInfo );
	},
	
	exportFunction: function( date ) {
		alert('overloadme: 1 param date obj ' );
	},
	
	exportTo: {
		form: {
			select: function ( date, exportInfo ){
				$A(exportInfo.form.elements[exportInfo.year]).each( function ( option ){
					if(option.value == date.year) option.selected = true;
				});
				$A(exportInfo.form.elements[exportInfo.month]).each( function ( option ){
					if(option.value == date.month) option.selected = true;
				});
				$A(exportInfo.form.elements[exportInfo.day]).each( function ( option ){
					if(option.value == date.day) option.selected = true;
				});
				Calendar.release();
			}
		}
	}
	
}


