var animatedIcon = new GIcon();
animatedIcon.image = "./images/maps/truckAni.gif";
animatedIcon.iconSize = new GSize(20, 20);
animatedIcon.iconAnchor = new GPoint(10, 10);
animatedIcon.infoWindowAnchor = new GPoint(5, 1);


function resetMap(){
    showPoints(true);
}

function showHidePolyline(){
    redrawPolylines();
}

function getLastTimeStamp(assetId){
    retVal = 0;    
    userObject = getUserObject(assetId);
    if(userObject && userObject.getNumPoints() > 0){
        retVal = userObject.getTimestampAt(userObject.getNumPoints() - 1);
    }
    
    return retVal;
}

function getFirstTimeStamp(assetId){
    retVal = null;
    userObject = getUserObject(assetId);
    if(userObject && userObject.getNumPoints() > 0){
        retVal = userObject.getTimestampAt(0);
    }    
    
    return retVal;
}

function removeFirstPoint(assetId){
    userObject = getUserObject(assetId);
    if(map){
        map.removeOverlay(userObject.getMarkerAt(0));
    }
    userObject.removePointAt(0);   
}

/////////////////////////////////////////////////////////////////////
// PRIVATE METHODS
/////////////////////////////////////////////////////////////////////


function redrawPolylines(){
    var userObject;
    for(i = 0; i < userCollection.getSize(); i++){
        userObject = userCollection.getElementAt(i);
        var points = userObject.getPointCollection();
        var polylines = userObject.getPolylineCollection();

        for(j = 0; j < polylines.getSize(); j++){
            map.removeOverlay(polylines.getElementAt(j));
        }
        var showPolyline = document.getElementById("showLine").checked;
        if(showPolyline){
          var polyline = drawPolyline(points);
          polylines.addElement(polyline);
          map.addOverlay(polyline);
        }
    }
}

function showPoints(resizeMap) {
    var visiblePoints = new Vector();    
    var userObject;
    
    if(resizeMap){
        for(i = 0; i < userCollection.getSize(); i++){        
            userObject = userCollection.getElementAt(i);
            points = userObject.getPointCollection();
            visiblePoints.insertVector(points);            
        }
        
        encompassPoints(visiblePoints);
    }
}

function getBounds(points){
    var bounds = new GLatLngBounds(DEFAULT_LOCATION, DEFAULT_LOCATION);
    if(points && points.getSize() > 0){
        //define LatLngBound
        var temp = points.getElementAt(0);
        bounds = new GLatLngBounds(temp, temp);

        for(i = 0; i < points.getSize(); i++){
            temp = points.getElementAt(i);
            bounds.extend(temp);
        }
    }
    return bounds;
}

function redraw(evtObj){
    var resizeMap = document.getElementById("autoTrack").checked;
    showPoints(resizeMap);
}

function getMap() {
    if (GBrowserIsCompatible()) {
        if(document.getElementById("map")){
            map = new GMap2(document.getElementById("map"));
            map.setCenter(DEFAULT_LOCATION);
            map.setZoom(4);

            map.addControl(new GMapTypeControl());
            map.addControl(new GLargeMapControl());
            map.addControl(new GScaleControl());
            map.enableDoubleClickZoom();
            map.enableContinuousZoom();
            map.setMapType(G_HYBRID_MAP);
            map.addMapType(G_PHYSICAL_MAP);
            map.enableScrollWheelZoom();
        }
    }
}