var selectedMarker = null;

function initMap(){
  
  var artistLocationsMap = {};
  $.each(_artists, function(id, data){
    var locationNumber = extractLocation(data.eastId);
    if(!(artistLocationsMap[locationNumber])){
       var location = {
        type: 'artist',
        icon: 'images/markers/a_s/'+locationNumber+'.png',
        latlng: new google.maps.LatLng(data.address.lat, data.address.lng),
        ids: []
      };
      artistLocationsMap[locationNumber] = location;
    }
    artistLocationsMap[locationNumber].ids.push(id);
  });
  
  var exhibitLocationMap = {};
  $.each(_exhibits, function(id, data){
    var locationNumber = extractLocation(data.eastId);
    if(!(exhibitLocationMap[locationNumber])){
       var location = {
        type: 'exhibit',
        icon: 'images/markers/e_e/'+locationNumber+'.png',
        latlng: new google.maps.LatLng(data.address.lat, data.address.lng),
        ids: []
      };
      exhibitLocationMap[locationNumber] = location;
    }
    exhibitLocationMap[locationNumber].ids.push(id);
  });
  
  var locations = [];
  $.each(artistLocationsMap, function(locationNumber, markerData){
    locations.push(markerData);
  });
  $.each(exhibitLocationMap, function(locationNumber, markerData){
    locations.push(markerData);
  });
  
  locations = shuffle(locations);
  
  var options = {
    zoom: 13,
    center: new google.maps.LatLng(30.2788, -97.6952),
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    styles: EAST_MAP_STYLE
  };
  map = new google.maps.Map(document.getElementById('map'), options);
  
  //var i = 0;
  $.each(locations, function(i, location){
	  //var location = locations[i];
	  var eastIds = location.eastIds;
    
    var marker = new google.maps.Marker({
      map:map,
      icon:location.icon,
      position:location.latlng
    });
    marker._icon = location.icon; // stash the origional
    
    var clickHandler = function(){
      doSelectedMarker(marker);
      
      if(location.type == 'artist'){
        goToArtist(location.ids[0]);
      }else if(location.type == 'exhibit'){
        goToExhibit(location.ids[0]);
      }
    };
    google.maps.event.addListener(marker, 'click', clickHandler);
    
    $.each(location.ids, function(j, id){
      if(location.type == 'artist'){
        _artists[id].marker = marker;
      }else if(location.type == 'exhibit'){
        _exhibits[id].marker = marker;
      }
    });
  });
}

function doSelectedMarker(marker){
  if(selectedMarker){
    selectedMarker.setShadow(null);
  }
  
  selectedMarker = marker;
  
  selectedMarker.setShadow('images/markers/selected_map_icon.png');
}

function extractLocation( input ){
    //bohemian is a special place...
    if(input === 'xx'){
      return input;
    }
    
    var index = input.search( /[a-z]/ );
    
    if(index === -1){
      return input;
    }
    return input.substring(0,index);
}

function shuffle(o){ //v1.0
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
};


var EAST_MAP_STYLE = [
	{
		stylers: [
		{ invert_lightness: true },
		{ saturation: 13 },
		{ gamma: 0.76 },
		{ lightness: -51 }
		]
	},{
		featureType: "road.highway",
		stylers: [
		{ hue: "#00b2ff" },
		{ lightness: 6 },
		{ saturation: -85 }
		]
	},{
		featureType: "road.arterial",
		stylers: [
		{ hue: "#00b2ff" },
		{ lightness: 7 },
		{ saturation: -85 }
		]
	},{
		featureType: "road.local",
		stylers: [
		{ hue: "#00b2ff" },
		{ saturation: -26 },
		{ lightness: 19 }
		]
	},{
		featureType: "poi.park",
		stylers: [
		{ visibility: "off" }
		]
 	},{
   		featureType: "poi.business",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
  		featureType: "administrative.neighborhood",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "poi.school",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "transit.station.rail",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "landscape.man_made",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "administrative.land_parcel",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "poi",
   		stylers: [
     	{ visibility: "off" }
   		]
 	},{
   		featureType: "road.highway",
   		stylers: [
     	{ visibility: "simplified" }
   		]
 	},{
   		featureType: "road",
   		stylers: [
     	{ visibility: "on" }
		]
	}
];
