// Utility functions for the d3mls search

var d3mls_search =
{
	setSearchDefaults : function($input)
	{
		if ($input.val()=="")
			$input.val($input.attr("default"));
	},

	setupSearchForm : function(divId)
	{
		$("#"+divId+" .searchnew").each(function(){
			$(this).bind("click", function(e){
				e.preventDefault();
				$("#"+divId+" input[type=text]").each(function(){
					if ($(this).val()==$(this).attr("default"))
						$(this).val("");
				});
				//$("form[name=search_form]").append("<input type='text' name='scrolltops' value='"+d3mls_search.selectScrollTops.join(",")+"' />");
				$("form[name=search_form]").submit();
			});
		});
//		$("#"+divId+" input[type=text]").each(function(){
//			var $input = $(this);
//			d3mls_search.setSearchDefaults($input);
//			$input.bind("click", function(){
//				if ($input.val()==$input.attr("default"))
//					$input.val("");
//			})
//			$input.bind("blur", function(){
//				d3mls_search.setSearchDefaults($input);
//			});
//		});

		this.setupLoadingIcon();
		this.setupButtonsToActivateLoadingIcon(divId)
	},
	
//	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 > 2 && parseInt($(input).val()) ) || $(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)
//	{
//		$(d3mls_property_location_result).parent().prev().val(value);
//		$(".d3mls_property_location_results").hide();
//	},

	/**
	 *
	 */
	$loadingBlanket : null,
	$loadingIcon : null,

	activateLoadingIcon : function()
	{
		this.$blanket.show();
		this.$loadingIcon.show();
	},

	setupLoadingIcon : function()
	{
		this.$blanket = $("<div class='loadingBlanket'></div>").css({
				height: $(document).height()
			}).appendTo(document.body);

		this.$loadingIcon = $("<img class='loadingIcon' src='/images/ajax-loader.gif' />").css({
				"top"		: ($(window).height() / 2) - (300 / 2),
				"left"		: ($(window).width() / 2) - (400 / 2)
			}).appendTo(document.body);
	},

	setupButtonsToActivateLoadingIcon : function(divId)
	{
		$("#"+divId+" a.searchnew").each(function(){
			$(this).bind("click", function(){
				d3mls_search.activateLoadingIcon();
			});
		});
	},

	setupSearchTabs : function(divId)
	{
		this.setupSearchForm(divId);
		this.setupLoadingIcon();

		$("#"+divId).tabs();

		this.setupButtonsToActivateLoadingIcon(divId);

//		$("#"+divId+" > #generalSearchDiv >#locationsSection > #locationsSelectsContainer div.locations input[type=checkbox]").each(
//			function()
//			{
//				$(this).bind("click",function(event){
//					d3mls_search.selectInputs($(this),parseInt($(this).attr("index")),event);
//				});
//			}
//		);
	},

	/**
	 *	2D Array to hold a list of geographic divisions (currently regions, counties, areas, cities and sub_divisions)
	 */
//	selectedLocations : [["Eastern Shore"],[],[],[],[]],
	/**
	 *	Array of scrollTops, one per select box, for remembering where in the box a location input was checked
	 */
//	selectScrollTops : [0,0,0,0,0],
	/**
	 *	Get a new set of locations select boxes for the container (#locationsSelectsContainer)
	 *	@param $input The clicked input
	 *	@param index The index of the locations select box
	 *	@param $event The click event
	 */
//	selectInputs : function($input, index, $event)
//	{
//		this._dim($(".locationsSelects:eq("+(index+1)+")"), true);
//		this.selectScrollTops[index] = $input.parents(".locationsSelects").scrollTop();
//		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/xml/getLocations.php",
//				{
//					'locations' : this.selectedLocations,
//					'index' : index
//				},
//				function(data, status)
//				{
//					$("#locationsSelectsContainer").replaceWith(data);
//					$(".locationsSelects").each(function(i){
//						var scrollTopValue = d3mls_search.selectScrollTops[i+1];
//						$(this).scrollTop(scrollTopValue);
//					});
//				}
//			);
//		}
//	},
//
//	updateScrolltops : function(scrolltopsAr)
//	{
//		this.selectScrollTops = scrolltopsAr;
//		$(".locationsSelects").each(function(i){
//			var scrollTopValue = d3mls_search.selectScrollTops[i+1];
//			$(this).scrollTop(scrollTopValue);
//		});
//	},

	/**
	 * Dim/undim a div's content
	 * @param jQuery $div Div to dim
	 * @param Boolean dim True to dim, false to undim
	 */
	_dim : function($div, dim)
	{
		$div.css({ 
			"color" :"black",
			"filter" : "alpha(opacity="+((dim)?40:100)+")",
			"-moz-opacity" : ((dim)?.40:1),
			"opacity" : ((dim)?.40:1)
			});
	}


	/**
	 *	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
	 */
//	locationsAccordion : 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();
//					}
//				);
//			});
//		}
//	},

	/**
	 *	Make an accordion. Assumes div.accordionPanel and then the div to collapse/show
	 *	@param divId String The div containing the accordionPanels
	 */
//	accordion : function(divId)
//	{
//		$('#'+divId+' .accordionPanel').click(function() {
//			$(this).next().toggle('slow');
//		}).next().hide();
//	}
};

