﻿function ShowMarker(latLng, itemId) {

    var marker = new google.maps.Marker({
        position: latLng,
        map: map
    });

    google.maps.event.addListener(marker, 'click', function () {
        var infowindow = new google.maps.InfoWindow({
            content: $j(".CompanyItem[RelatedId~=" + itemId + "] .Bubble").html()
        });
        infowindow.open(map, marker);

        //window.setTimeout(function () { map.setCenter(latLng); }, 1000);
    });
}

var bounds = null;
function AddMarker(address, zip, city, country, companyName, itemId) {
    if (!bounds) {
        bounds = new google.maps.LatLngBounds();
    }

    var addressToSearch = "";

    if (address)
        addressToSearch += address + ", "

    if (zip)
        addressToSearch += zip + " "

    if (city)
        addressToSearch += city + ", "

    if (country)
        addressToSearch += country

    if (addressToSearch != "") {
        coder.geocode({ address: address }, function (georesult, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latlng = new google.maps.LatLng(georesult[0].geometry.location.lat(), georesult[0].geometry.location.lng());

                bounds.extend(latlng);
                map.setZoom(map.fitBounds(bounds));
                map.setCenter(bounds.getCenter());

                ShowMarker(latlng, itemId);
            }
        });
    }
}

var map;
var coder;
$j(document).ready(function () {
    if ($j("#map_canvas.memberArea").length > 0) {
        coder = new google.maps.Geocoder();

        coder.geocode({ address: 'Germany' }, function (georesult, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var latlng = new google.maps.LatLng(georesult[0].geometry.location.lat(), georesult[0].geometry.location.lng());
                var myOptions = {
                    zoom: 13,
                    center: latlng,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };
                map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
                $j(".TabContent").css("visibility", "hidden");
                $j(".TabContent").css("display", "block");
                //google.maps.event.trigger(map, 'resize');
                window.setTimeout(function () {
                    $j(".TabContent").css("visibility", "visible");
                    $j(".TabContent").css("display", "none");
                    $j($j(".TabContent")[0]).css("display", "block");
                }, 500);

            }
            else {
                $j('#map_canvas').css('display', 'none');
            }
        });
    }
});
