function geo() {
	var self = this;
	
	// passed in to object
	this.current_location = false;
	this.current_radius = false;
	this.suggest_location = false;
	this.suggested_location = false;
	this.suggested_zip_code = false;
	
	// navigational
	this.abs_domain = 'http://'+window.location.hostname+window.location.pathname;
	
	// operational - don't adjust
	this.t_on = false; 
	this.t = null;
	this.loc_results = null; 
	this.curr_result = 0; 
	this.nav_expanded = 0;
	
	this.url_params = {};
	(function () {
	    var e,
	        a = /\+/g,  // Regex for replacing addition symbol with a space
	        r = /([^&=]+)=?([^&]*)/g,
	        d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
	        q = window.location.search.substring(1);

	    while (e = r.exec(q))
	       self.url_params[d(e[1])] = d(e[2]);
	})();
	
	this.beacon_pulse = function() {
		$("#suggested_location .beacon_pulse")
			.css({"borderWidth":"2px","width":"12px","height":"12px","opacity":0.9,"left":"-2px","top":"-2px"})
			.animate({"borderWidth":"10px",
						"width":"70px",
						"height":"70px",
						"opacity":0,
						"left":"-39px",
						"top":"-39px"},
					3000,
					function() {
						self.beacon_pulse();
					});
	}
	
	this.clear_location_object = function() {
		$("#enter_location #location_object").hide().text("");
		$("#enter_location .cancel").hide();
		$("input[name=\'zip_code\']").val("");
		$("#enter_location input").val("").fadeIn("fast").focus();
	}
	
	this.clear_notifications = function() {
		$("#top_nav_notification").hide().children().remove();
	}
	
	this.display = function(el) {
		if(!el) {
			if(self.current_location) {
				el = "location";
			}else if(self.suggest_location) {
				el = "suggested";
			}else{
				el = "form";
			}
		}
		
		if(el == "location") {
			$("#suggested_location, #top_nav_location_form").hide();
			$("#current_location .radius").text(self.current_radius);
			$("#current_location .name").text(self.current_location);
			$("#current_location").fadeIn("fast");
			$("#toggle_current_location").click( function() {
				self.display('form');
			});
			$("#top_nav_cancel").animate({right:-23},150,function() { $(this).animate({right:"+=3"},50); }).hover(function() { $(this).animate({right:"-=3"},150); }, function() { $(this).animate({right:"+=3"}); },150).click( function() { window.location=self.get_url({"clear_location":"true"},["zip_code","search_radius","geo","geo_name","suggestions"]);});
		
		}else if(el == "suggested") {
			$("#current_location, #top_nav_location_form").hide();
			
			if(self.suggested_location && self.suggested_zip_code) {
				self.display_suggestion(self.suggested_location,self.suggested_zip_code);
			}else{
				$.post("/ping/suggest_location",null, 
					function(data) {
						if(!data || data.length == 0) { self.suggest_location=false; self.display();}
						self.display_suggestion(data.city);
					},
				"json");
			}
		}else{
			$("#suggested_location, #current_location").hide();
			$("#top_nav_location_form").fadeIn("fast");
		}
	}
	
	this.display_suggestion = function(city,zip_code) {
		$("#suggested_location .name")
			.text(city)
			.parent()
			.parent()
			.fadeIn("fast",function() {
				$("#suggested_location_yes").click( function() {
					window.location = self.get_url({"geo":"true","suggestions":"false","search_radius":50,"zip_code":zip_code},["clear_location"]);
				});
				$("#suggested_location_no").click( function() {
					self.display('form');
					self.turn_off_suggestions();
				});
		
				self.beacon_pulse();
		});
		$("#suggested_location .message").hover( 
			function() { $("#suggested_location .message").animate({"left":"+=3"},150); },
			function() { $("#suggested_location .message").animate({"left":"-=3"},150); }
		);
	}
	
	this.expand_top_nav = function() {
		if(self.nav_expanded==0) {
			$("#top_nav li.l a").animate({width:"-=15"},250);
			$("#enter_location").animate({width:"+=40",paddingRight:"25"},250,function() {
				$("#top_nav li.r ul li#location_search ul").css("width",this.offsetWidth-2);	
			});
			self.nav_expanded=1;
		}
	}
	
	this.get_locs = function() {
		str = $("#enter_location input").attr("value");
		$.post("/ping/get_locations",
			{q:str}, 
			function(data) {
				results = "";
				location_results = data;
				for(i=0;i<data.length;i++) {
					result = "<li id=\"" + data[i].zip_code + "\"";
					if(i==0) { result += " class=\"selected\""; self.curr_result = 1;}
					result += ">" + data[i].display_as + "</li>";
					results = results + result;
				}
				$("ul#top_nav_loc_results").html(results).fadeIn("fast");
				$("ul#top_nav_loc_results li").click( function() {
					self.select_location_result($("ul#top_nav_loc_results li").index($(this))+1);
				});
				$(document).contents().not("#top_nav_location_form").click( function() { self.reset_location_results(); });
				$("#enter_location").removeClass("loading");
				
				if(data.length > 0) {
					$("#enter_location").addClass("loaded");
				}
			},
			"json");
	}
	
	
	this.get_url = function(params,clear) {
		if(clear) {
			if(clear == "all") {
				self.url_params = new Array();
			}else{
				for(i=0;i<clear.length;i++) {
					if(self.url_params[clear[i]]) {
						delete self.url_params[clear[i]];
					}
				}
			}
		}
		
		var abs_domain = self.abs_domain;
		for(var i in params) {
			self.url_params[i] = params[i];
		}
		
		var query_parts = new Array();
		for(var i in self.url_params) {
			query_parts.push(i+'='+self.url_params[i]); 
		}
		
		var query = "?"+query_parts.join('&');
		return abs_domain+query;
	}
	
	this.highlight_location_result = function(num) {
		$("ul#top_nav_loc_results li.selected").removeClass("selected");
		$("ul#top_nav_loc_results li:nth-child("+num+")").addClass("selected");
	}
	
	this.reset_location_results = function() {
		$("ul#top_nav_loc_results").hide(0,function() { $(this).html(""); });
		$("#enter_location").removeClass("loaded");
	}
	
	this.select_location_result = function(num) {
		var location_text = $("ul#top_nav_loc_results li:nth-child("+num+")").text();
		$("#enter_location input").val(location_text).hide();
		$("#enter_location #location_object span").text(location_text);
		$("#enter_location #location_object").show();
		$("input[name=\'zip_code\']").val($("ul#top_nav_loc_results li:nth-child("+num+")").attr("id"));
		$("input[name=\'geo_name\']").val(location_text);
		$("input[name=\'sub_search_radius\']").focus();
		self.reset_location_results();
	}
	
	this.turn_off_suggestions = function() {
		$.post("/ping/toggle_location_suggestions",{suggest:"false"});
	}
}

var geo = new geo();
var geo_in = "";

$("document").ready( function() {
	geo.display();
	$("#top_nav_location_form input:text").focus( function() { geo.expand_top_nav(); });
	
	$("#enter_location .cancel").click( function() { geo.clear_location_object(); });
	
	$("#current_location a#national_search").click( function(event) { 
		event.preventDefault();
		window.location = geo.get_url({"clear_location":"true"});
	});
	
	$("#learn_more_toggle a#learn_more").click( function(event) {
		event.preventDefault();
		$(this).parent().hide();
		$("#top_nav_about").slideDown("fast");
		$("#top_nav_about #cancel").click( function() {
			$("#top_nav_about").slideUp("fast",function() {
				$("#learn_more_toggle").fadeIn("fast");
			});
		});
	});
	
	$("#enter_location input").keyup( function(event) {
		
		kc = event.keyCode;
		
		// if no value
		if($(this).val() == "") { 
			geo_in = "";
			clearTimeout(geo.t); 
			geo.reset_location_results(); 
			return;
		}
		
		// make sure they are actually changing value
		if($(this).val() == geo_in) { 
			return;
		}else{ 
			geo_in = $(this).val();
		}
		
		// clear old timer
		if(geo.t_on) {
			clearTimeout(geo.t);
			geo.t_on = false;
		}
		
		// reset timer
		if(!$(this).parent().hasClass("loading")) { $(this).parent().attr("class","").addClass("loading");}
		geo.t = setTimeout(function() { geo.get_locs();},250);
		geo.t_on = true;
	});
	
	$("#enter_location input").keydown( function(event) {
		kc = event.keyCode;
		
		// only accept up, down, enter
		if(kc!=40 && kc!=38 && kc!=13) { return;}
		
		if(geo.curr_result!=0) { 
			
			// enter
			if(kc==13) {
				event.preventDefault();
				geo.select_location_result(geo.curr_result);
			}
			
			t_ch = $("ul#top_nav_loc_results").children().length;
			
			next = false;
			
			//down
			if(kc==40) {
				next = (geo.curr_result+1 > t_ch) ? 1 : geo.curr_result+1;
			// up	
			}else if(kc==38) {
				next = (geo.curr_result-1 < 1 ) ? t_ch : geo.curr_result-1;
			}
			
			if(next) {
				geo.curr_result = next;
				geo.highlight_location_result(next);
			}
		}
	});
	
	$("form#top_nav_location_form").submit( function() {
		if($("form#top_nav_location_form input[name=\'zip_code\']").val() == "" && $("#enter_location input").val() != "city or zip code") {
			locations = "<li class=\"cancel\"></li><li class=\"desc\">Hmmm, did you mean...</li>";
			for(i=0;i<location_results.length;i++) {
				result = "<li><a href=\"" + geo.get_url({"geo":"true","search_radius":$("form#top_nav_location_form input[name=\'search_radius\']").val(),"zip_code":location_results[i].zip_code}) + "\">" + location_results[i].display_as + "</a></li>";
				locations = locations + result;
			}
			geo.clear_notifications();
			$("#top_nav_notification").append(locations).slideDown(150).animate({"height":"-=3"},100,function() {
				$(this).animate({"height":"+=3"},100);
				$(document).contents().not($(this)).click( function() {
					geo.clear_notifications(); 
				});
			});
			return false;
		}else{
			return true;
		}
	});
});
