/**
 * SEARCH MODULE AJAX FUNCTIONS
 */
/**
 * Initialize Search module events
 */
initSearch = function(){
	/**
	 * Search Module Content Event Listeners
	 */
	var partOfTownSelection = $('partOfTownSelectionHijax');
	if(partOfTownSelection){
		partOfTownSelection.observe('change', partOfTownSelectionChange.bind());
		var citySelection = $('citySelectionHijax');
		citySelection.observe('change', citySelectionChange.bind());
		var availabilitySelection = $('availabilitySelectionHijax');
		availabilitySelection.observe('change', availabilitySelectionChange.bind());
		
		// Houston Disabling Logic -- Houston should be available for "Quick-Delivery Homes" but
		// not available for "All Floorplans"
		if(availabilitySelection.getValue() == "floorplans" && citySelection.options[3]) {	
			citySelection.options[3].disabled = true;
		}
	}
	

}
// Add the init function to the array of init functions (See hijax.js)
initFunctions.push('initSearch');

/**
 * citySelectionChange is bound to the city select box onchange in the search module
 *
 */
function citySelectionChange(evt){
	var cityId = this.value;
	var mainUrl = '/ajax/searchFunctions.php?hijax=1&city=' + cityId;
	var updateCommunityEl = 'communitySelectionHijax';
	var updatePartOfTownEl = 'partOfTownSelectionHijax';
	var updateSimpleMapEl = 'simpleMap';

	if(this.value == ""){
		$(updateCommunityEl)[0].selected = true;
		$(updateCommunityEl).disabled = true;
		$(updatePartOfTownEl)[0].selected = true;
		$(updatePartOfTownEl).disabled = true;

	}else{
		url = mainUrl + '&action=showCityCommunities';
		new Ajax.Updater(updateCommunityEl, url, {
			method: 'get',
			evalScripts: true,
			onSuccess: function(transport) {
				$(updateCommunityEl).disabled = false;
			}
		});
		url = mainUrl + '&action=showCityPartsOfTown';
		new Ajax.Updater(updatePartOfTownEl, url, {
			method: 'get',
			evalScripts: true,
			onSuccess: function(transport) {
				$(updatePartOfTownEl).disabled = false;
			}
		});

		url = mainUrl + '&action=showSimpleMap';
		new Ajax.Updater(updateSimpleMapEl, url, {
			method: 'get',
			evalScripts: true
		});
	}
	Event.stop(evt);
	return false;
}

function partOfTownSelectionChange(evt){
	//var partOfTown = this.value;
	var partOfTown = this.getValue();
	//alert ("this: " + this.getValue().length );
	var cityId = $('citySelectionHijax').value;
	var mainUrl = '/ajax/searchFunctions.php?hijax=1&city=' + cityId + '&partOfTown=' + partOfTown;
	var updateCommunityEl = 'communitySelectionHijax';

	url = mainUrl + '&action=showPartOfTownCommunities';
	new Ajax.Updater(updateCommunityEl, url, {
		method: 'get',
		evalScripts: true,
		onSuccess: function(transport) {
			$(updateCommunityEl).disabled = false;
			}
		});

	Event.stop(evt);
	return false;
}

function availabilitySelectionChange(evt){
	var availability = this.getValue();
	var cityselection = $('citySelectionHijax');
	
	if(availability == "floorplans") {	
		if(cityselection.options[3]) {cityselection.options[3].disabled = true;}
		if(cityselection.getValue() == "3") {
			cityselection.selectedindex = 0;
			// fire the change handler
			fireEvent(cityselection, 'change');
		}
	}
	else {
		if(cityselection.options[3]) { cityselection.options[3].disabled = false; }
	}
}

/**
 * A JSON ALTERNATIVE FOR FEWER SERVER HITS
 */
function getCitySearchSelections(evt){
	var cityId = this.value;
	var mainUrl = '/ajax/searchFunctions.php?hijax=1&city=' + cityId;
	var updateCommunityEl = 'communitySelectionHijax';
	var updatePartOfTownEl = 'partOfTownSelectionHijax';
	var updateSimpleMapEl = 'simpleMap';

	if(this.value == ""){
		$(updateCommunityEl)[0].selected = true;
		$(updateCommunityEl).disabled = true;
		$(updatePartOfTownEl)[0].selected = true;
		$(updatePartOfTownEl).disabled = true;

	}else{
		var url = mainUrl;
		new Ajax.Request(url, {
			method:'get',
			onSuccess: function(transport){

				var data = transport.responseText;
				JSON = Array.toJSON(data);
				parseJSON = JSON.evalJSON();
				//alert(JSON);
				alert("result 1: " + JSON[1]);
			}
		});

		/*
		new Ajax.Updater(updateCommunityEl, url, {
			method: 'get',
			evalScripts: true,
			onSuccess: function(transport) {
				$(updateCommunityEl).disabled = false;
				}
		});
		*/
	}
	Event.stop(evt);
	return false;
}

function fireEvent(element,event){
	if(document.createEvent){
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
	else{
		// dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt)
	}
}

