var map = null;
var geocoder = null;
var bounds = null;
var addresses = new Array();
var addresses_str = new Array();
var directions;
var fromAddress = null;
var toAddress = null;

jQuery(document).ready(function() {
    bounds = new GLatLngBounds;
    
    initialize();
});
function initialize() {
        // Create our "tiny" marker icon
    var blueIcon = new GIcon(G_DEFAULT_ICON);
    blueIcon.image = "../images/marker-blue.png";

    // Set up our GMarkerOptions object
    markerOptions = {icon:blueIcon};

    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("map_canvas"));
        map.setCenter(new GLatLng(lat, lng), 13);
        geocoder = new GClientGeocoder();
        map.addControl(new GLargeMapControl());
        map.addControl(new GMapTypeControl());
        
        point = new GLatLng(lat, lng);
        var marker = new GMarker(point,markerOptions);
        map.addOverlay(marker);
        
        bounds.extend(point);
        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.setCenter(bounds.getCenter());
        
        GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml("You are here.");
        });
        if(addresses_str.length > 0){
            for(var i=0; i<addresses_str.length; i++ ){
                showAddressByLatLng(lats[i],lngs[i],addresses_str[i]);
            }
        }
        directions = new GDirections(map, document.getElementById("dir"));
    }
}

function showAddressByLatLng(addrLat, addrLng, address_str)
{
    if(addrLat && addrLng){
        point = new GLatLng(addrLat, addrLng);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        bounds.extend(marker.getPoint());
        GEvent.addListener(marker, "mouseover", function() {
            marker.openInfoWindowHtml(address_str);
        });
        map.setZoom(map.getBoundsZoomLevel(bounds));
        map.setCenter(bounds.getCenter());
    }
}
function showinfo(storename,short_detail,street_direction,street_number,street_name,street_type,city,state,country,zipcode,phone_number,distance){

if(phone_number != "")
    phone_number = "(ph:"+phone_number+")";

var infoDetail = "<table cellspacing=\"0\" cellpadding=\"1\" border=\"0\" bgcolor=\"#cccccc\" align=\"left\" width=\"300\" style=\"margin-top: -8px;margin-right: -7px;margin-left: -7px; margin-bottom: -8px;font-size:12px; border: 1px solid rgb(230, 230, 230);\">";
    infoDetail += "<tbody>";
    infoDetail += "<tr bgcolor=\"#005E00\">";
    infoDetail += "<td style=\"padding: 0px;\" colspan=\"2\">";
    infoDetail += "<table width=\"100%\" style=\"border: medium none ;\">";
    infoDetail += "<tbody>";
    infoDetail += "<tr>";
    infoDetail += "<td style=\"border: medium none ; padding: 0px; color:#FFF\">";
    infoDetail += "<b>"+ storename +"</b>  (Distance: " + distance +" miles)";
    infoDetail += "</td>";
    infoDetail += "</tr>";
    infoDetail += "</tbody>";
    infoDetail += "</table>";
    infoDetail += "</td>";
    infoDetail += "</tr>";
    infoDetail += "<tr>";
    infoDetail += "<td bgcolor=\"#ffffff\" rowspan=\"3\" width=23% valign=\"top\">";
    infoDetail += "<b>Address :</b>";
    infoDetail += "</td>";
    infoDetail += "<td bgcolor=\"#ffffff\" > "+ street_direction +" "+ street_number + " " +street_name +" "+street_type;
    infoDetail += "</td>";
    infoDetail += "</tr>";
    infoDetail += "<tr>";
    infoDetail += "<td bgcolor=\"#ffffff\" >"+  city +" "+ state +"</td>";
    infoDetail += "</tr>";
    infoDetail += "<tr>";
    infoDetail += "<td bgcolor=\"#ffffff\" >"+ country+" " + zipcode+" "+ phone_number+"</td>";
    infoDetail += "</tr>";
    infoDetail += "<tr>";
    infoDetail += "<td bgcolor=\"#ffffff\" colspan=\"2\"> "+short_detail+"</td>";
    infoDetail += "</tr>";
    infoDetail += "<tr>";
    infoDetail += "<td bgcolor=\"#ffffff\" colspan=\"2\"> <b><a href=\"#here\" onClick=\"JavaScript:fnShowPath('"+street_direction+", "+street_number+", "+street_name+", "+street_type+", "+city+", "+state+", "+zipcode+", "+country+"')\" id=\"direction\">Get Direction</a></td>";
    infoDetail += "</tr>";
    infoDetail += "</tbody></table>";

    return infoDetail;
}
function fnShowPath(toAddress)
{
//    alert(fromAddress +" ----> "+toAddress);
    setDirections(fromAddress,toAddress);
}
function setDirections(fromAddress, toAddress) {
    directions.load("from: " + fromAddress + " to: " + toAddress,{"locale": "en_US"});
}

