var GPlacemarkGeocode;

function classPlacemarkGeocode( input_id, callback ) {
	//}
	var thisO = this;
	
	this.init = 0;
	this.input_id_name = input_id;
	this.keyup_delay = 1200;
	this.keyup_timeout;
	this.callback = callback;
	this.temp_detail = new Array();
	
	this.arr_callback_para = new Array();
	
	this._blured = true;
	
	if ( this.init == 0 ) {
		GPlacemarkGeocode = new GClientGeocoder();
		$('#' + this.input_id_name).keyup(
			function () {
				window.clearTimeout(thisO.keyup_timeout);
				thisO.keyup_timeout = window.setTimeout(function(){ thisO.geocode(); }, thisO.keyup_delay);
			}
		);
		var width = getElementWidth(this.input_id_name);
		if ( width < 150 ) {
			width = 150;
		}
		var temp = new flyBox('', { position: this.input_id_name, vertical: "bottom", horizontal: "inner_left", width: "object" }, { cursor: 'pointer', marginTop: "-1px" } );
		$(document).click(
			function(e) {
				thisO.checkClick(e);
			}
		);
		
		this.init = 1;
	}
	
	this.geocode = function() {
		var input_text = $("#" + this.input_id_name).val();
		if ( input_text.length > 2 && GPlacemarkGeocode ) {
			GPlacemarkGeocode.getLocations(
				input_text,
				function(placemark) {
					var response = '';
					var id = 0;
					thisO.arr_callback_para = new Array();
					if (placemark['Placemark']) {
						for ( var i in placemark['Placemark'] ) {
							id++;
							var accuracy = placemark['Placemark'][i].AddressDetails.Accuracy;
							var countryCode = placemark['Placemark'][i].AddressDetails.Country.CountryNameCode;
							var address = placemark['Placemark'][i].address;
							var addressDetail = accuracy > 0 ? thisO.getDetail(placemark['Placemark'][i].AddressDetails.Country) : '';
							var location_lat = placemark['Placemark'][i].Point['coordinates'][1];
							var location_lng = placemark['Placemark'][i].Point['coordinates'][0];
							var arr_temp = new Array();
							arr_temp['id'] = id;
							arr_temp['input_text'] = input_text;
							arr_temp['address'] = address;
							arr_temp['countryCode'] = countryCode;
							arr_temp['addressDetail'] = addressDetail;
							arr_temp['location_lat'] = location_lat;
							arr_temp['location_lng'] = location_lng;
							arr_temp['accuracy'] = accuracy;
							thisO.arr_callback_para.push(arr_temp);
							response += '<div class="placemark" id="' + thisO.input_id_name + '_callback_' + id + '" style="border-bottom: solid 1px #AAAAAA; padding: 2px;">'+ address +'<br /><span class="atrS">' + addressDetail + '</span></div>';
						}
						response = '<div style="border-top: solid 1px #AAAAAA; border-left: solid 1px #AAAAAA; border-right: solid 1px #AAAAAA; ">' + response + '</div>';
					} else {
						response += '<div class="infoRed">Not found...</div>';
					}
					response += '';
					thisO.response( response );
				}
			);
		}
	}

	
	this.addCallback = function () {
		if ( arguments.length == 8 ) {
			var id = arguments[0];
			var arr_temp = new Array();
			arr_temp['lat'] = arguments[4];
			arr_temp['lng'] = arguments[5];
			arr_temp['address'] = arguments[2];
			arr_temp['addressDetail'] = arguments[3];
			arr_temp['accuracy'] = arguments[6];
			arr_temp['input_text'] = arguments[1];
			arr_temp['countryCode'] = arguments[7];
			arr_temp['id'] = this.input_id_name + '_callback_' + id;
			$('#' + this.input_id_name + '_callback_' + id).click(
				function () {
					MY.box[thisO.input_id_name + "_fly_div"].hide();
					thisO.callback( arr_temp );
				}
			);
		} else if ( arguments.length == 1 ) {
			var arr_para = arguments[0];
			for ( var i in arr_para ) {
				this.addCallback( arr_para[i]['id'], arr_para[i]['input_text'], arr_para[i]['address'], arr_para[i]['addressDetail'], arr_para[i]['location_lat'], arr_para[i]['location_lng'], arr_para[i]['accuracy'], arr_para[i]['countryCode'] );
			}
		}
	}
	
	this.getDetail = function( obj ) {
		var arr = new Array();
		arr = this.iter( obj, arr );
		arr.reverse();
		var detail = arr.join(', ');
		return detail;
	}
	
	this.iter = function( obj, arr ) {
		for ( i in obj ) {
			if ( typeof obj[i] == 'string' || obj[i] == 'number' ) {
				arr.push(obj[i]);
			} else if ( typeof obj[i] == 'object' ) {
				this.iter( obj[i], arr );
			}
		}
		return arr;
	}
		
	this.response = function( response_text ) {
		$('#' + this.input_id_name).blur().focus();
		MY.box[this.input_id_name + "_fly_div"].html(response_text);
		MY.box[this.input_id_name + "_fly_div"].show();
		this.addCallback(this.arr_callback_para);
		$('.placemark').hover(
			function() { $(this).css( { backgroundColor: "#CCCCCC", color: "#000000" } ); }, 
			function() { $(this).css( { backgroundColor: "#FFFFFF", color: "#333333" } ); } 
		); 
	}
	
	this.checkClick = function(e) {
		if ( MY.box[thisO.input_id_name + "_fly_div"] && !objectClicked(e, [thisO.input_id_name + "_fly_div", thisO.input_id_name]) ) {
			MY.box[thisO.input_id_name + "_fly_div"].hide();
		}
	}
}





































function GPlacemark( write_id, callback ) {
	//}
	var thisO = this;
	
	this._init = false;
	this._write_id = write_id;
	this._Geocoder;
	this._delay = 1200;
	this._timeout;
	this.callback = callback;
	this._search_text = '';
	
	this._key;
	this._selected;
	this._arr_callback_para = [];
	
	this._info = '<div style="border: solid 1px #AAAAAA; padding: 2px;" class="atrS">Schreibe deinen Wohnort, warte einen Moment und w&#228;hle dann den korrekten Wohnort aus der Liste aus, die erscheint.</div>';
	this._waiting = '<div style="border: solid 1px #AAAAAA; padding: 2px;" class="atrS">Checking input...</div>';
	this._searching = '<div style="border: solid 1px #AAAAAA; padding: 2px;" class="atrS">Input OK<br />Fetching location...</div>';
	
	
	this.search = function(e) {
		if (!e) var e = window.event;
		e.cancelBubble = true;
		if (e.stopPropagation) e.stopPropagation();
		var arr_ignore = [16,17,18,20,91,93,112,113,114,115,116,117,118,119,120,121,123,191];
		this._blured = false;
		//this.init();
		var key = e.keyCode;
		//alert(key);
		if( key == 13 ) { //ENTER
			this.select(this._key);
			return false;
		} else if( key == 38 || key == 37 ) { // UP LEFT
			if( this._key >= 0 && this._arr_callback_para.length ) {
				var key; this.show('fast');
				if( this._key < 0  ) key = 0;
				else key = (this._key - 1 >= 0) ? this._key - 1 : this._arr_callback_para.length - 1;
				
				this.over(key);
			}
			return false;
		} else if( key == 40 || key == 39 ) { // DOWN RIGHT
			if( this._arr_callback_para.length ) {
				var key; this.show('fast');
				if( this._key < 0  ) key = 0;
				else key = (this._key + 1 >= this._arr_callback_para.length) ? 0 : this._key + 1;
				this.over(key);
			}
			return false;
		} else if( key == 9 ) { // TAB
			this.show();
			return false;
		} else if( key == 27 ) { // ESC
			this.hide();
			if( this._selected < 0 ) this.select(this._key);
			else this.select( this._selected );
			return false;
		} else if( in_array(key, arr_ignore) ) {
			return false;
		}
		$('#'+this._write_id).css({backgroundColor:'#FFFFFF'});
		this._key = -1;
		window.clearTimeout(this._timeout);
		this._search_text = $("#" + this._write_id).val();
		if( this._search_text.length < 3 ) {
			this.display(this._info);
		} else {
			this.display(this._waiting);
			this._timeout = window.setTimeout(function(){ thisO.geocode(); }, this._delay);
		}
	}
	
	this.geocode = function() {
		this._search_text = $("#" + this._write_id).val();
		if ( this._Geocoder ) {
			this.display(this._seraching);
			this._Geocoder.getLocations(
				this._search_text,
				function(placemark) { thisO.response(placemark); }
			);
		}
	}
	
	this.response = function( placemark ) {
		var response = '';
		var id = 0;
		this._arr_callback_para = [];
		this._key = -1;
		if (placemark['Placemark']) {
			this._key = 0;
			for ( var i in placemark['Placemark'] ) {
				var id = this._arr_callback_para.length;
				var accuracy = placemark['Placemark'][i].AddressDetails.Accuracy;
				var address = placemark['Placemark'][i].address;
				var addressDetail = accuracy > 0 ? thisO.getDetail(placemark['Placemark'][i].AddressDetails.Country) : '';
				var arr_temp = [];
				arr_temp['id'] = id;
				arr_temp['input_text'] = this._search_text;
				arr_temp['address'] = address;
				arr_temp['countryCode'] = placemark['Placemark'][i].AddressDetails.Country.CountryNameCode;
				arr_temp['addressDetail'] = addressDetail;
				arr_temp['lat'] = placemark['Placemark'][i].Point['coordinates'][1];
				arr_temp['lng'] = placemark['Placemark'][i].Point['coordinates'][0];
				arr_temp['accuracy'] = accuracy;
				this._arr_callback_para.push(arr_temp);
				response += '<div class="placemark" id="' + this._write_id + '_callback_' + id + '" style="border-bottom: solid 1px #AAAAAA; padding: 2px;">'+ address +'<br /><span class="atrS">' + addressDetail + '</span></div>';
			}
			response = '<div style="border: solid 1px #AAAAAA;">' + response + '</div>';
		} else {
			response += '<div style="border: solid 1px #AAAAAA;"><div class="infoRed">Not found...</div></div>';
		}
		this.display(response);
		this.addCallback();
		if( !this._blured ) thisO.show( response );
		else if( this._arr_callback_para.length ) this.select(0);
		if( this._arr_callback_para.length ) this.bg(0, '#E1E9F6');
	}
	
	this.addCallback = function() {
		for( var i in this._arr_callback_para ) {
			var id = this._arr_callback_para[i]['id'];
			//var html = $('#test').html();
			//html += '<br />ID: '+id;
			//$('#test').html(html);
			this.callbackId(id);
		}
	}
	
	this.callbackId = function( id ) {
		$('#' + this._write_id + '_callback_' + id).click( function() { thisO.select(id); } ).mouseover( function() { thisO.over(id); } );
	}
	
	this.bg = function( id, color ) {
		$('#' + this._write_id + '_callback_' + id).css({backgroundColor:color});
	}
	
	this.over = function( id ) {
		if( id == this._key ) return;
		if( this._key >= 0 ) thisO.bg(this._key, '#FFFFFF');
		this.bg(id, '#E1E9F6');
		this._key = id;
	}
	
	this.select = function( id ) {
		if( id >= 0 && this._arr_callback_para.length > id ) {
			this.over(id);
			this._key = id;
			this._selected = this._key;
			$('#'+this._write_id).val(this._arr_callback_para[this._key]['address']);
			$('#'+this._write_id).css({backgroundColor:'#E1E9F6'});
			this.callback(this._arr_callback_para[this._key]);
		}
		this.hide();
	}
	
	this.blur = function() {
		if( this._selected >= 0 ) this.select(this._selected);
		else if( this._key >= 0 && this._arr_callback_para.length ) this.select(this._key);
		this.hide();
		this._blured = true;
	}
	
	this.init = function() {
		if( this._init ) return;
		this._Geocoder = new GClientGeocoder();
		$('#' + this._write_id).keyup( function (event) {
			thisO.search(event);
		} ).click( function () {
			thisO.show();
		} ).blur( function() {
			thisO.blur();
		} );
		var width = $('#' + this._write_id).outerWidth();
		width = (width < 150) ? 150 : width;
		$('#' + this._write_id).after('<div style="position: relative; height: 0px;"><div id="'+this._write_id+'_div" style="position: absolute; top: -1px; width: '+width+'px; background-color: #FFFFFF; display: none;">'+this._info+'</div></div>');
		$(document).click( function(e) {
				thisO.checkClick(e);
		} );
		this._init = true;
	}
	
	this.display = function( html ) {
		$('#'+this._write_id+'_div').html(html);
	}
	
	this.show = function() {
		if( arguments.length && arguments[0] == 'fast' ) $('#'+this._write_id+'_div').show();
		else if( arguments.length ) this.display(arguments[0]);
		$('#'+this._write_id+'_div').slideDown(200);
		this._blured = false;
	}
	
	this.hide = function() {
		$('#'+this._write_id+'_div').slideUp();
	}
	
	this.checkClick = function(e) {
		if ( !objectClicked(e, [thisO._write_id+"_div", thisO._write_id]) ) {
			thisO.hide();
		}
	}
	
	this.getDetail = function( obj ) {
		var arr = new Array();
		arr = this.iter( obj, arr );
		arr.reverse();
		var detail = arr.join(', ');
		return detail;
	}
	
	this.iter = function( obj, arr ) {
		for ( i in obj ) {
			if ( typeof obj[i] == 'string' || obj[i] == 'number' ) {
				arr.push(obj[i]);
			} else if ( typeof obj[i] == 'object' ) {
				this.iter( obj[i], arr );
			}
		}
		return arr;
	}
	
	this.init();
}
