current_hood = current_month = '';

$(document).ready(function() {
	// Keep the form from being submitted.
	$('#form-refresh_events').submit(function(){ return false; });
	
	// Update events and map by neighborhood.
	$('#filter-neighborhood').change(function(){
			var ehood = $(this).val();
			current_hood = ehood;
			refresh_events();
			//update_events_by_hood(ehood);
		});
	
	// Update events and map by month.
	$('#filter-month').change(function(){
			var emonth = $(this).val();
			current_month = emonth;
			refresh_events();
			//update_events_by_month(emonth);
		});
});

// typical event class: event-details ehood-9 emonth_start-08-11 emonth_end-08-11

function refresh_map() {
	if(current_hood == '') {
		eval('map.setCenter(new GLatLng('+default_coords[0]+'), default_coords[1])');
	} else {
		jQuery.each(hoods, function(){
				if(current_hood == this[0]) {
					map.setZoom(14);
					eval('map.panTo(new GLatLng('+this[1]+'))');
					//eval('map.setCenter(new GLatLng('+this[1]+'), 14)');
				}
			});
	}
}

function refresh_titles() {
	if(current_month == '' && current_hood == '') {
		$('#events .section-title').slideDown();
	} else {
		var titles = $('#events .section-title');
		jQuery.each(titles, function(){
				var id = $(this).attr('id').split('-');
				if(id[1] == current_month) {
					$(this).slideDown();
				} else if(current_hood != '') {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			});
	}
}

function refresh_titles2() {
/*
	if(current_month != '') {
		var titles = $('#events .section-title');
		jQuery.each(titles, function(){
				var id = $(this).attr('id').split('-');
				if(id[1] == current_month) {
					$(this).slideDown();
				} else if(current_hood != '') {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			});
	}
*/
}

function update_description() {
	$('.hood_desc').hide();
	$('#hood_desc-'+current_hood).slideDown();
}

function update_select() {
	$('#filter-neighborhood').blur();
	var hoods = $('#filter-neighborhood').children();
	jQuery.each(hoods, function(){
			if(this.value == current_hood) {
				this.selected = true;
			} else {
				this.selected = false;
			}
		});
}

function refresh_events() {
	var events = $('#events .event-details');
	jQuery.each(events, function(){
			var classes = $(this).attr('class').split(' ');
			var hood = classes[1].split('-');
			var date_start = classes[2].split('-');
			var date_end = classes[3].split('-');
			
			if(current_month != '' && current_hood != '') {
				if(date_start[1] == current_month && hood[1] == current_hood) {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			} else if(current_month && current_hood == '') {
				if(date_start[1] == current_month || (date_start[1] <= current_month && date_end[1] >= current_month)) {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			} else if(current_hood && current_month == '') {
				if(hood[1] == current_hood) {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			} else {
				$(this).slideDown();
			}
		});
	
	// Redraw the previous and currently selected neighborhood.
	redraw_poly();
	
	// Update the map to show the current neighborhood.
	refresh_map();
	
	// Show only month titles where there are events displayed.
	refresh_titles2();
	
	update_description();
	
	update_select();
}

function update_events_by_month(emonth) {
	if(emonth == '') {
		$('#events .event-details').slideDown();
	} else {
		var events = $('#events .event-details');
		jQuery.each(events, function(){
				var classes = $(this).attr('class').split(' ');
				var date_start = classes[2].split('-');
				if(date_start[2] >= emonth) {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			});
	}
}

function update_events_by_hood(ehood) {
	if(ehood == '') {
		$('#events .event-details').slideDown();
		eval('map.setCenter(new GLatLng('+default_coords[0]+'), default_coords[1])');
	} else {
		var events = $('#events .event-details');
		var patt = /[0-9]{1,2}$/;
		jQuery.each(events, function(){
				var classes = $(this).attr('class').split(' ');
				if(classes[1].match(patt) == ehood && $(this).css('display') != 'none') {
					$(this).slideDown();
				} else {
					$(this).slideUp();
				}
			});
		jQuery.each(hoods, function(){
				if(ehood == this[0]) {
					eval('map.setCenter(new GLatLng('+this[1]+'), 14)');
				}
			});
	}
}


function reloadImg(){
	document.rotator.src = "images/ads/rotator.php?" + Date.parse(new Date().toString());
}


