var geocoder;
var geocoder_inited = false;

window.addEvent('domready', function() {
	if($('map')) {		
		onLoad();
		
		if($('map_jump')) {
			pointlist_html  = '<option value="" selected="selected">Wählen Sie einen MICROTREND-Partner:</option>' + pointlist_html;
			pointlist_html  = '<form action="#" method="post">' + pointlist_html;
			pointlist_html  = '<select id="plz_jumper" onchange="showPartnerInfoWindow(this);">' + pointlist_html;
			pointlist_html += '</select></form>';
			$('map_jump').setHTML(pointlist_html);
		}
	}
	
	// OK Adresseingabefeld
	if($('mt_search_form')) {
		
		$('mt_submit_button').remove();

		$('mt_form_submit').setStyle('display', '');
		$('mt_form_submit').addEvent('click', function(r) {
				var r = new Event(r).stop();
				var address = $('mt_search_address').value.trim();
				var no_filter = true;
				if($('mt_filter_webshop')) {
					if( $('mt_filter_webshop').checked || $('mt_filter_flyer').checked || 
						$('mt_filter_b2b').checked || $('mt_filter_certified').checked ||
						$('mt_search_fulltext').value.trim() != '') {
						var no_filter = false;
					}
				}
				if(no_filter && (address == '' || address == 'Straße, PLZ Ort, Land')) {
					alert('Bitte geben Sie mindestens Ihre PLZ im Adressfeld ein!');
					return null;
				};
				$('mt_search_form').submit();
			});

		$('mt_search_form').addEvent('submit', function(r) {
				var r = new Event(r).stop();
				var address = $('mt_search_address').value.trim();
				var no_filter = true;
				if($('mt_filter_webshop')) {
					if( $('mt_filter_webshop').checked || $('mt_filter_flyer').checked || 
						$('mt_filter_b2b').checked || $('mt_filter_certified').checked ||
						$('mt_search_fulltext').value.trim() != '') {
						var no_filter = false;
					}
				}
				if(no_filter && (address == '' || address == 'Straße, PLZ Ort, Land')) {
					alert('Bitte geben Sie mindestens Ihre PLZ im Adressfeld ein!');
					return null;
				};
				$('mt_search_form').submit();
			});

		if($('mt_search_address').value.trim() == '') {
			$('mt_search_address').setStyles('color: #666666;').value = 'Straße, PLZ Ort, Land';
		}
		$('mt_search_address').addEvents({
			focus: function(){
					if(this.value.trim() == 'Straße, PLZ Ort, Land') {
						this.setStyles('color: #000000;').value = '';
					}
				},
			blur: function(){
					this.value = this.value.trim();
					if(this.value == '') {
						this.value = 'Straße, PLZ Ort, Land';
					}
					if(this.value == 'Straße, PLZ Ort, Land') {
						this.setStyles('color: #666666;');
					}
				}
			});
		
		
		// initialize tooltips
		if($$('span.spanToolTip').length > 0) {
			new Tips($$('span.spanToolTip'), { 
				maxTitleChars: 100,
				offsets: {'x': -73, 'y': 3}
			});
		}
		
	}
	
});

function newSearch() {
	$('mt_partner_search_box').setStyle('display', '');
	$('mt_search_results_info').setStyle('display', 'none');
}

// Google Maps Sepcific Functions
function tryToSetLocation() {
	if($('google_map_latitude')) {
		if($('google_map_latitude').value == '' || $('google_map_longitude').value == '') {
			getLocation();
		}
	}
}

function showPartnerInfoWindow(idx) {
	idx = idx.options[ idx.selectedIndex ].value;
	if(idx != '') {
		click_sidebar(idx);
	}
}

function setLatLongInformation(response) {
	
	// Domkloster 3, 50667 Köln, Deutschland

	if (!response || response.Status.code != 200) {

		alert('Die Geokoordinaten für die Adresse \n['+getAddress()+']\nkonnten nicht ermittelt werden.');
	
	} else {
	
		place = response.Placemark[0];
		
		$('google_map_latitude').value = place.Point.coordinates[1];
		$('google_map_longitude').value = place.Point.coordinates[0];

	}
}

function getLocation() {
	
	if(!geocoder_inited) {
		geocoder_inited = true;
		geocoder = new GClientGeocoder();
		unloadGoogleMaps();
	}
	address = getAddress();
	if(address != '') {
		geocoder.getLocations( address , setLatLongInformation );
	}
}

function getAddress() {
	var address = '';
	return address;
}

function showMap() {
	
	if(!geocoder_inited) {
		geocoder_inited = true;
		geocoder = new GClientGeocoder(); 
		unloadGoogleMaps();
	}
	
	var lat = $('google_map_latitude').value;
	var lng = $('google_map_longitude').value;
	var adr = getAddress();
	
	if(lat != '' && lng != '' && adr != '') {
		point = new GLatLng(lat, lng);
		ssl = point.toUrlValue();
		
		mapurl  = 'http://maps.google.de/?hl=de&cd=1&q=';
		mapurl += encodeURI( adr );
		mapurl += '&sll=' + ssl + '&z=16&iwloc=addr&om=1';
		
		window.open(mapurl, 'GoogleMap');
		
	} else {
		alert('Bitte Adresse prüfen!');	
	}
}

function unloadGoogleMaps() {
	window.addEvent('unload', function() { GUnload(); } );
}
