var alreadyRun = false;

manipulateSearchForm = function() {
	//$ = jQuery;
	var fieldset = "<fieldset></fieldset>";
	function getLegend(legendName) {
		return jQuery("<legend>"+legendName+"</legend>");
	}
	jQuery('#AdvancedSearch br').remove();
	jQuery('#AdvancedSearch .reset').remove();
	if (!document.getElementById('fieldset0')) {
		jQuery('#AdvancedSearch input[type=submit]').wrap("<fieldset class='submissionFieldset'></div>");
		jQuery('#primary_searchsub_categories').addClass('checkboxGrouping');
	}
	jQuery('#AdvancedSearch input[type=checkbox]').addClass('checkboxInput');
	//go through the search form and apply DOM level changes to create fieldsets and other logical blocks
	var typeOfPropertyCollection = jQuery('#primary_searchcat_subcat_wrapper').add(jQuery('#primary_searchsub_categories'));
	var regionCollection = jQuery('#primary_search_region_location_search');
	
	var priceCollection = jQuery('#price_primary_search_wrapper');
	var propertyFeatures = jQuery('#number_bedrooms_primary_search_wrapper').add(jQuery('#number_bathrooms_primary_search_wrapper')).add(jQuery('#number_garages_primary_search_wrapper')).add(jQuery('#floor_space_primary_search_wrapper')).add(jQuery('#lot_size_primary_search_wrapper')).add(jQuery('#lot_size_units_primary_search_wrapper'));
	var schools = jQuery('#primary_school_primary_search_wrapper').add(jQuery('#intermediate_school_primary_search_wrapper')).add(jQuery('#secondary_school_primary_search_wrapper'));
	var dates;
	if (jQuery('#auction_date_primary_search_wrapper')) {
		dates = jQuery('#auction_date_primary_search_wrapper').add(jQuery('#tender_close_date_primary_search_wrapper'));	
	}
	else if (jQuery('#available_date_primary_search_wrapper')) {
		dates = jQuery('#available_date_primary_search_wrapper');
	}

	var misc = jQuery('#AdvancedSearch label:contains(Keywords)').add(jQuery('#primary_searchkeyword_search')).add(jQuery('#primary_search_last_7_days_only')).add(jQuery('#primary_search_last_7_days')).add(jQuery('#primary_searchhas_open_home_div'));

	if (!document.getElementById('fieldset0')) {
		getLegend("Type of property").insertBefore(typeOfPropertyCollection.wrapAll(fieldset)[0]);
		getLegend("Region").insertBefore(regionCollection.wrapAll(fieldset)[0]);
	}
	else {
		alreadyRun = true;
	}
		getLegend("Price").insertBefore(priceCollection.wrapAll(fieldset)[0]);
	if (alreadyRun != true) {
		
		getLegend("Property features").insertBefore(propertyFeatures.wrapAll(fieldset)[0]);
		getLegend("Schools in area").insertBefore(schools.wrapAll(fieldset)[0]);
		getLegend("Dates").insertBefore(dates.wrapAll(fieldset)[0]);
	}
	
	if (!document.getElementById('fieldset6')) {
		getLegend("Miscellaneous").insertBefore(misc.wrapAll(fieldset)[0]);
	
		jQuery('#primary_search_last_7_days_only').add(jQuery('#primary_search_last_7_days')).wrapAll("<div id='last_7_container'></div>");
	
		var checkboxGroup = jQuery('#last_7_container').add(jQuery('#primary_searchhas_open_home_div'));
		checkboxGroup.wrapAll("<div class='checkboxGrouping'></div>");
	}
	formatSearchForm();
}
formatSearchForm = function() {
	//$ = jQuery;
	//alert("somthing" + alreadyRun);
	jQuery('#AdvancedSearch fieldset fieldset legend').each(

		function(i) {
			if ((alreadyRun == true && (i > 1)) || alreadyRun == false) {
									// alert('running each');
				jQuery(this).addClass('unselectable toggled');
				if (!this.parentNode.id) {
					this.parentNode.id = 'fieldset' + i;
				}
			}
		}
	);
	jQuery('#AdvancedSearch fieldset fieldset').each(
		function(i) {
			if ((alreadyRun == true && i > 1) || alreadyRun == false) {
				var vPadding = Number(jQuery(this).css('paddingTop').replace("px","")) + Number(jQuery(this).css('paddingBottom').replace("px",""));
				//vPadding=0;
				var storeHeight = jQuery(this).height();
				var thisFieldset = 0;
			
				if (document.getElementById( jQuery(this).attr('id') )) {
					thisFieldset = document.getElementById( jQuery(this).attr('id') );
				}
				else {
					thisFieldset.offsetHeight = 0;
				}
			
				//safari includes the top of the legend in its height calculation, take this into account:
				if ($.browser.safari) { 
					jQuery( function() { 
						storeHeight = jQuery(jQuery('#AdvancedSearch fieldset fieldset')[i]).height() + Number(vPadding) + Number(17 + 4);
						storeHeight = thisFieldset.offsetHeight + 21;
					} 
				); 
				}
				//jQuery(this).append("<div style='clear:both;width:100%;'>CSS Height: " + jQuery(this).height() + " , offsetHeight: " + thisFieldset.offsetHeight + "</div>");
				jQuery(this).append("<div class='storeFieldsetHeight' style='display:none;'>"+storeHeight+"</div>");
			}
		}
	);
	jQuery('#AdvancedSearch legend').click(
		function() {
			
			function reveal(idName) {
				var hidden = '#' + idName + " .storeFieldsetHeight";
				jQuery('#'+idName).animate({height: jQuery(hidden).text()}, "fast");
				jQuery('#' + idName + " *").not(jQuery(hidden)).fadeIn("fast");
			}
			
			function collapse(idName) {
				jQuery('#'+idName).animate({height: 20}, "fast");
				var contents = jQuery('#' + idName + " *").not(jQuery('legend'));
				jQuery(contents).hide();		
			}
			
			//alert('toggled');
			if (this.className == 'unselectable unToggled') {
				this.className = ' unselectable toggled';
				reveal(this.parentNode.id);
			}
			else {
				this.className = 'unselectable unToggled';
				collapse(this.parentNode.id);
			}
		}
	);
}
function advancedSearchFormatter(startFieldsetNum) {
	//alert('calling');
	//if (!document.getElementById('fieldset0')) {
		window.setTimeout('manipulateSearchForm()', 700);
	//}


}
