
/*
	import de.teleson.ipcom.gui.domutil package along with this one
*/
var CalendarBlock = Class.create(DomDivExtended, {
	initialize: function($super) {
	  $super();
	  this._enabled = true;
	  this._name = null;
	},
	initListeners: function(){
	  var parObj = this;
	  this._domRef.observe('click', function(event){
		parObj._domRef.fire('calendar:onCalendarBlockClick', {name:parObj._name});
	  	//alert(parObj.getDate().day+' '+parObj.getDate().month+' '+parObj.getDate().year);
	  });
	},
	getEnabled: function(){
		return this._enabled;
	},
	setEnabled: function(val){
		this._enabled = val;
		if(this._enabled){
			this.setBackgroundColor('transparent');
		} else {
			this.setBackgroundColor('#ffd0d0');
		}
	},
	getName: function(){
		return this._name;
	},
	setName: function(val){
		this._name = val;
	}
});
var CalendarLeftArrow = Class.create(CalendarBlock, {
	initialize: function($super) {
	  $super();
	},
	initListeners: function(){
	  var parObj = this;
	  this._domRef.observe('mouseover', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/blue_arrows.png) no-repeat 0px -8px');
	  });
	  this._domRef.observe('mouseout', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/blue_arrows.png) no-repeat 0px 0px');
	  });
	  this._domRef.observe('click', function(event){
		parObj._domRef.fire('calendar:onCalendarLeftArrowClick', {name:parObj._name});
	  });
	}
});
var CalendarRightArrow = Class.create(CalendarBlock, {
	initialize: function($super) {
	  $super();
	},
	initListeners: function(){
	  var parObj = this;
	  this._domRef.observe('mouseover', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/blue_arrows2.png) no-repeat 0px -8px');
	  });
	  this._domRef.observe('mouseout', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/blue_arrows2.png) no-repeat 0px 0px');
	  });
	  this._domRef.observe('click', function(event){
		parObj._domRef.fire('calendar:onCalendarRightArrowClick', {name:parObj._name});
	  });
	}
});
var CalendarIcon = Class.create(CalendarBlock, {
	initialize: function($super) {
	  $super();
	},
	initListeners: function(){
	  var parObj = this;
	  this._domRef.observe('mousedown', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/calendar.png) no-repeat 1px 1px');
	  });
	  this._domRef.observe('mouseup', function(event){
	 	parObj.setBackground('url('+SITE_URL+'/img/calendar.png) no-repeat 0px 0px');
		parObj._domRef.fire('calendar:onCalendarIconClick', {name:parObj._name});
	  });
	},
	click: function(){
		this._domRef.fire('calendar:onCalendarIconClick', {name:this._name});
	}
});
var CalendarDay = Class.create(CalendarBlock, {
	initialize: function($super) {
	  $super();
	  this._day = null;
	  this._month = null;
	  this._year = null;
	},
	initListeners: function(){
	  var parObj = this;
	  this._domRef.observe('mouseover', function(event){
	  	if(parObj._enabled) parObj.setBackgroundColor('#d0ffd0');
	  });
	  this._domRef.observe('mouseout', function(event){
	  	if(parObj._enabled) parObj.setBackgroundColor('transparent');
	  });
	  this._domRef.observe('click', function(event){
		if(parObj._enabled) parObj._domRef.fire('calendar:onDayClick', {date:parObj.getDate()});
	  });
	},
	getDay: function(){
		return (parseInt(this._day) < 10) ? '0' + this._day: this._day;
	},
	setDay: function(val){
		this._day = val;
	},
	getMonth: function(){
		return (parseInt(this._month) < 10) ? '0' + this._month: this._month;
	},
	setMonth: function(val){
		this._month = val;
	},
	getYear: function(){
		return this._year;
	},
	setYear: function(val){
		this._year = val;
	},
	getDate: function(){
		return {day:(parseInt(this._day) < 10) ? '0' + this._day: this._day, month:(parseInt(this._month) < 10) ? '0' + this._month : this._month, year:this._year};
	}
});
var Calendar = Class.create(DomDivExtended, {
	initialize: function($super) {
	  $super();
	  this._days = new Array();
	  this._months = new Array('Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli',
	  		'August', 'September', 'Oktober', 'November', 'Dezember');
	  this._dayNames = new Array('Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So');
	  this._monthCont = new DomDiv();
	  this._weekCont = new DomDivExtended();
	  this._yearCont = new DomDiv();
	  this._close = null;
	  this._date = new Date();
	  this._data = null;
	  this._index = null;
	  this._weekendsDisabled = false;
	  this._dragged = false;
	  this._domDrag = new DomDrag();
	  document.observe('calendarData:onDataChange', this.draw.bind(this));
	},
	getWeekendsDisabled: function(){
		return this._weekendsDisabled;
	},
	setWeekendsDisabled: function(val){
		this._weekendsDisabled = val;
	},
	findCurrentIndex: function(){
		var month = this._date.getMonth();
		var year = this._date.getFullYear();
		var total = this._data.length;
		for(i = 0; i < total; i++){
			if((this._data[i]._month == (month + 1)) && (this._data[i]._year == year)){
				this._index = i;
				break;
			}
		}
	},
	prevMonth: function(event){
		if(this._index > 0){
			this._index--;
			this.updateMonth();
			this.drawDays();
		}
	},
	nextMonth: function(event){
		if(this._index < (this._data.length-1)){
			this._index++;
			this.updateMonth();
			this.drawDays();
		}
	},
	isDayValid: function(day){
		if(this._weekendsDisabled){
		    this._date.setDate(day);
		    this._date.setMonth(this._data[this._index]._month-1);
		    this._date.setFullYear(this._data[this._index]._year);
			if(this._date.getDay() == 0 || this._date.getDay() == 6) return false;
		}
		return true;
	},
	drawDays: function(){
		this._monthCont.setContent('');
		var firstDay = this._data[this._index]._firstDay - 1;
		if(firstDay < 0) firstDay += 7;
		for(i = 0; i < firstDay; i++){
			var div = new DomDiv();
		    div.create(this._monthCont.getDomRef(), 'absolute');
		    div.setX(i*21);
		    div.setY(0);
		    div.setWidth(18);
		    div.setHeight(18);
		    div.setBorder('none');
		    div.setBackgroundColor('#e0e0e0');
		}

		var numDays = this._data[this._index]._numDays;
		//var t = Math.round(this._monthCont.getWidth() / 8);
		var xOffset = 21 * firstDay;
		var yOffset = 0;
		var cnt = 1;
		var total = 42 - firstDay;
		for(i = 0; i < total; i++){
			if(i < numDays){
				var day = new CalendarDay();
			    day.create(this._monthCont.getDomRef(), 'absolute');
			    day.setX(xOffset);
			    day.setY(yOffset);
			    //if(i == 0) day.setMargin('0px 0px 0px '+xOffset+'px');
			    day.setWidth(16);
			    day.setHeight(16);
			    day.setBorder('none');
			    day.setBorderRight('1px solid #d0d0d0');
			    day.setBorderBottom('1px solid #d0d0d0');
			    //day.setFloat('left');
			    day.setPadding('1px');
			    day.setTextAlign('center');
			    if(!this.isDayValid(cnt)) day.setEnabled(false);
			    day.setDay(cnt);
			    day.setMonth(this._data[this._index]._month);
			    day.setYear(this._data[this._index]._year);
			    day.setContent(cnt);
			    day.initListeners();
			    day.setCursor('pointer');
				this._days.push(day);
				cnt++;
			} else {
				var div = new DomDiv();
			    div.create(this._monthCont.getDomRef(), 'absolute');
			    div.setX(xOffset);
			    div.setY(yOffset);
			    div.setWidth(18);
			    div.setHeight(18);
			    div.setBorder('none');
			    div.setBackgroundColor('#e0e0e0');
			}
			xOffset += 21;
			if((xOffset + 21) > this._monthCont.getWidth()){
				xOffset = 0;
				yOffset += 21;
			}
		}
	},
	updateMonth: function(){
		var yearName = this._data[this._index]._year;
		var monthName = this._months[(this._data[this._index]._month-1)];
		this._yearBlock.setContent(monthName + ' ' + yearName);
	},
	open: function(event){
		this.show();
	},
	close: function(event){
		if(event.memo.name == 'close'){
			this.hide();
		}
	},
	visible: function(){
		return this._domRef.visible();
	},
	draw: function(event){
		this._data = event.memo.data;

		this.findCurrentIndex();

	    this._monthCont.create(this._domRef, 'absolute');
	    this._monthCont.setX(7);
	    this._monthCont.setWidth(this._monthCont.getParent().getWidth() - 4);
	    this._monthCont.setHeight(this._monthCont.getParent().getHeight() - 52);
	    this._monthCont.setY(this._monthCont.getParent().getHeight() - this._monthCont.getHeight() + 4);
	    this._monthCont.setBorder('none');

	    this._weekCont.create(this._domRef, 'absolute');
	    this._weekCont.setX(7);
	    this._weekCont.setWidth(this._weekCont.getParent().getWidth() - 20);
	    this._weekCont.setHeight(20);
	    this._weekCont.setY(this._weekCont.getParent().getHeight() - this._monthCont.getHeight() - this._weekCont.getHeight());
	    this._weekCont.setBorder('none');
	    this._weekCont.setBorderTop('1px solid #d0d0d0');
	    this._weekCont.setBorderBottom('1px solid #d0d0d0');

	    this._yearCont.create(this._domRef, 'absolute');
	    this._yearCont.setX(7);
	    this._yearCont.setY(1);
	    this._yearCont.setWidth(this._weekCont.getParent().getWidth() - 20);
	    this._yearCont.setHeight(20);
	    this._yearCont.setBorder('none');

		var yearName = this._data[this._index]._year;
		var monthName = this._months[(this._data[this._index]._month-1)];

		var block = new CalendarLeftArrow();
	    block.create(this._yearCont.getDomRef(), 'absolute');
	    block.setName('prevMonth');
	    block.setY(15);
	    block.setWidth(12);
	    block.setHeight(20);
	    block.setBorder('none');
	    block.setMargin('0px 0px 0px 4px');
		block.initListeners();
		block.setCursor('pointer');
		block.setBackground('url('+SITE_URL+'/img/blue_arrows.png) no-repeat 0px 0px');
		block.setOverflow('hidden');
		block.setClip('rect(0px, 8px, 8px, 0px)');
	    block.getDomRef().observe('calendar:onCalendarLeftArrowClick', this.prevMonth.bind(this));
	    block.setX(0);

		this._yearBlock = new CalendarBlock();
	    this._yearBlock.create(this._yearCont.getDomRef(), 'absolute');
	    this._yearBlock.setX(10);
	    this._yearBlock.setWidth(this._yearCont.getParent().getWidth() - 40);
	    this._yearBlock.setHeight(20);
	    this._yearBlock.setY(9);
	    this._yearBlock.setBorder('none');
	    this._yearBlock.setTextAlign('center');
		this._yearBlock.setBold(true);
		this._yearBlock.setPadding('2px 0px 0px 0px');
		this._yearBlock.setCursor('move');
	    this._yearBlock.setContent(monthName + ' ' + yearName);
		this._domDrag.setDomRef(this._yearBlock.getDomRef());
		this._domDrag.setTarget(this);
	    this._domDrag.initListeners();

		this._close = new CalendarBlock();
	    this._close.create(this._yearCont.getDomRef(), 'absolute');
	    this._close.setName('close');
	    this._close.setX(this._yearCont.getWidth() - 11);
	    this._close.setY(0);
	    this._close.setWidth(4);
	    this._close.setHeight(4);
	    this._close.setBorder('none');
	    this._close.setTextAlign('center');
		this._close.setBold(true);
		this._close.setCursor('pointer');
	    this._close.setContent('x');
	    this._close.initListeners();
	    this._close.getDomRef().observe('calendar:onCalendarBlockClick', this.close.bind(this));

		var block = new CalendarRightArrow();
	    block.create(this._yearCont.getDomRef(), 'absolute');
	    block.setName('nextMonth');
	    block.setY(15);
	    block.setWidth(12);
	    block.setHeight(20);
	    block.setBorder('none');
	    block.setMargin('0px 4px 0px 0px');
		block.initListeners();
		block.setCursor('pointer');
		block.setBackground('url('+SITE_URL+'/img/blue_arrows2.png) no-repeat 0px 0px');
		block.setOverflow('hidden');
		block.setClip('rect(0px, 8px, 8px, 0px)');
	    block.getDomRef().observe('calendar:onCalendarRightArrowClick', this.nextMonth.bind(this));
	    block.setX(this._yearCont.getParent().getWidth() - 36);

		var xOffset = 0;
		var t = Math.round(this._monthCont.getWidth() / 8);
		for(i = 0; i < 7; i++){
			var day = new DomDivExtended();
		    day.create(this._weekCont.getDomRef(), 'absolute');
		    day.setX(xOffset);
		    day.setWidth(16);
		    day.setHeight(16);
		    day.setY(0);
		    day.setBorder('none');
		    day.setPadding('1px');
		    day.setTextAlign('center');
		    day.setBold(true);
		    day.setContent(this._dayNames[i]);
		    xOffset += 21;
		}
		this.drawDays();
	}
});
