/**
 * @author engel
 */
function Poi(id,standort,ortsname,text,oeffnungszeiten,icon,picture_url,picture_width,picture_height,lat,lng) {
	this.id = id;
	this.standort = standort;
	this.ortsname = ortsname;
	this.text = text;
	this.oeffnungszeiten = oeffnungszeiten;
	this.icon = icon;
	this.picture_url = picture_url;
	this.picture_width = picture_width;
	this.picture_height = picture_height;
	this.lat = lat;
	this.lng = lng;
	this.touren = [];
	this.marker = null;
};

Poi.prototype.onclick = function() { this.marker.openInfoWindow(this.get_info_dom()); };

Poi.prototype.get_show_func = function(){
	var _this = this;
	return function(){
		_this.onclick();
	};
};

Poi.prototype.get_info_dom = function() {

	var dom = document.createElement('div');
	dom.style.width="480px";
	var text = document.createElement('div');
	text.innerHTML = ['<h4>',this.standort,(this.ortsname?' ('+this.ortsname+')':''),'</h4><p>',
		'<img align="left" src="bilder/',this.picture_url,'" width="',this.picture_width,'" height="',this.picture_height,'">',
		this.text,(this.oeffnungszeiten?'<br/>'+this.oeffnungszeiten:''),'</p><hr style="clear:both;"/>'].join('');
	dom.appendChild(text);
	if (this.touren.length>0) {
		var p = document.createElement('h5');
		p.innerHTML = 'Tourenvorschläge in der Nähe:';
		dom.appendChild(p);
	
		var ul = document.createElement('ul');
		for(var i in this.touren) {
			var t = this.touren[i];
			var li = document.createElement('li');
			li.onclick = t.get_show_func();
			li.title = 'Tour anzeigen';
			li.style.cursor = 'pointer';
			li.innerHTML = t.name;
			ul.appendChild(li);
		}
		dom.appendChild(ul);
	}
	return dom;
};

Poi.prototype.get_marker = function() {
	if (this.marker) return this.marker;
	var latlng = new google.maps.LatLng(this.lat,this.lng);
	var icon = new google.maps.Icon();
	icon.image = 'media/'+this.icon;
	icon.iconSize = new google.maps.Size(20,20);
	icon.iconAnchor = new google.maps.Point(10,10);
	icon.infoWindowAnchor = new google.maps.Point(15,5);
	this.marker = new google.maps.Marker(latlng,{icon: icon, title: this.standort});
	google.maps.Event.bind(this.marker,'click',this,this.onclick);
	this.marker.parent_obj = this;
	return this.marker;
};

function Station(id,art,standort,info,anschrift,strasse,plz,ort,tel,oeffnungszeiten,lat,lng) {
	this.id = id;
	this.art = art;
	this.standort = standort;
	this.info = info;
	this.anschrift = anschrift;
	this.strasse = strasse;
	this.plz = plz;
	this.ort = ort;
	this.tel = tel;
	this.oeffnungszeiten = oeffnungszeiten;
	this.lat = lat;
	this.lng = lng;
	this.touren = [];
	this.marker = null;
};

Station.prototype.onclick = function() { this.marker.openInfoWindow(this.get_info_dom()); };

Station.prototype.get_show_func = function(){
	var _this = this;
	return function(){
		_this.onclick();
	};
};

Station.prototype.get_info_dom = function() {
	var arttxt =  {Master: 'Service-Station',LS: 'Verleihstation',Akku:'Akkuwechselstation'};
	var dom = document.createElement('div');
	dom.style.width="350px";
	var text = document.createElement('div');
	text.innerHTML = ['<h4>',this.standort,'</h4><p><b>',
		arttxt[this.art],'</b><br/>',
		(this.info?'<b>'+this.info+'</b><br/>':''),
		(this.oeffnungszeiten?'Öffnungszeiten: '+this.oeffnungszeiten+'<br/>':''),this.anschrift,/*'<br/>',this.strasse,'<br/>',this.plz,' ',this.ort,*/'<br/>Tel: ',this.tel,
		'</p><hr/>'].join('');
	dom.appendChild(text);
	if (this.touren.length>0) {
		var ul = document.createElement('ul');
		var p = document.createElement('h5');
		p.innerHTML = 'Tourenvorschläge in der Nähe:';
		dom.appendChild(p);
		
		for(var i in this.touren) {
			var t = this.touren[i];
			var li = document.createElement('li');
			li.onclick = t.get_show_func();
			li.title = 'Tour anzeigen';
			li.style.cursor = 'pointer';
			li.innerHTML = t.name;
			ul.appendChild(li);
		}
		dom.appendChild(ul);		
	}
	return dom;
};

Station.prototype.get_marker = function() {
	if (this.marker) return this.marker;
	var latlng = new google.maps.LatLng(this.lat,this.lng);
	var icon = new google.maps.Icon();
	switch (this.art) {
		case 'Master':
			icon.image = 'media/service.gif';
			break;
		case 'LS':
			icon.image = 'media/verleih.gif';
			break;
		case 'Akku':
			icon.image = 'media/akku.gif';
			break;
	}
	icon.iconSize = new google.maps.Size(26,26);
	icon.iconAnchor = new google.maps.Point(13,13);
	icon.infoWindowAnchor = new google.maps.Point(18,8);
	
	this.marker = new google.maps.Marker(latlng,{icon: icon, title: this.standort});
	google.maps.Event.bind(this.marker,'click',this,this.onclick);
	this.marker.parent_obj = this;
	return this.marker;
};

function Tour(id, name, km, hm, schwierigkeit, text, picture_url, picture_width, picture_height, points, levels, stationen_ids, poi_ids, start, mitte, ziel, bounds) {
	this.id = id;
	this.name = name;
	this.km = km;
	this.hm = hm;
	this.schwierigkeit = schwierigkeit;
	this.text = text;
	this.picture_url = picture_url;
	this.picture_width = picture_width;
	this.picture_height = picture_height;
	this.points = points;
	this.levels = levels;
	this.stationen_ids = stationen_ids;
	this.poi_ids = poi_ids;
	this.stationen = [];
	this.pois = [];
	this.polyline = null;
	this.start = start;
	this.mitte = mitte;
	this.ziel = ziel;
	this.bounds = bounds;
};

Tour.prototype.onclick = function(latlng) { this.show_infowindow(latlng) };

Tour.prototype.show_infowindow = function(latlng) {};

Tour.prototype.init_poi_stationen = function(pois, stationen) {
	for(var i=0;i<this.stationen_ids.length;i++) {
		var station = stationen[this.stationen_ids[i]];
		this.stationen.push(station);
		station.touren.push(this);
	}
	for(var i=0;i<this.poi_ids.length;i++) {
		var poi = pois[this.poi_ids[i]];
		this.pois.push(poi);
		poi.touren.push(this);
	}
};

Tour.prototype.get_polyline = function() {
	if (this.polyline) return this.polyline;
	switch (this.schwierigkeit) {
		case 1: //leicht
			var color = '#0000ff';
			break;	
		case 2: //mittelschwer
			var color = '#ff0000';
			break;
		case 3: //schwierig
			var color = '#000000';
			break;
	}
	this.polyline = new google.maps.Polyline.fromEncoded({
    color: color,
    weight: 3,
    opacity: 1,
    points: this.points,
    levels: this.levels,
    numLevels: 18,
    zoomFactor: 2
  });
	google.maps.Event.bind(this.polyline, 'click', this, this.onclick);
	return this.polyline;	
};

Tour.prototype.get_info_dom = function() {
	var stxt = {LS:'Leih- und Akkuladestation',Master:'Leih- und Akkuladestation',Akku:'Akkuladestation'};
	var dom = document.createElement('div');
	dom.style.width="450px";
	var schwtext = {1: 'leicht',2:'mittelschwierig',3:'schwer'};
	var text = document.createElement('div');
	text.innerHTML = ['<h4>',this.name,'</h4><p><b>',
		schwtext[this.schwierigkeit],', ',this.km+' km, '+this.hm+' Höhenmeter</b>','<br/>',this.text,
		'<br/><img src="bilder/',this.picture_url,'" width="',this.picture_width,'" height="',this.picture_height,'"/></p><hr/>'].join('');
	dom.appendChild(text);
	if (this.stationen.length>0) {
		var p = document.createElement('h5');
		p.innerHTML = 'Stationen im Streckenverlauf:';
		dom.appendChild(p);
		var ul = document.createElement('ul');
		for(var i in this.stationen) {
			var s = this.stationen[i];
			var li = document.createElement('li');
			li.onclick = s.get_show_func();
			li.title = 'Infos zur Station anzeigen';
			li.style.cursor = 'pointer';
			li.innerHTML = s.standort+' ('+stxt[s.art]+')';
			ul.appendChild(li);
		}
		dom.appendChild(ul);		
	}
	if (this.pois.length>0) {
		var p = document.createElement('h5');
		p.innerHTML = 'Sehenswürdigkeiten:';
		dom.appendChild(p);
		var ul = document.createElement('ul');
		for(var i in this.pois) {
			var p = this.pois[i];
			var li = document.createElement('li');
			li.onclick = p.get_show_func();
			li.title = 'Infos zu Poi anzeigen';
			li.style.cursor = 'pointer';
			li.innerHTML = p.standort;
			ul.appendChild(li);
		}
		dom.appendChild(ul);		
	}

	return dom;
};

Tour.prototype.onshow = function() {alert('Show Tour'+this.name); };

Tour.prototype.get_show_func = function(){
	var _this = this;
	return function(){
		_this.onshow();
	};
};

function Ebike_App(data,map_div_id,background_url) {
	this.data = data;
	this.map_div_id = map_div_id;
	this.tour_shown = null;
	this.background_url = background_url;
	for(var t in this.data.touren) {
		this.data.touren[t].init_poi_stationen(this.data.pois,this.data.stationen);
	}
	var _this = this;
	google.load("maps", "2.x");
	google.setOnLoadCallback(function() { _this.init_map(); });

	Tour.prototype.onshow = function() {
		_this.show_tour(this);
		this.onclick(new google.maps.LatLng(this.start[0],this.start[1]));
	};
	
	Tour.prototype.onclick = function(latlng) {
		_this.map.openInfoWindow(latlng, this.get_info_dom());
	};
};

Ebike_App.prototype.init_map = function() {
	//limit zoom: mt = MapTypes
	var sw = new google.maps.LatLng(this.data.wegnetz.sw.lat,this.data.wegnetz.sw.lng);
	var ne = new google.maps.LatLng(this.data.wegnetz.ne.lat,this.data.wegnetz.ne.lng);
	this.bounds = new google.maps.LatLngBounds(sw,ne);
	this.map = new google.maps.Map2(document.getElementById(this.map_div_id));
	var mt = this.map.getMapTypes();
	for (var i=0; i<mt.length; i++) { 
        mt[i].getMinimumResolution = function() {return 10;}
        mt[i].getMaximumResolution = function() {return 15;}
  }
	G_PHYSICAL_MAP.getMinimumResolution = function() {return 10;}
	G_PHYSICAL_MAP.getMaximumResolution = function() {return 15;}
	this.map.setCenter(this.bounds.getCenter(),this.map.getBoundsZoomLevel(this.bounds),G_PHYSICAL_MAP);
	this.map.setUIToDefault();
	this.overview = new google.maps.OverviewMapControl();
	this.map.addControl(this.overview);
	this.background = new google.maps.GeoXml(this.background_url);
	this.map.addOverlay(this.background);
	google.maps.Event.clearNode(this.background);
	for (var i in this.data.pois) {
		this.map.addOverlay(this.data.pois[i].get_marker());
	}
	for (var i in this.data.stationen) {
		this.map.addOverlay(this.data.stationen[i].get_marker());
	}
	if (window.location.search) {
		var query = window.location.search;
		query = query.substring(1,query.length);
		var search = query.match(/(station|tour|poi)=([0-9]+)/);
		if (search.length>2) {
			var id = parseInt(search[2]);
			switch (search[1]) {
				case 'station':
					var s = this.data.stationen[id];
					if (s) {s.onclick();}
					break;
				case 'poi':
					var s = this.data.pois[id];
					if (s) { s.onclick(); }
					break
				case 'tour':
					var t = this.data.touren[id];
					if (t) { t.onshow(); }
					break;
			}
		}
		//google.maps.Log.write(search.toSource());
	}
	window.focus();
};

Ebike_App.prototype.show_tour = function(tour) {
	if (this.tour_shown) {
		this.map.removeOverlay(this.tour_shown.get_polyline());
	}
	this.tour_shown = tour;
	this.map.addOverlay(tour.get_polyline());
};
