// prevent conflicts with other js libraries
var $j = jQuery.noConflict();

// on dom ready
$j(document).ready(function(){
	var height = $j('#calendar .current').height();
	$j('#calendar').css('height',height+'px');

	// assign event logic
	event_logic();

	// get previous month
	$j('#calendar_controls #back').click(function(){
		$j("#ajax_loader").html("<img src='/imgs/icons/ajax-loader.gif' />");
		$j.ajax({
			type: 'POST',
			url: $j(this).attr('action'),
			data: 'ajax=1&direction=back&'+$j(this).parents('form').serialize(),
			success: function(msg) {
				update_calendar_variables('back');
				insert_calendar('back', msg);
				$j("#ajax_loader").html("");
				event_logic();
			}
		});
	return false;
	});


	// reset month to current
	$j('#calendar_controls #reset').click(function(){
		$j("#ajax_loader").html("<img src='/imgs/icons/ajax-loader.gif' />");
		$j.ajax({
			type: 'POST',
			url: $j(this).attr('action'),
			data: 'ajax=1&direction=reset&'+$j(this).parents('form').serialize(),
			success: function(msg) {
				update_calendar_variables('reset');
				$j('#calendar').html( msg );
				$j("#ajax_loader").html("");
				event_logic();
				var height = $j('#calendar .current').height();
				$j('#calendar').animate({"height":height+"px"});
			}
		});
	return false;
	});


	// get next month
	$j('#calendar_controls #forward').click(function(){
		$j("#ajax_loader").html("<img src='/imgs/icons/ajax-loader.gif' />");
		$j.ajax({
			type: 'POST',
			url: $j(this).attr('action'),
			data: 'ajax=1&direction=forward&'+$j(this).parents('form').serialize(),
			success: function(msg) {
				update_calendar_variables('forward');
				insert_calendar('forward', msg);
				$j("#ajax_loader").html("");
				event_logic();
			}
		});
	return false;
	});
});


function event_logic() {
	// event tooltips
	$j('.event_link').click(function(){
		return false;
	});


	$j('.event_link').hover(function(e){
		// get height of tooltip
		$j('#tooltip').html( $j(this).next('div').html() )
		var height = $j('#tooltip').height();

		// get mouse position
		//var x = (e.pageX - this.offsetLeft) - 110;
		//var y = (e.pageY - this.offsetTop) - height;
		var x = (e.pageX ) -110;
		var y = (e.pageY ) -50;
		
		// set css rules for tooltip
		$j('#tooltip').css('left',x+'px');
		$j('#tooltip').css('top',y+'px');

		// fade in the tooltip if not IE
		if( jQuery.support.htmlSerialize ) {
			$j('#tooltip').fadeIn();
		} else {
			$j('#tooltip').show();
		}
	}, function(e){
		$j('#tooltip').hide();
	});
}


/**
 * Updates the calendar
 */
function insert_calendar(direction, html) {
	switch(direction)
	{
		case 'forward':
			// insert month after current month
			$j('#calendar .current').after(html);
			// move the slider to the right
			$j('#calendar .month').animate({"left":"-=580px"},"normal",'',function(msg){
				// remove the 1st month
				//$j('#months_slider .month:first').remove();
				// reset the slider position
				//$j('#months_slider').css('left','0px');
				// update month classes
				//$j('#months_slider .next').removeClass('next').addClass('current');
			});
			//$j('#calendar .month:first').remove();
			$j('#calendar .current').removeClass('current');
			$j('#calendar .next').removeClass('next').addClass('current');
		break;

		case 'back':
			// insert month before current month
			$j('#calendar .current').before(html);
			// set the slider position to still display current month
			//$j('#calendar #months_slider').css('left','-580px');
			// move the slider to the right
			$j('#calendar .month').animate({"left":"+=580px"},"normal",'',function(msg){
				// remove the last month
				//$j('#months_slider .month:last').remove();
				// reset the slider position
				//$j('#months_slider').css('left','0px');
				// update the month classes
				//$j('#months_slider .previous').removeClass('previous').addClass('current');
			});
			$j('#calendar .current').removeClass('current');
			$j('#calendar .previous').removeClass('previous').addClass('current');
		break;
	}

	var height = $j('#calendar .current').height();
	$j('#calendar').animate({"height":height+"px"});
}


/**
 * Updates the month and year variables on the calendar form
 */
function update_calendar_variables(direction) {
	// get values
	var month = $j('#month').val();
	var year = $j('#year').val();


	switch(direction)
	{
		case 'forward':
			month = (month*1) + 1;
			if(month > 12) {
				month = 1;
				year = (year*1) + 1;
			}
		break;
		case 'back':
			month = (month*1) - 1;
			if(month < 1) {
				month = 12;
				year = (year*1) - 1;
			}
		break;
		case 'reset':
			var currentDate = new Date();
			month = currentDate.getMonth() + 1;
			year = currentDate.getFullYear();
		break;
	}


	// update values
	$j('#month').val(month);
	$j('#year').val(year);
}