var MyEventsCalendar = {
		cal_months_labels : ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'],
		cal_days_labels : ['Su', 'Mo', 'Tu', 'We', 'Th', 'Fr', 'Sa'],
		cal_days_in_month : [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31],
		id 	:	String,
		events 	: 	Object,
		showDate: 	Object,
		endDate : 	Object,
		workingDate:	Object,
		today :		Object,
		html :		String,
		monthData:	Object,
		
		

		

		init : function(strDiv, oEvents, oShowDate){

			this.id = strDiv;
			
			this.events = oEvents;
			this.showDate = new Date( oShowDate.getFullYear(), oShowDate.getMonth(), MyEventsCalendar.getNumDayz(oShowDate));
			this.today = new Date();
			this.multidayEvent = null;
			this.showMonth();		
		},

		showMonth : function(){
			this.calculateDates();

			this.currMonthData = MyEventsCalendar.getMonthData(this.events, this.showDate);

			var tempDate = new Date(this.showDate.getFullYear(), this.showDate.getMonth(), 1);

			tempDate.setMonth(this.showDate.getMonth()-1);

			this.prevMonthData = MyEventsCalendar.getMonthData(this.events, tempDate);

			tempDate.setMonth(this.showDate.getMonth()+1);

			this.nxtMonthData = MyEventsCalendar.getMonthData(this.events, tempDate);

			document.getElementById(this.id).innerHTML = this.doTheHtml();
			document.getElementById("calendar-prevArrow").onclick = this.prevMonth;
			document.getElementById("calendar-nextArrow").onclick = this.nextMonth;
			$(BT_init);

		},

		nextMonth : function(){

			var nxt=MyEventsCalendar.showDate.getMonth();
			nxt = nxt+1;

			MyEventsCalendar.showDate.setMonth(nxt, 1);
			MyEventsCalendar.showMonth();
		},

		prevMonth : function(){
			
			var prev=MyEventsCalendar.showDate.getMonth();
			prev= prev - 1;
			MyEventsCalendar.showDate.setMonth(prev, 1);
			
			MyEventsCalendar.showMonth();
		},

		calculateDates : function(){
			// first calculate the last day of the month 

			this.endDate= new Date(this.showDate.getFullYear(), this.showDate.getMonth(), this.getNumDayz(this.showDate));
			
			// now find out the date which would fall at the end of the last week-row of the calendar
			if(this.endDate.getDay() < 6 && this.endDate.getDay() !== 0){
				this.endDate.setDate(this.endDate.getDate() + (6-this.endDate.getDay()));

			}
			
			// create a working date equal to the first of the month 

			this.workingDate= new Date(this.showDate.getFullYear(), this.showDate.getMonth(), 1);

			// now check if the first day is a sunday. if not slide the working date back to a sunday

			var firstDay = this.workingDate.getDay();

			if(firstDay>0){
					
				this.workingDate.setDate(this.workingDate.getDate() - firstDay); //get the working date to sunday
				
			}

		},

		getMonthData : function(varEvents, varShowDate){
			
			var daysInMonth=this.getNumDayz(varShowDate);

			var mnthDt=new Array(daysInMonth); //array of the whole month
			for(var i=0; i<mnthDt.length; i++){
				mnthDt[i]=new Array(); //each day slot can contain an array of events
				
			}

			for(i=0;i<varEvents.length;i++){
				
				if(varEvents[i].when.getFullYear()==varShowDate.getFullYear()){
					if(varEvents[i].when.getMonth()==varShowDate.getMonth()){

						mnthDt[varEvents[i].when.getDate()-1].push(varEvents[i]);
						
					
					}
				}
			
			}

			
			return mnthDt;
			
		},

		doTheHtml : function(){
			
			var monthName = this.cal_months_labels[this.showDate.getMonth()];
			var theHtml = '<table class="calendar-table">';
			theHtml += '<tr id="calendar-month_year_header"><th colspan="7">';
			theHtml += '<div id="calendar-prevArrow"> <img src="images/resultset_previous.png" /> </div>'; 
			theHtml += '<div id="calendar-monthyear">'+monthName + "&nbsp;" + this.showDate.getFullYear() + '</div>';
			theHtml += '<div id="calendar-nextArrow"> <img src="images/resultset_next.png" /> </div>';
			theHtml += '</th></tr>';
			theHtml += '<tr class="calendar-header">';

			for (var i = 0; i <= 6; i++ ){
  				theHtml += '<td class="calendar-header-day">';
  				theHtml += MyEventsCalendar.cal_days_labels[i];
  				theHtml += '</td>';
			}

			theHtml += '</tr>';		
			
			do{
				theHtml+="<tr>";
				for(i=0; i<7; i++){
					theHtml+='<td><div class="calendar-date-cell ';

					if(this.workingDate.getFullYear() < this.showDate.getFullYear()){ 

						theHtml += 'prevMonth ';
						var dayzEvents = this.prevMonthData[this.workingDate.getDate()-1];
					

					}else if(this.workingDate.getFullYear() == this.showDate.getFullYear()){

						if(this.workingDate.getMonth() < this.showDate.getMonth()){

							theHtml += 'prevMonth ';
							var dayzEvents = this.prevMonthData[this.workingDate.getDate()-1];
							

						}else if(this.workingDate.getMonth() == this.showDate.getMonth()){

							
							theHtml+='currentMonth ';
							dayzEvents= this.currMonthData[this.workingDate.getDate()-1];
							if(this.workingDate.getFullYear() == this.today.getFullYear()){
								if(this.workingDate.getMonth() == this.today.getMonth()){
									if(this.workingDate.getDate() == this.today.getDate()){
										theHtml+='today ';
									}else{}
								}else{}
							}
							else{}

						}else if(this.workingDate.getMonth() > this.showDate.getMonth()){

							theHtml+='nextMonth ';
							dayzEvents= this.nxtMonthData[this.workingDate.getDate()-1];
						}else{}

						
						

					}else if(this.workingDate.getFullYear() > this.showDate.getFullYear()){ 
						
						theHtml+='nextMonth ';
						dayzEvents= this.nxtMonthData[this.workingDate.getDate()-1];
					}else{
						
					}		
					

					if(dayzEvents.length > 0){

						for(var j=0;j<dayzEvents.length;j++){
							theHtml+= dayzEvents[j].type+ " ";
						}

						var tempId= i +"x"+ this.workingDate.getDate() +"x"+ this.workingDate.getMonth() +"x"+ this.workingDate.getFullYear();
						
						
						theHtml+= 'betterTip\" id=\"div'+ tempId+'\">';
						theHtml+= '<a class=\"betterTip\" id=\"a' + tempId +'\"' + 'href=\"$tip' + tempId + '?width=150\" title=\"' + this.workingDate.format("ddd-dd/mm/yyyy") + '\"></a>';
						theHtml+= this.workingDate.getDate();
						theHtml+="</div>";

						theHtml+='<div id=\"tip' + tempId+'\" style=\"display: none\">';
						theHtml+='<div class=\"tipinner\">';

						for(var k=0; k<dayzEvents.length; k++){
							theHtml+= "<p>";
							theHtml+= dayzEvents[k].what;
							if( dayzEvents[k].to){
								theHtml+= "&nbsp;(continues to " + dayzEvents[k].to.format("dd/mm/yy") + ")";
							}else{}
							theHtml+= ".";
							if(dayzEvents[k].where){
								theHtml+= "&nbsp;" + dayzEvents[k].where+".";
							}else{}
							if( dayzEvents[k].who){
								theHtml+= "&nbsp;Incharge(s):"+"&nbsp;" + dayzEvents[k].who+".";
							}else{}
							theHtml+= "</p>";
						}

						theHtml+= "</div></div>";
					}else{
						theHtml+= '\">' + this.workingDate.getDate() + '</div>';
						
					}


					theHtml+= "</td>";
					//console.log(this.workingDate);
					this.workingDate.setDate(this.workingDate.getDate()+1);
					//console.log(this.workingDate.getDate()+"==="+this.workingDate);
				}
				theHtml+="</tr>";
				
			
			} while(this.workingDate <= this.endDate);

			theHtml+="</table>";
			
			return theHtml;

		},

		getNumDayz : function(givenDate){

			var monthLength = this.cal_days_in_month[givenDate.getMonth()];

			if (givenDate.getMonth() == 1) { // February only!
  				if ((givenDate.getYear() % 4 === 0 && givenDate.getYear() % 100 !== 0) || givenDate.getYear() % 400 === 0){
  			  		monthLength = 29;
  				}
			}
			return monthLength;
		
		}

};

