/*
        - search_pois({ address: $address, filter: $filter })
        - update_pois()
*/
var mapobj = null;
google.load("maps", "2");
google.setOnLoadCallback(initialize);

var privMarkerOptions = {};
var pubMarkerOptions = {};

function initialize() {
    if (GBrowserIsCompatible()) {
        mapobj = new google.maps.Map2(document.getElementById("map"), {draggableCursor: 'cell'});
        if (google.loader.ClientLocation){
            var center = new google.maps.LatLng(
                google.loader.ClientLocation.latitude,
                google.loader.ClientLocation.longitude
            );
            mapobj.setCenter(center, 13);
        } else {
            mapobj.setCenter(new GLatLng(-10, 139), 2);
        }
        mapobj.setMapType(G_NORMAL_MAP);
        //mapobj.setMapType(G_SATELLITE_MAP);
        mapobj.setUIToDefault();

        // Create private marker icon
        var privMarkerIcon = new GIcon();
        privMarkerIcon.image = "/images/maps_privPOI.png";
        //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        privMarkerIcon.iconSize = new GSize(40, 50);
        //tinyIcon.shadowSize = new GSize(22, 20);
        privMarkerIcon.iconAnchor = new GPoint(4, 25);
        privMarkerIcon.infoWindowAnchor = new GPoint(20, 25);
        privMarkerOptions = { icon:privMarkerIcon };

        // Create public marker icon
        var pubMarkerIcon = new GIcon();
        pubMarkerIcon.image = "/images/maps_pubPOI.png";
        //tinyIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
        pubMarkerIcon.iconSize = new GSize(40, 50);
        //tinyIcon.shadowSize = new GSize(22, 20);
        pubMarkerIcon.iconAnchor = new GPoint(4, 25);
        pubMarkerIcon.infoWindowAnchor = new GPoint(20, 25);
        pubMarkerOptions = { icon:pubMarkerIcon };

        GEvent.addListener(mapobj, "moveend", function() {
            var center = mapobj.getCenter();
            //alert("LAT:"+center.lat()+" - LON:"+center.lng());
            var bndbox = mapobj.getBounds();
            var distance = bndbox.getSouthWest().distanceFrom(bndbox.getNorthEast())/2; 
            //alert(Math.round(distance));
            var new_io = {
                "lat" : center.lat(),
                "lon" : center.lng(),
                "distance" : distance
            };
            list_nearby(new_io);
        });
    }
}

function search_keydown(fe, e) {
    if(window.event) { // IE
        keynum = e.keyCode;
    } else if(e.which) { // Netscape/Firefox/Opera
        keynum = e.which;
    }
    if (keynum == 13) {
        search_pois({ "address" : fe.value });
    }
    return false;
}
function search_pois(io) {
    show_processing();
    var new_io              = {};
    new_io.BODY             = { "address" : io.address, "filter" : io.filter }; 
    //new_io.BODY             = { "source" : "login_form", "fields" : ["email_address", "password"] };
    return call_service(new_io, "pois", "search");
}
function search_pois_callback(resp, xhr) {
    if (resp.result()) {
        var BODY = resp.get("BODY");
        mapobj.setCenter(new GLatLng(BODY.lat, BODY.lon), 16);
    } else {
        var HEAD            = resp.get("HEAD");
        display_errors(HEAD.errors);
        hide_processing();
    }
}

function list_nearby(io) {
    //show_processing();
    var new_io = {};
    new_io.BODY = io;
    return call_service(new_io, "pois", "list_nearby");
}
function list_nearby_callback(resp, xhr) {
    if (resp.result()) {
        var BODY = resp.get("BODY");
        var lis_priv = "";
        var lis_pub = "";
        var lis_count = 0;
        for (var poi in BODY) {
            var latlng = new GLatLng(BODY[poi].lat, BODY[poi].lon);
            var markerOptions = {}; 
            if (BODY[poi].type == 2) {
                markerOptions = pubMarkerOptions;
            } else if (BODY[poi].type == 3) {
                markerOptions = privMarkerOptions;
            }
            var marker = new GMarker(latlng, markerOptions);
            if (BODY[poi].type == 2) {
                lis_pub += '<li><p><strong><a href="javascript:void(0)" onClick="poi_click('+lis_count+')">'+BODY[poi].title+'</a></strong></p></li>\n';
            } else {
                //lis_priv += '<li><p><strong><a href="javascript:void(0)" onClick="poi_click('+lis_count+')">'+BODY[poi].title+'</a></strong></p></li>\n';
            }
            marker.poi_id = lis_count;
            GEvent.addListener(marker, "click", function() {
                eval("poi_click("+this.poi_id+")");
            });
            mapobj.addOverlay(marker);
            BODY[poi].marker = marker;
            lis_count++;
        }
        buildAR.remove("pois");
        buildAR.set({ "pois" : BODY });
        if (lis_count > 0) {
            //$("#POI_list_priv").html(lis_priv);
            $("#POI_list_pub").html(lis_pub);
        } else {
            //$("#POI_list_priv").html('<li><p><strong>No Private Points of Interest found nearby</strong></p></li>');
            $("#POI_list_pub").html('<li><p><strong>No Public Points of Interest found nearby</strong></p></li>');
        }
    } else {
        var HEAD            = resp.get("HEAD");
        display_errors(HEAD.errors);
    }
    hide_modal();
}
function poi_click(id) {
    var pois = buildAR.get("pois");
    if (pois[id]) {
        var myHtml = '<div><strong>'+pois[id].title+'</strong></div>\n';
        myHtml += '<div>'+pois[id].line2+'</div>\n';
        myHtml += '<div>'+pois[id].line3+'</div>\n';
        myHtml += '<div>'+pois[id].line4+'</div>\n';
        for (links in pois[id].actions) {
            var label = "";
            var uri = "";
            var link_html = "";
            if (pois[id].actions[links].label) {
                label = pois[id].actions[links].label;
                if (pois[id].actions[links].uri) {
                    uri = pois[id].actions[links].uri;
                    link_html = '<div><a href="'+uri+'">'+label+'</a></div>\n';
                } else {
                    link_html = '<div>'+label+'</div>\n';
                }
            }
            myHtml += link_html;
        }
        pois[id].marker.openInfoWindowHtml(myHtml);
        mapobj.setCenter(new GLatLng(pois[id].lat, pois[id].lon), mapobj.getZoom());
    } else {
        alert("Invalid POI id:"+id);
    }
}
