/**
 * Route - Sammlung aller Routenpunkte einer Route.
 * @author Andreas Bethge
 * @copyright 2009, Andreas Bethge | it consulting
 */
function Route(){
	
	var newRpId = -1;
	this.id = -1;
	this.title = "";
	this.description = "";
	this.countryCodes = "";
	this.routepoints = new Array();
	this.tag = -1;
	this.track = '';
	
	this.fromJson = function(json){
		//alert(JSON.stringify(json));
		this.id = json.id;
		this.title = json.title;
		this.countryCodes = json.countryCodes;
		this.tag = json.tag;
		this.track = json.track;
		this.description = json.description;
		this.routepoints = new Array();
		if(json.routepoints){
			for(var i = 0; i < json.routepoints.length; i++){
				if(json.routepoints[i] != null){
					var rp = new Routepoint();
					this.routepoints.push(rp.fromJson(json.routepoints[i]));
				}
			}
		}
		return this;
	}
	
	this.getTrackpoints = function(){
		var tps = this.track.split(";");
		var ret = new Array();
		for(var i = 0; i < tps.length; i++){
			var coord = tps[i].split(" ");
			if(coord.length == 2){
				var tp = new Object();
				tp.lat = coord[0];
				tp.lng = coord[1];
				ret.push(tp);
			}
		}
		return ret;
	}

	this.getRoutepointById = function(id){
		//alert("gesucht: " + id);
		var ret = null;
		for(var i = 0; i < this.routepoints.length; i++){
			//alert("prüfe: " + this.routepoints[i].id);
			if(this.routepoints[i].id == id){
				ret = this.routepoints[i];
				break;
			}
		}
		return ret;
	}
	
	this.addRoutepoint = function(){
		var rp = new Routepoint();
		// ID für nicht persistentes Objekt vergeben
		rp.id = newRpId;
		// "ID-Generator"
		newRpId--;
		// Routepoint der Routepoint-Collection der Route hinzufügen
		this.routepoints.push(rp);
		return rp;
	}

	this.removeRoutepointWithId = function(id){
		var rpToRemove = this.getRoutepointById(id);
		if(rpToRemove){
			rpToRemove.marker.remove();
			var newArray = new Array();
			for(var i = 0; i < this.routepoints.length; i++){
				var rp = this.routepoints[i];
				if(rp.id != rpToRemove.id){
					newArray.push(rp);
				}
			}
			this.routepoints = newArray;	
		}
	}
	
	this.cloneFlat = function(){
		var temp = new Route();
		temp.id = this.id;
		temp.title = this.title;
		temp.description = this.description;
		temp.countryCodes = this.countryCodes;
		temp.tag = this.tag;
		temp.track = this.track;
		temp.routepoints = new Array();
		for(var i = 0; i < this.routepoints.length; i++){
			var flatRp = this.routepoints[i].cloneWithoutMarker();
			if(flatRp){
				temp.routepoints.push(flatRp);
			}
		}
		return temp;
	}
}
