/*-----------------------------------------------------------------------
module address JavaScript File

version: 	4.1
author:		sebastian kupke
email:		sebastian.kupke@baral-geohaus.de
website:	http://www.baral.de
-----------------------------------------------------------------------*/

/* =namespace module address
-----------------------------------------------------------------------*/
ws.m.address = {
	
	// max results
	maxResults: 50,
	
	// zoomto scale
	zoomtoScale: 4000,
	
	// delay
	delay: 800,
	
	// searchIfEmpty
	searchIfEmpty: true,
	
	// tooltipWidth
	tooltipWidth: 200,
		
	// current address id
	currentAddress: 0,
	
	// address pin
	pin: {
		point: new ws.cl.Point(0, 0),
		img: 'circle.png'
	},
	
	// language
	l: {
		searching: '',
		noAddressFound: '',
		oneAddressFound: '',
		addressesFound: '',
		moreAddressesFound: '',
		noStreetFound: '',
		oneStreetFound: '',
		moreStreetsFound: '',
		streetsFound: ''
	},
	
	searchCount: 0,
	
	/* =init
	-----------------------------------------------------------------------*/
	init: function() {
		
		// create pin
		var p = ws.map.coords2Px(ws.m.address.pin.point);
		$('#pins').append('<div id="m_address_pin" style="position: absolute; top: ' + p.y + 'px; left: ' + p.x + 'px;" class="pin"><img src="style/' + ws.c.style + '/img/modules/address/pin/' + ws.m.address.pin.img + '" /></div>');
	},
	
	/* =search
	-----------------------------------------------------------------------*/
	search: function() {
		ws.m.address.searchCount++;
		setTimeout('ws.m.address.searchGo(' + ws.m.address.searchCount + ')', ws.m.address.delay);
	},

	/* =searchGo
	-----------------------------------------------------------------------*/
	searchGo: function(currentCount) {
		
		if (currentCount == ws.m.address.searchCount) {

			if ($('#m_address_autocomplete_choices').is(':hidden')) {
				$('#m_address_autocomplete_choices').slideDown('slow');
			}

			ws.m.address.currentCount = 0;

			if (ws.m.address.searchIfEmpty || $('#m_address_autocomplete').val().match(/.*\S.*/g)) {
				
				ws.m.info('address', 'load', ws.m.address.l.searching);
		
				$.ajax({
			        url: 'scripts/modules/address/search.gsp' + ws.c.ur,
			        data: 'search_input=' + $('#m_address_autocomplete').val(),
			        dataType: 'html',
			        type: 'post',
			        success: function(data){
			        	
			        	$('#m_address_autocomplete_choices').html(data);
			        	
			        	var hits = parseInt($('#hits').val());
		
						if (hits == 0) {
							ws.m.info('address', 'failure', ws.m.address.l.noAddressFound);
						} else if (hits == 1) {
							ws.m.info('address', 'success', ws.m.address.l.oneAddressFound);
						} else if (hits > ws.m.address.maxResults) {
							ws.m.info('address', 'warning', ws.m.address.l.moreAddressesFound.replace(/\{0\}/,hits).replace(/\{1\}/,ws.m.address.maxResults));
						} else {
							ws.m.info('address', 'success', ws.m.address.l.addressesFound.replace(/\{0\}/,hits));
						}
			        }
			    });
			} else {
				$('#m_address_autocomplete_choices').empty();
				ws.m.info('address');
			}
		}
	},
	
	/* =searchByStartChar
	-----------------------------------------------------------------------*/
	searchByStartChar: function(character) {

		if ($('#m_address_autocomplete_choices').is(":hidden")) {
			$('#m_address_autocomplete_choices').slideDown("slow");
		}
		
		$('#m_address_autocomplete').val(character);

		ws.m.info('address', 'load', ws.m.address.l.searching);

		$.ajax({
	        url: 'scripts/modules/address/search_by_start_char.gsp' + ws.c.ur,
	        data: 'character=' + character,
	        dataType: 'html',
	        type: 'post',
	        success: function(data){
	        	
	        	$('#m_address_autocomplete_choices').html(data);
	        	
	        	var hits = parseInt($('#hits').val());

				if (hits == 0) {
					ws.m.info('address', 'failure', ws.m.address.l.noStreetFound);
				} else if (hits == 1) {
					ws.m.info('address', 'success', ws.m.address.l.oneStreetFound);
				} else if (hits > ws.m.address.maxResults) {
					ws.m.info('address', 'warning', ws.m.address.l.moreStreetsFound.replace(/\{0\}/,hits).replace(/\{1\}/,ws.m.address.maxResults));
				} else {
					ws.m.info('address', 'success', ws.m.address.l.streetsFound.replace(/\{0\}/,hits));
				}
	        }
	    });
	},

	/* =zoomto
	-----------------------------------------------------------------------*/
	zoomto: function(community, street, houseNumber) {
		
		if (houseNumber) {
			$('#m_address_autocomplete').val(street + ' ' + houseNumber + ', ' + community);
		} else {
			houseNumber = '';
			$('#m_address_autocomplete').val(street + ', ' + community);
		}
		
		$.ajax({
	        url: 'scripts/modules/address/get_coords.gsp' + ws.c.ur,
	        data: {
				community: community,
				street: street,
				house_number: houseNumber
			},
	        dataType: 'json',
	        type: 'post',
	        success: function(data){
				
	        	if (data.id != null && data.id > 0 && data.x != null && data.x > 0 && data.y != null && data.y > 0) {
					ws.map.center.x = data.x;
					ws.map.center.y = data.y;

					ws.map.setScaleFactor(ws.m.address.zoomtoScale);
					
					ws.m.address.currentAddress = data.id;
					
					ws.m.address.pin.point.x = data.x;
					ws.m.address.pin.point.y = data.y;

					ws.map.load();
				}
	        }
	    });
	},

	/* =setPin
	-----------------------------------------------------------------------*/
	setPin: function() {
		
		$('#m_address_pin img').attr('src','style/' + ws.c.style + '/img/modules/address/pin/' + ws.m.address.pin.img);
		
		var pos = ws.map.coords2Px(ws.m.address.pin.point);

		var pin = $('#m_address_pin');
		pos.y = pos.y - (pin.height() / 2);
		pos.x = pos.x - (pin.width() / 2);

		pin.css({
			top: pos.y + 'px',
			left: pos.x + 'px'
		}).show();
		
		// ws.tt
		ws.tt.show(pin, {
			header: $('#m_address_autocomplete').val(),
			content: function() {
			
				var content = '';
				
				$.ajax({
					url: 'scripts/modules/address/get_content.gsp' + ws.c.ur,
			        data: 'id=' + ws.m.address.currentAddress,
			        dataType: 'text',
			        async: false,
			        success: function(data){
						content = data;
			        }
				});
				
				return content;
			},
			width: ws.m.address.tooltipWidth
		});
	},
	
	/* =reset
	 * 
	 * reset module (pin, search, result, info...)
	-----------------------------------------------------------------------*/
	reset: function() {
		
		// pin
		ws.m.address.pin.point.x = 0;
		ws.m.address.pin.point.y = 0;
		ws.m.address.setPin();
		
		// search
		$('#m_address_autocomplete').val('');
		$('#m_address_autocomplete_choices').hide().html('');
		
		// info
		ws.m.info('address');
	}
}

















