/**
* Stellt ein die Funktionen für das Bearbeiten der Geo-Daten zur Verfügung zur Verfügung
*
* @package Ceasy_Core
* @author Ralf Glaser
*
*
*/

function MadChickGeoData(latFormFieldObj, lngFormFieldObj, formObj, settings)  {

	var self=this;

	settings=(settings ? settings : {});

	this._googleMapWin=false;
	this._madChickGoogleMapInstance=false;

	this._gLatFormFieldObj=latFormFieldObj;
	this._gLngFormFieldObj=lngFormFieldObj;
	this._formObj=formObj;
	this._streetFormFieldObj=(settings['streetField'] ? settings['streetField'] : (this._formObj && settings['streetFieldName'] ? this._formObj.elements[settings['streetFieldName']] : false));
	this._houseNumberFormFieldObj=(settings['houseNumberField'] ? settings['houseNumberField'] : (this._formObj && settings['houseNumberFieldName'] ? this._formObj.elements[settings['houseNumberFieldName']] : false));
	this._zipcodeFormFieldObj=(settings['zipcodeField'] ? settings['zipcodeField'] : (this._formObj && settings['zipcodeFieldName'] ? this._formObj.elements[settings['zipcodeFieldName']] : false));
	this._townFormFieldObj=(settings['townField'] ? settings['townField'] : (this._formObj && settings['townFieldName'] ? this._formObj.elements[settings['townFieldName']] : false));
	this._polygonGeoDataFieldObj=(settings['polygonGeoDataField'] ? settings['polygonGeoDataField'] : (this._formObj && settings['polygonGeoDataFieldName'] ? this._formObj.elements[settings['polygonGeoDataFieldName']] : false));
	this._googleMapWinUrl=settings['googleMapWinUrl'];

	if (settings['mapType']) {

		this._googleMapWinUrl+=(this._googleMapWinUrl.indexOf('?')>-1 ? '&' : '?')+'&mapType='+settings['mapType'];

	}

	/*********************/
	/*    public         */
	/*********************/

	// --------------------------------------------------------------------------------
	this.detect= function()  {

		if (this._streetFormFieldObj.value=='') {

			alert('Bitte geben Sie zuerst eine Straße an.');

		} else if (this._townFormFieldObj.value=='') {

			alert('Bitte geben Sie zuerst einen Ort an.');

		} else {

			self._displayGoogleMapWin(function () {

				var addressStr=self._streetFormFieldObj.value;
				addressStr+=(self._houseNumberFormFieldObj.value ? ' '+self._houseNumberFormFieldObj.value : '');
				addressStr+=', ';
				addressStr+=(self._zipcodeFormFieldObj.value ? self._zipcodeFormFieldObj.value+' ' : '');
				addressStr+=self._townFormFieldObj.value;

				self._initEditMode();
				self._madChickGoogleMapInstance.geocode(addressStr);

			});

		}

	};

	// --------------------------------------------------------------------------------
	this.show= function(lat, lng, mapType)  {

		self._displayGoogleMapWin(function () {

			if (mapType) {

				self._madChickGoogleMapInstance.setMapType(mapType);

			}

			self._initEditMode();

			if (lat && lng) {

				self._madChickGoogleMapInstance.setMarker(lat, lng);

			} else if (self._polygonGeoDataFieldObj) {

				var polygons=[
					{
						'latLngsString':self._polygonGeoDataFieldObj.value,
						'lineColor':'#F33F00',
						'color':'#FF0000'
					}
				];

				var item={
					//'itemName':itemName,
					//'itemId':itemId,
					'gLat':self._gLatFormFieldObj.value,
					'gLng':self._gLngFormFieldObj.value,
					'polygons':polygons
				};

				var markerIndex=self._madChickGoogleMapInstance.setItemData(item);
				self._madChickGoogleMapInstance.removeMarker();
				self._madChickGoogleMapInstance.setMarker(self._gLatFormFieldObj.value, self._gLngFormFieldObj.value, null, markerIndex);

			} else if (self._gLatFormFieldObj.value && self._gLngFormFieldObj.value) {

				self._madChickGoogleMapInstance.setMarker(self._gLatFormFieldObj.value, self._gLngFormFieldObj.value);

			} else {

				self._madChickGoogleMapInstance.setCenter();

			}

		}, lat, lng);

	};

	// --------------------------------------------------------------------------------
	this.showItem= function(itemName, itemId, lat, lng, mapType)  {

		self._displayGoogleMapWin(function () {

			if (mapType) {

				self._madChickGoogleMapInstance.setMapType(mapType);

			}

			if (itemName && itemId) {

				self._madChickGoogleMapInstance.performItemSearch(itemName, itemId);

			} else {

				self._madChickGoogleMapInstance.setCenter();

			}

		}, lat, lng);

	};

	// *****************************************************************
	// ******************** for internal use only **********************
	// *****************************************************************


	//---------------------------------------------------------------------------------
	this._displayGoogleMapWin=function(callback, centerLat, centerLng)  {

		if (this._googleMapWin && this._googleMapWin.closed) { // check wether googleMapWin was closed

			this._madChickGoogleMapInstance=false;
			this._googleMapWin=false;

		}

		if (this._googleMapWin && this._madChickGoogleMapInstance) { // googleMap is ready for use

			this._googleMapWin.focus();

			if (centerLat && centerLng) {

				this._madChickGoogleMapInstance.setCenter(centerLat, centerLng);

			}

			callback();

		} else { // (re)open googleMapWin

			if (centerLat && centerLng) {

				window.madChickGoogleMapCenter={
					'lat':centerLat,
					'lng':centerLng
				};

			}

			window.madChickGoogleMapCallback=function (madChickGoogleMapInstance) { // will be recalled by MadChickGoogleMap when it is initialized
				window.madChickGoogleMapCallback=false;
				self._madChickGoogleMapInstance=madChickGoogleMapInstance;
				callback();
			};

			this._googleMapWin=window.open(this._googleMapWinUrl, 'googleMapWin', 'resizable=yes, height=600px, width=1000px, dependent=yes');
			this._googleMapWin.focus();

		}

	};

	//---------------------------------------------------------------------------------
	this._initEditMode=function()  {

		this._madChickGoogleMapInstance.setSubmitHandler(function (gLat, gLng) {

			self._gLatFormFieldObj.ownerDocument.parentWindow.focus(); // if we wouldn't do this, the confirm-dialog will may be hidden behind the map-window
			
			if ((self._gLatFormFieldObj.value=='' && self._gLngFormFieldObj.value=='') || confirm('Sollen die bestehenden Geo-Daten ersetzt werden?')) {

				self._gLatFormFieldObj.value=gLat;
				self._gLngFormFieldObj.value=gLng;

				if (self._gLatFormFieldObj.onchange) {

					self._gLatFormFieldObj.onchange();

				}

				if (self._gLngFormFieldObj.onchange) {

					self._gLngFormFieldObj.onchange();

				}

			}

			return true;

		});

	};

	//--- Constructor ---------------------------------------------------------------
	this._init=function()  {


	};

	// call constructor
	this._init();

}
