
ALL_CALENDARS = new Object();

function Calendar(id){
	
	this.id = id;
	ALL_CALENDARS[id] = this;
	this.meetingCache = new Object();
	this.expandCache = new Object();
	this.monthCache = new Object();
	this.currentWeek = 0;
	this.currentMonth = 0;
	this.root = null;
	
	this.expanded = null;
	this.expandedWeek = null;
	this.expanding = false;
	
	this.activeMonths = []
	this.inactiveMonths = []	
	this.sliding = false;
	
}


function getRow(calendar, rowNumber, visible){
	
	var weekInfo = null;
	
	if (!(("" + rowNumber) in calendar.meetingCache)) {
		loadRowFromAjax(calendar, rowNumber);
		var loadString = calendar.loadTemplate.replace("${row-num}", rowNumber)
									          .replace("${display}", visible ? "table-row" : "none");
											 
		return loadString;
	}
	else
		weekInfo = calendar.meetingCache["" + rowNumber];
			
	var allDays = '';
	
 	for(var i = 0; i < 7; i++){
		
		var dayInfo = weekInfo[i];
		
		var template = i == 6 ? calendar.lastDayTemplate : calendar.dayTemplate;
				
		var dayString = template.replace(/\$\{day-number\}/g, dayInfo.dayNumber)
								.replace("${month-num}", dayInfo.monthNumber)
								.replace(/\$\{month-abbr\}/g, dayInfo.monthAbbr)
								.replace("${is-today}", dayInfo.isToday ? 't' : 'notT')
								
								.replace("${more-comm}", dayInfo.moreComm ? 'block' : 'none')
								.replace("${event-names}", dayInfo.eventNames)
								.replace("${event-big}", dayInfo.eventBig)
								.replace(/\$\{day-suff\}/g, dayInfo.daySuffix)
								.replace(/\$\{date-suff\}/g, dayInfo.dateSuffix)
								.replace(/\$\{row-suff\}/g, dayInfo.rowSuffix)
								.replace(/\$\{cal-id\}/g, calendar.id);
								
		allDays += dayString;
	}
	
	allDays = calendar.weekTemplate.replace("${all-days}", allDays)
						           .replace("${display}", visible ? "table-row" : "none");
	
	return allDays;
}

function addToCache(weekStruct){
	
	var calendar = ALL_CALENDARS[weekStruct.calId];

	calendar.meetingCache["" + weekStruct.weekNum] = weekStruct.weekInfo;
			
	//alert("WN: " + weekStruct.weekNum);
	
	var oldRow = calendar.root.find('.weekRow' + weekStruct.weekNum);
	if(oldRow.length == 0) return;
	
	var visible = !(oldRow.css("display") == "none");
	var renderedRow = getRow(calendar, weekStruct.weekNum, visible);
			
	oldRow.replaceWith(renderedRow);

}

function loadRowFromAjax(calendar, rowNum){
	$.ajax({dataType : 'script', url : ('/calendars/cal_get_json_dates/' + calendar.id + '?row_number=' + rowNum) });
}

function syncMonthBar(calendar, newMonthBoxes, forward){

	var monthNameBar = calendar.root.find('.monthNameBar')
	var monthNameAnchor = monthNameBar.find(forward ? '.endMonthName' : '.startMonthName')
	var monthColorBar = calendar.root.find('.monthColorBar')
	var monthColorAnchor = monthColorBar.find(forward ? '.endMonthColor' : '.startMonthColor')
		
	if(forward) monthNameAnchor.before(newMonthBoxes.monthBox)
	else monthNameAnchor.after(newMonthBoxes.monthBox)
	if(forward) monthColorAnchor.before(newMonthBoxes.monthColorBox)
	else monthColorAnchor.after(newMonthBoxes.monthColorBox)

	monthNameBar.css("left", ((-calendar.monthBarPos + calendar.nameCenterOffset) * calendar.increment) + "em")
	monthColorBar.css("left", (-calendar.monthBarPos * calendar.increment) + "em")
}


function expandMonths(calendar, forward, numMonths){
		
	if(!(("" + 0) in calendar.monthCache)){
	
		// We need to initialize our first month correctly to start
		calendar.monthBarPos = calendar.firstDayNum - 1
				
		monthBoxes = getMonth(calendar, !forward, 0)	

		syncMonthBar(calendar, monthBoxes, true)
		
		calendar.monthsStart = 0
		calendar.monthsEnd = 0
		
		calendar.currentMonthDay = calendar.firstDayNum
		calendar.currentMonth = 0
		
	}
	
	var startMonth = (forward ? calendar.monthsEnd + 1: calendar.monthsStart - 1)
	var endMonth = (forward ? startMonth + numMonths : startMonth - numMonths)
			
	for(var month = startMonth; 
		(forward && month < endMonth) || (!forward && month > endMonth);
		month = forward ? month + 1 : month - 1)
	{		
		//alert("adding" + month)
	
		monthBoxes = getMonth(calendar, !forward, month)
				
		if(!forward){
			calendar.monthBarPos += 31
		}
		
		syncMonthBar(calendar, monthBoxes, forward)
	}
	
	if(forward) calendar.monthsEnd += numMonths
	else calendar.monthsStart -= numMonths
}

function getMonth(calendar, inFront, monthId){
	
	var monthInfo = null
	
	if(!(("" + monthId) in calendar.monthCache)) {
	
		var loadMonthString = calendar.loadMonthTemplate.replace('${month-width}', 31 * calendar.increment)
														.replace('${month-id}', monthId)
		var loadMonthColorString = calendar.loadMonthColorTemplate.replace('${month-width}', 31 * calendar.increment)
																  .replace('${month-id}', monthId)
				
		var monthReturn = new Object()
		monthReturn.monthBox = loadMonthString
		monthReturn.monthColorBox = loadMonthColorString
		
		loadMonthFromAjax(calendar, inFront, monthId)
		
		calendar.monthCache["" + monthId] = true
		
		return monthReturn
	}
	else{
		monthInfo = calendar.monthCache["" + monthId]
		
		var template = calendar.monthTemplate
		
		var monthString = template.replace('${month-num}', monthInfo.monthNumber)
								  .replace('${month-name}', monthInfo.monthName)
								  .replace('${month-id}', monthId)
								  .replace('${month-active}', /* isActive */ false ? 'monthNameLinkActive' : '')
								  .replace('${year}', monthInfo.year)
								  .replace('${month-width}', monthInfo.numDays * calendar.increment)
								  .replace('${cal-id}', calendar.id)
								  .replace('${week-start}', monthInfo.startWeek)
		
		template = calendar.monthColorTemplate
		
		var monthColorString = template.replace('${month-num}', monthInfo.monthNumber)
									   .replace('${month-id}', monthId)
								  	   .replace('${month-width}', monthInfo.numDays * calendar.increment)
		
		var monthReturn = new Object()
		monthReturn.monthBox = monthString
		monthReturn.monthColorBox = monthColorString
		
		return monthReturn
	}
}

function addToBar(monthStruct){
	
	var calendar = ALL_CALENDARS[monthStruct.calId]
	
	calendar.monthCache["" + monthStruct.monthId] = monthStruct.monthInfo
	
	monthInfo = monthStruct.monthInfo	
	var monthId = monthStruct.monthId
	
	//alert(monthId)
	
	var monthNameBar = calendar.root.find('.monthNameBar')
	var monthColorBar = calendar.root.find('.monthColorBar')

	oldMonthName = monthNameBar.find('.monthNameBoxId' + monthId)
	oldMonthColor = monthColorBar.find('.monthColorId' + monthId)

	var newMonthBoxes = getMonth(calendar, false, monthId)
	
	if(monthStruct.inFront == "true"){
	
		var dayDiff = 31 - monthStruct.monthInfo.numDays
		calendar.monthBarPos -= dayDiff
		
		//alert(dayDiff)
		
		monthNameBar.css("left", ((-calendar.monthBarPos + calendar.nameCenterOffset) * calendar.increment) + "em")
		monthColorBar.css("left", (-calendar.monthBarPos * calendar.increment) + "em")
	}
		
	oldMonthName.replaceWith(newMonthBoxes.monthBox)
	oldMonthColor.replaceWith(newMonthBoxes.monthColorBox)
	
	
	var startWeekInDisplay = 
		(monthInfo.startWeek >= calendar.currentWeek && monthInfo.startWeek <= calendar.currentWeek + 4)
	var endWeekInDisplay = 
		(monthInfo.endWeek >= calendar.currentWeek && monthInfo.endWeek <= calendar.currentWeek + 4)
	
		
	if(startWeekInDisplay || endWeekInDisplay){
			
		calendar.activeMonths.push(monthId)
		
			
		if(calendar.currentMonth == null || calendar.currentMonth > monthId){
			calendar.currentMonth = monthId
		}
		
		calendar.root.find('.monthNameBoxId' + monthId + ' > * ').addClass('monthNameLinkActive')
	}
	
}

function loadMonthFromAjax(calendar, inFront, monthId){
	$.ajax({ dataType : 'script', url : ('/calendars/cal_get_json_month/' + calendar.id + '?' +
										 'month_id=' + monthId + '&' + 
										 'in_front=' + inFront) })
}

function weekDownStart(calId){
	var calendar = ALL_CALENDARS[calId];
	
	toWeekStart(calId, calendar.currentWeek + 1);
}

function weekUpStart(calId){
	
	var calendar = ALL_CALENDARS[calId];
	
	toWeekStart(calId, calendar.currentWeek - 1)
	
}

function toWeekStart(calId, weekNum){
		
	var calendar = ALL_CALENDARS[calId];
	
	if(calendar.currentWeek == weekNum) return;
	if(calendar.expanding || calendar.sliding) return;
		
	if(calendar.expanded != null)
		restoreCalendarFromDate(calendar, calendar.expanded);
	if(calendar.expandedRow != null)
		restoreCalendarFromDate(calendar, calendar.expandedRow);
	calendar.sliding = true;
			
	var weekDiff = weekNum - calendar.currentWeek;
	var forward = weekDiff < 0;
	
	calendar.root.find('.weekRow:' + (forward ? 'gt(' + (6 + weekDiff) + ')' : 'lt(' + weekDiff + ')')).remove();
	if (Math.abs(weekDiff) < 6) {
		calendar.root.find('.weekRow:' + (forward ? 'last' : 'first')).css('display', 'none');
		calendar.root.find('.weekRow:' + (forward ? 'first' : 'last')).css('display', '');
	}
	
	var newWeeks = Math.abs(weekDiff) > 7 ? 7 : Math.abs(weekDiff);
	var startNewWeeks = forward ? weekNum - 1 : weekNum + 6 - newWeeks;
	var loading = forward ? calendar.root.find('.loadingTopRow') : calendar.root.find('.loadingBottomRow');
	
	for(var i = 0; i < newWeeks; i++){
		var hideWeek = i == newWeeks - 1 || (newWeeks == 7 && i == 0);
		
		//if(!forward) alert(startNewWeeks + i + ":" + weekDiff);
		
		var replacementWeek = getRow(calendar, forward ? startNewWeeks + ((newWeeks - 1) - i) : startNewWeeks + i, !hideWeek);
		
		if(forward) loading.after(replacementWeek);
		else loading.before(replacementWeek);
	}
	
	calendar.currentWeek = weekNum
	
	var maxShiftedMonths = (Math.abs(weekDiff) + 7) / 4
	calendar.inactiveMonths = calendar.activeMonths
	calendar.activeMonths = []
	
	//alert(calendar.monthCache["" + calendar.currentMonth].monthName)
		
	var newCurrentMonth = null
		
	for(var i = -1; i <= maxShiftedMonths; i++){
	
		var monthId = 
			(weekDiff < 0 ? calendar.currentMonth - i :
							calendar.currentMonth + i)
							
		if(!(("" + monthId) in calendar.monthCache) || calendar.monthCache["" + monthId] == true)
			continue
							
		var monthInfo = calendar.monthCache["" + monthId]
		
		//alert("Start week: " + monthInfo.monthName + " " + monthInfo.startWeek + " / " + calendar.currentWeek)
		
		var startWeekInDisplay = 
			(monthInfo.startWeek >= calendar.currentWeek && monthInfo.startWeek <= calendar.currentWeek + 4)
		var endWeekInDisplay = 
			(monthInfo.endWeek >= calendar.currentWeek && monthInfo.endWeek <= calendar.currentWeek + 4)
		
		if(startWeekInDisplay || endWeekInDisplay){
		
			calendar.activeMonths.push(monthId)
			
			if(newCurrentMonth == null || newCurrentMonth > monthId)
				newCurrentMonth = monthId
		}
	}
	
	calendar.currentMonth = newCurrentMonth
	
	var extraForwardMonths = 
		(calendar.currentMonth + calendar.monthsAhead) - calendar.monthsEnd
	var extraBehindMonths = 
		calendar.monthsStart - (calendar.currentMonth - calendar.monthsBehind)
	
	//alert(extraForwardMonths)
	
	if(extraForwardMonths > 0)
		expandMonths(calendar, true, extraForwardMonths)
	if(extraBehindMonths > 0)
		expandMonths(calendar, false, extraBehindMonths)
			
	//alert("New Month: " + calendar.monthCache["" + calendar.currentMonth].monthName)
	
	//for(var i = 0; i < calendar.activeMonths.length; i++){
	//	alert("Active Month: " + calendar.monthCache["" + calendar.activeMonths[i]].monthName)
	//}
	
	setTimeout(function(){ fixMonthBar(calendar, 7 * weekDiff); }, 200);
}

function fixMonthBar(calendar, offset){
	
	// USE calendar.activeMonths and Slide!
	
	//offset = getNewMonths(calendar, offset);
		
	calendar.monthBarPos += offset;
	
	for(var i = 0; i < calendar.inactiveMonths.length; i++){
		var monthId = calendar.inactiveMonths[i]
		calendar.root.find('.monthNameBoxId' + monthId + ' > * ').removeClass('monthNameLinkActive')
	}
			
	for (var i = 0; i < calendar.activeMonths.length; i++) {
		var monthId = calendar.activeMonths[i]
		calendar.root.find('.monthNameBoxId' + monthId + ' > * ').addClass('monthNameLinkActive')
	}
	
	if(calendar.liteMode){
		calendar.root.find('.monthNameBar').css("left", ((-calendar.monthBarPos + calendar.nameCenterOffset) * calendar.increment) + "em");
		calendar.root.find('.monthColorBar').css("left", (-calendar.monthBarPos * calendar.increment) + "em");
	}
	else{
		calendar.root.find('.monthNameBar').animate({ left : ((-calendar.monthBarPos + calendar.nameCenterOffset) * calendar.increment) + "em" }, {queue : false, duration : 1000 })
		calendar.root.find('.monthColorBar').animate({ left : (-calendar.monthBarPos * calendar.increment) + "em" }, {queue : false, duration : 1000 })
	}
	
	calendar.sliding = false;
}

function loadingWeekUp(){
	restoreCalendar();
	$('#calendarFrame .weekRow:eq(4)').css("display", "none");
	$('#calendarFrame #loadingTop').css("display", "block");
	
	fixMonthBar(-7);
}

function loadedWeekUp(){

	$('#calendarFrame #loadingTop').css("display", "none");
	
}

function CachedData(calendar, rowString, dateString, dayString){
	
	this.calendar = calendar;
	
	var dayBoxSel = ".dayBox" + dateString
	var dayColSel = ".dayBox" + dayString
	var dayRowSel = ".dayBoxR" + rowString
	var dayNameSel = ".dayNameBox" + dayString
	
	var numberBoxSel = ".numberBox"
	var numberSel = numberBoxSel + " .numberText"
	var innerBoxSel = ".dayInnerBox"
	
	this.eventSel = ".eventBox"
	this.eventSmallSel = ".eventInnerBox"
	this.eventBigSel = ".eventBigBox"		
	this.numberBoxSel = numberBoxSel
	this.numberSel = numberSel
	this.innerBoxSel = innerBoxSel
	
	this.rowString = rowString
	this.dateString = dateString
	this.dayString = dayString
	
	this.allDayBoxes = calendar.root.find(".dayBox")
	this.dayBox = calendar.root.find(dayBoxSel)
	this.rowDayBoxes = calendar.root.find(dayRowSel)
	this.colDayBoxes = calendar.root.find(dayColSel)
	this.otherDayBoxes = calendar.root.find(".dayBox:not(" + dayBoxSel + ")")
	this.otherRowDayBoxes = calendar.root.find(".dayBox:not(" + dayRowSel + ")")
	this.otherColDayBoxes = calendar.root.find(".dayBox:not(" + dayColSel + ")")
	
	this.allDayLabels = calendar.root.find(".dayNameBox")
	this.dayLabel = calendar.root.find(dayNameSel)
	this.otherDayLabels = calendar.root.find(".dayNameBox:not(" + dayNameSel + ")")
	
	this.allWeekButtons = calendar.root.find(".rowSelect")
	this.weekButton = calendar.root.find(".rowSelect" + rowString)
	this.otherWeekButtons =  calendar.root.find(".rowSelect:not(.rowSelect" + rowString + ")")
		
	
		
		
		
	this.makeAllOtherRowNumbersSmall = function makeAllOtherRowNumbersSmall(){
		this.makeNumbersSmall(this.otherRowDayBoxes)
	}
	
	this.makeAllOtherNumbersSmall = function makeAllOtherNumbersSmall(){
		this.makeNumbersSmall(this.otherDayBoxes)
	}
	
	this.makeCurrentNumberLarge = function makeCurrentNumberLarge(){
		this.makeNumbersLarge(this.dayBox)
	}
	
	this.makeCurrentRowLarge = function makeCurrentRowLarge(){
		this.makeNumbersLarge(this.rowDayBoxes)
	}
	
	this.makeAllNumbersNormal = function makeAllNumbersNormal(){
		this.makeNumbersNormal(this.allDayBoxes)
	}
	
	this.makeCurrentNumberSelected = function makeCurrentNumberSelected(){
		this.makeNumbersSelected(this.dayBox)
	}
	
	
	
	
	
	this.hideEvents = function hideEvents(boxes){
		boxes.find(this.eventSel).css("display", "none")
	}
	
	this.showBigEvents = function showBigEvents(boxes){
		boxes.find(this.eventSel).css("display", "block")
		boxes.find(this.eventSmallSel).css("display", "none")
		boxes.find(this.eventBigSel).css("display", "block")
	}
	
	this.showSmallEvents = function showSmallEvents(boxes){
		boxes.find(this.eventSel).css("display", "block")
		boxes.find(this.eventSmallSel).css("display", "block")
		boxes.find(this.eventBigSel).css("display", "none")
	}
	
	
	
	
	this.getNumberBoxes = function(dayBoxes){
		return dayBoxes.find(this.numberBoxSel)
	}
		
	this.makeNumbersSelected = function(dayBoxes){
		var numberBoxes = this.getNumberBoxes(dayBoxes)
		numberBoxes.removeClass('numberBoxSmall numberBoxLarge')
		numberBoxes.addClass('numberBoxSel')
	}
	
	this.makeNumbersSmall = function(dayBoxes){
		var numberBoxes = this.getNumberBoxes(dayBoxes)
		numberBoxes.removeClass('numberBoxSel numberBoxLarge')
		numberBoxes.addClass('numberBoxSmall')
	}
	
	this.makeNumbersLarge = function(dayBoxes){
		var numberBoxes = this.getNumberBoxes(dayBoxes)
		numberBoxes.removeClass('numberBoxSmall numberBoxSel')
		numberBoxes.addClass('numberBoxLarge')
	}
	
	this.makeNumbersNormal = function(dayBoxes){
		var numberBoxes = this.getNumberBoxes(dayBoxes)
		numberBoxes.removeClass('numberBoxSmall numberBoxLarge numberBoxSel')
	}
	
	
	
	
	this.makeDayLabelsSmall = function makeDayLabelsSmall(labels){
		labels.addClass('dayNameBoxSmall')
	}
	
	this.makeDayLabelsLarge = function makeDayLabelsLarge(labels){
		labels.removeClass('dayNameBoxSmall')
	}
	
	
	
	
	this.makeWeekButtonsSelected = function(button){
		button.addClass('rowSelectPushed')
	}
	
	this.makeWeekButtonsNormal = function(button){
		button.removeClass('rowSelectPushed')
	}
	
	
	
	this.animExpandDay = function(speed){
		this.otherColDayBoxes.find(this.innerBoxSel).animate({ width : calendar.minDayWidth + "em" }, {queue : false, duration : speed })
		this.otherRowDayBoxes.find(this.innerBoxSel).animate({ height : calendar.minDayHeight + "em" }, {queue : false, duration : speed })
		this.colDayBoxes.find(this.innerBoxSel).animate({ width : calendar.maxDayWidth + "em" }, {queue : false, duration : speed })
		this.rowDayBoxes.find(this.innerBoxSel).animate({ height : calendar.maxDayHeight + "em" }, { queue: false, duration: speed })
	}
	
	this.animExpandWeek = function(speed){	
		this.otherRowDayBoxes.find(this.innerBoxSel).animate({ height : calendar.minDayHeight + "em" }, {queue : false, duration : speed })
		this.rowDayBoxes.find(this.innerBoxSel).animate({ height : calendar.maxDayHeight + "em" }, {queue : false, duration : speed })
		this.allDayBoxes.find(this.innerBoxSel).animate({ width : calendar.dayWidth + "em" }, {queue : false, duration : speed })
	}
	
	this.animRestore = function(speed){
		this.allDayBoxes
			.find(this.innerBoxSel)
				.animate({ width : calendar.dayWidth + "em" }, {queue : false, duration : speed })
				.animate({ height : calendar.dayHeight + "em" }, {queue : false, duration : speed })
	}
	
}



function toggleJumpTo(calId){
	
	var calendar = ALL_CALENDARS[calId];

	var jTo = calendar.root.find('.jumpTo');
	if (jTo.css("display") == "block") {
		jTo.css("display", "none")
	}
	else {
		jTo.css("height", "0em");
		jTo.css("display", "block");
		if(calendar.liteMode){
			jTo.css("height", "2.0em")
		}
		else{
			jTo.animate({ height : "2.0em" }, {queue : false, duration : 400 })
		}
	}
}

function getCached(calendar, rowString, dateString, dayString){
	var cData = calendar.expandCache[dateString]
	//if(undefined != cData) return cData
			
	/* alert("Caching new data..."); */
			
	cData = new CachedData(calendar, rowString, dateString, dayString)
			
	//cache[dateString] = cData
	return cData
}

function unPop(calId){
	
	var calendar = ALL_CALENDARS[calId];
	
	var popup = calendar.root.find('.eventPopup')
	popup.css('display', 'none')

	return
}

function expandCalendar(calId, rowString, dateString, dayString){
	
	var calendar = ALL_CALENDARS[calId];
	
	if(calendar.liteMode){
		var cached = getCached(calendar, rowString, dateString, dayString);
		var html = cached.dayBox.find(cached.eventBigSel).html()
				
		var popup = calendar.root.find('.eventPopup')
		var popupArea = calendar.root.find('.eventPopupArea')	
		var table = calendar.root.find('.calendarTable')
		
		var tableHeight = table.height()
		var tableWidth = table.width()
		var tablePos = table.position()
		var tableTop = tablePos.top
		var tableLeft = tablePos.left
		var popupWidth = tableWidth * 0.65;
		var popupHeight = tableHeight * 0.65;
		
		popup.css('display', 'block')
		popupArea.css('width', popupWidth + 'px');
		popup.css('left', ((tableWidth - popupWidth) / 2 + tableLeft) + 'px');	
		popupArea.css('height', popupHeight + 'px');			
		popup.css('top', ((tableHeight - popupHeight) / 2 + tableTop) + 'px');
		
		popupArea.html(html)
		
		return
	}
	
	if(calendar.expanding || calendar.sliding) return
	calendar.expanding = true
	calendar.expandedRow = null
	
	var cached = getCached(calendar, rowString, dateString, dayString);
	
	cached.makeNumbersSelected(cached.dayBox)
	cached.makeWeekButtonsNormal(cached.allWeekButtons);
	
	if(calendar.expanded != null && cached.dateString == calendar.expanded.dateString){
		cached.hideEvents(cached.dayBox)
		setTimeout(function(){ restoreCalendarFromDate(calendar, cached); }, 1);
		return;
	}
	calendar.expanded = cached
	
	setTimeout( function(){ continueExpand(calendar, cached); }, 1);
}
	
function continueExpand(calendar, cached){
		
	cached.makeDayLabelsLarge(cached.dayLabel)
	cached.makeDayLabelsSmall(cached.otherDayLabels)
	cached.makeNumbersLarge(cached.dayBox)
	cached.makeNumbersSmall(cached.otherDayBoxes)
	cached.hideEvents(cached.otherDayBoxes)
	cached.showBigEvents(cached.dayBox)
	
	cached.animExpandDay(350)
	
	setTimeout(function(){ calendar.expanding = false; }, 400);
}


function restoreCalendarFromDate(calendar, cached){
		
	calendar.expanded = null
	calendar.expandedRow = null
	
	cached.hideEvents(cached.allDayBoxes)
	cached.animRestore(400)
	
	setTimeout(function(){ restoreTheRest(calendar, cached); }, 450)
}

function restoreCalendar(calendar){
	
	if(calendar.expanded == null && calendar.expandedRow == null) return;
	
	speed = 350
	
	$("#calendarFrame .eventBox").css("display", "none")
	$("#calendarFrame .dayBox .dayInnerBox").animate({ width : "7.0em" }, {queue : false, duration : speed })
	$("#calendarFrame .dayBox .dayInnerBox").animate({ height : "4.0em" }, {queue : false, duration : speed })
	
	
	$("#calendarFrame .numberText").css({"font-size" : "1.1em", "color" : "black"})
	$("#calendarFrame .numberBox").css({"background-color" : "white"})
	
	
	/*
	$("#calendarFrame .dayBox").css("width", "14%")
	$("#calendarFrame .dayInnerBox").css("height", "4.0em")
	$("#calendarFrame .eventBox").css("display", "block")
	$("#calendarFrame .plusImage").css("display", "block")
	$("#calendarFrame .minusImage").css("display", "none")
	$("#calendarFrame .eventInnerBox").css("display", "block")
	$("#calendarFrame .eventBigBox").css("display", "none")
	*/
}

/*
var wRest = false
function restoreWidth(){
	wRest = true
	if(wRest && hRest) restoreTheRest()
}

var hRest = false
function restoreHeight(){
	hRest = true
	if(wRest && hRest) restoreTheRest()
}
*/

function restoreTheRest(calendar, cached){
	
	cached.makeDayLabelsLarge(cached.allDayLabels)
	cached.makeNumbersNormal(cached.allDayBoxes)
	cached.showSmallEvents(cached.allDayBoxes)
	
	calendar.expanding = false
}

/* expandedRow = "none" */

function expandRow(calId, rowString, dateString, dayString){
	
	var calendar = ALL_CALENDARS[calId];
	
	if(calendar.liteMode) return;
	
	if(calendar.expanding || calendar.sliding) return
	calendar.expanding = true
	
	var cached = getCached(calendar, rowString, dateString, dayString);
	
	if(calendar.expandedRow != null && calendar.expandedRow.rowString == cached.rowString) {
		calendar.expandedRow = null
		cached.hideEvents(cached.allDayBoxes)
		cached.makeWeekButtonsNormal(cached.allWeekButtons)
		setTimeout(function(){
			restoreCalendarFromDate(calendar, cached);
		}, 1);
		return
	}
	calendar.expandedRow = cached
	
	cached.makeWeekButtonsSelected(cached.weekButton)
	cached.makeWeekButtonsNormal(cached.otherWeekButtons)
	
	setTimeout( function(){ continueExpandRow(calendar, cached); }, 1);
}

function continueExpandRow(calendar, cached){
	
	cached.makeDayLabelsLarge(cached.allDayLabels)
	cached.makeNumbersSmall(cached.otherRowDayBoxes)
	cached.makeNumbersLarge(cached.rowDayBoxes)
	cached.hideEvents(cached.allDayBoxes)
	
	if (calendar.expanded != null) {
		calendar.expanded.animRestore(100)
		calendar.expanded = null
		setTimeout(function(){ cached.animExpandWeek(calendar, 400); }, 100)
		setTimeout(function(){ restoreTheEvents(calendar, cached); }, 400)				
	}
	else{
		cached.animExpandWeek(400)
		setTimeout(function(){ restoreTheEvents(calendar, cached); }, 400)
	}
	
}

function restoreTheEvents(calendar, cached){
	cached.showSmallEvents(cached.rowDayBoxes)
	calendar.expanding = false
}