var mapbig = null;
var geo = null;

function mapitem (lat, longitude, name, address, dirname, logo, description, reserveUrl, websiteUrl, ipixUrl)
{
    this.lat = lat;
    this.longitude = longitude;
    this.name = name;
    this.address = address;
    this.dirname = dirname;
    this.logo = logo;
    this.description = description;
    this.reserveUrl = reserveUrl;
    this.websiteUrl = websiteUrl;
    this.ipixUrl = ipixUrl;
}

function loadRestaurantMap(dirname, lat, lng)
{

    var point = new google.maps.LatLng(lat, lng);
    var map_opts = {
        zoom: 15,
        center: point,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    map = new google.maps.Map(document.getElementById("mapbig"), map_opts);

    var marker_opts =
    {
        position: point,
        map: map
    }
    var marker = new google.maps.Marker(marker_opts);
}

function showGoogleMaps(daddr, saddr) {
    url =
        'http://maps.google.com/maps?' +
    'f=d&source=s_d&saddr=' + saddr + '&daddr=' + daddr;


    window.open(url);
}

function searchAddress(address, p2) {

    var points = null;

    
    //    alert(p2);
    geo = new GClientGeocoder();
    geo.getLatLng(address, function(point)
    {
        if (!point)
        {
            // if point not exist
            alert(address + " not found!");
        }
        else
        {
            // center point
            mapbig.setCenter(point, 15);

            // add marker
            var icon = new GIcon();
            icon.image='http://maps.google.com/mapfiles/kml/pal3/icon52.png';
            icon.shadow='';
            icon.iconSize=new GSize(32,32);
            icon.iconAnchor=new GPoint(16,16);
            icon.infoWindowAnchor=new GPoint(16,16);

            var marker = new GMarker(point,{
                icon: icon,
                title: address
            });
            mapbig.addOverlay(marker);

            // route
            points= [point, p2];
            polilinia = new GPolyline(points,'#ff0000',4,0.7);
            mapbig.addOverlay(polilinia);

            directions = new GDirections(mapbig, null);
            GEvent.addListener(directions, "load",onGDirectionsLoad);
            directions.loadFromWaypoints(points);

            // open popup window with address
            marker.openInfoWindowHtml('<strong>Address:</strong><br />'+ address);
        }
    });
}

function onGDirectionsLoad() {
    alert("Distance: " + directions.getDistance().meters/1000 + ' [km]');
}
