/*
* author    Sam Greenhalgh
* created   2 May 2006
*/
var AdventurstsMap = {
    routeMap: null,
    currentRoute: null
};

AdventurstsMap.Map = function(a, b, c){

    var googlMap = null;
    var domId = (a)?a:'map';
    if(!b) b = new GLatLng(52, 2);
    if(!c) c = 5;
    
    //check for valid mapContainer
    var mapContainer = document.getElementById(domId);
    if(!mapContainer){
        alert('Map container not found');
        return;
    }

    if(!GMap2){
        alert('The Google maps API is not available. Make sure you have included the Version 2 script in the page.');
        return;
    }

    googlMap = new GMap2(mapContainer);
    googlMap.addControl(new GSmallMapControl());
    googlMap.addControl(new GMapTypeControl());
    googlMap.setCenter(b, c);
    
    //add the VML XML namespace declaration to the page if it's missing
    function addVmlNs(){
        if(!document.namespaces) return false;
        for(var i = 0, j = document.namespaces.length; i < j; i++){
            var a = document.namespaces[i];
		    if(a.name == 'v' && a.URN == 'schemas-microsoft-com:vml') return true;
	    }
	    document.namespaces.add('v', 'schemas-microsoft-com:vml');
	    return false;
    }
    addVmlNs();
    
    this.getGoogleMap = function(){return googlMap};
}

AdventurstsMap.TeamControl = function(){}

AdventurstsMap.Route = function(a, b, c, d, e, f){

    var id = a;
    var color = (b)? b : '#FFCC00';
    var weight = (c)? c : 3;
    var opacity = (d)? d : 0.5;
    var polyline = null;
    var points = [];
    var map = f;
    var recordRef;
    var hilighted = false;
    var caption = e;
    var comment = '';
    var icon = new GIcon();
    var marker = null;

    icon.image = "http://home.instituteofadventureresearch.com/index.php?mode=route&sub=mapicon&color=" + color.replace(/#/,'');
    //icon.shadow = "http://www.mongolrally.com/waypoint_shadow.png";
    icon.iconSize = new GSize(11, 11);
    //icon.shadowSize = new GSize(0, 0);
    icon.iconAnchor = new GPoint(5, 5);
    icon.infoWindowAnchor = new GPoint(5, 5);  

    function redraw(){
        if(polyline){
            if(polyline.remove){map.removeOverlay(polyline)};
            polyline = null;
        };

	if(!marker){
            marker = new GMarker(points[Math.floor(points.length/2)], icon);	
            
	    map.addOverlay(marker);
	    GEvent.addListener(marker, 'click', hilight);
	}	

        if(points.length > 0){
            if(hilighted){
                polyline = new GPolyline(points, color, weight + 2, 1);
            }else{
                polyline = new GPolyline(points, color, weight, opacity);
            };
            map.addOverlay(polyline);
	    marker.setPoint(points[Math.floor(points.length/2)]);
        }
    }

    var click = function(a, b){
        if(a) b = a.getPoint();
        points.push(new GLatLng(b.y, b.x));
        redraw();
    }
    
    var clear = function(){
        points = [];
        redraw();
    }
    
    var startRecording = function(){
        recordRef = GEvent.addListener(map, 'click', click);
    };
    
    var stopRecording = function() {
        if(recordRef) GEvent.removeListener(recordRef);
        recordRef = null;
    };
    
    var distance = function(){
        var a = 0;
        for(var i = 1, j = points.length; i < j; i++)
        a += points[i -1].distanceFrom(points[i]);
        return a;
    };
    
    var setComment = function(a){
        comment = a;
    }
    
    var hilight = function(a){
	if(typeof(a) != 'boolean'){a=true};
        if(a){
            var s = '';
            s += '<b>' + caption + '</b><br/>\n';
            s += comment + '<br/>\n';
            s += (Math.floor(distance() / 1609.344)) + ' miles';
            map.openInfoWindowHtml(points[Math.floor(points.length /2)], s, {onCloseFn: function(){hilight(false)}});
        }
        hilighted = a;
        redraw();
    }
    
    this.hilight = hilight;
    this.startRecording = startRecording;
    this.stopRecording = stopRecording;
    this.isRecording = function(){(!recordRef)};
    this.clear = clear;
    this.distance = distance;
    this.getPoints = function(){if(!points) points = []; return points;}
    this.setPoints = function(a){points = a; redraw();}
    this.getRouteId = function(){return id;};
    this.setComment = setComment;
}