$(document).ready(function() {
	//In Field Labels
	$('.infield_label').focus(function() {
		$(this).removeClass("infield_label").addClass("infield_label_selected");
        if (this.value == this.defaultValue)
        	this.value = '';
    });
    $('.infield_label').blur(function() {
		this.value = $.trim(this.value);
		if (this.value == ''){
	    	$(this).removeClass("infield_label_selected").addClass("infield_label");
        	this.value = (this.defaultValue ? this.defaultValue : '');
    	}
    });
	
	// Trim input on submit
	$('form').submit(function(e) {
		$(this).find('input[type=text]').each(function(i) {
			this.value = $.trim(this.value);
			if (this.value == 'Email Address' || this.value == 'Courriel'){
        		this.value = '';
    		}
		});
	});
	
	// Home Calendar
	$('#eventsNextMonth').click(function(e) {calendarFunction(e,getDate(1));});
	$('#eventsPrevMonth').click(function(e) {calendarFunction(e,getDate(-1));});
	
	function getDate(offset) {
		m = parseInt($('#currentMonth').text());
		y = parseInt($('#currentYear').text());

		if (offset<0) {
			m -= Math.floor((offset *-1) /13)*12 - offset;
			if (m == 0) {
				m = 12;
				y--;
			}
		} else {
			m += offset;
			y += Math.floor(m /13);
			m = m - Math.floor(m /13) *12;
		}
		return y + '/' + String("0" + m).slice(-2) + '/';
	}
	
	function calendarFunction(e,date) {
		e.preventDefault();
		$('#eventsNavigation').text('Loading...');
		
		if($('#lang')!=undefined && $('#lang')!=null && $('#lang').text()=='fr')
			var lang = 'fr/'; else
			var lang = '';
		
		$.getJSON('about-us/events/'+lang+'feeds/'+date, function(data){
			$('#eventsDate').html(data.date);
			$('#events').html(data.events);
			$('#calendar').html(data.calendar);
			$('#eventsNextMonth').click(function(e) {calendarFunction(e,getDate(1));});
			$('#eventsPrevMonth').click(function(e) {calendarFunction(e,getDate(-1));});
			$('#eventsDate, #events, #calendar').hide();
			$('#eventsDate, #events, #calendar').fadeIn('normal');
		});
		$("#eventsNavigation").bind("ajaxError", function(){
		   $('#eventsNavigation').text('Error');
		});
	}
});