jQuery(document).ready(function() {
	try {
		
		// Predefined vars from PHP
		var feed_location = CampsFeedLocation;
		var predefined_year = PredefinedStartYear;
		var predefined_month = PredefinedStartMonth
		var predefined_date = PredefinedStartDate
		var predefined_country_code = PredefinedCountryCode;
		
		// full-calendar
		jQuery('#r2gb_public_calendar').fullCalendar({
			header: {
				left: 'title',
				center: '',
				right: 'prev,next'
				//right: 'month,agendaWeek,agendaDay'
			},
			firstDay: 0,
			height: 350,
			events: feed_location,
			year: predefined_year,
			month: (predefined_month - 1), // zero-based !!!!
			date: predefined_date,
			
			eventClick: function(calEvent, jsEvent, view) {
				// OBSOLETE AUSTRIA
				if (predefined_country_code == "AT") {
					// get start_date and end_date strings ...
					date = calEvent.start;
					day_string = date.getDate();
					if (date.getDate() < 10)
						day_string = "0" + day_string;
					month_string = date.getMonth() + 1;
					if ((date.getMonth() + 1) < 10)
						month_string = "0" + month_string;
					date_string = date.getFullYear() + "-" + month_string + "-" + day_string;
					// calculate the end date
					end_date = calEvent.end;
					//add_days_string = "+" + (num_days - 1) + " day";
					//end_date = new Date(strtotime(add_days_string, date_string));
					end_day_string = end_date.getDate();
					if (end_date.getDate() < 10)
						end_day_string = "0" + end_day_string;
					end_month_string = end_date.getMonth() + 1;
					if ((end_date.getMonth() + 1) < 10)
						end_month_string = "0" + end_month_string;
					end_date_string = end_date.getFullYear() + "-" + end_month_string + "-" + end_day_string;
					
					// resets the booking form
					resetBookingForm();
					
					// set the infos from the prompt and the selection
					jQuery('#r2gb_request_start_date').val(date_string);
					jQuery('#r2gb_request_end_date').val(end_date_string);
					jQuery('#r2gb_request_camp_id').val(calEvent.id);
					jQuery('#r2gb_request_camp_id_hidden').val(calEvent.id);
					//jQuery('#r2gb_request_num_participants').val(add_num_participants);
					window.location.hash = "at_request_start"; jQuery('#r2gb_request_num_participants').focus();
				}
				
				if (predefined_country_code == "KE") {
					// get start_date and end_date strings ...
					date = calEvent.start;
					day_string = date.getDate();
					if (date.getDate() < 10)
						day_string = "0" + day_string;
					month_string = date.getMonth() + 1;
					if ((date.getMonth() + 1) < 10)
						month_string = "0" + month_string;
					date_string = date.getFullYear() + "-" + month_string + "-" + day_string;
					
					// set the infos from the prompt and the selection
					jQuery('#r2gb_request_start_date_ke').val(date_string);
				}
			},
			
			dayClick: function(date, allDay, jsEvent, view) {
				if (predefined_country_code == "KE") {
					// get start_date and end_date strings ...
					day_string = date.getDate();
					if (date.getDate() < 10)
						day_string = "0" + day_string;
					month_string = date.getMonth() + 1;
					if ((date.getMonth() + 1) < 10)
						month_string = "0" + month_string;
					date_string = date.getFullYear() + "-" + month_string + "-" + day_string;
					
					// set the infos from the prompt and the selection
					jQuery('#r2gb_request_start_date_ke').val(date_string);
				}
			}
		});
		
		//jQuery("#r2gb_request_start_date_ke").datepicker({ dateFormat: 'yy-mm-dd' });
		//jQuery("#r2gb_request_end_date_ke").datepicker({ dateFormat: 'yy-mm-dd' });
		Date.format = 'yyyy-mm-dd';
		jQuery('#r2gb_request_start_date_ke').datePicker({clickInput:true, createButton:false});
		jQuery('#r2gb_request_end_date_ke').datePicker({clickInput:true, createButton:false});
		
		
		// preload the mouse over images
		preload_image( ThemeDirPath + "/images/r2gb_link_to_austria_mouseover.jpg" );
		preload_image( ThemeDirPath + "/images/r2gb_link_to_kenya_mouseover.jpg" );
		
	} catch (e) {
	}
});

function preload_image( path ) {
	var tmp_pic = new Image();
	tmp_pic.src = path;
}

function mouseover_link_to_country( country_code ) {
	if (country_code == "AT") {
		document.getElementById("img_link_to_country").src = ThemeDirPath + "/images/r2gb_link_to_austria_mouseover.jpg";
	}
	else {
		document.getElementById("img_link_to_country").src = ThemeDirPath + "/images/r2gb_link_to_kenya_mouseover.jpg";
	}
}

function mouseout_link_to_country( country_code ) {
	if (country_code == "AT") {
		document.getElementById("img_link_to_country").src = ThemeDirPath + "/images/r2gb_link_to_austria.jpg";
	}
	else {
		document.getElementById("img_link_to_country").src = ThemeDirPath + "/images/r2gb_link_to_kenya.jpg";
	}
}


function resetBookingForm() {
	jQuery(':input','#r2gb_request_form')
	 .not(':button, :submit, :reset, :hidden')
	 .val('')
	 .removeAttr('checked')
	 .removeAttr('selected');
}

function strtotime(str, now)
{

	str = str.replace(/\s{2,}|^\s|\s$/g, ' '); // unecessary spaces
	str = str.replace(/[\t\r\n]/g, ''); // unecessary chars 

	if (str == 'now') return (new Date()).getTime();
	else if (!isNaN(parse = Date.parse(str))) return parse;
	else if (now) now = new Date(now);
	else now = new Date();

	str = str.toLowerCase();

	var __is =
	{
		day:
		{
			'sun': 0, 'mon': 1, 'tue': 2, 'wed': 3,
			'thu': 4, 'fri': 5, 'sat': 6
		},
		mon:
		{
			'jan': 0, 'feb': 1, 'mar': 2, 'may': 3, 'apr': 4,  'jun': 5,
			'jul': 6, 'aug': 7, 'sep': 8, 'oct': 9, 'nov': 10, 'dec': 11
		}
	}

	var match = str.match(/^(\d{2,4}-\d{2}-\d{2})(\s\d{1,2}:\d{1,2}(:\d{1,2})?)?$/);

	if (match != null)
	{
		if (!match[2])
			match[2] = '00:00:00';
		else if (!match[3])
			match[2] += ':00';

		var s = match[1].split(/-/g);

		for (var i in __is.mon)
			if (__is.mon[i] == s[1] - 1)
				s[1] = i;

		return strtotime(s[2] + ' ' + s[1] + ' ' + s[0] + ' ' + match[2]);
	}

	var regex = '([+-]?\\d+\\s'
			  + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
			  + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
			  + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday)'
			  + '|(last|next)\\s'
			  + '(years?|months?|weeks?|days?|hours?|min|minutes?|sec|seconds?'
			  + '|sun\.?|sunday|mon\.?|monday|tue\.?|tuesday|wed\.?|wednesday'
			  + '|thu\.?|thursday|fri\.?|friday|sat\.?|saturday))'
			  + '(\\sago)?';

	match = str.match(new RegExp(regex, 'g'));

	if (match == null)
	{
		return false;
	}

	for (var i in match)
	{
		if (!process(match[i].split(' ')))
		{
			return false;
		}
	}

	return now;

	function process(m)
	{
		var ago = (m[2] && m[2] == 'ago');
		var num = (num = m[0] == 'last' ? -1 : 1) * (ago ? -1 : 1);

		switch (m[0])
		{
			case 'last':
			case 'next':

				switch (m[1].substring(0, 3))
				{
					case 'yea':
						now.setFullYear(now.getFullYear() + num);
						break;
					case 'mon':
						now.setMonth(now.getMonth() + num);
						break;
					case 'wee':
						now.setDate(now.getDate() + (num * 7));
						break;
					case 'day':
						now.setDate(now.getDate() + num);
						break;
					case 'hou':
						now.setHours(now.getHours() + num);
						break;
					case 'min':
						now.setMinutes(now.getMinutes() + num);
						break;
					case 'sec':
						now.setSeconds(now.getSeconds() + num);
						break;
					default:

						var day;

						if (typeof (day = __is_day[m[1].substring(0, 3)]) != 'undefined')
						{
							var diff = day - now.getDay();

							if (diff == 0)
							{
								diff = 7 * num;
							}
							else if (diff > 0)
							{
								if (m[0] == 'last') diff -= 7;
							}
							else
							{
								if (m[0] == 'next') diff += 7;
							}

							now.setDate(now.getDate() + diff);
						}
				}

				break;

			default:

				if (/\d+/.test(m[0]))
				{
					num *= parseInt(m[0]);

					switch (m[1].substring(0, 3))
					{
						case 'yea':
							now.setFullYear(now.getFullYear() + num);
							break;
						case 'mon':
							now.setMonth(now.getMonth() + num);
							break;
						case 'wee':
							now.setDate(now.getDate() + (num * 7));
							break;
						case 'day':
							now.setDate(now.getDate() + num);
							break;
						case 'hou':
							now.setHours(now.getHours() + num);
							break;
						case 'min':
							now.setMinutes(now.getMinutes() + num);
							break;
						case 'sec':
							now.setSeconds(now.getSeconds() + num);
							break;
					}
				}
				else
				{
					return false;
				}

				break;

		}

		return true;
	}

}
