Event.observe(document, "click", function(e) {
	var xPos = Event.pointerX(e);
	var yPos = Event.pointerY(e);
	$$(".list_element").each(function(s, index) {
		if (s.visible()) {
			Position.prepare();
			if (!Position.withinIncludingScrolloffsets(s, xPos, yPos)) {
				new Effect.BlindUp(s.id.replace(/selected/, "list"), {
					duration: 0.3,
					beforeStart: function() {
						$(s.id.replace(/list/, "arrow")).src = "/graphics/select_down.gif";
					},
					afterFinish: function() {
						$(s.id.replace(/list/, "selected")).show();
						$(s.id.replace(/list/, "arrow")).onclick = function() {
							showList($(this.id.replace(/arrow/, "selected")));
						}
					}
				});
			}
		}
	});
});
var steps = {
	0: { "request": null, "return": "year" },
	1: { "request": "/ajax_searchmake.php", "return": "make" },
	2: { "request": "/ajax_searchmodel.php", "return": "model" },
	3: { "request": "/ajax_searchpubs.php", "return": "pub" }
};
var curr_step;
var max_width;
function showList(elem) {
	if ($(elem.id.replace(/selected/, "list_elements")).innerHTML.match(/[\w]+/)) {
		$(elem).hide();
		$(elem.id.replace(/selected/, "arrow")).onclick = function() {
			return false;
		};
		if (typeof pub_dialog != "undefined") {
			$(elem.id.replace(/selected/, "list_elements")).setStyle({
				height: "200px"
			});
		} else if (typeof $("step_two") != "undefined") {
			$(elem.id.replace(/selected/, "list_elements")).setStyle({
				height: "400px"
			});
		}
	new Effect.BlindDown(elem.id.replace(/selected/, "list"), {
			duration: 0.3,
			beforeStart: function() {
				$(elem.id.replace(/selected/, "arrow")).src = "/graphics/select_up.gif";
			}
		});
	}
}
function highlightRow(elem, status) {
	var selected_id;
	if ($$("#"+$(elem).ancestors()[1].id+" .list_element_selected").length > 0) {
		$$("#"+$(elem).ancestors()[1].id+" .list_element_selected").each(function(s, index) {
			selected_id = s.id;
		});
	}
	if (elem.id != selected_id) {
		if (status) {
			$(elem).className = "list_element_active";
		} else {
			$(elem).className = "list_element_inactive";
		}
	}
}
function selectElement(step, elem) {
	if (step == 4) {
		$("pubSelected").value = elem.id;
	}
	var this_width = elem.innerHTML.length * 8;
	if (this_width < 250) {
		this_width = 250;
	}
	$($(elem).ancestors()[1].id.replace(/list/, "selected")).setStyle({
		width: this_width+"px"
	});
	$($(elem).ancestors()[1].id.replace(/list/, "selected")).innerHTML = elem.innerHTML;
	$($(elem).ancestors()[1].id.replace(/\_list/, "")).value = elem.id;
	$$("#"+$(elem).ancestors()[1].id+" .list_element_selected").each(function(s, index) {
		s.className = "list_element_inactive";
	});
	$(elem).className = "list_element_selected";
	new Effect.BlindUp($(elem).ancestors()[1].id, {
		duration: 0.3,
		beforeStart: function() {
			$($(elem).ancestors()[1].id.replace(/list/, "arrow")).src = "/graphics/select_down.gif";
			$($(elem).ancestors()[1].id.replace(/list/, "arrow")).onclick = function() {
				showList($(this.id.replace(/arrow/, "selected")));
			}
			if (step < 4) {
				if (step == 3) {
					if (typeof $("step_two") == "undefined") {
						$(steps[step]["return"]+"_list_elements").update("");
					}
				} else {
					$(steps[step]["return"]+"_list_elements").update("");
				}
			}
			try {
				if (max_width > 550) {
					pub_dialog.updateWidth((max_width + 120));
					pub_dialog.showCenter();
				}
			} catch(e) { }
		},
		afterFinish: function() {
			$($(elem).ancestors()[1].id.replace(/list/, "selected")).show();
			$($(elem).ancestors()[1].id.replace(/list/, "selected")).setStyle({
				backgroundColor: "#669900"
			});
			new Effect.Morph($(elem).ancestors()[1].id.replace(/list/, "selected"), {
				style: {
					backgroundColor: "#fff"
				},
				duration: 0.5
			});
		}
	});
	
}
function loadElement(step) {
	if (step < curr_step) {
		for (i=step; i<=curr_step; i++) {
			$(steps[i]["return"]+"_selected").className = "selected_element_disabled";
			$(steps[i]["return"]+"_arrow").className = "arrow_graphic_disabled";
			$(steps[i]["return"]+"_selected").setStyle({
				width: "250px",
				backgroundColor: "#ccc"
			});
			$(steps[i]["return"]+"_list_elements").update("");
			$(steps[i]["return"]+"_selected").update("&nbsp;");
		}
	}
	curr_step = step;
	var pars = $("input_form").serialize();
	$(steps[step]["return"]+"_selected").className = "selected_element";
	$(steps[step]["return"]+"_arrow").className = "arrow_graphic";
	$(steps[step]["return"]+"_selected").update("Please wait. Loading...");
	new Ajax.Request(steps[step]["request"], {
		parameters: pars,
		onSuccess: function(transport) {
			$(steps[step]["return"]+"_list_elements").update(transport.responseText);
			$(steps[step]["return"]+"_selected").update("SELECT ONE");
			$(steps[step]["return"]+"_selected").setStyle({
				backgroundColor: "#fff"
			});
			// Get total height and largest width
			max_width = 0;
			$$("#"+steps[step]["return"]+"_list_elements div[class!=announce]").each(function(s, index) {
				var this_width = s.innerHTML.stripTags().length * 8;
				if (this_width > max_width) {
					max_width = this_width;
				}
			});
			if (max_width < 265) {
				max_width = 265;
			}
			var max_height = ($$("#"+steps[step]["return"]+"_list_elements div").length * 26);
			if (max_height > 400) {
				max_height = 400
			}
			if (typeof pub_dialog == "undefined") {
				$(steps[step]["return"]+"_list").setStyle({
					width: max_width+"px"
				});
			} else {
				$(steps[step]["return"]+"_list").setStyle({
					width: "280px"
				});
			}
			$(steps[step]["return"]+"_list_elements").setStyle({
				height: max_height+"px"
			});
		},
		onFailure: function(transport) {
			alert("There was a problem with the request. Please contact support for further assistance.");
		}
	});
}
function resetDropDown() {
	for (i=0; i<4; i++) {
		$(steps[i]["return"]).value = "";
		if (i == 0) {
			$(steps[i]["return"]+"_selected").update("Select a Year");
			$$("#"+$(steps[i]["return"]+"_selected").ancestors()[1].id+" .list_element_selected").each(function(s, index) {
				s.className = "list_element_inactive";
			});
		} else {
			$(steps[i]["return"]+"_selected").update("&nbsp;");
			$(steps[i]["return"]+"_selected").className = "selected_element_disabled";
			$(steps[i]["return"]+"_arrow").className = "arrow_graphic_disabled";
			$(steps[i]["return"]+"_list_elements").update("");
			$(steps[i]["return"]+"_selected").setStyle({
				width: "250px",
				backgroundColor: "#ccc"
			});
		}
	}
}