/**
 * jQuery script to turn the map canvas div into a google map
 */

//console.log("google_map.js");

$(document).ready( function(){
    document.lat = $('#map_container').attr('latitude');
    document.lng = $('#map_container').attr('longitude');
    document.locationName = $('#map_container').attr('location_name');
    $('#map_canvas').each( init_map );
    $('#large_map_link').click( large_map );
});

/** Initializes the map */
function init_map( canvas_id ){
    var canvas_id = canvas_id || 'map_canvas';
    
    // get lat and long from attributes on the map canvas
    var latlng = new google.maps.LatLng(document.lat, document.lng);
    var options = {
        zoom: 15,
        center: latlng,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };
    var map = new google.maps.Map( document.getElementById(canvas_id), options );
    var marker = new google.maps.Marker({
      position: latlng, 
      map: map, 
      title: document.locationName
    });
}

/** replace main content area with a larger view of the map */
function large_map( evt ){
    // replace the contact form area with a map element
    var map_html = "<div id='map_container_large'>" +
                   "  <div id='map_canvas_large'></div>" +
                   "  <div><a id='close_large_map' href=''>Close</a></div>" +
                   "</div>";
   
    document.htmlBuf = $('#location_view_main').html();
    $('#location_view_main').html(map_html);
    $('#map_container_small').hide();
    // call init map on our large element
    init_map('map_canvas_large');

    $('#close_large_map').click(closeLargeMap);
    // cancel the link action
    evt.preventDefault();
    return false;
}

function closeLargeMap(evt){
    $('#location_view_main').html(document.htmlBuf);
    $('#large_map_link').click( large_map );
    evt.preventDefault();
    return false;
}

