
	var firstRun = true;
//reset url vars for events calendar
 	var url = window.location.href;
 	url = url.replace('http://www.parksandpeople.org/events/calendar/', '');
 	url = url.substr( 0, url.length - 1 );
 	url = url.split('/');
 	
 	//console.log(url);
 	
 	the_date = url[0] + '/' + url[1] + '/' + url[2] + '/';
 	var start_date = new Date();
 	if(url[0] != '') {
		start_date.setFullYear(url[0], url[1]-1, url[2]);
	}
 	 
 	 if(url[3] != null)
		the_range = url[3] + '/';
	else
		the_range = 'day/';
	
	if(url[4] != null)
		the_category = url[4] + '/';
	else
		the_category = '';
			
$(document).ready(function() {
	
	//events page datepicker
	$('#calendar').datepicker({
		dayNamesMin: ['SUN', 'MON', 'TUES', 'WED', 'THUR', 'FRI', 'SAT'],
		dateFormat: 'yy-mm-dd',
		onSelect: changeDate,
		onChangeMonthYear: changeMonth,
		defaultDate: start_date
	});
	
	$("#event_view").dropdown({css: true, change: changeEventRange});
	$("#event_category").dropdown({css: true, change: changeEventCategory});
	//$(".page_sort li a").live("click", changeEventPage);
});

function changeDate(date, ui)
{
	current_date = date.replace(/-/gi, '/') + '/';
	new_location = 'http://www.parksandpeople.org/events/calendar/' + current_date + the_range + the_category;
	//console.log(new_location);
	window.location = new_location;
}

function changeMonth(year, month, ui) 
{
	if(!firstRun)
	{
		current_date = the_date.split('/');
		current_date = year + '/' + month + '/1/';
				
		new_location = 'http://www.parksandpeople.org/events/calendar/' + current_date + the_range + the_category;    // e/events/calendar/2009/11/1/9/day/
		//console.log(new_location);
		window.location = new_location;
	}
	else
	{
		firstRun = false;
	}
	//console.log(year, month, ui);
}


function changeEventRange(event, ui)
{
	if(url[0] == '')
		the_date = current_date + '/';
	the_range = ui.value + '/';		
	new_location = 'http://www.parksandpeople.org/events/calendar/' + the_date + the_range + the_category;
	//console.log(new_location);
	window.location = new_location;
}
	
function changeEventCategory(event, ui)
{
	if(url[0] == '')
		the_date = current_date + '/';
	the_category = ui.value + '/';
	new_location = 'http://www.parksandpeople.org/events/calendar/' + the_date + the_range + the_category;
	//console.log(new_location);
	window.location = new_location;
}

function changeEventPage(event, ui)
{
	var the_page = $(event.target).attr('href').split('/');
	the_page = the_page[the_page.length-2] + '/';
	new_location = 'http://www.parksandpeople.org/events/calendar/' + the_date + the_range + the_category + the_page;
	//console.log(new_location);
	window.location = new_location;
	return false;
}
