$(document).ready(function() {

		$('img[align="right"]').addClass('alignRight');
		$('img[align="left"]').addClass('alignLeft');
		
		$('.nav li.ON').parents('li').addClass('ON');

		
		// Highlight active nav within county pages
		if (typeof(county) == 'undefined') {
			county = $.query.get('county');
		}
		
		if(county) {
			$('#countyNav a[href*="'+ county +'"]').parent('li').addClass('ON');
		}
		
		// Add a PDF icon to any link to a .pdf file
		$('#content a[href$="pdf"]').addClass('pdf'); 
		$('#content a[href$="doc"]').addClass('doc'); 
		$('#content a[href$="xls"]').addClass('xls'); 
		$('#content a[href$="wmv"]').addClass('wmv'); 
		$('#content a[href^="http"]').addClass('external'); 
		
	
	/* Font size switcher -------------------------------------------------------- */
	
	sizes = new Array();
	
	// set up three standard font sizes
	sizes['size1'] = '.95em';
	sizes['size2'] = '1.1em';
	sizes['size3'] = '1.3em';
	
	cookie_name = "aaa8-FontSize";
	
	// if the cookie exists, update the global font size
	if($.cookie(cookie_name)) {
			var getSize = $.cookie(cookie_name);
			$('body').css('fontSize',sizes[getSize]);
		// if not, create the cookie and set to the default size
		} else {
			$.cookie(cookie_name, 'size1', { path: '/' });
		}
		
	
	$('#switcher a').click(function() {
		clickedSize = $(this).attr('class');
		$('body').css('fontSize',sizes[clickedSize]);
		
		// Set a cookie to remember the new size from page to page
		$.cookie(cookie_name, clickedSize, { path: '/' });
		
		return false;
	});
	
	
	
	
		
	/* Calendar functions -------------------------------------------------------- */
	
	// Ajax call to load events for this month
	function getEvents(year, month, inst) {

	month = parseInt(month);
	myDate = year + '-' + month + '-' + '01';

		$.ajax({
			type: 'GET',
			url: "/cms/modules/ajax_getevents.php?month=" + myDate,
			async:false,
			dataType: 'json',
			beforeSend: function() {
				$('div.ui-datepicker-inline').hide();
				$('#calendarWidget .loading').show();
			},
			success: function(data){
				events = data;
				$('#calendarWidget .loading').hide();
				$('div.ui-datepicker-inline').show();
			},
			
			error: function(){
				//alert('something went wrong!');
			}
			});
	}
	
	// function is called for every day that loads in the calendar widget.
	// If the date called exists in the array created above, add the 'custom' class to it.
	function addDays(date) {
	
	    for (var i=0; i < events.length; i++) {
	      	if (date.getMonth() == (events[i]['month'] - 1) && date.getDate() == events[i]['day']) {
	      		return [true, 'hasEvent'];
	      	}
	      	
	    }
	    
	  	return [true, ''];
	}
	
	// If a date has been set in the query string, 
	// load that month in the calendar widget by default
	if ($.query.get('date') != '' || $.query.get('month') != '') {

		if($.query.get('date') != '') { queryString = $.query.get('date'); }
		if($.query.get('month') != '') { queryString = $.query.get('month'); }

		array = queryString.split('-');
      	defaultDate = new Date(array[0],array[1] - 1,array[2]);

    // default to current month
	} else {
		defaultDate = '';
	}
	
	$("#calendarWidget").datepicker({ 
		dateFormat: $.datepicker.W3C,
		defaultDate: defaultDate,
		onChangeMonthYear: getEvents,
		beforeShowDay: addDays,
	    onSelect: function(date) { 
	      	location.href = '/calendar/?date=' + date; 
	    },
	    changeMonth: false,
	    changeYear: false,
	    dayNamesMin: ['S', 'M', 'T', 'W', 'T', 'F', 'S']
	}); 
	
	
	if($("#contact").length > 0) {
		// validate  form on submit
		$("#contact").validate({
			event: "submit",
			rules: {
				name: { required: true },
				email: { required: true, email: true },
				phone: { required: true }
			},
			messages: {
				name: 'Full name required',
				email: 'Valid email address required',
				phone: 'Phone required'
			}
		});
	}
	
		// validate signup form on keyup and submit
	if($("#subscribe").length > 0) {
		$("#subscribe").validate({
			event: "submit",
			rules: {
				email: { required: true, email: true }
			},
			messages: {
				email: "Please enter a valid email address"
			}
		});
	}
		

});
