function Courses(){};
Courses.prototype = {
	geolocation_data:null,
	pin:false,
	init:function(map) {
		var self = this;
		
		$(".course-search").submit(function(evt){
			evt.preventDefault();
			window.location = window.location.protocol+'//'+window.location.host+'/courses/results/'+self._build_query_string();
		});
		
		/*if ($('#ajax_viewport').length > 0) {
			$('.pagination a').live('click', function(evt){
				evt.preventDefault()
				var ajax_location = this.href.toString()
				$('#ajax_viewport').load(ajax_location.replace(/courses\/results/, 'ajax/courses_results'))
			})
		} functionality is broken on production but working on staging*/
	},
	init_geo_search:function(map) {
		var self = this;
		
		$(window).unbind('maps.dragged maps.zoom_changed');
		$('#geo_search_bounded_refresh').unbind('click');
		
		$('#geo_search_bounded_refresh').click(function(){
			if ($('#geo_search_bounded_refresh').is(':checked')) {
				self.do_bounded_search(map);
			}
		});
		
		$(window).bind('maps.dragged maps.zoom_changed', function(){
			if ($('#geo_search_bounded_refresh').is(':checked')) {
				self.do_bounded_search(map);
			}
		});
	},
	init_geo_localization:function() {
		
		var ll = null;
		var self = this;
		
		$(window).bind('geolocation.loaded', function(evt, data) {
			self.geolocation_data = data;
			self.set_location_display(data);
			$("#location-modal").fadeOut();
		});
		
		
		$(window).bind('geolocation.error', function(evt) {
			if ($("#location-modal").is(':visible')) {
				$("#geo-error").show();
			}
		});
		
		var geolocation = new Geolocation();
		
		ll = $('#ll').val();
		
		if (!ll) {
			var default_city = $('#default-city').val();
			
			if (default_city) {
				geolocation.geocode(default_city);
			}
			else {
				geolocation.get_location_async();
			}
		}
		else {
			coords = ll.split(',');
			geolocation.geocode_reverse({latitude:coords[0], longitude:coords[1]});
		}
	
		$("#edit-location").click(function(evt){
			$("#location-modal").fadeIn();
			evt.preventDefault();
		});
		
		$('#find-location').click(function(evt){
			// var geolocation = new Geolocation();
			geolocation.geocode($('#city').val());
			evt.preventDefault();
		});
		
		$('#city').bind('keypress', function(evt)
		{	// make tab and return to do a search instead of posting the form
		     if(evt.keyCode == 13 || evt.keyCode == 9)
		     {
				evt.preventDefault();
				$('#find-location').click();
		     }
		});
		
		
		$('#nearby').click(function(evt) {
			geolocation.get_browser_location();
			evt.preventDefault();
		});
		
	},
	do_bounded_search:function(map) {
		var self = this;
		if ($('#geo_search_bounded_refresh').is(':checked')) {
			this.bounds = map.get_map_instance().getBounds().toUrlValue(15);
		}
		
		if ($('#ajax_viewport').length > 0) {
			
			result_url = window.location.protocol+'//'+window.location.host+'/ajax/courses_results/'+this._build_query_string();
			self.toggle_spinner(true);
			$('#ajax_viewport').load(result_url, function(){
				map.clear_markers().load_markers();
				self.toggle_spinner(false);
			});
		}
		else {
			window.location = window.location.protocol+'//'+window.location.host+'/courses/results/'+this._build_query_string();
		}
		
		
	},
	set_location_display:function(geolocation) {
		$('#city-display').text(geolocation.locality);
		$('#state-display').text(geolocation.state_code);
		$('#city').val(geolocation.locality);
		$('#state option[value="'+geolocation.state+'"]').attr('selected', 'selected');
		$('#ll').val(geolocation.latitude+','+geolocation.longitude);
	},
	drop_pin:function(location) {
		if (map.pin != null) {
			map.pin.setMap(null);
		}
		if (typeof map.drop_pin != 'undefined') {
			map.drop_pin(location);
		}
		
	},
	toggle_spinner:function(show_spinner){
		if (show_spinner) {
			$('#geo_search_bounded_refresh').hide();
			$('#map_loader').show();
		}
		else {
			$('#map_loader').hide();
			$('#geo_search_bounded_refresh').show();
		}
	
	},
	_build_query_string:function() {
		
		var search_values = new Array();
		var search_string = '';
		
		var search_within = $('#within').val();
		
		if ($('#keywords').val()) {
			search_values.push('keywords:' + $('#keywords').val());
		}
		
		$('input[type=text], input[type=hidden], input[type=checkbox]:checked, select', $('.course-search')).each(function(){
			$this = $(this);
			if ($this.val()) {
				name = $this.attr('name');
				
				switch(name) {
					case 'll':
						if (!search_within) {
							return;
						}
						break;
					case 'city-region':
					case 'state':
						return;
				}

				search_values.push(name + ':' + $this.val());
			}
		})
		
		if (typeof this.bounds != 'undefined') search_values.push('bounds:'+this.bounds);
		
		return search_values.join('/');
	}
}
var map = {};
var courses = {};
$(function(){
	courses = new Courses();
	if (typeof GTMap != 'undefined') {
		map = new GTMap();
		courses.init(map);
		$(window).bind('maps.loaded', function(){

			courses.init_geo_localization();
			courses.init_geo_search(map);

			if (courses.geolocation_data) {
				courses.drop_pin(courses.geolocation_data);
			}
			$(window).bind('geolocation.loaded', function(evt, data) {
				courses.drop_pin(data);
			});

		});	

		map.init();
	}
	else {
		courses.init_geo_localization();
	}

	
})

