var search_simple	= new ninja('/app/modules/d3mls/ninja_code/search_simple.php');
var search_field	= null;
var results_div		= null;

// callback function to handle returned data
search_simple.callback = function(response_data) {
	if (response_data != '') {
		results_div.innerHTML = response_data;		
		results_div.style.visibility = 'visible';
	} else {
		results_div.style.visibility = 'hidden';
	}
}

// handle a user typing into the property location's box
function location_change() {
	var location = search_field.value;
	if ((typeof(location) != 'undefined') && (location != '') && (location.length >= 3)) {
		// send the location to be "searched"
		search_simple.abort();
		search_simple.update("loc="+encodeURIComponent(location), "post");
	} else {
		results_div.style.visibility = 'hidden'
	}
}

// a suggestion has been "selected"
function location_suggest_fill(location) {
	search_field.value = location;
	results_div.style.visibility = 'hidden'
}

// setup the property location's changing events
function setup_location_change() {
	search_field	= document.getElementById('property_location');
	results_div		= document.getElementById('property_location_results');
	if (search_field != null) {
		function keyup_handler() {
			return function () { location_change() };
		}
		// add a key(up) listener for the search-suggestion box
		search_field.onkeyup = keyup_handler();
		
		function mousedown_handler() {
			return function() { results_div.style.visibility = 'hidden'; };
		}
		// add a mouse(up) listener to the entire "body" of the page to hide the suggestion box
		var body = document.getElementsByTagName('body')[0];
		if (body.parentNode != null) body.parentNode.onmousedown = mousedown_handler();
		else body.onmouseup = mousedown_handler();
		
		if (results_div == null) {
			// the "results" div doesn't exist, so create one!
			results_div = document.createElement('div');
			results_div.setAttribute('id', 'property_location_results');
			results_div.setAttribute('class', 'd3mls_property_location_results');
			document.getElementsByTagName('body')[0].appendChild(results_div);
		}
		var padd_left = getStyle(results_div, 'padding-left');
		var padd_right = getStyle(results_div, 'padding-right');
		var border_left = getStyle(results_div, 'border-left-width');
		var border_right = getStyle(results_div, 'border-right-width');
		var total = parseInt(padd_left.substring(0, (padd_left.length - 2))) + parseInt(padd_right.substring(0, (padd_right.length - 2))) + parseInt(border_left.substring(0, (border_left.length - 2))) + parseInt(border_right.substring(0, (border_right.length - 2)));
		var dims = search_field.getBoundingClientRect();
		var css = 'visibility: hidden; left: '+dims.left+'px; top: '+(dims.top + search_field.offsetHeight)+'px; width: '+(search_field.offsetWidth - total)+'px;';
		// IE doesn't allow setAttribute('style'), so we have to do style.setAttribute
		(results_div.style.setAttribute) ? results_div.style.setAttribute('cssText', css) : results_div.setAttribute('style', css);
	}
	
	// add the "submit" button's functionality
	var submit_btn = document.getElementById('property_search_submit');
	if (submit_btn != null) {
		function mouseclick_handler() {
			return function() { if (document.search_form != null) document.search_form.submit(); }
		}
		submit_btn.onclick = mouseclick_handler();
		submit_btn.setAttribute('href', 'javascript:void(0);');
	}
}

// IE doesn't work well with getComputedStyle, this function will use it (if allowed) and otherwise get some "deep CSS"
function getStyle(elm, css){
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		css_val = document.defaultView.getComputedStyle(elm, "").getPropertyValue(css);
	} else if(elm.currentStyle) {
		css = css.replace(/\-(\w)/g, function (str, p1) { return p1.toUpperCase(); });
		css_val = elm.currentStyle[css];
	}
	return css_val;
}

// add the "onload" event(s)
if (window.attachEvent) {
	window.attachEvent('onload', setup_location_change);
} else if (window.addEventListener) {
	window.addEventListener('load', setup_location_change, false);
} else {
	document.addEventListener('load', setup_location_change, false);
}
