Namespace("GF.map");

GF.map.Map = function(id, div, xZoom) {
			
	this.map = null;
	this.wpMrkMap = [];
	this.rteMrkMap = [];
	this.trkMrkMap = [];
	
	this.Map = function(id, div, xZoom) {		
      if (typeof(GBrowserIsCompatible) != "undefined" && GBrowserIsCompatible()) {
    	var mapdiv = document.getElementById(div)       
     	var inst = this;    	    	
        EditManager.getMap(id, function(map){        	
        	inst.map = new GMap2(mapdiv);
        	if(!xZoom) {
        		xZoom = 0;
        	}
            inst.map.addMapType(G_PHYSICAL_MAP);
            var types = inst.map.getMapTypes();
            for(i = 0; i < types.length; i++) {
            	if(map.view == types[i].getName()) {
            		inst.map.setMapType(types[i]);
            	}
            }       
            inst.map.setUIToDefault();
            inst.map.setCenter(new GLatLng(map.center[1], map.center[0]), map.zoom + xZoom);                        
	        inst.renderWaypoints(map.layer.id); 
		    inst.renderRoutes(map.layer.id);
		    inst.renderTracks(map.layer.id); 
        });
      }     
    };	
    
    this.renderTracks = function(id) {
     	EditManager.getTracks(id, this.getTrkCallback());
    }

    this.getTrkCallback = function() {
	    var inst = this;	
		function handler(trks) {
			 for(i = 0; i < trks.length; i++) {
				 if(trks[i].points != null && inst.trkMrkMap[trks[i].id] == null) {
					inst.trkMrkMap[trks[i].id] = inst.renderTrk(trks[i]);
				 }
			 }
		}
		return handler;
	};
	
	this.renderTrk = function(trk) {
		var pline = new GPolyline.fromEncoded({
	 	    color: "#cc0000",
	 	    weight: 3,
	 	    opacity: 0.9,
			points: trk.points,
			levels: trk.levels,
	 	    zoomFactor: 2,
	 	    numLevels: 18
	 	});
	 	this.map.addOverlay(pline);
	    GEvent.addListener(pline, "click", this.onClickTrk());	    
	 	pline.gf_tag = trk;
	 	return pline;
	}

	this.onClickTrk = function() {
	    var inst = this;	
		function handler() {
			inst.showTrkProps(this.gf_tag);
		}
		return handler;
	}
		
	this.onMapTypeChanged = function() {
	    var inst = this;	
		function handler() {
			inst.saveMap();
		}
		return handler;
	}
	
	this.renderWp = function(wp) {
		var baseIcon = new GIcon(G_DEFAULT_ICON);
	    baseIcon.shadow = GF.icon.prefix + "shadow" + GF.icon.suffix;
	    baseIcon.iconSize = new GSize(15, 15);
	    baseIcon.shadowSize = new GSize(15, 15);
	    baseIcon.iconAnchor = new GPoint(8, 8);
	    baseIcon.infoWindowAnchor = new GPoint(8, 8);
	    var letteredIcon = new GIcon(baseIcon);
	    var icon = "waypoint";
	    if(wp.icon) {
	    	icon = wp.icon;
	    }
	    letteredIcon.image = GF.icon.prefix + icon + GF.icon.suffix;
		
		var marker = new GMarker(new GLatLng(wp.location[1], wp.location[0]), {draggable: true, icon:letteredIcon});
		marker.gf_tag = wp;
	    GEvent.addListener(marker, "click", this.onMarkerClick(marker));			
		this.map.addOverlay(marker);
		return marker;
	}
    
    this.renderWaypoints = function(id) {   	
    	EditManager.getWaypoints(id, this.getWpCallback());
    }
    
    this.getWpCallback = function() {
	    var inst = this;	
		function handler(wps) {
    		for(i = 0; i < wps.length; i++) {
    			if(inst.wpMrkMap[wps[i].id] == null) {
					inst.wpMrkMap[wps[i].id] = inst.renderWp(wps[i]);
    			}
 	    	}
		}
		return handler;
	};
    
	this.renderRoutes = function(id) {
     	EditManager.getRoutes(id, this.getRteCallback());
 	}
		
	this.getRteCallback = function() {
		var inst = this;	
		function handler(rtes) {
			for(i = 0; i < rtes.length; i++) {
				if(inst.rteMrkMap[rtes[i].id] == null) {
		    		var wps = rtes[i].waypoints;
		    		var vtxs = [];
		    		for(j = 0; j < wps.length; j++) {
		    			vtxs[j] = new GLatLng(wps[j].location[1], wps[j].location[0]);
		    		}				    		
		    		var pline = inst.renderRoute(vtxs, rtes[i]);		
		    		pline.gf_tag = rtes[i];
		    		inst.rteMrkMap[rtes[i].id] = pline;	
				}
	    	}	
		}
		return handler;
	}
	
	this.decToDegMin = function(dec) {
		var deg = Math.floor(dec);
		var min = ((dec - deg) * 60).toFixed(3);
		return deg + "&deg;" + min;
	}
	
	this.displayLat = function(lat) {
		if(lat > 0) {
			return "N " +  this.decToDegMin(lat);
		}
		else {
			return "S " +  this.decToDegMin(-lat);
		}
	}
	
	this.displayLng = function(lng) {
		if(lng > 0) {
			return "E " +  this.decToDegMin(lng);
		}
		else {
			return "W " +  this.decToDegMin(-lng);
		}
	}
	
	this.showWpById = function(id) {
		this.showWpProps(this.wpMrkMap[id].gf_tag);
	}
	
	this.showRteById = function(id) {
		this.showRteProps(this.rteMrkMap[id].gf_tag);
	}
	
	this.showTrkById = function(id) {
		this.showTrkProps(this.trkMrkMap[id].gf_tag);
	}
	
    this.showWpProps = function(wp) {
        var html = "<small>Name:</small><br/><b>" + wp.name + "</b><br/><br/><small>Position:</small><br/><b>" + 
        			this.displayLat(wp.location[1]) + " " + this.displayLng(wp.location[0]) + "</b>";		
    	this.map.openInfoWindowHtml(new GLatLng(wp.location[1],wp.location[0]), html);
	}
    
    this.showTrkProps = function(trk) {
 	   var html = "<small>Name:</small><br/><b>" + trk.name + "</b><br/><br/><small>Starting Position:</small><br/><b>" + 
		this.displayLat(trk.startingPoint.location[1]) + " " + this.displayLng(trk.startingPoint.location[0]) + "</b>";		
	       this.map.openInfoWindowHtml(new GLatLng(trk.startingPoint.location[1], trk.startingPoint.location[0]), html);
    }
    
    this.showRteProps = function(rte) {
	   var html = "<small>Name:</small><br/><b>" + rte.name + "</b><br/><br/><small>Starting Position:</small><br/><b>" + 
		this.displayLat(rte.startingPoint.location[1]) + " " + this.displayLng(rte.startingPoint.location[0]) + "</b>";		
	       this.map.openInfoWindowHtml(new GLatLng(rte.startingPoint.location[1], rte.startingPoint.location[0]), html);
	} 
    
    this.renderRoute = function(latlngs) {
/*    	    	
    	var line = new GPolyline([
	    	new GLatLng(59.88893689676585, 10.7666015625),
	    	new GLatLng(60.59775635343141, 10.4150390625),
	    	new GLatLng(61.10078883158897, 8.96484375),
	    	new GLatLng(61.60639637138628, 8.0419921875)
	    	], "#0000dd", 7, 0.6);

    	
    	var line = new GPolyline([
    	                  	    new GLatLng(59.88893689676585, -102.1419),
    	                  	    new GLatLng(60.59775635343141, -102.1429),
    	                  	    new GLatLng(61.10078883158897, -102.1459),
    	                	    new GLatLng(61.60639637138628, -102.1519)
    	                	], "#0000dd", 7, 0.6);
    	
    	
*/    	
    	var line = new GPolyline(latlngs, "#0000dd", 7, 0.6);

    	this.map.addOverlay(line);

	    GEvent.addListener(line, "click", this.onClickRoute());	    
    	return line;
    }

    this.onClickRoute = function() {
	    var inst = this;	
		function handler(latlng) {
			inst.showRteProps(this.gf_tag);
		}
		return handler;
	}
        
    this.onMarkerClick = function(marker) {
	    var inst = this;	
		function handler(latlng) {			
			inst.showWpProps(marker.gf_tag)			
		}
		return handler;
	}	
	 
	this.Map(id, div, xZoom);
}




 