// JavaScript Document

var d3mls_search =
{	
	showHideRefineOptions : function(showHideLink)
	{
		if (document.getElementById("refineOptionsDiv").style.display =="none"){
			document.getElementById("refineOptionsDiv").style.display ="block";
			showHideLink.innerHTML = "Hide Options";
		}
		else {
			document.getElementById("refineOptionsDiv").style.display ="none";
			showHideLink.innerHTML = "More Options";
		}

		return false;
	},
	
	checkAlwaysChecked : function(checkbox)
	{
		if ($(checkbox).not(":checked"))
			$(checkbox).click();
	},

	getLocationChange : function(input)
	{
		if ($(input).val().length > 5){
			$.post('/app/modules/d3mls/ninja_code/search_simple.php', { 'loc' : $(input).val()	},
				function(data){
					if (data.length > 1)
						$(input).next().html(data).show();
					else
						$(input).next().hide();
				}
			);
		}
	},

	locationSuggestFill : function(value, d3mls_property_location_result)
	{
		$(document.forms.search_form.loc).val(value);
		if (typeof d3mls_property_location_result != "undefined" && d3mls_property_location_result != null)
			$(d3mls_property_location_result).parent().hide();
		else
			$(".d3mls_property_location_results").hide();
	},

	/**
	 *	2D Array to hold a list of geographic divisions (currently regions, counties, areas, cities and sub_divisions)
	 */
	selectedLocations : [["Central Maryland"],[],[],[],[]],
	/**
	 *	Get a new container of locations select boxes
	 *	@param $input The clicked input
	 *	@param index The index of the locations select box
	 *	@param $event The click event
	 */
	selectInputs : function($input, index, $event)
	{
		var locations = $input.val().split("|");
		if ($input.is(":checked") && $.inArray(locations[index],this.selectedLocations[index])<0)
			this.selectedLocations[index].push(locations[index]);
		else 
			this.selectedLocations[index] = this._removeElements([locations[index]], this.selectedLocations[index]);

		if (index < 4)//sub_division inputs don't have children
		{
			$.post("/app/modules/d3mls/ninja_code/getLocations.php",
				{
					'locations' : this.selectedLocations,
					'index' : index
				},
				function(data, status)
				{	
					$("#locationsSelectsContainer").replaceWith(data);
				}
			);
		}
	},
	/**
	 *	Remove an array of elements from an array
	 *	@param theseElements Array of strings to remove
	 *	@param fromThisArray Array to remove from
	 *	@return	fromThisArray
	 */
	_removeElements : function(theseElements, fromThisArray)
	{
		$.unique(theseElements);
		$.unique(fromThisArray);

		for (var i=0; i<theseElements.length;i++)
			fromThisArray = $.grep(fromThisArray, function(value) {
				return value != theseElements[i];
			});
		return fromThisArray;
	},

	/**
	 *	Group all locations under location parent (i.e. parent county > child city) and collapse the group
	 *	@param divId String The id of the node containing location parent/child group
	 */
	accordion : function(divId)
	{
		//set each group to open and collapse
		$('#'+divId+' .locationsSelectSection .locationsHeader').click(function() {
			var $locationsHeaderIcon = $(this).find("div.locationsHeaderIcon");
			if ($locationsHeaderIcon.hasClass('locationsHeaderIconClicked'))
				$locationsHeaderIcon.removeClass("locationsHeaderIconClicked");
			else
				$locationsHeaderIcon.addClass("locationsHeaderIconClicked");
			$(this).next().toggle('slow');
		}).next().hide();
		//if location select contains only one group, open that group
		if ($('#'+divId+' > .locationsSelectSection').children().size()<=2)
		{
			$('#'+divId).find(">:first-child .locationsHeader .locationsHeaderIcon")
				.addClass("locationsHeaderIconClicked").end()
				.find(".locationsContent").show();
		}
		else {
			//search each group for checked inputs and open if any found
			$('#'+divId+' .locationsSelectSection').each(function(){

				$(this).find(".locationsContent input:checked").each(
					function()
					{
						$(this).parents(".locationsSelectSection")
							.find(".locationsHeader .locationsHeaderIcon")
							.addClass("locationsHeaderIconClicked").end()
							.find(".locationsContent").show();
					}
				);
			});
		}
	}
};

