// Load calendar events
window.addEvent("domready",function(){

	var RightReservations = $('RightReservations');
	
	if(RightReservations) {

		var CalendarOpenButton = $$('#oCalendarDateFind img')[0];
		var CalendarOpenText = $$('#oCalendarDateFind #DateIn')[0];
		var fullMonth = ["JAN","FEB","MAR","APR","MAY","JUN","JUL","AUG","SEP","OCT","NOV","DEC"];
		
		if (CalendarOpenText) {
		
			var ReservationSelects = $$('#RightReservations select');
		
			var CalendarOverlay = new Element('div', {
				'styles': {
					'display': 'none',
					'background-color': $('RightReservations').getStyle('background-color'),
					'top': "0px",
					'left': "0px",
					'width': '231px',
					'height': '160px',
					'position': 'absolute',
					'opacity': 0
				},
				'id' : 'oCalendarOverlay'
			});
			
			CalendarOverlay.injectInside($$('body')[0]);
		
			CalendarOpenButton.addEvent("click", function(){ functionCalendarShow(null); });
			CalendarOpenText.addEvent("focus", function(){ functionCalendarShow(null); });
			
			var currentDateItem;
		
			function functionCalendarShow(dateIn) {
			
				// IE Fix Hide selects becuase they overlay
				//if(document.all) {
					ReservationSelects.setStyle('visibility', 'hidden');
				//}
			
				//if(currentDateItem) { currentDateItem.setStyle('background-color', $('MainContainer').getStyle('background-color')); }
			
				// Get calendar select template
				var xmlloc = "/templates/calendar.html";
				var xmlAjax = new Ajax(xmlloc, { method: 'get', onComplete: function(text, xmldata) {
					
					// Very current date
					var veryCurntDate;
					veryCurntDate = new Date();
					
					// Get current date or if not then dateIn
					var curntDate;
					
					if ((!dateIn) && (CalendarOpenText.value != '')) {
						dateIn = CalendarOpenText.value;
					}
					
					if (!dateIn) {
						curntDate = new Date();
					} else {
						curntDate = new Date(dateIn);
					}
					
					// Get Dates part
					firstDate = new Date(curntDate.getFullYear(),curntDate.getMonth(),1);
					lastMonth = new Date(curntDate.getFullYear(),curntDate.getMonth()-1,1);
					nextMonth = new Date(curntDate.getFullYear(),curntDate.getMonth()+1,1);
					offSet   = firstDate.getDay() + 1;
					lastSlot = offSet + ((nextMonth - firstDate)/(3600*24*1000)) - 1;
					lastSlot = Math.round(lastSlot);
					calWeeks = Math.ceil(lastSlot/7);
					calDays  = calWeeks * 7;
					day = 1;
									
					// Do the date field parts
					var calendarFieldsHTML;
					calendarFieldsHTML = '<tr class="CalendarSelectRow">';
								
					for (i=1; i<=calDays; i++) {
						if (i >= offSet && i <= lastSlot) {
							calendarFieldsHTML = calendarFieldsHTML + '<td class="CalendarSelectDay"><div>' + day + '</div></td>';
							day++;
						} else {
							calendarFieldsHTML = calendarFieldsHTML + '<td>&nbsp;</td>';
						}
						if (i%7 == 0) {
					    	calendarFieldsHTML = calendarFieldsHTML + '</tr><tr class="CalendarSelectRow">';
						}
					}
					
					calendarFieldsHTML = calendarFieldsHTML + '</tr>';
					
					text = text.replace('xxxCALENDAROWSxxx', calendarFieldsHTML);
					
					// Fill with calendar template
					CalendarOverlay.innerHTML = text;
					
					// Get position
					CalendarOverlay.setStyle('top', RightReservations.getTop()+40);
					CalendarOverlay.setStyle('left', RightReservations.getLeft()+10);
					
					var CalendarSelectMonth = $('oCalendarSelectMonth');
					var CalendarSelectLastMonth = $('oCalendarSelectLastMonth');
					var CalendarSelectNextMonth = $('oCalendarSelectNextMonth');
					
					// Fill the header
					CalendarSelectMonth.innerHTML = '<div>' + fullMonth[curntDate.getMonth()] + '&nbsp;' + curntDate.getFullYear() + '</div>';
					
					// Dec Fix
					if((curntDate.getMonth()-1) < 0) {
						CalendarSelectLastMonth.innerHTML = '<div>' + '&lt;&nbsp;' + fullMonth[11] + '</div>';
					} else {
						CalendarSelectLastMonth.innerHTML = '<div>' + '&lt;&nbsp;' + fullMonth[(curntDate.getMonth()-1)] + '</div>';
					}
					
					// Jan fix
					if((curntDate.getMonth()+1) > 11) {
						CalendarSelectNextMonth.innerHTML = '<div>' + fullMonth[0] + '&nbsp;&gt;' + '</div>';
					} else {
						CalendarSelectNextMonth.innerHTML = '<div>' + fullMonth[(curntDate.getMonth()+1)] + '&nbsp;&gt;' + '</div>';
					}
					
					// Mouseover colors
					var strMouseEnterColor = $('MainContainer').getStyle('background-color');
					var strMouseLeaveColor = CalendarSelectNextMonth.getStyle('background-color');
					
					var newDate = new Date(curntDate.getMonth + 1);
					
					var lastDate = ((lastMonth.getMonth()+1) + "/1/" + lastMonth.getFullYear());			
					var nextDate = ((nextMonth.getMonth()+1) + "/1/" + nextMonth.getFullYear());
					
					// Dont allow them to select a date that below current date	
					if ((lastMonth.getMonth() < veryCurntDate.getMonth()) && (lastMonth.getFullYear() == veryCurntDate.getFullYear())) {
		
						CalendarSelectLastMonth.getFirst().setStyle('opacity', 0.5);				
						
					} else {
						
						CalendarSelectLastMonth.addEvent("click",function(){
							functionCalendarShow(lastDate);
						});
						
						CalendarSelectLastMonth.addEvent("mouseenter",function(){
							CalendarSelectLastMonth.setStyle('background-color', strMouseEnterColor);
						});
						CalendarSelectLastMonth.addEvent("mouseleave",function(){
							CalendarSelectLastMonth.setStyle('background-color', strMouseLeaveColor);
						});		
					}
					
					// Dont allow them to select a date 2 years ahead
					if (nextMonth.getFullYear() == veryCurntDate.getFullYear()+2) {
						
						CalendarSelectNextMonth.getFirst().setStyle('opacity', 0.5);		
					
					} else {
					
						CalendarSelectNextMonth.addEvent("click",function(){
							functionCalendarShow(nextDate);	
						});
					
						CalendarSelectNextMonth.addEvent("mouseenter",function(){
							CalendarSelectNextMonth.setStyle('background-color', strMouseEnterColor);
						});
						CalendarSelectNextMonth.addEvent("mouseleave",function(){
							CalendarSelectNextMonth.setStyle('background-color', strMouseLeaveColor);
						});	
					
					}
							
					// Set day clicks and mouseovers			
					$$('.CalendarSelectDay').each(function(dayItem, i) {
						
						// Mark out dates behind
						if ((curntDate.getMonth() == veryCurntDate.getMonth()) && (curntDate.getFullYear() == veryCurntDate.getFullYear()) && (dayItem.getFirst().innerHTML < veryCurntDate.getDate())) {
							
							//alert(dayItem.getFirst().innerHTML);
							//alert(veryCurntDate.getDay());
							
							dayItem.getFirst().setStyle('opacity', 0.5);		
							
						} else {
					
							dayItem.addEvent("mouseenter",function(){
								if(currentDateItem) { currentDateItem.setStyle('background-color', strMouseLeaveColor); }
								dayItem.setStyle('background-color', strMouseEnterColor);
							});
							dayItem.addEvent("mouseleave",function(){
								dayItem.setStyle('background-color', strMouseLeaveColor);
							});		
							
							dayItem.addEvent("click",function(){
													
								$('DateIn').value = (curntDate.getMonth()+1) + '/' + dayItem.getFirst().innerHTML + '/' + curntDate.getFullYear();
								
								var fxhide = CalendarOverlay.effects({duration: 500, transition: Fx.Transitions.Quart.easeOut, onComplete : function() {
									
									ReservationSelects.setStyle('visibility', 'visible');
		
								}});
								fxhide.start({'opacity': 0});
								
								currentDateItem = dayItem;
								
							});
							
						}
				
					});
					
					// Show calendar
					if(CalendarOverlay.style.display != 'block') {
						CalendarOverlay.style.display = 'block';
						var fxshow = CalendarOverlay.effects({duration: 500, transition: Fx.Transitions.Quart.easeOut});
						fxshow.start({'opacity': 1.0});
					} else {
						var fxshow = CalendarOverlay.effects({duration: 500, transition: Fx.Transitions.Quart.easeOut});
						fxshow.start({'opacity': 1.0});
					}
						
				} });
				xmlAjax.request();	
					
			}
		
		}
		
	}

	function functionCalendarInit(text) {
			
		$('oCalendarOverlay').innerHTML = text;
		objCalendarOverlay.style.display = 'block';
	
	}
	
});