/*
 * 
 */

function AvailabilityCal(){
	/* 
	 * Parameters: number of months, display mode (both optional, defaults to 12 month full calendar view)
	 * */
  this.numMonths = (typeof(arguments[0]) == 'number' ? arguments[0] : 12);
  this.displayMode = (typeof(arguments[1]) == 'string' ? arguments[1] : 'availCal');
  this.todaysDate = new Date();
  this.firstMonth = this.todaysDate.getMonth() > 3 ? this.todaysDate.getMonth() : 4;
  this.workingYear = this.setWorkingYear(this.todaysDate);
  this.workingMonth = this.setWorkingMonth(new Date(this.todaysDate.getFullYear(), this.firstMonth, 1)); //this.todaysDate.getMonth()
  this.workingDay = 1;
  this.rtnStr = '';
  this.reservations = {};
  var me = this;

  $.getJSON("reservationstatus.php", function(json){
	  me.drawCal(json);
  });  
}

AvailabilityCal.prototype.drawCal = function(data){
  this.reservations.dates = data;
  this.reservations.currentIndex = -1;
  this.reservations.calClass = ' availableDay';
  this.reservations.resLength = 0;
  this.loadNextReservation();
  //console.log(this.reservations);
  if(this.displayMode == 'availCal'){

	  this.rtnStr += '<div id="availCalMain">'
		    + '<h2 id="availCalMainLabel">El Capitan Unit 306 Availability</h3>'
		    + '<div id="availCalLegend">'
		    + '  <div class="colorGuide"><div class="availableDay"></div><h6>Available</h6></div>'
		    + '  <div class="colorGuide"><div class="bookedDay"></div><h6>Booked</h6></div>'
		    + '  <div class="colorGuide"><div class="reservedDay"></div><h6>Held</h6></div>'
		    + '</div>';
	  this.rtnStr += '<div id="availCalMainMonthWrappers">';  
  }
  for(var i = 0; i < this.numMonths; i++){
	this.rtnStr += this.drawMonth();
	if(this.workingMonth.month != 11){
	  this.workingMonth = this.setWorkingMonth(new Date(this.workingYear.year, (this.workingMonth.month + 1), 1));
	}
	else{
	  this.workingYear = this.setWorkingYear(new Date((this.workingYear.year + 1), 0, 1));
	  this.workingMonth = this.setWorkingMonth(new Date(this.workingYear.year, 0, 1));
	}
  }
  if(this.displayMode == 'availCal'){
    this.rtnStr += '</div>'
	    + '</div>';  
  }
  this.printCal();	  
	    
};

AvailabilityCal.prototype.loadNextReservation = function(){
  this.reservations.currentIndex++;
  if(this.reservations.dates != null){
    if(this.reservations.currentIndex < this.reservations.dates.length){
	  this.reservations.nextRes = new Date(this.reservations.dates[this.reservations.currentIndex].startDate * 1000);
      this.reservations.moreToDraw = true;
    }
    else{
	  this.reservations.moreToDraw = false;  
    }
  }
};

AvailabilityCal.prototype.advanceWorkingDay = function(){
	this.workingDay++;	 // console.log(this.reservations.nextRes.getDate() + ' == ' + this.workingDay);
	if(this.reservations.resLength == 0){
  	  this.reservations.calClass = ' availableDay'; 
	  if(this.reservations.moreToDraw){
	    if(this.reservations.nextRes.getFullYear() == this.workingYear.year && this.reservations.nextRes.getMonth() == this.workingMonth.month){
	  
	      if(this.reservations.nextRes.getDate() == this.workingDay){
	  	    this.reservations.calClass = ' ' + this.reservations.dates[this.reservations.currentIndex].status + 'Day';
	  	    this.reservations.resLength = this.reservations.dates[this.reservations.currentIndex].numDays - 1;
	  	    //window.alert('Reservation on : ' + this.reservations.nextRes.toString());	
	  	    this.loadNextReservation();
	  	  }
	  	  else{
	  	  }
	    }
	  }
	}
	else{
		//window.alert(this.reservations.resLength);
		if(this.workingDay != 1){
		  this.reservations.resLength--;
		}
	}
};

AvailabilityCal.prototype.isLeap = function(){
	  year = (arguments[0] == null ? this.workingYear.year : arguments[0]);  
		if(year % 4 == 0){
		    if(year % 100 != 0){
		      return true;
		    }
		    else{
		      if(year % 400 == 0){
		    	  return true;
		      }
		      else{
		    	  return false;
		      }
		    }
		  }
		  else{
			  return false;
		  }
};

AvailabilityCal.prototype.setWorkingMonth = function(pDate){
	//window.alert( pDate.getMonth());
  var tObj = {
	month: 	  pDate.getMonth(),
    numDays: this.numDaysInMonth(pDate),
    firstDay: pDate.getDay()
  };
  
  return tObj;
};

AvailabilityCal.prototype.setWorkingYear = function(pDate){
  var tObj = {
	year: 	  pDate.getFullYear(),
    isLeap: this.isLeap(pDate.getFullYear())
  };
  
  return tObj;
};

AvailabilityCal.prototype.numDaysInMonth = function(pDate){
	month = pDate.getMonth();
	  if(month < 7){
	    if(month % 2 == 0){
	      return 31;
	    }
	    else if(month != 1){
	    	return 30;
	    }
	    else if(!this.isLeap()){
	    	return 28;
	    }
	    else return 29;
	  }
	  else{
	    if(month % 2 == 1) return 31;
	    else return 30;
	  }
		//window.alert("function: month= " + month + " num days in month = " + this.monthDayCount);
};  

AvailabilityCal.prototype.printMonthName = function(){
	var monthNames = new Array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
	return monthNames[arguments[0]];
};

AvailabilityCal.prototype.printDayName = function(){
	  this.dayNames = new Array('Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat');
	  return this.dayNames[arguments[0]];
};

AvailabilityCal.prototype.drawMonth = function(){
	this.workingDay = 0;
	  this.advanceWorkingDay();	
//	window.alert(nextRes.getFullYear());
	var cardinalMonth = ((this.workingMonth.month < this.firstMonth ? 12 : 0) + (this.workingMonth.month - this.firstMonth + 1));
	var rtnStr = '<div id="' + this.displayMode + 'Month' + cardinalMonth + '" class="' + this.displayMode + 'Month' + (this.displayMode == 'availCal' ? ' floatLeft' : '') + (this.displayMode == 'availCal' ? (cardinalMonth % 2 == 0 ? ' noRightMargin' : ' noLeftMargin') : '') + '">'
	 + '<div class="' + this.displayMode + 'MonthLabelWrapper"><h4>' + this.printMonthName(this.workingMonth.month) + ' ' + this.workingYear.year + '</h4></div>'
	 + '<div class="' + this.displayMode + 'DayNamesWrapper">';
	for(var i = 0; i < 7; i++){
		rtnStr += '<div class="' + this.displayMode + 'DayNames floatLeft' + (i == 0 ? ' noLeftMargin' : (i == 6 ? ' noRightMargin' : '')) + '">' + this.printDayName(i) + '</div>';
	}
	rtnStr += '</div>';
	//window.alert(this.printMonthName(this.workingMonth.month)+ ': ' + this.workingMonth.firstDay);
	for(var i = 0; i < 6; i++){
	  rtnStr += this.drawWeek();
	}
	rtnStr += '</div>';
	
	return rtnStr;
};

AvailabilityCal.prototype.drawWeek = function(){
	var rtnStr = '';
	if(this.workingDay > this.workingMonth.numDays){
	  rtnStr += '<div class="' + this.displayMode + 'EmptyWeek">';	
	}
	else{
	  rtnStr += '<div class="' + this.displayMode + 'Week">';	
	  for(var i = 0; i < 7; i++){
	    if((this.workingDay == 1 && this.workingMonth.firstDay > i) || this.workingDay > this.workingMonth.numDays){
		  rtnStr += '<div class="' + this.displayMode + 'Day inActiveDay floatLeft' + (i == 0 ? ' noLeftMargin' : (i == 6 ? ' noRightMargin' : '')) + '"></div>';
	    }
	    else {
	      rtnStr += '<div class="' + this.displayMode + 'Day activeDay floatLeft' + (i == 0 ? ' noLeftMargin' : (i == 6 ? ' noRightMargin' : '')) + this.reservations.calClass + '"><div class="' + this.displayMode + 'Date">' + this.workingDay + '</div></div>';
		  this.advanceWorkingDay();	
	    }
	  }
    }
	
	rtnStr += '</div>';
	
	return rtnStr;
};

AvailabilityCal.prototype.printCal = function(){
	switch(this.displayMode){
	  case 'availCal':
	    $("#availCalendarTarget").html(this.rtnStr);
	    break;
	  case 'previewCal':
		$("#miniCal").html(this.rtnStr);
	}
};


	
/*=============================================================================================================================
 * 
 * ============================================================================================================================
 */

	function arrayIndex(pValue, pArray){
	  for(var i = 0; i < pArray.length; i++){
	    if(pArray[i] == pValue) return i;
	  }
	  return false;
	}
	

