if(typeof String.prototype.trim == "undefined") {
	String.prototype.trim = function() {
		return this.replace(new RegExp("(^\s+)|(\s+$)", "g"), "");
	}
}

function SuggestAddress(street) {
	(function () { /* init */
		var container = document.createElement('div');
		container.className = "eSuggest-container";

		var crnrL = document.createElement('div');
		crnrL.className = "eSuggest-crnr eSuggest-crnr-l";
		var crnrR = document.createElement('div');
		crnrR.className = "eSuggest-crnr eSuggest-crnr-r";

		var div = document.createElement('div');
		div.className = 'eSuggest';
		div.$str = '';
		div.afterclick = function() {
			street.onblur();
		}
		div.afterblur = function() {
			if (!this.$cur) {
				var str = this.$input.value.trim().toLowerCase();
				for (var s in this.$list) {
					if (s.toLowerCase() == str) {
						this.$cur = this.$list[s];
					}
				}
			}
			if (this.$cur) {
				this.$input.value = this.$cur.value;
			}
		}
		street.$div = div;
		div.$input = street;
		container.appendChild(div);
		container.appendChild(crnrL);
		container.appendChild(crnrR);

		street.parentNode.insertBefore(container, street.nextSibling);

		street.$div.buildList = function() {
			var str = this.$input.value.trim().toLowerCase();
			if (str == this.$str) return;
			this.$str = str;
			Debug('Build: ' + this.$input.name)
			while(this.firstChild) {
				this.removeChild(this.firstChild);
			}
			if ((str == '')) {
				return;
			}
			this.$cur = null;
			for (var s in this.$list) {
				if (s.toLowerCase().indexOf(str) == 0) {
					this.appendChild(this.$list[s]);
					if (this.$list[s].className != '') {
						this.$cur = this.$list[s];
					}
				}
			}
		}

		street.$div.showList = function (show) {
			if(typeof show == 'undefined') {
				show = true;
			}
			var isShow = (show && this.firstChild);
			if(isShow) {
				if(!this.parentNode.className.match(" eSuggest-shown")) {
					this.parentNode.className = this.parentNode.className + " eSuggest-shown";
				}
			} else {
				this.parentNode.className = this.parentNode.className.replace(" eSuggest-shown", "");
			}
			if (!isShow) return;
			if (this.$cur) {
				var fco = this.firstChild.offsetTop;
				if (this.offsetHeight + this.scrollTop < this.$cur.offsetTop + this.$cur.offsetHeight - fco) {
					this.scrollTop = this.$cur.offsetTop + this.$cur.offsetHeight - this.offsetHeight - fco + 2;
				}
				if (this.scrollTop > this.$cur.offsetTop - fco) {
					this.scrollTop = this.$cur.offsetTop - fco;
				}
			} else {
				this.scrollTop = 0;
			}
		}

		street.$div.onmousedown = function(e) {
			Debug('MouseDown');
			try {
				e.preventDefault();
			} catch(err) {
				this.$input.preventHide = true;
			}
		}
		street.$div.onmouseover = function(e) {
			e = e || event;
			var target = e.target || e.srcElement;
			if (target.tagName.toLowerCase() == 'a') {
				if (this.$cur) {
					this.$cur.className = '';
				}
				target.className = 'current';
				this.$cur = target;
			}
		}
		street.$div.onclick = function(e) {
			e = e || event;
			var target = e.target || e.srcElement;
			if (target.tagName.toLowerCase() == 'a') {
				if (this.$cur) {
					this.$cur.className = '';
				}
				target.className = 'current';
				this.$cur = target;
				this.$input.preventHide = false;
				this.afterclick();
				try {
					e.preventDefault();
				} catch (err) {
					e.returnValue = false;
				}
			}
		}
		street.$div.ondragstart = function() {return false}
	})();

	function Debug(s) {
	/*
		document.getElementById('debug').innerHTML += s + '<br>';
		document.getElementById('debug').scrollTop = 10000;
	*/
	}

	street.onfocus = function (e) {
		Debug('focus: ' + this.name);
		this.$div.buildList();
		this.$div.showList();
	}

	street.onblur = function (e) {
		Debug('blur: ' + this.name)
		if ((typeof this.preventHide != 'undefined') && this.preventHide) {
			Debug('blur: prevent hide')
			this.preventHide = false;
		} else {
			Debug('blur: hide')
			this.$div.showList(false);
			this.$div.afterblur();
		}
	}

	var onKeyDown = function(e) {
		var key = (e || event).keyCode;
		var next = null;
		switch (key) {
			case 38:
				next = (this.$div.$cur) ? this.$div.$cur.previousSibling : this.$div.lastChild;
				break;
			case 40:
				next = (this.$div.$cur) ? this.$div.$cur.nextSibling : this.$div.firstChild;
				break;
			case 13:
				this.$div.afterclick();
				try {
					e.preventDefault();
				} catch (err) {
					event.returnValue = false;
				}
				return false;
			case 27:
				break;
			default:
				return true;
		}
		if (this.$div.$cur) {
			this.$div.$cur.className = '';
		}
		this.$div.$cur = next;
		if (next) {
			next.className = 'current';
		}
		this.$div.showList();
		try {
			e.preventDefault();
		} catch (err) {
			event.returnValue = false;
		}
	}
	if (document.all && !window.opera) {
		street.onkeydown = onKeyDown;
	} else {
		street.onkeypress = onKeyDown;
	}

	street.onkeyup = function (e) {
		var c = this.value.trim().substr(0, 3).toLowerCase();
		if ((c.length > 2) && (this.$div.$letter != c)) {
			var onfunction = SaveResponse;
			var onOptions  = c;
			var city = $('#city').val();
			city = city == null ? $("#dropdown-alt-for-city-selector ul.dropdown-alt-values li.current").attr('value') : city;
			city = city == null ? $("#dropdown-alt-for-region-selector ul.dropdown-alt-values li.current").attr('value') : city;

			$.get('/regions.action', {action: 'streets', letter: c, city: city}, function(req) {
				if (typeof onfunction.call == 'undefined') {
					/* for IE 5.01 */
					req.responseJS.$call = onfunction;
					req.responseJS.$call(req, onOptions);
				} else {
					onfunction.call(req, req, onOptions);
				}
			}, 'json');
			return;
		}
		this.$div.buildList();
		this.$div.showList();
	}

	var SaveResponse = function (req, letter) {
		if (street.value.trim().substr(0, 3).toLowerCase() != letter) return;
		var streets = {};
		if (typeof this.data === 'object') {
			for (var s in this.data) {
				streets[s] = {};
				var curstr = this.data[s];
				var link = document.createElement('a');
				link.value = curstr;
				link.href = '#' + curstr;
				link.appendChild(document.createTextNode(curstr));
				streets[curstr] = link;
			}
		}
		street.$div.$letter = letter;
		street.$div.$list = streets;

		street.$div.buildList();
		street.$div.showList();
	}
}

var street = document.getElementById('street');

if(street) {
	SuggestAddress(street);
}


