/*
    +++++ Google Map Control Class ++++++
*/
function GoogleMapControl(id, init_lat, init_lng) {
    this._map = new GMap2($(id));
    this._map.addControl(new GLargeMapControl());
    this._map.addControl(new GOverviewMapControl());
    this._map.addControl(new GMapTypeControl());
    this._map.setCenter(new GLatLng(init_lat, init_lng), 15);

    this.iconSetup();
}

GoogleMapControl.prototype.iconSetup = function() {
    this.baseIcon = new GIcon();
    this.baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
    this.baseIcon.iconSize = new GSize(20, 34);
    this.baseIcon.shadowSize = new GSize(37, 34);
    this.baseIcon.iconAnchor = new GPoint(9, 34);
    this.baseIcon.infoWindowAnchor = new GPoint(9, 2);
    this.baseIcon.infoShadowAnchor = new GPoint(18, 25);

    this.naviIcon = new GIcon();
    this.naviIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
    this.naviIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
    this.naviIcon.iconSize = new GSize(12, 20);
    this.naviIcon.shadowSize = new GSize(22, 20);
    this.naviIcon.iconAnchor = new GPoint(6, 20);
    this.naviIcon.infoWindowAnchor = new GPoint(5, 1);
}

GoogleMapControl.prototype.showRoute = function(gps) {
    var points = [];
    for(var i=0; i<gps.length; i++) {
        points.push(new GLatLng(gps[i]['lat'], gps[i]['lng']));
        this.addGPSPoint(gps[i], this.naviIcon);
    }
    this._map.addOverlay(new GPolyline(points));
}

GoogleMapControl.prototype.showUsers = function(users) {
    for(var i=0; i<users.length; i++) {
        var letter = String.fromCharCode("A".charCodeAt(0) + i);
        var icon = new GIcon(this.baseIcon);
        icon.image = "http://www.google.com/mapfiles/marker" + letter + ".png";
        objMap.addUserPoint(users[i], icon);
    }
}

GoogleMapControl.prototype.addUserPoint = function(user, icon) {
    var point = new GLatLng(user['lat'], user['lng']);
    marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
        getUserGPS(user['mob_id']);
    });
    GEvent.addListener(marker, "mouseover", function() {
        $('gpsinfo').innerHTML = user['mob_id'] + '<br>' + user['time'];
    });
    GEvent.addListener(marker, "mouseout", function() {
        $('gpsinfo').innerHTML = "";
    });
    this._map.addOverlay(marker);
}

GoogleMapControl.prototype.addGPSPoint = function(gps, icon) {
    var point = new GLatLng(gps['lat'], gps['lng']);
    marker = new GMarker(point, icon);
    GEvent.addListener(marker, "click", function() {
    });
    GEvent.addListener(marker, "mouseover", function() {
        $('gpsinfo').innerHTML = gps['mob_id'] + '<br>' + gps['time'];
    });
    GEvent.addListener(marker, "mouseout", function() {
        $('gpsinfo').innerHTML = "";
    });
    this._map.addOverlay(marker);
}

GoogleMapControl.prototype.panTo = function(lat, lng) {
    var point = new GLatLng(lat, lng); 
    this._map.panTo(point);
}

GoogleMapControl.prototype.clearOverlays = function() {
    this._map.clearOverlays();
}

/*
    +++++ Route Control Class ++++++
*/
function RouteControl(gps) {
    this.route = gps;
    this.pos = 0;
    this.maxpos = gps.length;
    this.setPosition();

    $('routeControl').style.visibility = "visible";
}
RouteControl.prototype.setPosition = function() {
    objMap.panTo(this.route[this.pos]['lat'], this.route[this.pos]['lng']);
    $('gpsinfo').innerHTML = this.route[this.pos]['mob_id'] + '<br>' + this.route[this.pos]['time'];
}

RouteControl.prototype.next = function() {
    if(this.pos + 1 == this.maxpos) {
        return;
    }
    this.pos++;
    this.setPosition();
}
RouteControl.prototype.prev = function() {
    if(this.pos == 0) {
        return;
    }
    this.pos--;
    this.setPosition();
}

RouteControl.prototype.clear = function() {
    $('routeControl').style.visibility = "hidden";
    getRecentUsers();
}

/*
    Global Val
*/
var objMap;
var objRoute;
/*
    ::::: initialize :::::
*/

function load() {
    var init_lat = 35.65943;
    var init_lng = 139.74139;

    objMap = new GoogleMapControl('map', init_lat, init_lng);

    getRecentUsers();
    getUserGPS('00011122233344_ed.ezweb.ne.jp');
}

/* 
    ::::: get & disp all entry users :::::
*/

function getRecentUsers() {
    var ajax = new Ajax.Request(
        './api_gps.php',
        {method: 'get', parameters: 'time='+getUtime(), onSuccess: dispRecentUsers}
        );
}

function dispRecentUsers(oj) {
    objMap.clearOverlays();
    eval('var users = ' + oj.responseText);
    objMap.showUsers(users);
}

/*
    ::::: get GPS ROUTE par user :::::
*/

function getUserGPS(mob_id) {
    var ajax = new Ajax.Request(
        './api_gps.php',
        {method: 'get', parameters: 'mob_id='+mob_id+'&time='+getUtime(), onSuccess: dispUserGPS}
        );
}

function dispUserGPS(oj) {
    eval('var gps = ' + oj.responseText);
    objMap.showRoute(gps);
    objRoute = new RouteControl(gps);
}

/*
    ::::: misc :::::
*/

function showInfo() {
    $('info').style.visibility = "visible";
}
function hideInfo() {
    $('info').style.visibility = "hidden";
}

function getUtime() {
    var dd = new Date();
    return dd.getTime();
}

