mapStatic = {
	geocoder:'',
	location1:'',
	location2:'',
	gDir:'',
	resultDiv:'costcalc-result',
	mapCanvas:'costcalc-result-canvas',
	directionsPanel:'costcalc-result-detail',
	fromInputId:'address1',
	toInputId:'address2',
	submitButton:'search',
	distanceSpan:'distanceres',
	distanceInput:'distance',
	fuelSpan:'fuel',
	othercostSpan:'othercostres',
	allcostSpan:'allcost',
	passengercountSpan:'passengercount',
	personalcostSpan:'personalcost',
	consuptionInput:'consuption',
	fuelcostInput:'fuelcost',
	othercostInput:'othercost',
	passengerCountInput:'passenger_count',
}

mapViewer=Class.create();
mapViewer.prototype = {
	initialize:function() {
		mapStatic.geocoder = new GClientGeocoder();
		mapStatic.gDir = new GDirections();
		GEvent.addListener(
			mapStatic.gDir, 
			'load',
			this.viewResult
		);
		MyEvent.add(mapStatic.submitButton,'click',this.showLocation, this);
	},
	viewResult:function(dist) {
		//alert(typeof(dist));
		if (typeof(dist) == 'object') {
			var drivingDistanceKilometers = mapStatic.gDir.getDistance().meters / 1000;
		} else {
			var drivingDistanceKilometers = dist;
		}
		if (drivingDistanceKilometers) {
			var fuelCost = drivingDistanceKilometers * $Ov(mapStatic.consuptionInput) * $Ov(mapStatic.fuelcostInput) / 100;
			var otherCost = $Ov(mapStatic.othercostInput);
			var allCost = Math.round(fuelCost) + Math.round(otherCost);
			var passengerCount = $Ov(mapStatic.passengerCountInput);
			
			$O(mapStatic.distanceSpan).innerHTML = Math.round(drivingDistanceKilometers) + ' Km';
			$O(mapStatic.fuelSpan).innerHTML = Math.round(fuelCost) + ' Ft';
			$O(mapStatic.othercostSpan).innerHTML = Math.round(otherCost) +' Ft';
			$O(mapStatic.allcostSpan).innerHTML = Math.round(allCost) + ' Ft';
			$O(mapStatic.passengercountSpan).innerHTML = Math.round(passengerCount);
			$O(mapStatic.personalcostSpan).innerHTML = Math.round(allCost / passengerCount) +' Ft';
			$O(mapStatic.resultDiv).style.display='';
			if (typeof(dist) == 'object') {
				map = new GMap2($O(mapStatic.mapCanvas));
				map.setCenter(new GLatLng(47.15984,19.500732), 7);
				map.enableContinuousZoom();
				directions = new GDirections(map, $O(mapStatic.directionsPanel));
				directions.load("from: "+$Ov(mapStatic.fromInputId)+" to: "+$Ov(mapStatic.toInputId));
			}
		} else {
			ValidatorHandle.visibleValidatorMessageByID('error_'+mapStatic.toInputId, 'Ismeretlen hiba');
		}
	},
	showLocation:function(a, b, caller){
		if (ValidatorHandle.getIsValid() === true) {
			if ($Ov(mapStatic.fromInputId) == '' || $Ov(mapStatic.toInputId) == '') {
				if ($Ov(mapStatic.distanceInput) != '') {
					caller.viewResult($Ov(mapStatic.distanceInput));
					return;
				} else {
					ValidatorHandle.visibleValidatorMessageByID('error_'+mapStatic.distanceInput, 'Kötelező!');
					return;
				}
			}
			$O(mapStatic.resultDiv).style.display='none';
			mapStatic.geocoder.getLocations(
				$Ov(mapStatic.fromInputId), 
				function (response) {
					if (
						!response || 
						response.Status.code != 200 ||
						response.Placemark[0].AddressDetails.Country == undefined
					){
						ValidatorHandle.visibleValidatorMessageByID('error_'+mapStatic.fromInputId, 'Nem található');
					} else {
						mapStatic.location1 = {
							address: response.Placemark[0].address
						};
						mapStatic.geocoder.getLocations(
							$Ov(mapStatic.toInputId), 
							function (response) {
								if (
									!response || 
									response.Status.code != 200 ||
									response.Placemark[0].AddressDetails.Country == undefined
								){
									ValidatorHandle.visibleValidatorMessageByID('error_'+mapStatic.toInputId, 'Nem található');
								} else {
									mapStatic.location2 = {
										address: response.Placemark[0].address
									};
									mapStatic.gDir.load('from: ' + mapStatic.location1.address + ' to: ' + mapStatic.location2.address);
								}
							}
						);
					}
				}
			);
		}
	}
}

