﻿
var map = null;
var geocoder = null;


//function Initialize() {
//    alert('Initialize');
//    if (GBrowserIsCompatible()) {
        //geocoder = new GClientGeocoder();

//        map = new GMap2(document.getElementById("map_canvas"));
//        map.addControl(new GSmallMapControl());
//        map.addControl(new GMapTypeControl());
//    }

//    var latLng = new google.maps.LatLng(62, 15);
//    map.setCenter(latLng);
//    map.setZoom(4);
//}



var _info;
var found = false;
function ShowMap(address, zipcode, city, country, info) {
    _info = info;
    //$("#divMap").show();
    //$("#divMap").centerInClient();
    map = new GMap2(document.getElementById("map_canvas"));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());

    AddPoint(address, zipcode, city, country, info);
}

// Used for several points
function AddPointSimple(address, zipcode, city, country, info) {
    geocoder.getLatLng(address + ", " + zipcode + " " + city + ", " + country,
function (point) {
    if (!point)
        found = false;
    else {
        //map.clearOverlays()
        //map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        //marker.openInfoWindowHtml(info);
        marker.bindInfoWindow(info);
        found = true;
    }
}
);

    if (!found) {
        geocoder.getLatLng(zipcode + " " + city + ", " + country,
function (point) {
    if (!point)
        found = false;
    else {
        //map.clearOverlays()
        //map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.bindInfoWindow(info);
        //marker.openInfoWindowHtml(info);
        found = true;
    }
}
);

    }

    if (!found) {
        geocoder.getLatLng(zipcode + country,
function (point) {
    if (!point)
        found = false;
    else {
        //map.clearOverlays()
        //map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.bindInfoWindow(info);
        //marker.openInfoWindowHtml(info);
        found = true;
    }
}
);
    }

}


function AddPoint(address, zipcode, city, country, info) {
    geocoder.getLatLng(address + ", " + zipcode + " " + city + ", " + country,
function (point) {
    if (!point)
        found = false;
    else {
        map.clearOverlays()
        map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(info);
        found = true;
    }
}
);

    if (!found) {
        geocoder.getLatLng(zipcode + " " + city + ", " + country,
function (point) {
    if (!point)
        found = false;
    else {
        map.clearOverlays()
        map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(info);
        found = true;
    }
}
);

    }

    if (!found) {
        geocoder.getLatLng(zipcode + country,
function (point) {
    if (!point)
        found = false;
    else {
        map.clearOverlays()
        map.setCenter(point, 14);
        var marker = new GMarker(point);
        map.addOverlay(marker);
        marker.openInfoWindowHtml(info);
        found = true;
    }
}
);
    }

}
    


function ShowPosition(point) {
    if (!point)
        return false;
    map.clearOverlays();
    map.setCenter(point, 13);
    var marker = new GMarker(point);
    map.addOverlay(marker);
    marker.openInfoWindowHtml(_info);

}




$.fn.centerInClient = function (options) {
    /// <summary>Centers the selected items in the browser window. Takes into account scroll position.
    /// Ideally the selected set should only match a single element.
    /// </summary>    
    /// <param name="fn" type="Function">Optional function called when centering is complete. Passed DOM element as parameter</param>    
    /// <param name="forceAbsolute" type="Boolean">if true forces the element to be removed from the document flow 
    ///  and attached to the body element to ensure proper absolute positioning. 
    /// Be aware that this may cause ID hierachy for CSS styles to be affected.
    /// </param>
    /// <returns type="jQuery" />
    var opt = { forceAbsolute: false,
        container: window,    // selector of element to center in
        completeHandler: null
    };
    $.extend(opt, options);

    return this.each(function (i) {
        var el = $(this);
        var jWin = $(opt.container);
        var isWin = opt.container == window;

        // force to the top of document to ENSURE that 
        // document absolute positioning is available
        if (opt.forceAbsolute) {
            if (isWin)
                el.remove().appendTo("body");
            else
                el.remove().appendTo(jWin.get(0));
        }

        // have to make absolute
        el.css("position", "absolute");

        // height is off a bit so fudge it
        var heightFudge = isWin ? 2.0 : 1.8;

        var x = (isWin ? jWin.width() : jWin.outerWidth()) / 2 - el.outerWidth() / 2;
        var y = (isWin ? jWin.height() : jWin.outerHeight()) / heightFudge - el.outerHeight() / 2;
        if (y < 0) y = 5;
        var newTop = y + jWin.scrollTop();
        if (newTop < 20)
            newTop = 20;
        el.css("left", x + jWin.scrollLeft());
        el.css("top", newTop);

        // if specified make callback and pass element
        if (opt.completeHandler)
            opt.completeHandler(this);
    });
}
