// --ninja (also known as ajaxObject;)
function ninja(url,callbackFunction){var that=this;this.updating=false;this.change_url=function(new_url){this.abort();urlCall=new_url;}
this.abort=function(){if(that.updating){that.updating=false;that.AJAX.abort();that.AJAX=null;}}
this.update=function(passData,postMethod){if(that.updating)return false;that.AJAX=(window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");if(that.AJAX==null)
return false;else{that.AJAX.onreadystatechange=function(){if(that.AJAX.readyState==4){that.updating=false;that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);that.AJAX=null;}}
that.updating=new Date();if(/post/i.test(postMethod)){var uri=urlCall+'?'+that.updating.getTime();that.AJAX.open("POST",uri,true);that.AJAX.setRequestHeader("Content-type","application/x-www-form-urlencoded");that.AJAX.setRequestHeader("Content-Length",passData.length);that.AJAX.send(passData);}else{var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime());that.AJAX.open("GET",uri,true);that.AJAX.send(null);}
return true;}}
var urlCall=url;this.callback=callbackFunction||function(){};}
// --ninja

/* Global variables are bad form, unless you work at D3  */

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(searchField, resultsDiv) {
	search_field = document.getElementById(searchField);
	results_div = document.getElementById(resultsDiv)
	
	if (search_field != null ) {
		// add a key(up) listener for the search-suggestion box
		search_field.onkeyup = function () { location_change() };
		
		// add a mouse(up) listener to the entire "body" of the page to hide the suggestion box
		function body_click_handler() {
			return function() {
				results_div.style.visibility = 'hidden';
				results_div.scrollTop = 0;
			};
		}
		var body = document.getElementsByTagName('body')[0];
		if (body.parentNode != null) body.parentNode.onmousedown = body_click_handler();
		else body.onmousedown = body_click_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);
		}
		
		results_div.onmousedown = function(e) {
			evt = (e) ? e : window.event;
			evt.cancelBubble = true;
		}
		
		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) {
		submit_btn.onclick = function(e) {
			if (document.search_form != null) document.search_form.submit();
		};
		submit_btn.setAttribute('href', 'javascript:void(0);');
	}
	
	var advanced_btn = document.getElementById('property_show_advanced');
	if (advanced_btn != null) {
		advanced_btn.onclick = function(e) {
			var css;
			if (document.getElementById('d3mls_search_advanced').style.display == 'none') {
				css = 'visibility: visible; display: block;';
				document.getElementById('property_show_advanced').innerHTML = 'Hide Advanced Search';
			} else {
				css = 'visibility: hidden; display: none;';
				document.getElementById('property_show_advanced').innerHTML = 'Show Advanced Search';
			}
			(document.getElementById('d3mls_search_advanced').style.setAttribute) ? document.getElementById('d3mls_search_advanced').style.setAttribute('cssText', css) : document.getElementById('d3mls_search_advanced').setAttribute('style', css);
		};
		advanced_btn.setAttribute('href', 'javascript:void(0);');
	}
	
	var wishlist_link = document.getElementById('wishlist_link');
	if (wishlist_link != null) {
		var wishlist_jax = new ninja('/app/modules/d3mls/ninja_code/wishlists.php');
		var system_id = wishlist_link.getAttribute('title');
		wishlist_link.setAttribute('title', wishlist_link.innerHTML);
		// callback function to handle returned data
		wishlist_jax.callback = function(response_data) {
			wishlist_link.innerHTML = (wishlist_link.innerHTML == 'Add this property to your Wishlist.') ? 'Remove this property from your Wishlist.' : 'Add this property to your Wishlist.';
			wishlist_link.setAttribute('title', wishlist_link.innerHTML);
		}
		
		wishlist_link.onclick = function(e) {
			wishlist_jax.abort();
			wishlist_jax.update("system_id="+system_id, "post");
		}
	}
}

// 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;
}

function swap_thumb(image, system, width, height) {
	var large = document.getElementById('property_image_large');
	if (large != null) {
		large.src = '/app/modules/d3mls/ninja_code/ninja_resize.php?img='+image+'&s='+system+'&w='+width+'&h='+height;
	}
}

// add the "onload" event(s)
if (window.attachEvent) {
	window.attachEvent('onload', function() {setup_location_change("property_location","property_location_results"); });
} else if (window.addEventListener) {
	window.addEventListener('load', function() {setup_location_change("property_location","property_location_results"); }, false);
} else {
	document.addEventListener('load', function() {setup_location_change("property_location","property_location_results"); }, false);
}