
var wtaxi = {}

wtaxi.DBG = false;

wtaxi.priceGrund = 2.5;
wtaxi.priceAnfahrt = 0.7;
wtaxi.priceKm = 1.5;
wtaxi.priceWarten = 0.4;

wtaxi.locationFieldIds = ['locPanelFieldFrom', 'locPanelFieldTo'];
wtaxi.locationPanelIds = ['locPanelFrom', 'locPanelTo'];
wtaxi.locationPanelCtrlIds = ['locPanelCtrlFrom', 'locPanelCtrlTo'];

wtaxi.mainContainerId = 'wtaxiContainer';
wtaxi.mapCanvasId = 'mapCanvas';
wtaxi.routeResultPanelId = 'wtaxiRouteResultPanel';
wtaxi.routeErrorPanelId = 'wtaxiErrorPanel';
wtaxi.defaultLocIdFrom = 'bhfwn';
wtaxi.defaultLocs = [
	{id: 'bhfwn', 	descr: 'Bahnhof Waiblingen', 		lat: 48.82636184048252, lng: 9.301385879516602},
	{id: 'sthbf', 	descr: 'Stuttgart Hauptbahnhof', 	lat: 48.78329986937028, lng: 9.181180000305176},
	{id: 'stfh', 	descr: 'Stuttgart Flughafen', 		lat: 48.69062043835693, lng: 9.193475246429443},
	{id: 'fellb', 	descr: 'Fellbach', 					lat: 48.80900441464461, lng: 9.272933006286621},
	{id: 'stihl', 	descr: 'Firma Stihl, Waiblingen', 	lat: 48.84598678559207, lng: 9.315258264541626},
	{id: 'bosch', 	descr: 'Firma Bosch, Waiblingen', 	lat: 48.82327510241731, lng: 9.296965599060059},
	{id: 'winn', 	descr: 'Winnenden', 				lat: 48.87530041538759, lng: 9.398417472839355},
	{id: 'korb', 	descr: 'Korb', 						lat: 48.842731794028424, lng: 9.361000657081604},
	{id: 'rommel', 	descr: 'Rommelshausen', 			lat: 48.80838969475986, lng: 9.317693710327148},
	{id: 'schaik', 	descr: 'Schwaikheim', 				lat: 48.87473590373213, lng: 9.351511001586914},
	{id: 'weinst', 	descr: 'Weinstadt', 				lat: 48.81291159588547, lng: 9.370222091674805} 
];
wtaxi.homeLoc = {lat: 48.826786, lng: 9.300785}

wtaxi.mapfront = null;
wtaxi.init = function(mapfrontObj) {
	
	wtaxi.mapfront = mapfrontObj;
	var el = null;
	
	for(var l = 0; l < wtaxi.locationPanelIds.length; l++) {
		el = document.getElementById(wtaxi.locationPanelIds[l]);
		var inner = "";
		for(var i = 0; i < wtaxi.defaultLocs.length; i++) {
			var locDef = wtaxi.defaultLocs[i];
			inner = inner + 
				"<div id=\"sel_" + locDef.id + "\">" +
				"<a href=\"javascript: void(0);\" class=\"wtaxiLocSel\" " + 
					"onclick=\"wtaxi.onSelectLoc('" + locDef.id + "', '" + (l==0?"from":"to") + "')\">" +  
				locDef.descr + 
				"</a>" + 
				"</div>\n";
		}
		if(el) el.innerHTML = inner;
	}
	wtaxi.onSelectLoc(wtaxi.defaultLocIdFrom, 'from');
	if(wtaxi.DBG) wtaxi.writeOut("wtaxi.init DONE");
}

wtaxi.initLegende = function() {
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Grund", 
						wtaxi.formatPrice(wtaxi.priceGrund));
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Anfahrt", 
						wtaxi.formatPrice(wtaxi.priceAnfahrt));
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Km", 
						wtaxi.formatPrice(wtaxi.priceKm));
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Warten", 
						wtaxi.formatPrice(wtaxi.priceWarten));
}

wtaxi.onLocPanelCtrl = function(el, choice) {
	if(choice == null) {
		if(el.id == wtaxi.locationPanelCtrlIds[0]) choice = 0;
		if(el.id == wtaxi.locationPanelCtrlIds[1]) choice = 1;
	}
	
	if(choice !== null) {
		wtaxi.showLocPanel(choice, 'toggle');
		wtaxi.showLocPanel(choice == 0 ? 1 : 0, false);
	}
	
}

wtaxi.showLocPanel = function(choice, doShow) {
	var elId = wtaxi.locationPanelIds[choice];
	return wtaxi.showElement(elId, doShow);
}

wtaxi.onSearch = function(from, to) {
	var fromIsEmpty = wtaxi.stringIsEmpty(from);
	var toIsEmpty = wtaxi.stringIsEmpty(to);
	if(fromIsEmpty || toIsEmpty) {
		wtaxi.showError("Bitte f&uuml;llen Sie beide Felder (von, nach) aus.");
		if(fromIsEmpty) wtaxi.markLocField(0, true);
		if(toIsEmpty) wtaxi.markLocField(1, true);
		return false;
	} else {
		wtaxi.resetMarkLocFields();
	}
	wtaxi.showElement(wtaxi.mainContainerId, true);
	wtaxi.showElement(wtaxi.mapCanvasId, true);
	wtaxi.showElement(wtaxi.routeErrorPanelId, false);
	wtaxi.showElement(wtaxi.routeResultPanelId, false);
	var homeLoc = wtaxi.defaultLocs[0];
	var homeLocStr = homeLoc.descr + " @ " + homeLoc.lat + "," + homeLoc.lng;;
	wtaxi.mapfront.getDirections(homeLocStr, to, from);
}

wtaxi.stringIsEmpty = function(str) {
	var nstr = str.replace(/\s/g, '');
	return (nstr.length == 0);
}

wtaxi.resetMarkLocFields = function() {
	wtaxi.markLocField(0, false);
	wtaxi.markLocField(1, false);
}

wtaxi.markLocField = function(choice, doMark) {
	var el = document.getElementById(wtaxi.locationFieldIds[choice]);
	if(el) {
		if(doMark) {
			el.style.backgroundColor = '#FDFF03';
		} else {
			el.style.backgroundColor = '#fff';
		}
	}
}

wtaxi.onSelectLoc = function(locId, toFrom) {
	wtaxi.resetMarkLocFields();
	var choice = toFrom == 'from' ? 0 : 1;
	var locDef = wtaxi.getLocDefById(locId);
	var el = document.getElementById(wtaxi.locationFieldIds[choice]);
	if(el) {
		el.value = locDef.descr + " @ " + locDef.lat + "," + locDef.lng;
		wtaxi.showLocPanel(choice, false);
	}
}

wtaxi.getLocDefById = function(locId) {
	for(var i = 0; i < wtaxi.defaultLocs.length; i++) {
		var locDef = wtaxi.defaultLocs[i];
		if(locDef.id == locId) return locDef;
	}
	return null;
}

wtaxi.writeOut = function(s) {
	if(!wtaxi.DBG) return false;
	var el = document.getElementById('out');
	if(el) {
		el.innerHTML = el.innerHTML + s + "<br/>\n"; 
	} else {
		alert(s);
	}
}

wtaxi.onLoad = function(mapfront) {
	
	var routeComplete = mapfront.gdir;
	var routeAnfahrt =  mapfront.gdir.getRoute(0);
	var routeFahrt =  mapfront.gdir.getRoute(1);
	
	var distAnfahrt = routeAnfahrt.getDistance();
	var distFahrt = routeFahrt.getDistance();
	var dauerFahrt = routeFahrt.getDuration();
	
	var price = wtaxi.calcPrice(distAnfahrt, distFahrt);
	
	var el = document.getElementById(wtaxi.routeResultPanelId);
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Dauer", dauerFahrt.html); 
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Distanz", distFahrt.html); 
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "Preis", price);

	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "AnfahrtDistanz", distAnfahrt.html);
	wtaxi.setSubElementHtml(wtaxi.routeResultPanelId, "FahrtDistanz", distFahrt.html);
	 
	wtaxi.initLegende();
	
	wtaxi.showElement(wtaxi.routeResultPanelId, true);
}

wtaxi.onError = function(mapfront, errorText, errorCode) {
	if(errorCode == G_GEO_BAD_REQUEST) {
		wtaxi.showError("Bitte f&uuml;llen Sie beide Felder aus.");
	} else if(errorCode == G_GEO_UNKNOWN_ADDRESS) {
		wtaxi.showError("Die Adresse konnte nicht gefunden werden. <br/>" + 
			"Bitte geben sie Stra&szlig;e und Ort an.");
	} else {
		wtaxi.showError(errorText);
	}
}

wtaxi.showError = function(errorText) {
	var el = document.getElementById(wtaxi.routeErrorPanelId);
	el.innerHTML = errorText;
	wtaxi.showElement(wtaxi.routeErrorPanelId, true);
}

wtaxi.calcPrice = function(distanceAnfahrt, distanceFahrt) {
	var kmAnfahrt = Math.round(distanceAnfahrt.meters / 100 ) / 10;
	var kmFahrt = Math.round(distanceFahrt.meters / 100 ) / 10;
	var p = wtaxi.priceGrund 
				+ wtaxi.priceAnfahrt * kmAnfahrt 
				+ wtaxi.priceKm * kmFahrt;
	p = Math.round(p*10) / 10;
	return wtaxi.formatPrice(p);
}

wtaxi.formatPrice = function(price) {
	var pstr = String(Math.round(price*100) / 100);
	var pstr_parts = pstr.split('.');
	return pstr_parts[0] + "," + (
		pstr_parts.length > 1
			?  wtaxi.padAndCropRight(pstr_parts[1], 2, '0')
			: "00"
	);
}

wtaxi.padAndCropRight = function(str, num, fillChar) {
	var fill = num - str.length;
	if(fill < 0) {
		return str.substr(0, num);
	} else if(fill == 0) {
		return str;
	} else if(fill > 0) {
		for(var i = 0; i < fill; i++) {
			str = str + fillChar;
		}
	}
	return str;
}

wtaxi.getSubElement = function(rootId, subId) {
	return document.getElementById(rootId + subId);
}

wtaxi.setSubElementHtml = function(rootId, subId, html) {
	var el = wtaxi.getSubElement(rootId, subId);
	if(el) el.innerHTML = html;
}

wtaxi.showElement = function(elId, doShow) {
	var el = document.getElementById(elId);
	if(el) {
		if(doShow == 'toggle' || doShow == null) {
			doShow = !(el.style.display == 'block');
		}
		if(doShow && (el.style.display == 'none' || el.style.display == "")) {
			el.style.display = 'block';
		} else if(!doShow) {
			el.style.display = 'none';
		}
		return true;
	}
	return false;
}
