var mylocation = {};

mylocation.countrycodes = [];
mylocation.countrynames = [];

// Diese Funktion wird aufgerufen, wenn auf einen Resultat-Link geklickt wird
mylocation.onclick = function() {}
mylocation.ondelete = function() {}

mylocation.startLocSearch = function() {
	if($('startLoc').value.length == 0 || $('startLoc').value == $('startLoc').title) {
		var c = getCookie('mylocation');
		if(c == null) {
			alert('Bitte einen Ort oder eine Adresse angeben.');
			return;
		}

		var values = decodeURIComponent(c).replace('+', ' ', 'g').split('|');

		mylocation.ondelete(values[0]);

		return;
	}

	var place = $('startLoc').value;

	display('startLocResults', 'none');
	display('startLocThrobber', '');
	display('startLocBox', '');

	var url = '/ajax/api.php'

	var p = 'method=topin.geo.findplace';
	p = p + '&p=' + encodeURIComponent(place);
	p = p + '&countrybias=';
	p = p + '&limit=10';
	p = p + '&format=json';
	p = p + '&callback=mylocation.startLocSearchResults';

	var script = document.createElement('script');
	script.setAttribute('src', url + '?' + p);
	script.setAttribute('type', 'text/javascript');
	document.documentElement.firstChild.appendChild(script);
}

mylocation.startLocSearchResults = function(response) {
	$('startLocSubmit').disabled = false;

	var geo = false;
	if(response)
		geo = response.data.places;

	if(!geo || geo.totalResultsCount < 0) {
		display('startLocBox', 'none');

		alert('Die Suche ist zu Zeit leider nicht verfügbar. Bitte versuchen Sie es später nochmals.\n\nVielen Dank');

		return;
	}

	if(geo.totalResultsCount == 0) {
		display('startLocBox', 'none');

		alert('Der Ort "' + $('startLoc').value + '" wurde nicht gefunden.');

		return;
	}

	clear('startLocResults');

	var places = [];
	var place, address, location;

	for(var i = 0; i < geo.geonames.length; i++) {
		place = geo.geonames[i];

		if(mylocation.countrycodes.length != 0 && mylocation.countrycodes.indexOf(place.countryCode) == -1)
			continue;

		address = place.name + ', ';
		if(place.adminName1 && place.adminName1 != place.name)
			address = address + place.adminName1 + ', ';
		address = address + place.countryName;

		location = {
			address: address,
			cc: place.countryCode,
			plz: '',
			ort: place.name,
			lat: place.lat,
			lng: place.lng
		};

		places.push(location);
	}

	var nresults = places.length;

	if(nresults == 0) {
		display('startLocBox', 'none');

		var text = 'Der Ort "' + $('startLoc').value + '" wurde nicht gefunden.';

		if(mylocation.countrycodes.length != 0) {
			text = text + '\n\nEs werden nur folgende Länder durchsucht:\n';

			for(var i = 0; i < mylocation.countrynames.length; i++)
				text = text + mylocation.countrynames[i] + '\n';
		}

		alert(text);

		return;
	}

	if(nresults == 1) {
		var p = places[0];

		var ml = [];
		ml.push(encodeURIComponent(p.address));
		ml.push(encodeURIComponent(p.cc));
		ml.push(encodeURIComponent(p.plz));
		ml.push(encodeURIComponent(p.ort));
		ml.push(p.lat);
		ml.push(p.lng);

		Cookies.set('mylocation', ml.join('|'), 2);
		Cookies.set('sortby', 'distanz', 2);

		mylocation.onclick(ml.join(';'), p.cc.toLowerCase() + '-' + p.ort.toLowerCase());

		return;
	}

	for(var i = 0; i < nresults; i++) {
		var li = $Element('li');

		var a = $Link('#', places[i].address);
		a.setAttribute('ml-address', places[i].address);
		a.setAttribute('ml-cc', places[i].cc);
		a.setAttribute('ml-plz', places[i].plz);
		a.setAttribute('ml-ort', places[i].ort);
		a.setAttribute('ml-lat', places[i].lat);
		a.setAttribute('ml-lng', places[i].lng);
		a.onclick = function() {
			var ml = [];
			ml.push(encodeURIComponent(this.getAttribute('ml-address')));
			ml.push(encodeURIComponent(this.getAttribute('ml-cc')));
			ml.push(encodeURIComponent(this.getAttribute('ml-plz')));
			ml.push(encodeURIComponent(this.getAttribute('ml-ort')));
			ml.push(this.getAttribute('ml-lat'));
			ml.push(this.getAttribute('ml-lng'));

			Cookies.set('mylocation', ml.join('|'), 2);
			Cookies.set('sortby', 'distanz', 2);

			mylocation.onclick(ml.join(';'), this.getAttribute('ml-cc').toLowerCase() + '-' + this.getAttribute('ml-ort').toLowerCase());

			return false;
		}

		li.appendChild(a);

		$('startLocResults').appendChild(li);
	}

	display('startLocThrobber', 'none');
	display('startLocResults', '');
}

mylocation.resetCountry = function() {
	this.countrycodes = [];
	this.countrynames = [];
}

mylocation.setCountry = function(countrycode, name) {
	this.countrycodes.push(countrycode);
	this.countrynames.push(name);
}
