jQuery(document).ready( function() {
    var map = null;
    var geocoder = null;
    var markers = [];
    loadMap();
});

function loadMap() {

    if (GBrowserIsCompatible()) {
        map = new GMap2(document.getElementById("location_map"));
        
        // vars for center coordinates will be output via MODx snippet, according to location
        map.setCenter(new GLatLng(centerLat,centerLong), 12);
        var topLeft = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10,10));
        map.addControl(new GSmallMapControl(), topLeft);
    }

    
    jQuery("#map_filters input").click( function() {
        jQuery(this).change();
    });
    
    jQuery("#map_filters input").change( function() {
        
        map.clearOverlays();
        markers = [];
        jQuery("#map_filters input").each( function() {
            if(jQuery(this).attr("checked")) {
                addLocationSet(jQuery(this).attr("id"));
            }
        });
        
        // reset the center and zoom level to fit selected markers
        var coords = [];
        jQuery.each(markers, function(i,marker) {
            coords.push(marker.getLatLng());
        });
        
        var latlngbounds = new GLatLngBounds();
        for (var i = 0; i < coords.length; i++) {
           latlngbounds.extend(coords[i]);
        }
        if (coords.length) // otherwise we go to china
            map.setCenter( latlngbounds.getCenter(), map.getBoundsZoomLevel(latlngbounds));       
    });
    
    jQuery("#bakeries").attr("checked",true);
    jQuery("#markets").attr("checked","");
    jQuery("#restaurants").attr("checked","");
    jQuery("#grocery").attr("checked","");
    jQuery("#ingredients").attr("checked","");
    jQuery("#bakeries").change();
}

function addLocationSet(locationsType) {
    var locationsArray;
    switch(locationsType) {
        case "bakeries":
            locationsArray = bakeryLocations;
            break;
        case "markets":
            locationsArray = marketLocations;
            break;
        case "restaurants":
            locationsArray = restaurantLocations;    
            break;
        case "grocery":
            locationsArray = groceryLocations; 
            break;
        case "ingredients":
            locationsArray = ingredientLocations; 
            break;
    }
    
    for (var i = 0; i < locationsArray.length; i++) {
        addLocationMarker(locationsArray[i],locationsType);
    }
}

function showCallout(location,marker,type) {
    var locationHTML;
    var address = location.address1 + ', ' + location.address2;
    
    switch(type) {
        case "bakeries":
            locationHTML = ['<div class="mapCallout"><h3>Grand Central Bakery</h3><strong>', location.title, '</strong><br />',
                           location.address1, '<br>', location.address2].join('');
            break;
        case "markets":
            locationHTML = ['<div class="mapCallout"><h3>', location.title, '</h3>',
                           location.address1, '<br />', location.address2, '<br />',
                           '<a href="', location.url, '" target="_blank">', location.url, '</a>'].join('');
            break;
        case "restaurants":
            locationHTML = ['<div class="mapCallout"><h3>', location.title, '</h3>',
                            location.address1, '<br />', location.address2, '<br />',
                            '<a href="', location.url, '" target="_blank">', location.url, '</a>'].join('');          
            break;
        case "grocery":
            locationHTML = ['<div class="mapCallout"><h3>', location.title, '</h3>',
                            location.address1, '<br />', location.address2, '<br />',
                            '<a href="', location.url, '" target="_blank">', location.url, '</a>'].join('');
            break;
        case "ingredients":
            locationHTML = ['<div class="mapCallout"><h3>', location.title, '</h3>',
                            location.address1, '<br />', location.address2, '<br />',
                            '<a href="', location.url, '" target="_blank">', location.url, '</a>'].join('');
            break;            
    }

                 
    locationHTML += ['<br />','Get directions: ', '<a target="_blank" ', 'href="http://maps.google.com/maps?saddr=&daddr=',
                 address, '">to here</a> - ', '<a target="_blank" ', 'href="http://maps.google.com/maps?saddr=',
                 address, '&daddr=', '"> from here <br /></div>'].join('');

    marker.openInfoWindowHtml(locationHTML);
}

function addLocationMarker(location,type) {
    var marker = new GMarker(new GLatLng(location.lat, location.long));
    markers.push(marker)
    GEvent.addListener(marker, 'click', function() {
        showCallout(location,marker,type)
    });
    
    map.addOverlay(marker);
}

