//////////////////////////////////////////////////////////////////////////////////
// CalendarScroller manages the slideshow effect for the six home page mini-calendars
//////////////////////////////////////////////////////////////////////////////////

function CalendarScroller(calendarScroller){
	this.calendarScroller = calendarScroller;
	this.init();
}

CalendarScroller.prototype.init = function(){
	me = this;
	
	this.calendarVisible = $(this.calendarScroller).find('.calendarVisible').get(0);
	this.calendarContainers = $(this.calendarScroller).find('.calendarContainer');
	
	this.width = $(this.calendarScroller).width();
	this.height = $(this.calendarScroller).height();
	
	$(this.calendarContainers).each(function(){
		$(this).width(me.width);
	});
	
	$(this.calendarVisible).width(me.width * this.calendarContainers.length);
	$(this.calendarScroller).width(this.width); // need to force this back to size for IE6
	
	// hide existing prev/next buttons (.NET controls)
	$('.prevMonth').hide();
	$('.nextMonth').hide();
	
	// add prev/next buttons
	this.prevBtn = document.createElement('a');
	this.prevBtn.innerHTML = 'Prev';
	this.calendarScroller.parentNode.appendChild(this.prevBtn);
	$(this.prevBtn).click(function(){
		me.prevSlide();
	});
	
	this.nextBtn = document.createElement('a');
	this.nextBtn.innerHTML = 'Next';
	this.calendarScroller.parentNode.appendChild(this.nextBtn);
	$(this.nextBtn).click(function(){
		me.nextSlide();
	});
	
	// class manipulation has to happen after the nodes have been added to the DOM or IE won't recognize them for some reason
	$(this.prevBtn).addClass('prevMonth');
	$(this.nextBtn).addClass('nextMonth');
	
	// set defaults
	this.index = 0;
	this.margin = 0;
	$(this.prevBtn).hide();
	
}

CalendarScroller.prototype.prevSlide = function(){
	var me = this;
	if (this.index > 0) {
		this.index--;
		this.margin -= this.width;
		$(this.calendarVisible).animate({marginLeft: '-' + this.margin + 'px'}, 300, '', function(){
			me.checkButtonState();
		});
	}
}

CalendarScroller.prototype.nextSlide = function(){
	var me = this;
	if (this.index < this.calendarContainers.length - 1) {
		this.index++;
		this.margin += this.width;
		$(this.calendarVisible).animate({marginLeft: '-' + this.margin + 'px'}, 300, '', function(){
			me.checkButtonState();
		});
	}
}

CalendarScroller.prototype.checkButtonState = function(){
	if (this.index == 0)
		$(this.prevBtn).hide();
	else
		$(this.prevBtn).show();
	if (this.index == this.calendarContainers.length - 1)
		$(this.nextBtn).hide();
	else
		$(this.nextBtn).show();
}

//////////////////////////////////////////////////////////////////////////////////
// MiniCalendarHoverManager manages event detail hover pop-ups on home page mini-calendars
//////////////////////////////////////////////////////////////////////////////////

function MiniCalendarHoverManager(calendarDates) {
	this.calendarDates = calendarDates;
	this.calendarEvents = new Array();
	for (var i=0; i < this.calendarDates.length; i++) {
		this.calendarDates[i] = new MiniCalendarHoverItem(this.calendarDates[i],this);
	}
}

function MiniCalendarHoverItem(calendarDate, mgr) {
	this.calendarDate = calendarDate;
	this.mgr = mgr;
	var me = this;
	if (this.event = $("#calendarEvent" + this.calendarDate.title.replace(' ','')).get(0)) {	
		this.mgr.calendarEvents.push(this.event);
		// set calendarDate mousover event
		$(this.calendarDate).mouseover(function(){
			me.calculatePosition();
			me.showEvent();
		});
		// set calendarDate mouseout event
		$(this.calendarDate).mouseout(function(){
			me.hideEvent();
		});
	}
}

MiniCalendarHoverItem.prototype.calculatePosition = function() {

	this.offsetLeft = ($(this.calendarDate).offset()).left;
	this.offsetTop = ($(this.calendarDate).offset()).top;
	this.calendarOffsetLeft = ($('.homeCalendar').offset()).left;
	this.calendarOffsetTop = ($('.homeCalendar').offset()).top;
	this.width = $(this.event).width();
	this.height = $(this.event).height();
	this.xPos = this.offsetLeft - this.calendarOffsetLeft - (this.width/2) + 15;
	this.yPos =  this.offsetTop - this.calendarOffsetTop - this.height - 5;
	
	$(this.event).css({
		'top' : this.yPos,
		'left' : this.xPos
	});
}

MiniCalendarHoverItem.prototype.showEvent = function() {
	var me = this;
	clearTimeout(this.mgr.delayTimer);
	// hide any visible events before displaying a new one
	$(this.mgr.calendarEvents).each(function(){
		$(this).hide();
	});
	// set mouseover/mouseout events for event (b/c it will disappear when you leave the calendarDate)
	$(this.event).mouseover(function(){
		clearTimeout(me.mgr.delayTimer);
		$(this).show();
	});
	$(this.event).mouseout(function(){
		me.hideEvent();
	});
	$(this.event).show();
}

MiniCalendarHoverItem.prototype.hideEvent = function(){
	var me = this;
	clearTimeout(this.mgr.delayTimer);
	// set a slight delay on the hide
	this.mgr.delayTimer = setTimeout( function() {
		$(me.event).hide();
	}, 200)
}

//////////////////////////////////////////////////////////////////////////////////

$(document).ready(function(){
	
	$('#calendarScroller').each(function(){
		var cal = new CalendarScroller(this);
	});
	
	// initialize home page mini-calendar rollovers
	var miniCalendarMgr = new MiniCalendarHoverManager($(".homeCalendarEvent a"));
	
	// disable click event for all mini-calendar dates
	$(".day a").click(function(){
		return false;
	});
	
});