/* Calendar */

Object.extend(Date.prototype, {
	monthnames: ['Januar','Februar','Marts','April','Maj','Juni','Juli','August','September','Oktober','November','December'],
    daynames: ['S','M','T','O','T','F','L'],
    succ: function(){
        var sd = new Date(this.getFullYear(),this.getMonth(),this.getDate()+1);
        sd.setHours(this.getHours(),this.getMinutes(),this.getSeconds(),this.getMilliseconds());
        return sd;
    },
    firstofmonth: function(){
        return new Date(this.getFullYear(),this.getMonth(),1);
    },
    lastofmonth: function(){
        return new Date(this.getFullYear(),this.getMonth()+1,0);
    },
    formatPadding: true,
    format: function(f) {
    	var d = this;
    	switch(f) {
    		default:
    			return '&nbsp;';
    			break;
    		case 'headertext':
    			return this.monthnames[d.getMonth()]+" "+d.getFullYear();
    			break;
    	}
    }
});

var scal = {};
scal = Class.create();
scal.prototype = {
    initialize: function(element,update) {
    	this.element = $(element);
    	this.update = $(update);
        var type = Try.these(
            function(){ if(!Object.isUndefined(Effect)) { return 'Effect'; }},
            function(){ return 'Element'; }
        );
        this.options = Object.extend({
          oncalchange: Prototype.emptyFunction,
          daypadding: false,
          titleformat: 'mmmm yyyy',
          updateformat: 'yyyy-mm-dd',
          closebutton: 'X',
          prevbutton: '&lt;',
          nextbutton: '&gt;',
          yearnext: '&raquo;&raquo;',
          yearprev: '&laquo;&laquo;',
          openeffect: type == 'Effect' ? Effect.Appear : Element.show,
          closeeffect: type == 'Effect' ? Effect.Fade : Element.hide,
          exactweeks: false,
          dayheadlength: 2,
          weekdaystart: 1,
          planner: false
        }, arguments[2] || { });  
        this.startdate = this._setStartDate(arguments[2]);
		this._setCurrentDate(this.startdate);
		this.seletedDay;
		this.now = new Date();
		
		this._drawMaster();
    },
    _setStartDate: function() {
		var args = arguments[0];
        var startday = new Date();
        this.options.month = args && args.month && Object.isNumber(args.month) ? args.month - 1 : startday.getMonth();
        this.options.year = args && args.year && Object.isNumber(args.year) ? args.year : startday.getFullYear();
        this.options.day = args && args.day && Object.isNumber(args.day) ? args.day : (this.options.month != startday.getMonth()) ? 1 : startday.getDate();
        startday.setHours(0,0,0,0);
        startday.setDate(this.options.day);
        startday.setMonth(this.options.month);
        startday.setFullYear(this.options.year);
        return startday;
    },
    _setCurrentDate: function(date){
        this.currentdate = new Date(date.getFullYear(),date.getMonth(),date.getDate());
        this.firstofmonth = this.currentdate.firstofmonth();
        this.lastofmonth = this.currentdate.lastofmonth();
    },
    _drawMaster: function() {
    	this.element.className='mcalMaster';
    	this._drawHeader();
    	this._drawCal();
    },
    _drawHeader: function() {
    	this.headerDiv = document.createElement('DIV');
    	this.headerDiv.className='mcalHeader';
    	//Prev button
    	this.prevBut = document.createElement('DIV');
    	this.prevBut.innerHTML=this.options.prevbutton;
    	this.prevBut.className='mcalPrevButton';
    	this.prevBut.onclick=function() {
    		sampleCal._prev();
    	}
//    	this.prevBut.observe('click', this._prev.bindAsEventListener(this));
    	this.headerDiv.appendChild(this.prevBut);
//    	Header text
    	this.headerText = document.createElement('DIV');
    	this.headerText.className='mcalHeaderText';
    	this.headerText.innerHTML=this.currentdate.format('headertext');
    	this.headerDiv.appendChild(this.headerText);
//    	Next button
    	this.nextBut = document.createElement('DIV');
    	this.nextBut.innerHTML=this.options.nextbutton;
    	this.nextBut.className='mcalNextButton';
    	this.nextBut.onclick=function() {
    		sampleCal._next();
    	}
    	this.headerDiv.appendChild(this.nextBut);
    	
    	this.element.appendChild(this.headerDiv);
    },
    _drawCal: function() {
//    	alert(this.firstofmonth+"\r\n"+this.lastofmonth);
    	this.calDiv = document.createElement('DIV');
    	var html="<table class=\"mcalCalTable\">";
    	
    	// Indlæs ugenavne
    	html+="<tr>";
    	for(i=1;i<7;i++) {
    		html+="<td class=\"mcalDayHeader\">"+this.firstofmonth.daynames[i]+"</td>";
    	}
    	html+="<td class=\"mcalDayHeader\">"+this.firstofmonth.daynames[0]+"</td>";
    	html+="</tr>";
    	
    	//Draw first week
    	if(this.firstofmonth.getDay()==0) pre=6;
    	else pre = this.firstofmonth.getDay()-1;
    	preDate = new Date();
    	html+="<tr>";
    	cellCounter=0;
    	for(i=pre; i>0; i--) {
    		preDate.setDate(this.firstofmonth.getDate()-i);
    		html+="<td class=\"mcalDayInactive\">"+preDate.getDate()+"</td>";
    		cellCounter++;
    	}
    	dayAdder = 0; // Variable to add to first of month var
    	theDay = new Date();
    	theDay.setFullYear(this.firstofmonth.getFullYear(), this.firstofmonth.getMonth(), this.firstofmonth.getDate());
    	if(this.firstofmonth.getDay()==0) n=7;
    	else n=this.firstofmonth.getDay();
    	for(i=n; i<=7; i++) {
    		if(this.now.getDate()>theDay.getDate() && this.now.getMonth() == theDay.getMonth()) {
    			html+='<td class="mcalDayInactive">'+theDay.getDate()+'</td>';
    		} else if(this.now.getDate()==theDay.getDate() && this.now.getMonth() == theDay.getMonth()) {
				html+="<td class=\"mcalDayNow\" name=\"now\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
			} else {
				html+="<td class=\"mcalDay\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
			}
    		theDay.setDate(theDay.getDate()+1);
    	}
    	html+="</tr>";
    	
    	//Loop gennem 5 uger for at være 100% sikker
    	for(w=0; w<5; w++) {
    		html+="<tr>";
    		for(i=0; i<7; i++) {
//    			theDay.setDate(theDay.getDate()+1);
    			if(theDay.getMonth()==this.lastofmonth.getMonth()) {
    				if(this.now.getDate()>theDay.getDate() && this.now.getMonth() == theDay.getMonth()) {
		    			html+='<td class="mcalDayInactive">'+theDay.getDate()+'</td>';
		    		} else if(this.now.getDate()==theDay.getDate() && this.now.getMonth() == theDay.getMonth()) {
						html+="<td class=\"mcalDayNow\" name=\"now\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
					} else {
						html+="<td class=\"mcalDay\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
					}
//    				if(this.now.getDate()==theDay.getDate() && this.now.getMonth() == theDay.getMonth()) {
//    					html+="<td class=\"mcalDayNow\" name=\"now\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
//    				} else {
//						html+="<td class=\"mcalDay\" onclick=\"sampleCal._selDay('"+theDay+"', this);\">"+theDay.getDate()+"</td>";
//    				}
    			} else {
    				html+='<td class="mcalDayInactive">'+theDay.getDate()+'</td>';
    				w=10;
    			}
    			theDay.setDate(theDay.getDate()+1);
    		}
    		html+="</tr>";
    	}
    	
    	html+="</table>"
		this.calDiv.innerHTML=html;
    	this.element.appendChild(this.calDiv);
    },
    _prev: function() {
    	if(this.firstofmonth.getMonth()>this.now.getMonth()) {
	    	var date = new Date();
	    	date.setFullYear(this.firstofmonth.getFullYear(), this.firstofmonth.getMonth()-1, this.firstofmonth.getDate());
	    	this._setCurrentDate(date);
	    	this.element.removeChild(this.calDiv);
	    	this.headerText.innerHTML=this.currentdate.format('headertext');
	    	this._drawCal();
    	}
    },
    _next: function() {
    	var date = new Date();
    	date.setFullYear(this.firstofmonth.getFullYear(), this.firstofmonth.getMonth()+1, this.firstofmonth.getDate());
    	this._setCurrentDate(date);
    	this.element.removeChild(this.calDiv);
    	this.headerText.innerHTML=this.currentdate.format('headertext');
    	this._drawCal();
    },
    _selDay: function(date, me) {
    	var selDay = new Date(date);
    	if(selDay.getMonth()<9) month = "0"+(selDay.getMonth()+1);
    	else month=selDay.getMonth()+1;
    	if(selDay.getDate()<10) day = "0"+selDay.getDate();
    	else day = selDay.getDate();
    	this.update.value=selDay.getFullYear()+"-"+month+"-"+day;
    	if(typeof(this.selectedDay)!='undefined') {
    		if(this.selectedDay.name=='now') {
    			this.selectedDay.className='mcalDayNow';
    		} else {
    			this.selectedDay.className='mcalDay';
    		}
    	}
    	me.className='mcalDaySelected';
    	this.selectedDay=me;
    }
};