var eventMap = {
    map: null,
    markers : {},
    infoWindows : {},
    details : false
}

$(function(){
    var latlng = new google.maps.LatLng(50.14940257525264, 10.480425781250009);
    var myOptions = {
      zoom: 5,
      center: latlng,
      mapTypeId: google.maps.MapTypeId.TERRAIN
    };
    eventMap.map = new google.maps.Map(document.getElementById("event_map"), myOptions);
   
    $.getJSON($('#event_map').data('api'), function(events){
        geocoder = new google.maps.Geocoder();
        $.each(events, function(index, value){
            options = {'address' : value.fields.address, 'region' : value.fields.country}
            geocoder.geocode(options, function(results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var icon;
                    if (index > 0) {
                        icon = $('#event_map').data('icon-upcoming');
                    } else {
                        icon = $('#event_map').data('icon-next');
                    }
                    
                    eventMap.markers[value.pk] = new google.maps.Marker({
                        map: eventMap.map,
                        position: results[0].geometry.location,
                        icon: icon
                    });

                    if ($('.event-' + value.pk).length) {
                        eventMap.details = true
                        eventMap.infoWindows[value.pk] = new google.maps.InfoWindow({
                            content:
                                '<div class="event-dialog" style="display: block;">' +
                                    //$('.event-' + value.pk).find('.span-3 a').html() +
                                    //$('.event-' + value.pk).find('.span-9').html() +
                                    $('.event-' + value.pk).html() + 
                                '</div>'
                            //disableAutoPan: true
                        })
                    }

                    google.maps.event.addListener(eventMap.markers[value.pk], 'mouseover', function() {
                        $('.event-' + value.pk).stop(true, true).addClass('active', 100);
                        eventMap.markers[value.pk].setShadow($('#event_map').data('shadow'));
                        eventMap.infoWindows[value.pk].open(eventMap.map, eventMap.markers[value.pk])
                    });
                    google.maps.event.addListener(eventMap.markers[value.pk], 'mouseout', function() {
                        $('.event-' + value.pk).stop(true, true).removeClass('active', 500);
                        eventMap.markers[value.pk].setShadow(null);
                        eventMap.infoWindows[value.pk].close()
                    });
                    google.maps.event.addListener(eventMap.markers[value.pk], 'click', function() {
                        url = $('.event-' + value.pk).find('.event-link').attr('href');
                        
                        if (url) {
                            window.location.href = $('.event-' + value.pk).find('.event-link').attr('href')
                        } else {
                            window.location.href = '/de/veranstaltungen/'
                        }
                    });
                } else {
                    log("Geocode was not successful for the following reason: " + status)
                }
           });
           
       });
    });
    
    $('.event-entry').hover(function(event){
        target = $(event.currentTarget);
        target.stop(true, true).addClass('active', 100);
        eventMap.markers[target.data('id')].setShadow($('#event_map').data('shadow'));
    }, function(event){
        target = $(event.currentTarget);
        target.stop(true, true).removeClass('active', 500);
        eventMap.markers[target.data('id')].setShadow(null);
    });
});
