function Google_callback_dummy() {
  $(document.body).trigger('google_maps_load');
}

function Google_map(center_location) {
  var self = this;
  console.log(this.location_name == '');
  if (this.location_name == '') { this.is_run = true; return this; }

  this.g_map = this.root[0];
  this.geocodes = new Array();
  this.markers = new Array();
  this.location_name = '';
  
  this.center_location = center_location;
  
  this.start_up = function() {
    if (self.is_run) { return this; }
    console.log('me');
    $.ajax({
      url: 'http://maps.google.com/maps/api/js?sensor=false&callback=Google_callback_dummy',
      dataType: 'script',
      success: function() {$(document.body).bind('google_maps_load', self.build_map);}
    });
  }

  this.build_map = function() {
    var myOptions = {
      zoom: 14,
      mapTypeId: google.maps.MapTypeId.ROADMAP,
      scrollwheel: false,
      backgroundColor: 'white'
    };
    self.g_map = new google.maps.Map(self.g_map, myOptions);
    self.populate_map();
  }
  
  this.populate_map = function() {
    self.geocodes[0] = new google.maps.LatLng(self.center_location.latitude, self.center_location.longitude)
    self.markers[0] = new google.maps.Marker({
      position: self.geocodes[0],
      map: self.g_map,
      title: self.location_name
    });
    self.g_map.setCenter(self.geocodes[0], 11);
  }
  
  return this;
}

function Google_directions(center_location, g_directions) {
  var self = this;
  var parent = new Object();
  Google_map.call(this, center_location);
  
  this.g_directions = g_directions[0];
  this.directions_form = g_directions.siblings('form');
  this.starting_location = this.directions_form.find('input');
  this.directions_display = null;
  
  parent.build_map = this.build_map;
  this.build_map = function() {
    parent.build_map();
    self.directions_service = new google.maps.DirectionsService();
    self.directions_display = new google.maps.DirectionsRenderer();
    self.directions_display.setMap(self.g_map);
    self.directions_display.setPanel(self.g_directions);
    self.directions_form.bind('submit', self.process_address);
    self.starting_location.bind('focus', function() {self.starting_location.removeClass('error')});
    if (self.initial_location) {
      self.get_route(self.initial_location);
    }
  }
  
  this.get_route = function(address) {
    self.markers[0].setMap(null);
    var request = {
      origin : address, 
      destination : self.geocodes[0],
      travelMode : google.maps.DirectionsTravelMode.DRIVING
    };
    self.directions_service.route(request, self.process_response);
  }
  
  this.process_address = function(e) {
    e.preventDefault();
    self.get_route(self.starting_location.val());
    return false;
  }
  
  this.process_response = function(response, status) {
    self.directions_display.setDirections(response);
    if (status != google.maps.DirectionsStatus.OK) {
      var temp_value = self.starting_location.addClass('error').val();
      self.starting_location.val('Not Found');
      var timer = setTimeout(function() { 
        self.markers[0].setMap(self.g_map);
        self.starting_location.val(temp_value);
      }, 1500);
    }
  }
  
  return this;
}

function Google_locations(center_location, location_list) {
  var self = this;
  Google_map.call(this, center_location);

  this.locations = new Object();
  this.geocode_elements = new Array();
  this.location_list = location_list;
  
  this.populate_map = function(e) {
    $.ajax({
      type: "GET",
      datatype: 'json',
      url: '/locations.json',
      contentType: "application/json",
      scriptCharset: 'utf-8',
      success: self.reload_locations
    });
  }

  this.reload_locations = function(json, status) {
    self.locations = json;
    self.redraw_map();
  }

  this.redraw_map = function() {
    self.cache_geocodes();
    self.g_map.setCenter(self.geocodes[0]);

    for (var i = 0; i < self.geocodes.length; i++) {
      self.marker_maker(i);
      self.bubble_marker(i);
    }
    self.g_map.setZoom(16);
  }

  this.cache_geocodes = function() {
    self.geocode_elements = self.locations;
    self.geocodes = new Array();
    self.markers = new Array();
    var j = 0;
    for (i in self.geocode_elements) {
      self.geocodes[j] = new google.maps.LatLng(Number(self.geocode_elements[i].geocode.latitude), Number(self.geocode_elements[i].geocode.longitude));
      j++;
    }
  }

  this.inner_map_bubble = function(json_content) {
    var content = '<div style="" class="map_location_contact deskman">'
    content += '<h3>' + json_content.name + '</h3>';
    content += '<h5><a href="http://' + json_content.web_address + '" target="_blank">' + json_content.web_address + '</a></h5>';
    content += '<p>' + json_content.description + '</p>';
    content += '<p>' + json_content.address + '</p>';
    content += '</div>';
    return content;
  }
  
  this.bubble_marker = function(iter) {
  }

  this.marker_maker = function(iter) {
    var new_marker = new google.maps.MarkerImage('/media/markers/' + (iter + 1) + '.png',
      // This marker is 20 pixels wide by 32 pixels tall.
      new google.maps.Size(20, 34),
      // The origin for this image is 0,0.
      new google.maps.Point(0,0),
      // The anchor for this image is the base of the flagpole at 0,32.
      new google.maps.Point(20, 32));

    self.markers[iter] = new google.maps.Marker({
      position: self.geocodes[iter],
      map: self.g_map,
      title: self.geocode_elements[iter].name,
      icon: new_marker,
      shadow: '/media/markers/shadow.png',
      flat: false
    });
    self.active_info = new google.maps.InfoWindow();
    google.maps.event.addListener(self.markers[iter], "click", function(e) {
      if (self.active_info) {self.active_info.close();}
      self.active_info.setContent(self.inner_map_bubble(self.geocode_elements[iter]));
      self.active_info.setOptions();
      self.active_info.open(self.g_map, self.markers[iter]);
      self.location_list.children('a').removeClass('active');
      self.location_list.children('a').eq(iter).addClass('active');
    });
    self.location_list.append('<a href="#' + self.geocode_elements[iter].name + '"> <img src="/media/markers/' + (iter + 1) + 'b.png" />' + self.geocode_elements[iter].name + '</a>');
    
    self.location_list.children('a:last').toggle(function(e) {
      e.preventDefault();
      var me = $(e.currentTarget);
      me.addClass('active').siblings().removeClass('active');
      google.maps.event.trigger(self.markers[iter], "click");
    }, function(e) {
      e.preventDefault();
      var me = $(e.currentTarget);
      me.removeClass('active');
      self.info_windows[iter].close();
    });
  }
  return this;
}



