//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$    Google Map Version 2  $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//
//  File: google_map_v2.js                                                                      //
//  Authors: Travis Glines, Ian Reardon                                                         //
//                                                                                              //
//  Setup Instructions:                                                                         //
//  add google script,meta tag,div                                                              // 
//$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$//


////////  Class /////////

/////// Constructor ///////
function GoogleMapV2(map_canvas_id_arg,zoom_arg,lat_arg,lng_arg) {
  google_map_v2_ref=this;

  this.icons = new Array();
  this.icons['blue_icon'] = new GIcon();
  this.icons['blue_icon'].image = state_machine.get_static_url() + "images/a.blue.png"; 
  this.icons['blue_icon'].iconSize = new GSize(20,34);
  this.icons['blue_icon'].iconAnchor = new GPoint(10,17);
  this.icons['blue_icon'].shadow = state_machine.get_static_url() + "images/a.shadow.png";
  this.icons['blue_icon'].shadowAnchor = new GPoint(10,17);
  this.icons['blue_icon'].shadowSize = new GSize(38,34);
  this.icons['college_icon'] = new GIcon();
  this.icons['college_icon'].image = state_machine.get_static_url() + "images/college.png"; 
  this.icons['college_icon'].iconSize = new GSize(40,52);
  this.icons['college_icon'].iconAnchor = new GPoint(20,26);
  this.icons['college_icon'].shadow = state_machine.get_static_url() + "images/college.shadow.png";
  this.icons['college_icon'].shadowAnchor = new GPoint(20,26);
  this.icons['college_icon'].shadowSize = new GSize(67,52);

  this.icons_package = {
    "blue_icon": {
      "image": state_machine.get_static_url() + "images/a.blue.png",
      "image_clicked": state_machine.get_static_url() + "images/a.red.png",
      "icon_object": this.icons['blue_icon']
    },
    "college_icon": {
      "image": state_machine.get_static_url() + "images/college.png",
      "image_clicked": state_machine.get_static_url() + "images/college.png",
      "icon_object": this.icons['college_icon']
    }
  }

  this.map = new GMap2(document.getElementById(map_canvas_id_arg));
  this.map.setCenter(new GLatLng(lat_arg, lng_arg), zoom_arg);
  var customUI = this.map.getDefaultUI();
  customUI.maptypes.hybrid = false;
  customUI.maptypes.satellite = false;
  customUI.maptypes.physical = false;
  customUI.controls.menumaptypecontrol = false;
  this.map.setUI(customUI);
  this.map.disableScrollWheelZoom();

  //Hold the currently active marker.
  this.active_marker = null;

  //Used to calculate the zoom level
  this.map_bounds = new GLatLngBounds();

  //List of markers.  Loop through to remove
  this.marker_list = new Array();

  GEvent.addListener(this.map,'moveend',function(){
    return;
    //alert(google_map_v2_ref.map.getBounds());
  });
}
////// End Constructor ///////

GoogleMapV2.prototype.clear_markers = function () {
  $('#map_info_window').dialog('option', 'hide');
 
  this.map.clearOverlays();
 
  for(i=0;i<this.marker_list.length;i++) {
    //this.map.removeOverlay(this.marker_list[i]);
    this.map.addOverlay(this.marker_list[i]);
  }
  //this.marker_list = new Array();
  this.map_bounds = new GLatLngBounds();
}

GoogleMapV2.prototype.calculate_zoom = function () {
  this.map.setZoom(this.map.getBoundsZoomLevel(this.map_bounds));
  this.map.setCenter(this.map_bounds.getCenter());
}

///// Method add_marker //////
GoogleMapV2.prototype.add_marker = function (lat,lng,type,is_perm,tooltip_text,info_win_text,title_text){
  var point = new GLatLng(lat,lng); 
  var icon_type = type+"_icon";
  var markerOptions = { icon:this.icons_package[icon_type].icon_object };
  var marker = new GMarker(point,markerOptions);

  if (is_perm) {
    this.marker_list.push(marker);
  }

  this.map_bounds.extend(point);
  
  var tooltip = new Tooltip(marker,tooltip_text,4); 
  marker.tooltip = tooltip; 

  google_map_v2_ref.map.addOverlay(marker);
  google_map_v2_ref.map.addOverlay(tooltip);

  GEvent.addListener(marker,'mouseover',function(){ 
    this.tooltip.show(); 
  }); 
  GEvent.addListener(marker,'mouseout',function(){
    this.tooltip.hide(); 
  });

  if (info_win_text == null) { return; }

  var info_window_text = info_win_text;

  GEvent.addListener(marker, 'click', function() {
    if(google_map_v2_ref.active_marker != null) {
      google_map_v2_ref.active_marker.setImage(google_map_v2_ref.icons_package[icon_type].image);
    }
    google_map_v2_ref.set_active_marker(marker);

    marker.setImage(google_map_v2_ref.icons_package[icon_type].image_clicked);
    marker.tooltip.hide();
 
    var markerPosition = google_map_v2_ref.map.fromLatLngToContainerPixel(marker.getLatLng());

    var msg_box = new MapMessageBox(marker,google_map_v2_ref);
    msg_box.set_content(info_window_text);
    msg_box.set_title(title_text);
    var d = document.getElementById('map_canvas');
    var pos = google_map_v2_ref.getPosition(d);
    msg_box.setPosition(pos,markerPosition,$("#map_canvas").width(),$("#map_canvas").height());
    msg_box.open_message_box(icon_type)

  });
}



///// End Method add_marker //////
GoogleMapV2.prototype.set_active_marker = function (marker) {
  this.active_marker = marker;
}

GoogleMapV2.prototype.getPosition = function (obj){
  var topValue= 0,leftValue= 0;
  while(obj){
    leftValue+= obj.offsetLeft;
    topValue+= obj.offsetTop;
    obj= obj.offsetParent;
  }
  finalvalue = [leftValue,topValue];
  return finalvalue;
}

/////// End Class //////////

