filterList = {
	child: false,
	
	TextFilter: function(el,qKey) {
		this.init = function(el) {
			this.queryKey = qKey;
			this.element = el;
			var parent = this;
			this.element.bind('keypress', function(e) {
				if(e.keyCode==13){
					filterList.child.updateResults();
					return false;
				}
			});
		};
		this.get = function() {
			if(this.val) {
				return this.val;
			} else {
				return el.val();
			}
		};
		this.init(el);
	},
	SelectFilter: function(el,qKey) {

		/* METHODS */
		this.init = function(el) {
			this.queryKey = qKey;
			this.element = el;
			this.element.bind('change', function(e) {
				filterList.child.updateResults();
			});
		};
		this.get = function() {
			if(this.val) {
				return this.val;
			} else {
				return el.val();
			}
		};
		this.init(el);
	},
	ResultsPerPageFilter: function(els,qKey) {

		/* METHODS */
		this.init = function(el) {
			this.queryKey = qKey;
			this.els = els;
			this.set($(els[0]).val());
			var parent = this;
			el.each(function(index) {
				$(this).bind('change', function(e) {
					parent.update($(this).val());
				});
			})
		};
		this.update = function(val) {
			var parent = this;
			this.els.each(function(index) {
				if($(this).val() != val) $(this).val(val);
				parent.set($(this).val());
			});
			filterList.child.updateResults();
		};
		this.set = function(val) {
			this.val = val;
		};
		this.get = function() {
			return this.val;
		};
		this.init(els);
	},
	PagesFilter: function(els,qKey) {
		this.init = function(els) {
			// we default to page 1.
			this.set(1);

			this.queryKey = qKey;
			this.els = els;
			var parent = this;
			els.each(function(index) {
				if($(this).hasClass('activePage')) {
					parent.set($(this).attr('rel'));
				}
				$(this).bind('click', function(e) {
					parent.set($(this).attr('rel'));
					filterList.child.updateResults();
					return false;
				})
			});
		};
		this.set = function(val) {
			this.val = val;
		};
		this.get = function() {
			return this.val;
		};
		this.init(els);
	},
	CheckboxesFilter: function(el,qKey,updateOnChange) {

		/* METHODS */
		this.init = function(el) {
			this.queryKey = qKey;
			this.checkboxes = el;
			if(updateOnChange) {
				el.each(function(index) {
					$(this).bind('change', function(e) {
						filterList.child.updateResults();
					});
				});
			}
		};
		this.get = function() {
			var values = new Array();
			this.checkboxes.each(function() {
				if($(this).attr('checked')) {
					values.push($(this).val());
				}
			});
			return values;
		};
		this.init(el);
	},
	GeoLocationFilter: function(el,qKey) {
		this.init = function(el) {
			this.element = el;
			this.queryKey = qKey;
			var parent = this;
			this.element.slider({
				max: 5,
				min: 1,
				step: 1,
				value: $('#formFilter-location').val(),
				change: function(e,ui) {
					filterList.child.updateResults();
				}
			});
		}	
		this.set = function(val) {
			this.val = val;
		}	
		this.get = function() {
			return this.element.slider( "option", "value" );
		}	

		this.init(el);
	},	
	FilterForm: function(el) {

		/* METHODS */
		this.init = function(el) {
			this.element = el;
			this.element.submit(function() {
				return filterList.child.updateResults();
			})
		};
		this.set = function(val) {
			this.val = val;
		};
		this.get = function() {
			return this.val;
		};

		this.init(el);
	},
	RefineSearchInterface: function() {
		
		this.open = function() {
			this.isOpen = true;
			$('body').addClass('overlay');
			$('#overlay-bg').height($('body').height());
			$('#filter-geoScope').hide();
			$('#filter-state').hide();
			
			
			this.searchFiltersBox.fadeIn('fast');
			this.searchFiltersBoxTrigger.addClass('bringForward');
			var pos = this.searchFiltersBoxTrigger.offset();
			pos.top -= 6;
			pos.left -= 20;
			this.searchFiltersBox.offset(pos);
		};
		
		this.close = function() {
			$('body').removeClass('overlay');
			$('#filter-geoScope').show();
			$('#filter-state').show();
			
			this.updateActiveFilters();
			
			var parent = this;
			this.searchFiltersBox.fadeOut(
				'fast',
				function() {
					parent.searchFiltersBoxTrigger.removeClass('bringForward');
					return filterList.child.updateResults();
				}
			);
		};

		this.updateActiveFilters = function() {
			// update the list of filters
			this.searchFiltersBox.find('input[type=checkbox]').each(function(index) {
				// check for filter link to show
				var relatedId = '#sF-' + $(this).attr('id');
				var related = $(relatedId);
				if($(this).is(':checked')) {
					related.show();
					related.removeClass('disabled');
					related.addClass('enabled');
				} else {
					related.hide();
					related.removeClass('enabled');
					related.addClass('disabled');
				}
			});
			filterList.child.checkAndHideFilterHeaders();
		};
		
		this.init = function() {
			this.searchFiltersBox = $('#searchFiltersBox');
			this.searchFiltersActiveList = $('#searchFiltersActiveList');
			this.searchFiltersBoxTrigger = $('#searchFiltersBoxTrigger');
			$('.selectAll a').each(function(index) {
				var trigger = $(this);
				trigger.bind('click',function() {
					checkboxes = trigger.parents('div.col').find('input');
					checkboxes.each(function(index) {
						if(!$(this).attr('checked')) {
							$(this).click();
						}
					})
					return false;
				});
			});
			
			var parent = this;
			$('#searchFilterBoxClose').bind('click',function() {
				parent.close();
			});
			
			$('.selectNone a').each(function(index) {
				var trigger = $(this);
				trigger.bind('click',function() {
					checkboxes = trigger.parents('div.col').find('input');
					checkboxes.each(function(index) {
						if($(this).attr('checked')) {
							$(this).click();
						}
					})
					return false;
				});
			});

			$('.clearAll').each(function(index) {
				$(this).bind('click',function() {
					$('.selectNone a').each(function(index) {
						$(this).click();
					});
					return false;
				})
			})

			$('#searchFiltersActiveList li ul li').each(function(index) {
				$(this).bind('click',function() {
					var id = $(this).attr('id').replace('sF-','');
					var c = $('#'+id);
					c.click();
					//c.attr('checked',false);
					$(this).removeClass('enabled');
					$(this).addClass('disabled');
					$(this).fadeOut('fast');
					filterList.child.checkAndHideFilterHeaders();
					return filterList.child.updateResults();
				})
			});
			
			var parent = this;
			
			this.searchFiltersBoxTrigger.click(function() { parent.open(); });
			$('#overlay-bg').bind('mouseenter',function() {
				parent.close();
			});
		}
		
		this.init();
	},
	ListInterface: function() {
		this.init = function() {
			$('#projDirResultsTable tbody tr').hover(
				function() {
					$(this).addClass('active');
				},
				function() {
					$(this).removeClass('active');
				}
			);
		};
		
		this.init();
	},
	TabInterface: function() {
		this.init = function() {
			$('#mapTab').bind('click',function() {
				filterList.child.interfaces.mapInterface.redraw();
			})
			$('#listTab').bind('click',function() {
			})
		}
		this.init();
	},
	MapInterface: function() {
		this.viewed = false;
		this.mapCenter = new google.maps.LatLng(37.0902, -95.7129);

		/* This gets called when the map tab is clicked on; it only needs to redraw once */
		this.redraw = function() {
			if(this.viewed == false || true) {
				google.maps.event.trigger(this.map, 'resize');
				this.map.setZoom(this.map.getZoom());
				this.map.setCenter(this.mapCenter);
				this.viewed = true;
			}
		}
		
		var parent = this;

		this.makeMap = function(data) {
			// Check if the mapCanvas element exists before calling the GM API, this fixes an error on the single view page
			if(document.getElementById('mapCanvas')) {
				
				/* 
				if the map is being remade, we need to flag it as unviewed so we redraw it correctly when
				the tab is clicked
				*/
				this.viewed = false
				allowNavControls = true;
				allowStreetView = true;
				var mapOptions = {
					streetViewControl: allowStreetView,
					zoom: 4,
					center: parent.mapCenter,
					mapTypeId: google.maps.MapTypeId.ROADMAP
				}
				this.map = new google.maps.Map(document.getElementById("mapCanvas"), mapOptions);				
			}
		}

		this.init = function() {
			this.makeMap();
			postFilterHash = filterList.child.getPostFilterHash();
			filterList.child.updateMap(postFilterHash)
		}
		this.init();
	}	
}



