var map;
var geocoder;


function gMapsCodeAddress(address_par, location_coords)
{
//    address_par = address_par.replace(/<\/?[^>]+>/gi, '')
    
    var address = 'Polska, '+address_par;
    if (geocoder) {
        geocoder.geocode( { 'address': address}, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {

                var coords_str = results[0].geometry.location.toString();
                coords_str = coords_str.replace('(', '').replace(')', '');

                location_coords.html( coords_str );

                var marker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
                map.setCenter(results[0].geometry.location);
            } else {
                alert("Geocode was not successful for the following reason: " + status);
            }
        });
    }
}


function gMapsHide()
{
    $('#gmaps-popup').hide();
    $('.gmaps-button').removeClass('gmaps-button-hover');
}

$(document).ready(function () {
    gMapsInitialize();

    // gmaps buttons
    $('.gmaps-button').mouseover(function () {
        $('.gmaps-button').removeClass('gmaps-button-hover');
        $(this).addClass('gmaps-button-hover');
        $('#gmaps-popup').show();
        $('#gmaps-popup').css('top', '0px');
        gMapsHover(parseInt($(this).attr('lokalizacja')));
    });

    $('#gmaps-area').mouseout(function () { gMapsHide(); });
    $('#gmaps-area1').mouseover(function () { gMapsHide(); });
    $('#gmaps-area2').mouseover(function () { gMapsHide(); });
});

function gMapsInitialize()
{
    var latlng = new google.maps.LatLng(52.025459,18.204102);
    var myOptions = { center: latlng, zoom: 15, scrollwheel: false, mapTypeId: google.maps.MapTypeId.ROADMAP, mapTypeControl: true, mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU}, navigationControl: true, navigationControlOptions: {style: google.maps.NavigationControlStyle.SMALL} }

    geocoder = new google.maps.Geocoder();
    map = new google.maps.Map(document.getElementById('map'), myOptions);

    for (var i=1; i<=4; i++)
        gMapsSetMarker(i);
}

function gMapsSetMarker(lokalizacja)
{
    var location_coords = $("#lokalizacja-"+lokalizacja).find('> span:nth-child(1)');
    var location_address = $("#lokalizacja-"+lokalizacja).find('> span:nth-child(2)').html();

    gMapsCodeAddress(location_address, location_coords);
}

function gMapsHover(lokalizacja)
{
    var location_coords = $("#lokalizacja-"+lokalizacja).find('> span:nth-child(1)').html();
    var location_address = $("#lokalizacja-"+lokalizacja).find('> span:nth-child(2)').html();
    var location;

    if (location_coords=='?') {
        location = new google.maps.LatLng(52.025459,18.204102);
    } else {
        var location_arr = location_coords.split(',');
        location = new google.maps.LatLng(parseFloat(location_arr[0]), parseFloat(location_arr[1]));
    }

    map.setCenter(location);
    $("#map-address").html(location_address);
}
