window.addEvent("domready",function () 
{
	// function to hide a question and update contents
		var ToggleQuestion = function (question) 
		{
			// uncollapse question
				if (question.hasClass("question_collapsed"))			
				{
					question.getElement("span.question_summary").innerHTML = "";
					question.toggleClass("question_collapsed");

					var clear_question = function (el)
					{
						if (el.get("tag") == "option")
							el.selected = false;
						else if (el.get("tag") == "input" && el.get("type").match(/radio|check/i))
							el.checked = false;
						else
							el.value = "";
					};

					var a = new Element('span', { "class" : "question_clear", "text" : "clear" } );

					a.addEvent("click", function (e) 
					{
						e.stop();

						question.getElements("input").each(clear_question);
						question.getElements("textarea").each(clear_question);
						question.getElements("select option").each(clear_question);
						question.getElements("select").each(clear_question);
					});

					a.inject(question.getElement("span.question_summary"));
				}
			// collapse question
				else
				{
					// go through each element in question and stick them in the summary
						var summary = "";

						// see if its a date range
							if (question.getElement("#date_start_year"))
							{
								// date dropdowns
									var date_end_month = question.getElement("#date_end_month");
									var date_end_year = question.getElement("#date_end_year");
									var date_start_month = question.getElement("#date_start_month");
									var date_start_year = question.getElement("#date_start_year");

								if (date_start_year.value)
								{
									summary+= "From: ";
									if (date_start_month && date_start_month.value) summary+= date_start_month.options[date_start_month.selectedIndex].get("text") + " ";
									summary+= date_start_year.value + " ";
								}

								if (date_end_year.value)
								{
									summary+= "Until: ";
									if (date_end_month && date_end_month.value) summary+= date_end_month.options[date_end_month.selectedIndex].get("text") + " ";
									summary+= date_end_year.value + " ";
								}
							}
						// normal objects
							else
							{
								var inputs = question.getElements("input");

								if (inputs)
								{
									inputs.each(function (obj) 
									{
										var type = String(obj.get("type")) || "";
										var this_summary = "";

										if (type == "text")
											this_summary = obj.value;
										else if (type.match(/radio|checkbox/i) && obj.checked)
										{
											label = obj.nextSibling;
											this_summary = label.innerHTML;
										}

										// if there's something then add it to the summary
											if (this_summary)
											{
												if (summary) summary+= ", ";
												summary+= this_summary;
											}

									});
								}

								var select_options = question.getElements("select option");

								if (select_options)	
								{
									select_options.each(function (obj) 
									{
										var this_summary = "";

										if (obj.selected && obj.value)
											this_summary = obj.get("text");

										// if there's something then add it to the summary
											if (this_summary)
											{
												if (summary) summary+= ", ";
												summary+= this_summary;
											}

									});
								}
								
							}


						if (!summary)
							summary = '<span class="question_summary_all">All</span>';
						else
							summary = '<span class="question_summary_label">Selected:</span> ' + summary;

						question.getElement("span.question_summary").innerHTML = summary;

						question.toggleClass("question_collapsed");
				}
		}

		$$("div.search_question").each(function (question) 
		{
			// do toggle
				ToggleQuestion(question);

			// add toggle to first label
				var label = question.getElement("label");

				label.addClass("clickable");
				if (Browser.Engine.trident4) label.innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;" + label.innerHTML;

				label.addEvent("click",function (e) 
				{
					e.stop();

					// toggle the rest
						$$("#wrapper_search > div.wrapper_search_group div.search_question").each(function (q) 
						{
							if (q == question) return;
							if (!q.hasClass("question_collapsed")) ToggleQuestion(q);							
						});

					ToggleQuestion(this.parentNode);
				});

				label.title = "Click to Show/Hide";
		});


	// function to hide a group
		var ToggleGroup = function (group) 
		{
			group.toggleClass("group_hidden");

			// toggle the questions
				$$("#wrapper_search > div.wrapper_search_group div.search_question").each(function (q) 
				{
					if (!q.hasClass("question_collapsed")) ToggleQuestion(q);							
				});

			// toggle out first question
				var question = group.getElement("div.search_question");
				if (0&&question) ToggleQuestion(question);
		}

	// get groups
		var groups = $$("div.search_group");

	// add toggle group event to each group title
		groups.each(function (group) 
		{
			var wrapper_group = $(group.parentNode);

			var h4 = wrapper_group.getElement("h4");
			h4.addClass("clickable");

			h4.addEvent("click",function (e) 
			{
				e.stop();
				ToggleGroup(this.parentNode);
			});

			h4.title = "Click to Show/Hide";
		});

	// add ordering events
		var search_click = function () 
		{
			$("submit_search").click();//fireEvent("click");
		}


		$$("#order_by", "#order_direction").each(function (obj) 
		{
			obj.addEvent("change",function () 
			{
				if (Browser.Engine.trident)
				{
					search_click.delay(500);

					return;
				}

				search_click();
			});
			
		});

	
});
