$(function(){

	$calHead = $('#kcalHead');
	$cal = $('#kcal');
	
	$ajaxLoad = $('<span class="ajaxLoad"><img src="http://www.arc-matson.org/wp-content/themes/default/img/loading-spinner.gif" alt="" /></span>')
					.appendTo('#calendar_wrapper')
						.css({'left':($cal.width()-16)/2,'bottom':($cal.height()-16)/2}); 

	$('.monthnav').live('click', function(e){
		e.preventDefault();
		$nav = $(this);
		var moveDate = $nav.attr('rel').split(',');
		var href = $nav.attr('href');
		var dateupdate = {'month':moveDate[0],'year':moveDate[1]};
		$calHead.fadeTo(250, 0);
		$ajaxLoad.fadeIn(250);
		$cal.fadeTo(250, 0, function(){
			$.post(href,dateupdate, function(r){
				$ajaxLoad.fadeOut(250);
				$calHead.html($('#kcalHead', r).children()).fadeTo(250, 1);
				$cal.html($('#kcal tbody', r)).fadeTo(250, 1);
				$('a.event_day').betterTooltip();
			});
		});
	});

	$('a', $cal).live('click', function(e){
		e.preventDefault();
		var href = $(this).attr('href');
		$eventSlideCont = $('#event_slider_cont'); 
		$eventSlideCont.stop().scrollTo(href,1000,function(){
			var setHeight = 
				$(href).outerHeight() +
				$(href).next().not('.buffer').outerHeight() +
				$(href).next().next().not('.buffer').outerHeight() +
				$(href).next().next().next().not('.buffer').outerHeight() +
				$(href).next().next().next().next().not('.buffer').outerHeight();
			$(href).addClass('visible').siblings('.visible').removeClass('visible');
			$eventSlideCont.animate({height:setHeight}, 500);
			checkIndex();
		});
	});

	$('a.event_day').betterTooltip();

}); 
