var WhereIsAndy = {
    routeMap: null,
    currentRoute: null
};

WhereIsAndy.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 GLargeMapControl());
//    googlMap.addControl(new GScaleMapControl());
    googlMap.addControl(new GMapTypeControl());
    googlMap.setCenter(b, c);

   googlMap.setMapType(G_HYBRID_TYPE);
    
    //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};
}

WhereIsAndy.TeamControl = function(){}

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

    var id = a;
    var color = (b)? b : '#FFCC00';
    var weight = (c)? c : 4;
    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 = "/maps/info_01_small.png";
//    icon.image = "http://labs.google.com/ridefinder/images/mm_20_blue.png";
//    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
//    icon.image = "/maps/blob.php?color=" + color.replace(/#/,'');
    //icon.image = "http://www.mongolrally.com/blob.php?color=" + color.replace(/#/,'');

    //icon.shadow = "http://www.mongolrally.com/waypoint_shadow.png";
    //icon.iconSize = new GSize(11, 11);
    icon.iconSize = new GSize(20, 20);
    //icon.shadowSize = new GSize(0, 0);
    //icon.iconAnchor = new GPoint(5, 5);
    icon.iconAnchor = new GPoint(10, 10);
    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, {maxWidth:400, 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;
}

WhereIsAndy.Waypoint = function(a, b, c){

    var id = a;
    var map = c;
    var recordRef;
    var icon = new GIcon();
    var caption = b;
    var comment = '';
    
    icon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    icon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    icon.iconSize = new GSize(12, 20);
    icon.shadowSize = new GSize(22, 20);
    icon.iconAnchor = new GPoint(6, 20);
    icon.infoWindowAnchor = new GPoint(5, 1);
    
    var marker;
    
    var click = function(a, b){
        if(b){
            setPoint(b);
        }
    }
    
    var startRecording = function(){
        recordRef = GEvent.addListener(map, 'click', click);
    };
    
    var stopRecording = function() {
        if(recordRef) GEvent.removeListener(recordRef);
        recordRef = null;
    };
    
    var setPoint = function(a){
        if(!marker){
            marker = new GMarker(a, icon);
            GEvent.addListener(marker, 'click', hilight);
            map.addOverlay(marker);
        }else{
            marker.setPoint(a);
        }
    };
    
    var setComment = function(a){
        comment = a;
    }
    
    var hilight = function(){
        //marker.openInfoWindowHtml('<b>' + caption + '</b><br/>\n' + comment);
        marker.openInfoWindowHtml('<b>' + caption + '</b><br/>\n' + comment, { maxWidth:400 });
    }
    
    //Accessors
    this.hilight = hilight;
    this.startRecording = startRecording;
    this.stopRecording = stopRecording;
    this.isRecording = function(){(!recordRef)};
    this.getPoint = function(){return marker.getPoint();}
    this.setPoint = setPoint;
    this.getPointId = function(){return id;};
    this.setComment = setComment;
}
