var normalIcon = new GIcon();
normalIcon.image = "./images/maps/dotIconSmallGreen.png";
normalIcon.iconSize = new GSize(10, 10);
normalIcon.iconAnchor = new GPoint(5, 5);
normalIcon.infoWindowAnchor = new GPoint(5, 1);

var idleIcon = new GIcon();
idleIcon.image = "./images/maps/dotIconSmallYellow.png";
idleIcon.iconSize = new GSize(18, 18);
idleIcon.iconAnchor = new GPoint(5, 5);
idleIcon.infoWindowAnchor = new GPoint(5, 1);

var ignitionOn = new GIcon();
ignitionOn.image = "./images/maps/ignitionOn.png";
ignitionOn.iconSize = new GSize(18, 18);
ignitionOn.iconAnchor = new GPoint(5, 5);
ignitionOn.infoWindowAnchor = new GPoint(5, 1);

//END STATIC BLOCK

/////////////////////////////////////////////////////////////////////
// VARIABLES
/////////////////////////////////////////////////////////////////////
var DATEFORMAT = "MMM d h:mm:ss a";

/////////////////////////////////////////////////////////////////////
// VARIABLES
/////////////////////////////////////////////////////////////////////
var userCollection = new Vector();
var map = null;

/////////////////////////////////////////////////////////////////////
// PUBLIC METHODS
/////////////////////////////////////////////////////////////////////
function getUserObject(assetId){
    userObject = userCollection.getElementById(assetId);    
    return userObject;
}

function addPointArgs(params){
    var id = params.id;
    var assetId = params.assetId;
    var lat = params.lat;
    var lng = params.lng;
    var timestamp = params.timestamp;
    var assetName = params.assetName;
    var animated = params.animated;
    var speed = params.speed;
    var type = params.type;
    var duration = params.duration;
    
    var tempPoint = new GLatLng(lat, lng);    
    var marker;
    
    var userObject = userCollection.getElementById(assetId);
    if(!userObject){
        userObject = new UserObject(assetId);
        userCollection.addElement(userObject);
    }

    var icon = normalIcon
    if(type == 'idle'){
        icon = idleIcon;
    }else if(type == 'ignitionOn'){
        icon = ignitionOn;
    }
    
    marker = createMarker({id: id, point: tempPoint,  icon: icon, timestamp: timestamp, assetName: assetName, speed: speed, type: type, duration: duration});
    userObject.insertPoint(tempPoint, timestamp, marker, null, null);
    map.addOverlay(marker);
    //remove the animated marker
    currentAnimatedMarker = userObject.animatedMarker;
    if(currentAnimatedMarker){
        map.removeOverlay(currentAnimatedMarker);
        //replace the animated marker with a non animated one
        //map.addOverlay(userObject.getMarkerAt(userObject.getNumPoints() -1 ));
    }
    userObject.animatedMarker = null;
    if(animated){
        marker = createMarker({id: id, point: tempPoint, icon: animatedIcon, timestamp: timestamp, assetName: assetName, speed: speed, type: type, duration: duration});
        userObject.animatedMarker = marker;
        map.addOverlay(marker);
    }
    
}
    
function createMarkerHTML(params){
    var id = params.id;
    var point = params.point;
    var timestamp = params.timestamp;
    var assetName = params.assetName;
    var speed = params.speed;
    var full = params.full;
    var type = params.type;
    var duration = params.duration;
    
    var html = "<font size='1'><b>" + assetName +  "</b><br/>";
    var dtObj = new Date(timestamp);
    
    if(type == 'ignitionOn'){
        html += '<b>Ignition On</b><br/>';
    }
    
    html += formatDate(dtObj, DATEFORMAT);
    
    if(type == 'idle'){
        html += "<br/><b>Idle Duration: </b>" + formatSeconds(duration);
    }else if(speed){
        html += "<br/><b>Speed: </b>" + speed + " km/hr";
    }   
    
    if(full){
        html += "<br/><b>Lat:</b> " + point.lat() + "<br/><b>Long:</b> " + point.lng();
    }
    html += "</font>";
    
    return html;
}

function locateZIndexProcess(){
    return 1;
}

function idleZIndexProcess(){
    return 2;
}

function ignitionOnZIndexProcess(){
    return 3;
}

var infoWindowPersistentClick = false;
var infoWindowMarker = null;
function createMarker(params){
    var id = params.id;
    var point = params.point;
    var icon = params.icon;
    var timestamp = params.timestamp;
    var assetName = params.assetName;
    var speed = params.speed;
    var type = params.type;
    var duration = params.duration;
    
    var ico = new GIcon(icon);
    var zIndex;

    /* Return the z-index function because that's what google uses,
       You cannot substitute a constant.
     */
    if(type == 'location'){
        zIndex = locateZIndexProcess;
    }else if(type == 'idle'){
        zIndex = idleZIndexProcess;
    }else if(type == 'ignitionOn'){
        zIndex = ignitionOnZIndexProcess;
    }

    var marker = new GMarker(point, {icon: ico, title : assetName, zIndexProcess : zIndex});
    var html = createMarkerHTML({id: id, point: point, timestamp: timestamp, assetName: assetName, speed: speed, full: false, type: type, duration: duration});
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
        infoWindowMarker = marker;
        infoWindowPersistentClick = true;
    });

    return marker;
}

function drawPolyline(points){
    arr = points.toArray();
    var polyline = new GPolyline(arr);
    return polyline;
}

function encompassPoints(points){
    var bounds = getBounds(points);
    var zoomLevel = map.getBoundsZoomLevel(bounds);
    if(zoomLevel > 17){
        zoomLevel = 17;
    }
    
    if(points.size == 0){
      zoomLevel = 4;
    }
    
    map.setZoom(zoomLevel);
    map.panTo(bounds.getCenter());
}

function clearAllPoints(){
    var i;
    for(i = 0; i < userCollection.getSize(); i++){
        userCollection.getElementAt(i).removeAllPoints(map);
    }
    map.clearOverlays();
}

function killAsset(assetId){
    var userObject = userCollection.getElementById(assetId);
    if(userObject){
      userObject.removeAllPoints(map);
      userCollection.removeElementById(assetId);
    }
}