// calendar functions

var cal_ActiveYear = 0;
var cal_ActiveMonth = 1;
var cal_MonthSelectorObj = 0;
var cal_YearSelectorObj = 0;
var cal_TableObj = 0;
var cal_FirstYear = 0;
var cal_LastYear = 0;
var cal_Date = new Date();
var cal_Language = 'es';
var cal_Events = null;
var cal_WeekDays = Array();
var cal_LoadingIconTimer = null;
cal_WeekDays['es'] = Array ('L', 'M', 'Mc', 'J', 'V', 'S', 'D');
cal_WeekDays['ca'] = Array ('Dl', 'Dm', 'Dc', 'Dj', 'Dv', 'Ds', 'Dg');

function calendar_Init (month_selector_id, year_selector_id, table_id, month, year, first_year, last_year, language)
{
//	month = month || parseInt(getCookie ('cal_ActiveMonth')) || cal_Date.getMonth() + 1;
//	year = year || parseInt(getCookie ('cal_ActiveYear')) || cal_Date.getFullYear();
	month = month || cal_Date.getMonth() + 1;
	year = year || cal_Date.getFullYear();

	cal_MonthSelectorObj = document.getElementById (month_selector_id);
	cal_YearSelectorObj = document.getElementById (year_selector_id);
	cal_TableObj = document.getElementById (table_id);
	cal_ActiveMonth = month;
	cal_ActiveYear = year;
	cal_FirstYear = first_year;
	cal_LastYear = last_year;
	cal_Language = language;

	calendar_Refresh();
}

function calendar_PrevMonth()
{
	if (cal_ActiveYear > cal_FirstYear || cal_ActiveMonth > 1)
	{
		cal_ActiveMonth -= 1;
		if (cal_ActiveMonth <= 0)
		{
			cal_ActiveMonth = 12;
			cal_ActiveYear -= 1;
		}
		calendar_Refresh();
	}
}

function calendar_NextMonth()
{
	if (cal_ActiveYear < cal_LastYear || cal_ActiveMonth < 12)
	{
		cal_ActiveMonth += 1;
		if (cal_ActiveMonth > 12)
		{
			cal_ActiveMonth = 1;
			cal_ActiveYear += 1;
		}
		calendar_Refresh();
	}
}

function calendar_SelectMonth (month)
{
	cal_ActiveMonth = month;
	calendar_Refresh();
}

function calendar_SelectYear (year)
{
	cal_ActiveYear = year;
	calendar_Refresh();
}

function calendar_MonthSelected()
{
	calendar_SelectMonth (cal_MonthSelectorObj.selectedIndex + 1);
}

function calendar_YearSelected()
{
	calendar_SelectYear (cal_YearSelectorObj.selectedIndex + cal_FirstYear);
}

function calendar_Hilite (tdCell, hilite)
{
	if (hilite)
	{
		tdCell.style.borderWidth = "1px";
		tdCell.style.borderStyle = "solid";
		tdCell.style.borderColor = "#CCCCFF";
	}
	else
	{
		tdCell.style.borderWidth = "";
		tdCell.style.borderStyle = "";
		tdCell.style.borderColor = "";
	}
}

function calendar_Refresh()
{
	calendar_LoadingIcon (true);
	
//	setCookie ('cal_ActiveYear', cal_ActiveYear);
//	setCookie ('cal_ActiveMonth', cal_ActiveMonth);
	
	var firstday = new Date(cal_ActiveYear, cal_ActiveMonth-1, 1);
	firstday.setDate(1 - (7 + firstday.getDay() - 1) % 7);
	day = firstday.getDate();
	month = firstday.getMonth()+1;
	year = firstday.getFullYear();
	params = 'd='+day+'&m='+month+'&y='+year+'&p=42';

	if (!ajax_Post ('/'+cal_Language+'/actividades/calendar.php', params, calendar_Callback))
	{
		calendar_LoadingIcon (false);
		
		calendar_Events = null;
		cal_MonthSelectorObj.selectedIndex = cal_ActiveMonth - 1;
		cal_YearSelectorObj.selectedIndex = cal_ActiveYear - cal_FirstYear;

		calendar_Build();
	}
}

function calendar_Callback (result)
{
	calendar_LoadingIcon (false);
	
//	alert (result);
	
	cal_MonthSelectorObj.selectedIndex = cal_ActiveMonth - 1;
	cal_YearSelectorObj.selectedIndex = cal_ActiveYear - cal_FirstYear;

	cal_TableObj.innerHTML = result;
}

function calendar_Build()
{
	table = '<table id="calendar" cellpadding="0" cellspacing="0">';
	
	table += '<tr>';
	for (var i=0; i<7; i++)
		table += '<th>' + cal_WeekDays[cal_Language][i] + '</th>';
	table += '</tr>';
	
	// get first day to display in the grid for current month
	var today = new Date();
	var dt_firstday = new Date(cal_ActiveYear, cal_ActiveMonth-1, 1);
	dt_firstday.setDate(1 - (7 + dt_firstday.getDay() - 1) % 7);
	
	var dt_current_day = new Date(dt_firstday);
	for (i = 0; i < 6; i++)
	{
		table += '<tr>';
		for (var n_current_wday=0; n_current_wday<7; n_current_wday++)
		{
			table += '<td class="';
				
			if (dt_current_day.getMonth() == cal_ActiveMonth-1)
				table += ' current-month';
			else 
				table += ' other-month';

			if (dt_current_day.getDay() == 0 || dt_current_day.getDay() == 6)
				table += ' weekend';
			else
				table += ' workingday';

			if (dt_current_day.getDate() == today.getDate() && dt_current_day.getMonth() == today.getMonth() && dt_current_day.getFullYear() == today.getFullYear())
				table += ' today';
			
			if (cal_Events && cal_Events[i*7+n_current_wday])
			{
				id = cal_Events[i*7+n_current_wday];
				if (id != "")
					table += ' general_event';
			}

			table += '" onmouseover="calendar_Hilite(this,true)" onmouseout="calendar_Hilite(this,false)">';
			table += dt_current_day.getDate();
			table += '</td>';
			
			dt_current_day.setDate(dt_current_day.getDate()+1);
		}
		table += '</tr>';
	}
	
	table += '</table>';
	
	cal_TableObj.innerHTML = table;
}



function calendar_LoadingIcon (show)
{
	if (show && cal_LoadingIconTimer == null)
	{
		cal_LoadingIconTimer = setTimeout ("calendar_LoadingIcon(true)", 400);
		return;
	}
	
	if (cal_LoadingIconTimer)
	{
		clearTimeout (cal_LoadingIconTimer);
		cal_LoadingIconTimer = null;
	}
	
	obj = document.getElementById ('calendar_loading_icon');
	if (obj)
	{
		obj.style.display = show ? '' : 'none';
	}
}
