function dynamicCalendar()
{
  /*
    NOTE force browser not to cache ajax call
  */
  $.ajaxSetup({
    cache:false
  });
  
  $calendar = $('#calendarwrap');
  $calendarLinks = $('#calendar-sm .nav a');
  $calendarLinks.live('click', function(e){
    e.preventDefault();
    $cursor = $(this);
    $cursor.css('cursor','wait');
    var $fwd = $cursor.is('.next');
    var $url = $cursor.attr('href').substring(20);
    $url = '/index.php/calendar/embed/'+ $url + ' #calendar-sm';
    
    $calendar.load($url, function(data, textStatus){
      if(textStatus)
      {
        var cal_dir_in = ($fwd) ? 'right':'left';
        var cal_dir_out = ($fwd) ? 'left':'right';
        $calendar.stop().hide();
        $calendar.hide().stop().show('slow',function(){$cursor.css('cursor','pointer');});
      }
      else
      {
        return false;
      }
    });
    return false;
  });
}

$('.external,[rel="external"]').click(function(e){e.preventDefault(); 
window.open(this.href);});

function setVolume(map,locations)
{
  for (var i = 0; i < locations.length; i++)
  {
    var sound=locations[i],myLatLng = new google.maps.LatLng(sound[1],sound[2]),
    contentString = '<div class="marker_content">'+
    '<div class="title"><h2>Area</h2><p>' + sound[5] + '</p></div>'+
    '<div class="location"><h2>Info</h2><p>' + sound[4] + '</p>'+
    '<p>'+sound[6]+' decibels</p></div>'+
    '</div>'+
    '</div>',
    infowindow = new google.maps.InfoWindow({
        content: contentString,
        maxWidth: 350
    }),
    image = new google.maps.MarkerImage('http://assets.hasa.org/images/_s/map-sound.png',
      new google.maps.Size(25,29),
      new google.maps.Point(0,0),
      new google.maps.Point(9,26)
    ),
    shape = {
      coord: [0,10,9,3,18,12,8,20,10,0],
      type: 'poly'
    },
    marker = new google.maps.Marker({
      map: map,
      position: myLatLng,
      icon: image,
      shape: shape,
      title: sound[0],
      html: contentString
    }),
    circle = new google.maps.Circle({
      map: map,
      radius: 100, /* 100m */
      /*center: myLatLng,*/
      fillColor: '#e15644',
      fillOpacity: 0,
      strokeColor: '#e15644',
      strokeOpacity: (100*sound[6]/130)/100,
      strokeWeight: 30
    });
    google.maps.event.addListener(marker,'click',function(){
      infowindow.setContent(this.html);
      infowindow.open(map,this);
    });
    circle.bindTo('center',marker,'position');
  }
}

function setMarkers(map,locations)
{
  var marker_arr = [];
  function infoCallback(infowindow,marker)
  {
    return function() 
    {
      infowindow.open(map, marker);
    };
  }
  function autoCenter() {
  //  Create a new viewpoint bound
  var bounds = new google.maps.LatLngBounds();
  //  Go through each...
  $.each(marker_arr, function (index, marker) {
  bounds.extend(marker.position);
  });
  //  Fit these bounds to the map
  map.fitBounds(bounds);
  }
  for (var i = 0; i < locations.length; i++)
  {
    var sound = locations[i], myLatLng = new google.maps.LatLng(sound[1], sound[2]), image = new google.maps.MarkerImage('http://assets.hasa.org/images/_s/map_icon.png',
      new google.maps.Size(45,53),
      new google.maps.Point(0,0),
      new google.maps.Point(19,50)),shape = {
      coord: [1,3,37,3,37,37,1,37],
      type: 'poly'
    },contentString = '<div class="marker_content">'+
    '<div class="title"><h2>Sound</h2><p>' + sound[4] + '</p></div>'+
    '<div class="location"><h2>Location</h2><p>' + sound[5] + '</p></div>'+
    '<div class="sound">'+
    '<object name="audioplayer_'+sound[3]+'" type="application/x-shockwave-flash" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0" width="265" height="24">'+
    '<param name="movie" value="http://assets.hasa.org/swf/player.swf">'+
    '<param name="bgcolor" value="#FFFFFF">'+
    '<param name="wmode" value="transparent">'+
    '<param name="menu" value="false">'+
    '<param name="flashvars" value="soundFile='+sound[6]+'&amp;titles='+sound[4]+'&amp;playerID=audioplayer_'+sound[3]+'">'+
    '<embed src="http://assets.hasa.org/swf/player.swf" width="265" height="24" flashvars="soundFile='+sound[6]+'&amp;titles='+sound[4]+'&amp;playerID=audioplayer_'+sound[3]+'" menu="false" wmode="transparent" bgcolor="#FFFFFF">'+
    '</embed>'+
    '</object>'+
    '</div>'+
    '</div>',infowindow = new google.maps.InfoWindow({
        content: contentString,
        maxWidth: 350
    }),marker = new google.maps.Marker({
      position: myLatLng,
      map: map,
      icon: image,
      shape: shape,
      title: sound[0],
      zIndex: sound[3],
      html: contentString
    });
    google.maps.event.addListener(marker,'click',function(){
      infowindow.setContent(this.html);
      infowindow.open(map,this);
    });
    marker_arr.push(marker)
  }
}

function initializeMap() {
    var latlng = new google.maps.LatLng(39.2858482, -76.6131105);
    
    var stylez = [
      {
        featureType: "administrative.country",
        elementType: "labels",
        stylers: [
        { visibility: "off" }
        ]
      },{
        featureType: "administrative.locality",
        elementType: "labels",
        stylers: [
        { visibility: "off" }
        ]
      },{
        featureType: "administrative.province",
        elementType: "labels",
        stylers: [
        { visibility: "off" }
        ]
      },{
        featureType: "water",
        elementType: "labels",
        stylers: [
        { visibility: "off" }
        ]
      },{
        featureType: "road.arterial",
        elementType: "labels",
        stylers: [
        { visibility: "off" }
        ]
      },{
        featureType: "road",
        elementType: "geometry",
        stylers: [
        { visibility: "simplified" }
        ]
      }
    ];
    
    var myOptions = {
      zoom: 13,
      center: latlng,
      mapTypeControlOptions: {
        mapTypeIds: [google.maps.MapTypeId.ROADMAP, 'soundscape']
      }
    };
    var map = new google.maps.Map(document.getElementById("map_canvas"),
        myOptions);
    var styledMapsOptions = {
      name:'SoundScape'
    };
    var soundscapeMapType = new google.maps.StyledMapType(stylez,styledMapsOptions);
    map.mapTypes.set('soundscape', soundscapeMapType);
    map.setMapTypeId('soundscape');
    
    setMarkers(map,soundpoints);
    setVolume(map,volumepoints);
}
