
var app = app || {};


app.RecordStore = function(address,name,description){
  if(address && name && description){
  	this.address = address;
  	this.name = name;
  	this.description = description;
  }else{
  	return undefined;
  }

};

app.RecordMap = function(config){
  this.map = null;
  this.markers = [];
  this.windows = [];
  this.geocoder = new google.maps.Geocoder();
  if(config.id){
   this.id = config.id;
   this.zoom = config.zoom;
   this.center = config.center;
   this.mapTypeId = config.mapTypeId;
  }else{
   return undefined; 
  }
};

app.RecordMap.prototype.init = function(){
  var mapConfig = {
    zoom: (this.zoom)?this.zoom:8,
    center: (this.center)?this.center:new google.maps.LatLng(40.730885,-73.997383),
    mapTypeId: (this.mapTypeId)?this.mapTypeId:google.maps.MapTypeId.ROADMAP   
  };
  
  this.map = new google.maps.Map(document.getElementById(this.id), mapConfig);
  var recordSto = new app.RecordStore('957 Commonwealth Ave #2, Boston, MA 02215-1311','In Your Ear','9/10');
  this.addMarkerFromRecordStore(recordSto);
  this.addMarkerFromAddress("4122 Arbor Drive, Shrewsbury, MA","INSANE!");
 // this.addMarkerFromAddress("171 South St, Hopkinton, MA", "NUGGETS!");
  recordSto = new app.RecordStore('171 South st, Hopkinton, MA', 'Headquarters','EMC HQ.');
  this.addMarkerFromRecordStore(recordSto);
 

};

app.RecordMap.prototype.initFromAddress = function(address){
  var _this = this;
  this.geocoder.geocode( { 'address': address}, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
       _this.center = results[0].geometry.location;
       _this.init();
      } else {
        alert("Geocode was not successful for the following reason: " + status);
      }
    });
  app.RecordMap.prototype.addMarker = function(latlng,msg){
        var _this = this;
        var coords = latlng;
        var marker = new google.maps.Marker({
            map: _this.map, 
            position: coords
        });
    if(msg){

      var infowindow = new google.maps.InfoWindow({
        content: msg
      });
      google.maps.event.addListener(marker, 'click', function() { 
        for (i in _this.windows) {
          _this.windows[i].close();
        }
        _this.windows.push(infowindow);
        infowindow.open(_this.map,marker);
      }); 
    }
  };
  app.RecordMap.prototype.addMarkerFromAddress = function(address,msg){
    this.geocoder.geocode( { 'address': address}, function(results, status) {
 		if (status == google.maps.GeocoderStatus.OK) {
         var latlng = results[0].geometry.location;
         _this.addMarker(latlng);
         if(msg){ _this.addMarker(latlng,msg); }
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
    });
  };
  app.RecordMap.prototype.addMarkerFromRecordStore = function(obj){
    this.geocoder.geocode( { 'address': obj.address}, function(results, status) {
 		if (status == google.maps.GeocoderStatus.OK) {
         var latlng = results[0].geometry.location;
         _this.addMarker(latlng);
         var tpl = obj.name;
         if(msg){ _this.addMarker(latlng,tpl); }
        } else {
          alert("Geocode was not successful for the following reason: " + status);
        }
    });
  };
};
app.RecordMap.prototype.loadFeed = function(){
   $.ajax({
     type: 'GET',
 	url: 'sheetfeedhere',
 	dataType: 'xml',
 	error: function(xhr) {
 		alert('Failed to parse feed');
 	},
 	success: function(xml) {
 		var feed = $('feed', xml).eq(0);
 		var items = [];
 		$('entry', xml).each( function() {
 			var item = {};
 			item.content = $(this).find('content').eq(0).text();
 			alert(item.content);
 			items.push(item);
         
 		});
         
 		//console.dir(items);
 	}
 });  
};

window.onload = function(){
  var map = new app.RecordMap({id:'map_canvas',zoom:9});
  map.initFromAddress("Shrewsbury, MA");
  
};
