var rrc;
var rrc_data = { charts: [] };

$(document).ready(function()
	{
		var guide = new rrc_guide();
		
		$("ul.sf-menu").superfish();
		$(".blank").attr("target","_blank");

		function cycle_divs($parent,dir,cls)
		{
			var $div = $parent.children("div." + cls);
			jQuery.each($div,function(i,val)
				{
					if($(this).is(":visible"))
					{
						current = i;
					}
				});

			if(dir !== 0)
			{
				$parent.children("div." + cls + ":eq(" + current + ")").slideUp("slow",function()
					{
						current = parseInt(current + dir,10);
						current = (current >= $div.length) ? 0 : (current < 0) ? $div.length - 1 : current;
	
						$parent.children("div." + cls + ":eq(" + current + ")").slideDown("slow");
					});
			}
			else
			{
				$parent.children("div." + cls + "").slideDown("slow");
			}
			return false;
		}

		$(".comment_body").corner();
		$(".nav_head").corner("8px");

		$(".closed-route-reason").live("click",function()
			{
				var id = $(this).attr("rel").split("-")[1];
				$.post(guide.ajax_page, { action: "get_route_closed_reason", token: guide.sid, route: id },
						function(data)
						{
							if(data.message !== undefined)
							{
								$.jGrowl(data.message, {sticky:true});
							}
							else
							{
								guide.speak("error");
							}
						},"json");
				return false;
			});

		$(".closed-wall-reason").live("click",function()
			{
				var id = $(this).attr("rel").split("-")[1];
				$.post(guide.ajax_page, { action: "get_wall_closed_reason", token: guide.sid, wall: id },
						function(data)
						{
							if(data.message !== undefined)
							{
								$.jGrowl(data.message, {sticky:true});
							}
							else
							{
								guide.speak("error");
							}
						},"json");
				return false;
			});

		$(".show-prev-div").click(function()
			{
				var cls = ($(this).attr("rel") == "pics") ? "home-images" : "chart";
				cycle_divs($(this).parent().parent(),-1,cls);
				return false;
			});
		
		$(".show-next-div").click(function()
			{
				var cls = ($(this).attr("rel") == "pics") ? "home-images" : "chart";
				cycle_divs($(this).parent().parent(),1,cls);
				return false;
			});
		
		$.ui.dialog.defaults.bgiframe = true;

		//changed handle param. orig below
		//$(".home_list1").sortable({ connectWith: ['.home_list2'], handle: ".widget_title", placeholder: "ui-state-highlight", update: function(el,ui) {guide.update_home_columns(); }});
		$(".home_list1").sortable({ connectWith: ['.home_list2'], placeholder: "ui-state-highlight", update: function(el,ui) {guide.update_home_columns(); }});
		$(".home_list2").sortable( { connectWith: ['.home_list1'], placeholder: "ui-state-highlight", update: function(el,ui) { guide.update_home_columns(); } });
		$(".home_list1").disableSelection();
		$(".home_list2").disableSelection();
		
		$.tablesorter.addParser({ id: 'key_text', is: function(s) {return false;},
				format: function(s) { try { return $(s).html().toLowerCase(); } catch(e) { return 'a'; }}, type: 'text' });
		
		$.tablesorter.addParser({ id: 'deep_key_text', is: function(s) {return false;}, 
				format: function(s) { try { return $(s).find("a").html().toLowerCase(); } catch(e) { return 'a'; }}, type: 'text'});
		
		$.tablesorter.addParser({ id: 'key_numeric', is: function(s) {return false;},
				format: function(s) { try { return $(s).html(); } catch(e) { return 1; }}, type: 'numeric' });
		
		$("table.sort_this").tablesorter({cssAsc: "header_sort_up",cssDesc: "header_sort_down"});
		$("table.sort_this").bind("sortStart",function(){ $("#sort_progress").show(); }).bind("sortEnd",function() { $("#sort_progress").fadeOut("slow"); });
		
		$("#prefs_tabs a").live("click",function()
			{
				var wrapper = $(this).attr("rel");
				var title = $(this).attr("name");

				jQuery.each($("#prefs_tabs a"),function()
					{
						if($(this).attr("name") === title)
						{
							$(this).addClass("selected");
						}
						else
						{
							$(this).removeClass("selected");
						}

						if($(this).attr("rel") === wrapper)
						{
							$("#" + $(this).attr("rel")).removeClass("hidden");
						}
						else
						{
							$("#" + $(this).attr("rel")).addClass("hidden");
						}
					});
				return false;
			});
		
		$(".pref_slider").each(function()
			{
				var info = $(this).attr("id").split("-")[0];
				var val = $("input." + info + "_count").val();
				
				$(this).slider({ min: 0, value: val, max: 10, step: 1, range: "min", 
						slide: function(event,ui)
						{
							var info = $(this).attr("id").split("-");
							var el_name = info[0].toString();
							$("input[name='" + el_name + "_count']").val(ui.value);
							$("td." + el_name + "_countcol").html(ui.value);
						}
					});
			});

		if($(".search_stars").length > 0)
		{
			$(".search_stars").rating();
		}

		$(".route-star").rating(
			{
				callback: function(value,link)
					{
						var route_id = guide.route_id;

						$.post(guide.ajax_page, { stars: value, action: "stars_vote", token: guide.sid, route: route_id },
								function(data)
								{
									if(data.success == 1)
									{
										if(data.action == "insert")
										{
											guide.speak("Your vote has been counted. Whether or not it will be respected is up to the masses.");
											$("#star_status").html("<p id='last_star_rating'><strong><u>Previous Vote: </u></strong><br />" + data.stars + " stars</p>");
										}
										else if(data.action == "update")
										{
											$("#last_star_rating").html("<strong><u>Previous Vote: </u></strong><br />" + value + " stars");
											guide.speak("Your vote has been counted. Whether or not it will be respected is up to the masses.");
										}
										else if(data.action == "delete")
										{
											if($("#last_star_rating").length > 0)
											{
												$("#last_star_rating").remove();
											}
											guide.speak("Your vote has been removed.");
										}

										$("#route_stars_consensus").attr("class","stars " + data.star_class);
										$("#star_votes_meta").html("<strong>" + data.consensus + "</strong> (" + data.total_votes + " votes)");
									}
									else
									{
										guide.speak("error");
									}
								},'json');
					}
			});
		//catch for.. something.. logged in?
		//$('.route-star').rating(); 

		$(".blockbit").live("mouseover",function()
			{
				if(!$(this).hasClass("inactive"))
				{
					if(!$(this).hasClass("hover"))
					{
						$(this).addClass("hover");
					}
				}
			}).live("mouseout",function()
			{
				if(!$(this).hasClass("inactive"))
				{
					$(this).removeClass("hover");
				}
			});

		if($("#selectable_grade").length > 0)
		{
			$("#selectable_grade").selectable({
					stop: function()
					{
						var val;
						var grades = [];
						$(".ui-selected", this).each(function()
							{
								val = jQuery.trim($(this).html());
								if(val != "Clear" && val != "All")
								{
									grades.push(val);
								}
							});
						
						if(grades.length > 0)
						{
							$("#grades_selected span").html(grades.join(", "));
							$("#_grades").val(grades.join(","));
						}
						else
						{
							$("#grades_selected span").html("None Selected");
							$("#_grades").val("");
						}
					},
					selected: function(event,ui)
					{
						var val;
						if(jQuery.trim($(ui.selected).html()) == "All")
						{
							jQuery.each($(ui.selected).parent().find("li"),function()
								{
									val = jQuery.trim($(this).html());
									if(val != "Clear" && val != "All")
									{
										if(!($(this).hasClass("ui-selected")))
										{
											$(this).addClass("ui-selected");
										}
									}
								});
						}
					}
				});

			$("#gradevalue_clear").click(function()
				{
					$("#selectable_grade").selectable('disable');
					$("#selectable_grade").selectable('enable');
					return false;
				});
			
			$(".clear_selections").click(function()
				{
					$(this).parent().selectable('disable');
					$(this).parent().selectable('enable');
					return false;
				});

		}
		
		if($(".selectable_options").length > 0)
		{
			$(".selectable_options").selectable({
					stop: function()
					{
						var options = [];
						var message = "";
						var val;
						$(".ui-selected", this).each(function()
							{
								val = jQuery.trim($(this).html());
								if(val != "Clear" && val != "All")
								{
									options.push(val);
								}
							});

						$(this).next().val(options.join(","));
					},
					selected: function(event,ui)
					{
						var val;
						if(jQuery.trim($(ui.selected).html()) == "All")
						{
							jQuery.each($(ui.selected).parent().find("li"),function()
								{
									val = jQuery.trim($(this).html());
									if(val != "Clear" && val != "All")
									{
										if(!($(this).hasClass("ui-selected")))
										{
											$(this).addClass("ui-selected");
										}
									}
								});
						}
					}
				});

			jQuery.each($(".selectable_grade_form"),function()
				{
					var defaults;
					var val;
					defaults = $(this).next().val().split(",");
					jQuery.each($(this).find("li"),function()
						{
							val = jQuery.trim($(this).html());
							if(jQuery.inArray(val,defaults) > -1)
							{
								$(this).removeClass("ui-selectee").addClass("ui-selected");
							}
						});
				});
			jQuery.each($(".selectable_options"),function()
				{
					var defaults;
					var val;
					defaults = $(this).next().val().split(",");
					jQuery.each($(this).find("li"),function()
						{
							val = jQuery.trim($(this).html());
							if(jQuery.inArray(val,defaults) > -1)
							{
								$(this).removeClass("ui-selectee").addClass("ui-selected");
							}
						});
				});
		}
		
		$("#route_spray").dialog( { modal:true, autoOpen:false, width:500, height:600, buttons: { Ok: function() {$(this).dialog("close"); }}});
		$("#better_browsers").dialog( { modal:true, autoOpen:false, width:775, height:300, buttons: { 
				Ok: function() 
				{
					guide.create_cookie("rrc_msie_warning",1,30);
					$(this).dialog("close"); 
				}}
				, beforeclose: function()
				{
					guide.create_cookie("rrc_msie_warning",1,30);
					return true;
				}
			});

		if((guide.browser === "msie") && (guide.browser_version == 6) && (guide.read_cookie("rrc_msie_warning") != 1))
		{
			$("#better_browsers").dialog("open");
		}

		$("#show_help").dialog( { bgiframe: true, modal:false, autoOpen:false, width:500, height:600, buttons: { Ok: function() {$(this).dialog("close"); }}});
		$("#user_prefs").dialog( { modal:true, autoOpen:false, width:700, height:600, buttons: { 
						Save: function() 
						{
							var widgets = "";
							var collection = $("#widgets_selection input");
							var count; 

							jQuery.each(collection,function(i,val)
								{
									var name = $(val).attr("name");
									var items = $("input." + name + "_count").val();
									var column = $("[name=" + name + "_column]:checked").val();
									if($(val).attr("type") == "checkbox" && $(val).attr("checked"))
									{
										widgets += name + "=true," + items + "," + column + "&";
									}
								});
								
							count = widgets.length;
							widgets = widgets.substr(0,count - 1);
							
							guide.simple_post({action: "widgets_save", token: guide.sid, widgets: widgets },"Your preferences have been saved.");
							
							$(this).dialog("close");
						},
						Cancel: function() { $(this).dialog("close"); }
					}
			});

		$("#route_projects").dialog( { modal:true, autoOpen:false, width:500, height:600, buttons: { Ok: function() {$(this).dialog("close"); }}});
		$("#notable_owner").dialog({ modal:true, autoOpen:false, width:760, height:600, buttons: { 
				Ok: function() 
				{
					var owner = $("input#hidden_owner_info").val();
					$.post(guide.ajax_page, { action: "owner_note_remove", token: guide.sid, owner: owner },
						function(data)
						{
							var owners = "";
							var cookie_data = " ";
							if(data.result < 0)
							{
								cookie_data = guide.read_cookie("owner_note");
								if(cookie_data.length > 0)
								{
									if((jQuery.inArray(owner,cookie_data.split(",")) < 0))
									{
										owners = cookie_data + owner + ",";
										guide.create_cookie("owner_note",owners,30);
									}
								}
								else
								{
									guide.create_cookie("owner_note",owner + ",",30);
								}
							}
						},"json");
					guide.ep();
					$(this).dialog("close"); 
				}}
				, beforeclose: function()
				{
					var owner = $("input#hidden_owner_info").val();
					$.post(guide.ajax_page, { action: "owner_note_remove", token: guide.sid, owner: owner },
						function(data)
						{
							var owners = "";
							var cookie_data = " ";
							if(data.result < 0)
							{
								cookie_data = guide.read_cookie("owner_note");
								if(cookie_data.length > 0)
								{
									if((jQuery.inArray(owner,cookie_data.split(",")) < 0))
									{
										owners = cookie_data + owner + ",";
										guide.create_cookie("owner_note",owners,30);
									}
								}
								else
								{
									guide.create_cookie("owner_note",owner + ",",30);
								}
							}
						},"json");
					guide.ep();
					return true;
				}
			});
		
		$("#gumby_alert").dialog( { modal:true, autoOpen:false, width:500, height:600, buttons: { Ok: function() {$(this).dialog("close"); }}});
		$("#grade_error").dialog( { modal:true, autoOpen:false, width:500, height:600, buttons: { Ok: function() {$(this).dialog("close"); }}});

		$("#comment_form").dialog( { modal:false, autoOpen:false, width:600, buttons: {
					Save: function()
					{
						var route_id = (guide.page_type == "route") ? guide.route_id : guide.wall_id;
						route_id = (route_id == "") ? $("#_backuprouteid").val() : route_id;
						var comment = $("#comment_textarea").val();
						var cap = $("input[name='security_code']").val();

						$.post(guide.ajax_page, { action: "comment_submit", token: guide.sid, route: route_id, type:guide.page_type, captcha: cap, comment: comment },
							function(data)
							{
								var nice_date;
								var date = new Date();
								var link;
								var html = "";
								var count = $("div#comment_list").find(".comment_number:last").text();

								count = (parseInt(count,10) > 0) ? parseInt(count,10) + 1 : 1;

								date = date.toString().split(" ");
								nice_date = date[1] + " " + date[2] + ", " + date[3];

								if(data.result === undefined)
								{
									guide.speak("error");
								}
								else
								{
									if(guide.userid > 0)
									{
										link = '<a class="comment_user block_link" href="' + guide.base + 'profile.php?mode=viewprofile&u=' + guide.userid;
										link += '">' + guide.username + '</a>';
									}
									else
									{
										link = '<a class="comment_user block_link" href="#">Anonymous Gumby</a>';
									}
									
									if(data.result > 0)
									{
										$("#comment_form").dialog("close");

										
										html = '<div class="">';
										html += '<div class="comment_head">';
										html += '<div class="comment_number">' + count + '</div>';
										html += '<div>';
										html += link;
										html += '<em>said on ' + nice_date + '</em>';
										html += '</div> </div>';
										html += '<div class="comment_body"> <div class="comment_text">' + comment + '</div> </div>';
										html += '<div class="comment_foot"></div>';
										html += '</div>';
										$("#comment_list").append(html);
										guide.speak("Great.. just what the community needed. More words of your whiny wisdom..", {life:6000});
									}
									else if(data.result === 0)
									{
										guide.speak("error");
									}
									else if(data.result < 0)
									{
										guide.speak("Make sure all the information, especially the security code, is correct.","sticky");
									}
								}
							},"json");
						},
						Cancel: function() { $(this).dialog("close"); }
				}
			});

		$("#picture_form").dialog({ modal:true, autoOpen:false, width:500, buttons: {
					Save: function() { $("#submit_route_photo").submit(); },
					Cancel: function() { $(this).dialog("close"); }
				}
			});

		$("#wall_map").dialog({ modal:false, autoOpen:false, width:500, buttons: { Close: function(){$(this).dialog("close");}}});
		$(".show_map").click(function()
			{
				$("#wall_map").dialog("open");
				return false;
			});

		$(".view_victims").click(function()
			{
				$.post(guide.ajax_page, { action: "view_victims", token: guide.sid },
						function(data)
						{
							if(data.message !== undefined)
							{
								$.jGrowl(data.message, {header: "Victims",sticky:true});
							}
							else
							{
								guide.speak("error");
							}
						},"json");
			});
			
		$("table.wall_overview tbody tr").click(function()
			{
				var grade = $(this).children().eq(0).html();
				$.post(guide.ajax_page, { grade: grade , action: "get_wall_routes_like" , token: guide.sid, wall: guide.wall_id },
						function(data)
						{
							if(data.html !== undefined)
							{
								$.jGrowl(data.html, {header: "Selected Routes",sticky:true});
							}
							else
							{
								guide.speak("error");
							}
						},"json");
			});
			
		$(".show_prefs").live("click",function()
			{
				$("#user_prefs").dialog("open");
				return false;
			});

		$("div.tag_list a").live("click",function()
			{
				guide.edit_tags($(this),"add");
				return false;
			});

		$("span.remove_tag span").mouseover(function()
			{
				$(this).addClass("hover");
			}).mouseout(function()
			{
				$(this).removeClass("hover");
			}).live("click",function()
			{
				guide.edit_tags($(this),"delete");
				return false;
			});

		$(".owner_more_info").live("click",function()
			{
				$("#notable_owner").dialog("open");
				return false;
			});

		$("a.add_tag").click(function()
			{
				$("#tag_container").animate({ marginTop: "-10px"	}, 500 );
				$("a.add_tag").hide();
				return false;
			});

		$("div.close a").click(function()
			{
				$("#tag_container").animate({marginTop: "-210px"}, 500);
				$("a.add_tag").show();
				return false;
			});

		$(".add_comment_dialog").click(function()
			{
				var name = (guide.page_type == "route") ? guide.route_name : guide.wall_name;
				$("div.hidden_route_name").html("<h2 style='text-align:center;'>" + name + "</h2>");
				$("#comment_form").dialog("open");
				return false;
			});

		$(".add_new_photo").click(function()
			{
				$("#picture_form").dialog("open");
				return false;
			});

		$("#rrc_search").focus(function()
			{
				if($(this).val() == "Find a route, wall, or user..")
				{
					$(this).val("");
				}
			}).blur(function()
			{
				if($(this).val() == "")
				{
					$(this).val("Find a route, wall, or user..");
				}
			});
		
		$("#user_search").focus(function()
			{
				if($(this).val() == "Find a user..")
				{
					$(this).val("");
				}
			});

		$(document).keyup(function(event)
			{
				guide.keystrokes(event);
			});
		
		$("#share-search-link").click(function()
			{
				$(this).select();
				return false;
			});
		
		$(".quick_reply a.full_spy").click(function()
			{
				$("#_backuprouteid").val($(this).attr("name"));
				$("#comment_form").dialog("open");
				return false;
			});

		$("#page_help").click(function()
			{
				$("#show_help").dialog("open");
				return false;
			});
		
		$("#spray_menu").dialog(
			{
				modal:true, autoOpen:false, width:600, buttons: {
					Save: function()
						{
							var result;
							var lead;
							var date;
							var attempts;
							var route_id = guide.route_id;

							if(route_id < 0 || route_id === undefined)
							{
								if($("#spray_update_route_id").length > 0)
								{
									route_id = $("#spray_update_route_id").val();
								}
								route_id = route_id < 0 ? $("#temprouteid").val() : route_id;
							}

							date = $("#_datesent").val();

							if($("#route_led").is(":checked"))
							{
								lead = 1;
							}
							else if($("#route_not_led").is(":checked"))
							{
								lead = 0;
							}
							else
							{
								lead = 1;
							}

							attempts = $("#_routeattempts").val();
							if(attempts < 0)
							{
								attempts = 0;
							}

							guide.spray(route_id,lead,attempts,date);
							guide.reset_spray_form();

							$(this).dialog("close");
						},
					Cancel: function() { $(this).dialog("close"); guide.reset_spray_form(); }
				},
				open: function(event,ui) { guide.prevent_keystrokes = true; }
			});
			
		$("select[name='rrg_owners']").change(function()
			{
				var owner = $("select[name='rrg_owners']").val();
				if(owner != "" && owner !== undefined)
				{
					$.post(guide.ajax_page, { action: "settings_owner_note", token: guide.sid, owner: owner },
						function(data)
						{
							if(data.result === undefined)
							{
								guide.speak("error");
							}
							else
							{
								if(data.result == 1)
								{
									$("textarea[name='owner_html']").val(data.html);
								}
								else if(data.result === 0)
								{
									guide.speak("error");
								}
								else if(data.result < 0)
								{
									$.jGrowl("You must be an administrator to do this.. what are you doing here?!",{sticky:true});
								}
							}
						},"json");
				}
			});
		
		$(".grade_select").change(function()
			{
				var str = "";
				var selected = $(".grade_select option:selected").length;

				if(selected == 0)
				{
					str = "None Selected";
				}
				else
				{
					$(".grade_select option:selected").each(function() { str += $(this).text() + ", "; });
				}
				
				if(str == ", ")
				{
					str = "None Selected";
				}

				$(".grade_select_status p").html(str);
			});

		$("#paginator a").mouseover(function() { $(this).addClass("highlight"); }).mouseout(function() { $(this).removeClass("highlight"); });

		$("#route_add_spray").click(function()
			{ 
				var name;
				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					name = (guide.page_type != "route") ? $(this).attr("title") : guide.route_name;
					$("#_sprayname").html(name);
					$("#spray_menu").dialog("open"); 
				}
			});

		$("a.lightbox").lightBox({fixedNavigation:true,imageLoading:'images/lightbox-ico-loading.gif',imageBtnPrev:'images/lightbox-btn-prev.gif',imageBtnNext:'images/lightbox-btn-next.gif',imageBtnClose:'images/lightbox-btn-close.gif',imageBlank:'images/lightbox-blank.gif'});

		$("#user_search").autocomplete(search_users,{autoFill:true});
		$("#rrc_search").autocomplete(search_routes,{autoFill:true});
		
		if($("#lost_rrc_search").length > 0)
		{
			$("#lost_rrc_search").autocomplete(search_routes,{autoFill:true});
			$("#lost_rrc_search").focus(function()
				{
					if($(this).val() == "Find a route or wall..")
					{
						$(this).val("");
					}
				});
		}

		$(".remove_dogear").live("click",function()
			{
				var $el = $(this);
				var id = (guide.page_type == "wall") ? guide.wall_id : guide.route_id;
				var name = (guide.page_type == "wall") ? guide.wall_name : guide.route_name;

				var message = "Alright.. that page is no longer dogeared.";
				var nav = "denav_" + guide.page_type + "_" + id;
				var pref = "depref_" + guide.page_type + "_" + id;

				$.post(guide.ajax_page, {action:"delete_dogear",token: guide.sid, id: id, type: guide.page_type },
						function(data)
						{
							if(guide.post_results(data,message) === 1)
							{
								$("li#" + nav).remove();
								$("div#" + pref).remove();
								$el.parent().attr("id","dogear");
								$el.html("Dogear");
								$el.addClass("add_dogear").removeClass("remove_dogear");
							}
						},"json");
				return false;
			});

		$(".add_dogear").live("click",function()
			{
				var nav;
				var $el = $(this);
				var id = (guide.page_type == "wall") ? guide.wall_id : guide.route_id;
				var name = (guide.page_type == "wall") ? guide.wall_name : guide.route_name;

				var message = "Cool.. we've added that to your dogear list, essentially a list of bookmarks. Enjoy faster navigation ;)";
				var row = '<div class="drow center_row" id="depref_' + guide.page_type + '_' + id + '"><div class="column dgrid_4"><strong>' + name;
				row += '</strong></div><div class="column dgrid_2">' + guide.page_type + '</div><div class="column dgrid_2">';
				row += '<a title="Delete this DogEar" id="dogear_delete_' + id + '" rel="' + guide.page_type + '" class="dogear_delete"';
				row += ' href="#">x delete</a></div></div>';

				nav = '<li id="denav_' + guide.page_type + '_' + id + '">';
				nav += '<a title="Jump to ' + name + '" href="?type=' + guide.page_type + '&id=' + id + '">' + name.substr(0,15) + '..</a>';
				nav += '</li>';

				$.post(guide.ajax_page, {action:"add_dogear",token: guide.sid, id: id, type: guide.page_type },
						function(data)
						{
							if(guide.post_results(data,message) === 1)
							{
								$("#dogear_nav").append(nav);
								$("#de_preferences").append(row);
								$el.parent().attr("id","remove_dogear");
								$el.html("Remove Dogear");
								$el.addClass("remove_dogear").removeClass("add_dogear");
							}
						},"json");
				return false;
			});

		$("div.quick_comment").mouseover(function() { $(this).addClass("hover"); }).mouseout(function() { $(this).removeClass("hover");	});
		$("#route_select_jump").change(function() { window.location = "?type=route&id=" + $(this).val();});
		
		var preference_notification = false;
		$(".remove_widget").click(function()
			{
				var li = $(this).parent().parent().parent().parent();
				$(li).remove();
				guide.update_home_columns();
			});
		
		var setdate = "";
		if($("#_datesent").length > 0)
		{
			if($("#_datesent").val() != "")
			{
				setdate = guide.parse_nice_date($("#_datesent").val());
			}
		}

		setdate = setdate === "" ? new Date() : setdate;
		$("#date_sent").datepicker( { maxDate: new Date(), changeMonth: true, changeYear: true, altField:"#_datesent", gotoCurrent: true, defaultDate: setdate });
		$("#route_edit_date").datepicker({ maxDate: new Date(), changeMonth: true, changeYear: true, gotoCurrent:true });
		$(".date_sent").datepicker({ maxDate: new Date(), changeMonth: true, changeYear: true, gotoCurrent:true });
			
		$("#view_previous_attempts").live("click",function()
			{
				var route_id = guide.route_id;

				if(route_id > 0)
				{
					guide.simple_post({ action: "get_attempts", token: guide.sid, route: route_id},"data.message");
				}
				return false;
			});
			
		$(".spray_edit").live("click",function()
			{
				var route_id = -1;
				var selector = "";
				var date_sent = "";
				var attempts = 0;
				var lead = 1;
				var name = "Spray: ";
				name += (guide.page_type != "route") ? $(this).attr("title") : guide.route_name;
				
				if(guide.page_type != "route")
				{
					route_id = $(this).attr("id").split("_")[1];
					$("#temprouteid").val(route_id);

					selector = ".spray_" + route_id + " td.column-";
					
					date_sent = $(selector + "date_sent").attr("title");
					attempts = $(selector + "tries").html();
					lead = (jQuery.trim($(selector + "lead").html()) == "Lead") ? 1 : 0;
				}
				else
				{
					route_id = guide.route_id;
					
					date_sent = $("#_datesent").val();
					attempts = $("#_routeattempts").val();
					lead = $("#_routeled").val() ? 1 : 0;
				}
				$("#_sprayname").html(name);
				
				guide.set_spray_metadata({attempts: attempts, lead: lead, sprayDate: date_sent});
				
				$("#spray_update_route_id").val(route_id);
				$("#spray_menu").dialog("open");
			});

		$(".spray_delete").click(function()
			{
				var route_id = $(this).attr("id");
				var str_array = route_id.split("_");
				var info;
				route_id = str_array[1];
				if(confirm("Are you sure you want to delete this from your spray?"))
				{
					guide.sp();
					$.post(guide.ajax_page, { action: "delete_spray_route", token: guide.sid, route: route_id },
							function(data)
							{
								var success = "Ok.. that was deleted.. why did you enter it in the first place?";
								success += "Was it to entice a member of the other sex?.. Poser..";
								
								if(guide.post_results(data,success) === 1)
								{
									$(".spray_" + route_id).remove();
									guide.run_down($("#user_score").html(),data.score,$("#user_score"));
									guide.run_down($("#user_total_sent").html(),data.total,$("#user_total_sent"));
								}
								guide.ep();
							},"json");
				}
				else
				{
					$.jGrowl("Weirdo..");
				}
			});

		$("body").ajaxStart(function() { guide.sp(); }).ajaxStop(function() { guide.ep(); });

		$(".projects_delete").click(function()
			{
				var route_id = $(this).attr("id");
				var str_array = route_id.split("_");
				route_id = str_array[1];
				if(confirm("Are you sure you want to delete this from your projects?"))
				{
					$.post(guide.ajax_page, { action: "delete_project", token: guide.sid, route: route_id },
							function(data)
							{
								var success = "Giving up that easily.. weaksauce..";
								if(guide.post_results(data,success) === 1)
								{
									$(".project_" + route_id).remove();
								}
							},"json");
				}
				else
				{
					$.jGrowl("Weirdo..");
				}
			});

		$("input[name='item_toggle']").click(function()
			{
				var el = $(this).val();
				var item = el.split("-");
				var opt = $(this).attr("value");
				var val = $(this).is(":checked");
				
				//PJV: if is-spray or is-full-spy
				// see templates.php
				el = "column-" + item[1];
				
				//else
				el = item[1];
				var status;

				if($(this).attr("checked"))
				{
					$("." + el).removeClass("hidden");
					status = "show";
					
				}
				else
				{
					$("." + el).addClass("hidden");
					status = "hide";
				}

				//PJV: check for page = spray||route||spy
				// see templates.php
				// return json
				$.post(guide.ajax_page, { action: "update_screen_options", token: guide.sid, page: "route", option: opt, value: val },
						function(data)
						{
							//$.jGrowl("Something's gone to the pie hole.. Let us know and we'll maybe do something about it. Maybe.. as in 'maybe the cubs'll win the penant");
						},"json");
			});
			
		$("#spray_options_link").click(function () 
			{
				$("#spray_screen_options_wrap").slideToggle("slow", function()
					{
						if($(this).hasClass("spray_screen_options_open") ) 
						{
							$(this).removeClass("spray_screen_options_open");
						} 
						else 
						{
							$(this).addClass("spray_screen_options_open");
						}
					});
				return false;
			});

		$('.route_pic').tooltip({ delay: 0, showURL: false, top: 30, positionLeft: true,
				bodyHandler: function() { var img = $(this).next().attr("title"); return $("<img height='400' />").attr("src", img); } 
			});
		
		$('#user_search').tooltip({ delay: 0, showURL: false, top: 10 });
		$('#rrc_search').tooltip({ delay: 0, showURL: false, top: 10 });
		$('.fa_list').tooltip({track: true, delay: 0, showURL: false, top: 10, positionLeft: true, showBody: " - "});

		$(".wall_overview tr").mouseover(function() { guide.row_over(this); }).mouseout(function() { guide.row_over(this); });
		$(".wall_list tr").mouseover(function() { guide.row_over(this); }).mouseout(function() { guide.row_over(this); });

		$(".nav_head").click(function()
			{
				var name = $(this).attr("name");
				var selector = "[name='" + name + "']";
				if($(selector + " + div").is(":hidden"))
				{
					$(selector + " + div").slideDown("slow");
				}
				else
				{
					$(selector + " + div").slideUp("slow");
				}
				return false;
			});

		$(".expand_header").click(function()
			{
				var div;
				if(!$(this).hasClass("search_cell"))
				{
					div = $(this).next();
					guide.slide(div);
					return false;
				}
			});

		$(".close_el").click(function()
			{
				var div = $(this).parent().parent().next();
				guide.slide(div);
				$(this).toggleClass("open_el");
				return false;
			});

		$("#wall_hide_sent").click(function()
			{
				if($(this).hasClass("shown"))
				{
					$(this).removeClass("shown");
					$(this).html("Show Sent");
					$(".route_sent").hide();
				}
				else
				{
					$(this).addClass("shown");
					$(this).html("Hide Sent");
					$(".route_sent").show();
				}
				return false;
			});

		$("#route_delete_spray").live("click",function()
			{
				var route_id = guide.route_id;

				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					if(route_id > 0)
					{
						if(confirm("Are you sure you want to delete this from your spray?"))
						{
							$.post(guide.ajax_page, { action: "delete_spray_route", token: guide.sid, route: route_id, type: "route" },
									function(data)
									{
										var message = "Ok.. that was deleted.. why did you enter it in the first place?";
										message += "Was it to entice a member of the other sex?.. Poser..";
										
										if(guide.post_results(data,message) === 1)
										{
											message = "";
											if(guide.page_type == "route")
											{
												$("#route_add_spray").removeClass("hidden");
												$("#route_add_spray").removeClass("inactive");
												$("#spray_edit_options").addClass("hidden");
												$(".project_col").removeClass("inactive");
												$(".attempts_col").removeClass("inactive");

												if($("#route_sent").length > 0)
												{
													$("#route_sent").remove();
												}
											}

											message = "<br /><b>Score:</b> " + data.score;
											message += "<br /><b>Routes:</b> " + data.total;
											message += "<br /><b>Oldest Send:</b> " + data.oldest;
											message += "<br /><b>Most Recent Send:</b> " + data.recent;
											message += "<br /><b>Hardest Send:</b> " + data.hardest;

											guide.speak(message,"sticky");
										}
										
										return false;
									},"json");
						}
					}
				}
			});

		$(".grade_vote").click(function()
			{
				var grade_val = $(".grade_select").val();
				var route_id = guide.route_id;

				if(grade_val > 0)
				{
					$.post(guide.ajax_page, { grade: grade_val, action: "grade_vote", token: guide.sid, route: route_id },
							function(data)
							{
								if(data !== undefined)
								{
									if(data.success == 1)
									{
										if(data.action == "insert")
										{
											$.jGrowl("Your vote has been counted. Whether or not it will be respected is up to the masses.", {life:6000});
											$("#grade_form").after("<p id='last_rating'><strong><u>Previous Vote: </u></strong><br />" + data.grade + "</p>");
										}
										else
										{
											$.jGrowl("Your vote has been counted. Whether or not it will be respected is up to the masses.", {life:6000});
											$("#last_rating").html("<strong><u>Previous Vote: </u></strong><br />" + data.grade);
										}
										$("#grade_consensus").html("<strong>" + data.consensus + "</strong> (" + data.total_votes + " votes)");
									}
									else
									{
										guide.speak("error");
									}
								}
							},'json');
				}
				else
				{
					$("#grade_error").dialog("open");
				}
			});

		$("a.spray_button").click(function()
			{
				var route_id = guide.route_id;

				$.post(guide.ajax_page, { action: "show_route_spray", token: guide.sid, route: route_id },
						function(data)
						{
							$("#route_spray tbody").html(data);
							$("#route_spray").dialog("open");
							$("table.sort_this").trigger("update");
						});
				return false;
			});

		$("#remove_recommended").mouseover(function() { $(this).addClass("hover"); }).mouseout(function() { $(this).removeClass("hover"); }).click(function() { guide.rec(-1); });
		$("#not_recommended").mouseover(function() { $(this).addClass("hover"); }).mouseout(function() { $(this).removeClass("hover");}).click(function() { guide.rec(0); });
		$("#recommended").mouseover(function() { $(this).addClass("hover"); }).mouseout(function() { $(this).removeClass("hover"); }).click(function() { guide.rec(1); });
		
		$("a.project_button").click(function()
			{
				var route_id = guide.route_id;

				$.post(guide.ajax_page, { action: "show_route_projects", token: guide.sid, route: route_id },
						function(data)
						{
							$("#route_projects tbody").html(data);
							$("#route_projects").dialog("open");
							$("table.sort_this").trigger("update");
						});
				return false;
			});
		
		$(".dogear_delete").live("click",function()
			{
				var ear_num = $(this).attr("id").split("_")[2];
				var type = $(this).attr("rel");
				var $el = $(this);
				
				$.post(guide.ajax_page, {action:"delete_dogear",token: guide.sid, id:ear_num,type:type },
						function(data)
						{
							if(guide.post_results(data,"Dogear Removed") === 1)
							{
								$el.parent().parent().remove();
								$("#denav_" + type + "_" + ear_num).remove();
							}
						},"json");
			});

		$(".delete_victim").live("click",function()
			{
				var id = $(this).attr("rel");
				var $el = $(this);
				
				$.post(guide.ajax_page, {action:"delete_victim",token: guide.sid, user:id },
						function(data)
						{
							if(guide.post_results(data,"The poor sap is free now. You're not spying on them anymore.") === 1)
							{
								$el.parent().parent().remove();
								$("#spynav_" + id).remove();
							}
						},"json");
			});
		
		$(".add_partner").live("click",function()
			{
				var message = "";
				if(guide.logged_in)
				{
					message = "Are you sure you want to add " + guide.currently_viewing_username;
					if(guide.partner > 0)
					{
						message += " and drop " + guide.partner_name + " as your partner?";
					}
					else
					{
						message += " as your partner?";
					}
					
					if(confirm(message))
					{
						$.post(guide.ajax_page, { action: "add_partner", token: guide.sid, user: guide.currently_viewing },
							function(data)
							{
							
								if(data.result === undefined)
								{
									guide.speak("error");
								}
								else
								{
									if(data.result > 0)
									{
										$.jGrowl("Hopefully you don't trust this person to actually belay you.. do you!?", {life:6000});
										$(".add_partner").addClass("remove_partner").removeClass("add_partner");
										$(".remove_partner").next().html("Remove partner");
									}
									else if(data.result === 0)
									{
										guide.speak("error");
									}
									else if(data.result < 0)
									{
										guide.speak("login");
									}
								}
							},"json");
						return false;
					}
				}
				else
				{
					guide.speak("login");
				}
			});
		
		$(".remove_partner").live("click",function()
			{
				if(guide.logged_in)
				{
					if(confirm("Are you sure you want to remove this climber as your partner?"))
					{
						$.post(guide.ajax_page, { action: "remove_partner", token: guide.sid },
							function(data)
							{
								if(data.result === undefined)
								{
									guide.speak("error");
								}
								else
								{
									if(data.result > 0)
									{
										$.jGrowl("What'd crept into your panties this morning?!", {life:6000});
										$(".remove_partner").addClass("add_partner").removeClass("remove_partner");
										$(".add_partner").next().html("Add as partner");
									}
									else if(data.result === 0)
									{
										guide.speak("error");
									}
									else if(data.result < 0)
									{
										guide.speak("login");
									}
								}
							},"json");
					}
				}
				else
				{
					guide.speak("login");
				}
			});

		$(".spy_climber").live("click",function()
			{
				if(guide.logged_in)
				{
					$.post(guide.ajax_page, { action: "spy_climber", token: guide.sid, user: guide.currently_viewing },
						function(data)
						{
							if(data.result !== undefined)
							{
								if(data.result >= 5)
								{
									$.jGrowl("Whoa.. you're being way too creepy. Your legal limit of victims is capped at five. Go read another climbing magazine and be sure to bring a paper towel..", {life:6000});
								}
								else if(data.result == 1)
								{
									$.jGrowl("You really do need a life..", {life:6000});
									$(".spy_climber").addClass("stop_spying").removeClass("spy_climber");
									$(".stop_spying").next().html("Stop spying on this climber");
								}
								else if(data.result == 2)
								{
									$.jGrowl("Looks like you weren't spying this person in the first place. What are you doing here?", {life:6000});
								}
							}
							else
							{
								guide.speak("error");
							}
						},"json");
				}
				else
				{
					guide.speak("login");
				}
			});
		
		$(".stop_spying").live("click",function()
			{
				if(guide.logged_in)
				{
					$.post(guide.ajax_page, {action: "stop_spying", token: guide.sid, user: guide.currently_viewing },
						function(data)
						{
							if(data.result !== undefined)
							{
								if(data.result === 1)
								{
									$.jGrowl("Well it's about time..", {life:6000});
									$(".stop_spying").addClass("spy_climber").removeClass("stop_spying");
									$(".spy_climber").next().html("Spy on this climber");
								}
								else if(data.result === 0)
								{
									guide.speak("error");
								}
								else if(data.result === 2)
								{
									$.jGrowl("Looks like you weren't spying this person in the first place. What are you doing here?", {life:6000});
								}
								else
								{
									guide.speak("error");
								}
							}
							else
							{
								$.jGrowl("Looks like you weren't spying this person in the first place. What are you doing here?", {life:6000});
							}
						},"json");
				}
				else
				{
					guide.speak("login");
				}
			});
		
		$("div.spy_comment").mouseover(function()
			{
				var name = $(this).attr("name").split("_")[2];
				$("a[name='" + name + "']").parent().removeClass("hidden");
			}).mouseout(function()
			{
				var name = $(this).attr("name").split("_")[2];
				$("a[name='" + name + "']").parent().addClass("hidden");
			});

		$("div.quick_reply a").click(function()
			{
				$("input#hidden_route_id").val($(this).attr("name"));
				var route_name = $(this).attr("title").replace(/quick reply to/i,"");
				$("div.hidden_route_name").html("<h2 style='text-align:center;'>" + route_name + "</h2>");
				$("input[name='comment_submit']").next().remove();
			});
		
		$(".attempted").live("click",function()
			{
				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					guide.attempt("add");
				}
			});
		
		$(".attempt_subtract").live("click",function()
			{
				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					guide.attempt("subtract");
				}
			});

		$("#wizard_tables").click(function(event)
			{
				if($(event.target).is(".submit_spray img"))
				{
					var route_id = $(event.target).parent().attr("name");
					var row = $(event.target).parent().parent().parent();
					var lead = $("select[name='lead_" + route_id + "']").val();
					var tries = $("select[name='tries_" + route_id + "']").val();
					
					$.post(guide.ajax_page, { action: "spray_wizard_submit", token: guide.sid, route: route_id, lead: lead, tries: tries },
						function(data)
						{
							if(data !== 0 && data < 10000)
							{
								var td_array = $(row).children();
								$(td_array[0]).html("<img border='0' alt='Sent!' title='Sent!' src='images/check.gif' style='height:20px' />");
								$(td_array[5]).html("<strong>Added<\/strong>");
								$(td_array[6]).html("<strong>Added<\/strong>");
								$(td_array[7]).html("<strong>Added<\/strong>");
							}
						});
				}
				return false;
			});

		$("img.save_search").click(function()
			{
				$.post(guide.ajax_page, { action: "save_advanced_search" , token: guide.sid, params: $("input[name='search_params']").val() },
					function(data)
					{
						if(data.result === undefined)
						{
							guide.speak("error");
						}
						else
						{
							if(data.result > 0)
							{
								$.jGrowl("Your search will be saved..");
							}
							if(data.result === 0)
							{
								guide.speak("error");
							}
							if(data.result < 0)
							{
								guide.speak("login");
							}
						}
					},"json");
			});

		$('td.spray_now').click(function(e)
			{
				var id = -1;
				var name  = ($(this).is("td")) ? $(this).next().text() : $(this).parent().next().text();
				if($(this).is("td"))
				{
					id = $(this).find("span").attr("id").split("_")[2];
				}
				else
				{
					id = $(this).attr("id").split("_")[2];
				}
				
				$("#_sprayname").html(name);
				$("#temprouteid").val(id);
				$("#spray_menu").dialog("open");
				return false;
			});

		$(".spray_attempts").mouseover(function()
			{
				$(this).addClass("highlight");
			}).mouseout(function()
			{
				$(this).removeClass("highlight");
			});

		$(".spray_attempts").live("click",function(e)
			{
				var attempts;
				jQuery.each($(".spray_attempts"),function() { $(this).removeClass("active"); });
				
				$(this).addClass("active");
				attempts = $(e.target).attr("id").split("_")[1];
				$("#_routeattempts").val(attempts);
				
				return false;
			});

		$("#project_remove").live("click",function()
			{
				var $pe = $(this);
				var route_id = guide.route_id;
				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					$.post(guide.ajax_page, { action: "delete_project" , token: guide.sid, route: route_id },
						function(data)
						{
							if(data.result === undefined)
							{
								$.jGrowl("Heh.. something went wrong. Let us know about it nicely and we might do something about it. Otherwise, just deal..", {life:6000});
							}
							else
							{
								if(data.result >= 1)
								{
									$.jGrowl("Don't give up so easily.. one more go is all it needs..", {life:6000});
									$pe.attr("id","project_add").html("+ add");
								}
								else if(data.result === 0)
								{
									$.jGrowl("Heh.. something went wrong. Let us know about it nicely and we might do something about it. Otherwise, just deal..", {life:6000});
								}
								else if(data.result === -9999)
								{
									$.jGrowl("You're not projecting this to begin with.. how'd you do this?!", {life:6000});
								}
								else if(data.result < 0)
								{
									guide.speak("login");
								}
							}
						},"json");
					return false;
				}
			});
			
		$("#project_add").live("click",function()
			{
				var $pe = $(this);
				var route_id = guide.route_id;

				if($(this).hasClass("inactive"))
				{
					if($("#_backupuserid").val() > 0)
					{
						$.jGrowl("This feature is currently inactive. You might have previously sent or.. well.. maybe we messed up. If so, let us know.",{sticky:true});
					}
					else
					{
						guide.speak("login");
					}
					return false;
				}
				else
				{
					$.post(guide.ajax_page, { action: "project_route" , token: guide.sid, route: route_id },
						function(data)
						{
							if(data.result === undefined)
							{
								guide.speak("error");
							}
							else
							{
								if(data.result >= 1)
								{
									$.jGrowl("So.. this is your new project. You know what Ghandi's project was? World peace.. yours is some pile of choss in eastern Kentucky.. dream big!", {life:6000});
									$pe.attr("id","project_remove").html("x end");
								}
								else if(data.result === 0)
								{
									guide.speak("error");
								}
								else if(data.result === -9999)
								{
									$.jGrowl("Relax.. you're already projecting this.. sheesh..", {life:6000});
								}
								else if(data.result < 0)
								{
									guide.speak("login");
								}
							}
						},"json");
					return false;
				}
			});
			
		$("a.ajax_wall").click(function()
			{
				$("#wizard_tables table tbody").html("<tr><td colspan='8'><h1>Loading Wall Data..<\/h1><br /><img src='images/loading_animation.gif' alt='Loading..'/><\/td><\/tr>");
				$.post(guide.ajax_page, { action: "spray_wizard", token: guide.sid, wall: $(this).attr("name") },
					function(data)
					{
						$("#wizard_tables table tbody").html(data);
						$("table.sort_this").trigger("update");
						$('.route_pics').tooltip({ delay: 0, showURL: false, top: 30, positionLeft: true,
								bodyHandler: function() 
									{ 
										var img = $(this).parent().attr("rel");
										return $("<img height='400' />").attr("src", img);
									} 
							});
					});
			});
		
		if(guide.userid < 0)
		{
			$("input[name='username']").focus();
		}
		
		$("input.checkall").click(function()
			{
				var searchname = $(this).attr("name");
				var els = $("input[name='" + searchname.slice(0,searchname.length-1) + "[]']");
				for(var i=0;i<els.length;i++)
				{
					if($(this).is(":checked"))
					{
						$(els[i]).attr("checked","checked");
					}
					else
					{
						$(els[i]).removeAttr("checked");
					}
				}
			});
		$(".view_charts").click(function()
			{
				var id = (guide.currently_viewing > 0) ? guide.currently_viewing : -1;
				window.location = "?type=spray&view=charts&id=" + id;
			});
		
		$(".view_table").click(function()
			{
				var id = (guide.currently_viewing > 0) ? guide.currently_viewing : -1;
				window.location = "?type=spray&id=" + id;
			});
	});
