function gpc( element ) {
	var coords = { x:0, y:0 };
	while (element) {
		coords.x += element.offsetLeft;
		coords.y += element.offsetTop;
		element = element.offsetParent;
	}
	return coords;
}

function trim( str ) {
	str = str.replace( /^\s+/, '' );

	for (var i = str.length - 1; i >= 0; i--) {

		if (/\S/.test( str.charAt( i ) ) === true) {
			str = str.substring( 0, i + 1 );
			break;
		}
	}

	return str;
}


function setup_search() {
	var frm = document.getElementById( 'search' );
	var qry = document.getElementById( 'qry' );
	var sel = document.getElementById( 'sel' );
	var sel_opt = null;
	var ths = null, tsr = null, tfq = null;
	var lst = '';

	if (frm === null) {return;}
	if (qry === null) {return;}
	if (sel === null) {return;}

	var pos = gpc( document.getElementById( 'combo_box' ) );
	sel.style.display = 'none';//req'd
	sel.style.left = pos.x+'px';
	sel.style.top = (pos.y+24)+'px';

	var fnc_hide_sel = function() {//ths
		sel.style.display = 'none';
	};

	var fnc_search_req = function() {//tsr
		var params = 'sender=search&locale=home&search_query='+qry.value;
		var ax = new ajax( ax_event, 9000 );
		ax.post( '/home/ax_autocpl.php', params, true );
	};

	var fnc_focus_qry = function() {//tfq
		qry.focus();
	};

	qry.onblur = function( evt ) {
		clearTimeout( ths );
		ths = setTimeout( fnc_hide_sel, 500 );
	};

	qry.onfocus = function( evt ) {
		clearTimeout( ths );

		if ((sel.childNodes.length || 0) > 2) {
			sel.style.display = 'block';
		}
	};

	function set_active( ndx ) {
		var items = sel.getElementsByTagName( 'li' );

		for (var i = items.length-1; i >= 0; i--) {
			items[i].className = '';
		}

		if (ndx >=0 && ndx < items.length) {
			items[ndx].className = 'inv';
		}
	}

	qry.onkeydown = function( evt ) {
		//var from = (window.event ? window.event.srcElement : evt.target);
		var code = (window.event ? window.event.keyCode : evt.keyCode);
		//var char = String.fromCharCode( code );

		if ((sel.childNodes.length || 0) !== 0) {
			var ndx = parseInt( sel.getAttribute( 'ndx' ) );

			switch (code) {
				case 38:
					ndx = (ndx > 0 ? ndx-1 : 0);
					break;
				case 40:
					ndx = (ndx < sel.childNodes.length-1 ? ndx+1 : sel.childNodes.length-1);
					break;
				case 27:
					sel.style.display = 'none';
					break;
				case 13:
					if (ndx >= 0) {
						var loc = sel.childNodes[ndx].getAttribute( 'loc' );

						if (loc !== null) {
							loc = loc.split( '|' );
							location.href = ('/'+ loc[0] +'/'+ loc[1] +'-apartments/').toLowerCase();
						}
					}
					return false;
			}
			sel.setAttribute( 'ndx', ndx );
			set_active( ndx );
		}
	};

	qry.onkeyup = function( evt ) {
		//var from = (window.event ? window.event.srcElement : evt.target);
		//var code = (window.event ? window.event.keyCode : evt.keyCode);
		//var char = String.fromCharCode( code );

		if (lst != qry.value) {
			frm.onsubmit = submit_nak;
			lst = qry.value;

			if (qry.value.length > 2) {
				clearTimeout( tsr );
				tsr = setTimeout( fnc_search_req, 250 );
			}
		}
	};

	function ax_event( ax, msg ) {
		if (msg == 'timeout') {alert( 'The server seems busy, try again' );	return;}
		if (msg == 'error') {alert( 'An error occured, try again' ); return;}

		var res = eval( '('+ msg +')' );

		switch (res.loc.length) {
			case 0:
				sel.style.display = 'none';
				sel.innerHTML = '';
				break;
			default:
				sel.style.display = 'none';
				sel.innerHTML = '';

				for (var i = 0, title = ''; i < res.loc.length; i++) {
					var txt = document.createTextNode( res.loc[i][1] +', '+ res.loc[i][0] );
					var elm = document.createElement( 'li' );
					var att = document.createAttribute( 'loc' );

					att.nodeValue = res.loc[i].join( '|' );
					elm.setAttributeNode( att );
					elm.appendChild( txt );
					elm.onclick = clk;
					elm.onmouseover = ent;

					sel.appendChild( elm );
				}
				
				if (res.loc.length == 1) {
					frm.onsubmit = submit_ack;
					sel_opt = res.loc[0];
					
					var s1 = (res.loc[0][1] +', '+ res.loc[0][2]).toLowerCase();
					var s2 = (res.loc[0][1] +', '+ res.loc[0][0]).toLowerCase();
					
					if ( qry.value.indexOf( ',' ) == -1) {
						lst = qry.value = res.loc[0][1] +', '+ res.loc[0][2];
						clearTimeout( tfq );
						setTimeout( fnc_focus_qry, 1250 );
						qry.blur();
					}
					else if (/(.*),\s\w{3,}$/i.test( qry.value ) === true) {

						if (s2.indexOf( qry.value.toLowerCase() ) != -1) {
							lst = qry.value = res.loc[0][1] +', '+ res.loc[0][0];
							clearTimeout( tfq );
							setTimeout( fnc_focus_qry, 750 );
							qry.blur();
						}
					}
					else if (/(.*),\s\w{2}$/i.test( qry.value ) === true) {

						if (qry.value.toLowerCase() == s1) {
							lst = qry.value = res.loc[0][1] +', '+ res.loc[0][2];
							clearTimeout( tfq );
							setTimeout( fnc_focus_qry, 750 );
							qry.blur();
						}
					}
				}

				sel.setAttribute( 'ndx', -1 );
				sel.style.display = 'block';
		}
	}

	function ent( evt ) {
		var from = (window.event ? window.event.srcElement : evt.target);
		var t = from, ndx = 0;;
		while ((t = t.previousSibling) !== null) {
			ndx++;
		}
		sel.setAttribute( 'ndx', ndx );
		set_active( ndx );
	}

	function clk( evt ) {
		var from = (window.event ? window.event.srcElement : evt.target);
		var loc = from.getAttribute( 'loc' );

		if (loc !== null) {
			loc = loc.split( '|' );
			location.href = ('/'+ loc[0] +'/'+ loc[1] +'-apartments/').toLowerCase();
		}
	}


	function submit_nak( evt ) {
		alert( 'I cannot find what you are looking for' );
		return false;
	}

	function submit_ack( evt ) {
		location.href = ('/'+ sel_opt[0] +'/'+ sel_opt[1] +'-apartments/').toLowerCase();
		return false;
	}

	sel.setAttribute( 'ndx', -1 );
	sel.innerHTML = '';

	frm.onsubmit = submit_nak;

	qry.focus();
}

/*
//ie
e.cancelBubble = true;
e.returnValue = false;

//other
e.stopPropagation();
e.preventDefault();
*/
