var numberDays = 1; // will be updates when initialized
var currentLeftDayIndex = 0;
var currentlyMoving = false;
var selectedSlot = '';
function setupArrowClicks() {
	if (numberDays > 3 && currentLeftDayIndex > 0) {
		$('.prev_day').parent('.day_nav').removeClass('off');
		$('.prev_day').click (function (){
			moveDays('prev');			
		});
	} else {
		$('.prev_day').parent('.day_nav').addClass('off');
		$('.prev_day').unbind('click');
	}
	if (numberDays > 3 && currentLeftDayIndex + 4 < numberDays) {
		$('.next_day').parent('.day_nav').removeClass('off');
		$('.next_day').click (function () {
			moveDays('next');
		});
	} else {
		$('.next_day').parent('.day_nav').addClass('off');
		$('.next_day').unbind('click');
	}
}
function moveDays(dir) {
	var newLoc = 0;
	if (currentlyMoving) { return; }
	switch(dir) {
		case 'prev':			
			//console.log($('.days').css('left')+', '+$('.calendar').width());
			newLoc = parseInt($('.days').css('left')) + 188;//(parseInt($('.calendar').width()+7));
		break;
		case 'next':
			newLoc = parseInt($('.days').css('left')) - 188;//(parseInt($('.calendar').width()+7));
		break;
	}
	currentlyMoving = true;
	$('.days').animate(
		{ 'left': newLoc },
		300,
		'easein',
		function () { 
			currentlyMoving = false;
			if (dir == 'prev') { currentLeftDayIndex--; } else { currentLeftDayIndex++; }
			setupArrowClicks();
		}
	);
}
function formatSlotDateTimeAry (slotsAry) {
	var slotsDateAry = {};	
	// loop over available slots
	$.each(slotsAry, function (i, val) {
		// pull the date and time: 2008-08-29T13:30:00
		var dateString = val.start.substring(0,10);
		// create a date array
		if (typeof(slotsDateAry[dateString]) == 'undefined') {
			slotsDateAry[dateString] = {};
		}
		// format the date for display
		slotsDateAry[dateString]['dateDisplay'] = formatDateDisplay(dateString);
		// format the times for display
		if (typeof(slotsDateAry[dateString]['slots']) == 'undefined') {
			slotsDateAry[dateString]['slots'] = [];
		}
		var slotInfo = {
			'slotDisplay': formatSlotDisplay(val.start)+' - '+formatSlotDisplay(val.end)+' PST',
			'slotReport': val.start+','+val.end
		};
		slotsDateAry[dateString]['slots'].push(slotInfo);
	});
	//console.log(slotsDateAry);
	return slotsDateAry;
}
function formatDateDisplay (dateString) {
	// Week Day, Month Day# 
	var theDate = new Date(dateString.substring(0,4), dateString.substring(5,7)-1, dateString.substring(8,10));
	return dayStrings[theDate.getDay()]+', '+monthStrings[theDate.getMonth()]+' '+theDate.getDate();
	// day/month/year	
	//return dateString.substring(5,7)+'/'+dateString.substring(8,10)+'/'+dateString.substring(0,4);
}
function formatSlotDisplay (dateTimeString) {
	// hour:minutes am/pm
	var hour = dateTimeString.substring(11,13);
	var mins = dateTimeString.substring(14,16);
	var ampm = 'AM';
	if (hour >= 12) {
		ampm = 'PM';
		if (hour > 12) { hour -= 12; } 
	}
	return hour+':'+mins+' '+ampm;
}
// write out the calendar tables with selectable time slots
function getSlotsHTML (slotsDateAry) {
	numberDays = 1;
	var slotsHTML = '';
	slotsHTML += '<div class="day_nav"><a href="javascript:;" class="prev_day"></a></div>';
	slotsHTML += '<div class="days_wrapper"><div class="days">';
	$.each(slotsDateAry, function (i,day) {
		numberDays++;
		slotsHTML += '<div class="calendar">';
			slotsHTML += '<div class="current_day">'+day.dateDisplay+'</div>';
				slotsHTML += '<div class="slots">'
					slotsHTML += '<div class="slot"></div>'; // because IE is a POS
					$.each(day.slots, function (j,slot) {
						slotsHTML += '<div class="slot"><a href="javascript:;" class="'+slot.slotReport+'">'+slot.slotDisplay+'</a></div>';
					});
				slotsHTML += '</div>';
			//slotsHTML += '</div>';
		slotsHTML += '</div>';		
	});
	slotsHTML += '<div class="clear_div"></div>';
	slotsHTML += '</div></div>';
	slotsHTML += '<div class="day_nav"><a href="javascript:;" class="next_day"></a></div>';
	//console.log(slotsHTML);
	return slotsHTML;
}
var dayStrings = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
var monthStrings = ['Jan.','Feb.','Mar.','Apr.','May.','Jun.','Jul.','Aug.','Sep.','Oct.','Nov.','Dec.'];
