/* asset_compress 1306293600 */
var PlanetBrokerMap = Class.create({
  initialize: function(properties, center, zoom){
    this.map_canvas = $('map_canvas');
    this.street_view = $('pano');
    this.similar_view = $('similar_view');
    this.map_tab = $('map-tab');
    this.street_tab = $('street-tab');
    this.similar_tab = $('similar-tab');
    this.panoClient = new google.maps.StreetviewClient(); //new GStreetviewClient(); 
    this.image_width = 100;
    this.properties = properties; //all properties
        
    if(!Object.isArray(this.properties)){
      this.properties = [this.properties];
    }
    this.property = this.properties[0]; //default property
        
    this.map_tab.observe('click', this.showMap.bindAsEventListener(this));
    this.street_tab.observe('click', this.showStreet.bindAsEventListener(this));
    this.similar_tab.observe('click', this.showSimilar.bindAsEventListener(this));
    
    this.center = (center.lat != undefined) ? center : {lat: this.property.Property.lat, lng: this.property.Property.lng};
    
    this.zoom = zoom;
    
    if(google.maps.BrowserIsCompatible()){ //Yay!
      this.map = new google.maps.Map2(this.map_canvas);
      this.pano = new google.maps.StreetviewPanorama(this.street_view);
      this.map.setCenter(new google.maps.LatLng(this.center.lat, this.center.lng), this.zoom);
      
      this.addPropertyMarkers();      
      if(this.property && this.properties.length == 1){
        this.setLabel(this.setPropertyInfoWindow(this.property));
        latlng = new GLatLng(this.property.Property.lat, this.property.Property.lng);
        this.panoClient.getNearestPanorama(latlng, this.showPanoData.bind(this));
      }
      this.map.setUIToDefault();
    }
    else{ //Uh Oh!
      alert('browser not compatible');
    }
    
  },
  
  showPanoData: function(panoData){
    if(panoData.code != 200) {
      this.street_view.update('<div class="center">No Street View Available.</div>');
    }
    else {
      this.street_view.update();
      nextPanoId = panoData.links[0].panoId;
      this.pano.setLocationAndPOV(panoData.location.latlng);
    }
    return;
  },
  
  setInfoWindow: function (html){
    if(html) this.html = html;
  },
  
  setPropertyInfoWindow: function(property){
    var html = "";
    html += "<div class='window_box_body'>";
    html +=  "<div class='window_box_text'>";
		html +=    "<p>" + this.formatPropertyAddress(property) + "</p>";
		html +=    "<p>Price: $ "+ parseFloat(property.Property.price_dollars).toFixed(2) +"</p>";
		html +=    "<p>"+ this.formatBedBath(property) + "</p>";
		html +=  "</div>";
		html +=  "<div class='window_box_details'><br />";
		html +=    "<p class='window_box_image'><a href='/properties/view/"+ property.Property.id +"'><img src='"+ property.Property.thumb_path +"' width='"+ this.image_width +"' alt='' /></a></p>";
		html +=    "<h3><a href='/properties/view/"+ property.Property.id +"'>Property details</a></h3>";
    if(property.User.id){
      html +=    "<ul>";
      html +=    "<li><a href=\"javascript:openWindow('/chats/start/"+property.User.id+"');\"><img src='/img/ico_chat.gif' alt='' /></a></li>";
      html +=    "</ul>";
    }
    html +=   "</div>"
    html += "</div>";
    
		return html;
  },
  
  openReport: function(propertyId){
    new Ajax.Updater('message', '/reports/add/Property/' + propertyId, {
      onComplete: function(){this.toggleLoader();}.bind(this),
      onLoading: function(){this.toggleLoader();}.bind(this)
    });
  },
  
  toggleLoader: function(){
    if(loader = $('loader')){
      loader.toggle();
    }
  },
  
  addPropertyMarkers: function(){
    
    this.properties.each(function(property){

      var pushPin = new google.maps.Icon(G_DEFAULT_ICON);
      pushPin.image = "http://planetbroker.com/img/red_marker.png";
      pushPin.iconSize = new google.maps.Size(16,26);
      pushPin.shadowSize = new google.maps.Size(30, 26);
      pushPin.iconAnchor = new google.maps.Point(6, 26);
      pushPin.infoWindowAnchor = new google.maps.Point(6, 6)
      
      var point = new google.maps.LatLng(property.Property.lat, property.Property.lng);
      var marker = new google.maps.Marker(point, {icon:pushPin});
      
      google.maps.Event.addListener(marker, "click", function(latlng){
        this.panoClient.getNearestPanorama(latlng, this.showPanoData.bind(this));
      }.bind(this));
      
      marker.bindInfoWindowHtml(this.setPropertyInfoWindow(property));
      this.map.addOverlay(marker);
      
    }.bind(this));
  },
  
  reset: function(){
    this.map.panTo(new google.maps.LatLng(this.center.lat, this.center.lng)); 
  },
  
  showAddress: function(address){
    this.geocoder = new google.maps.ClientGeocoder();
    this.geocoder.getLatLng(
      address,
      function(point) {
        if (!point) {
          alert(address + " not found");
        } else {
          this.map.setCenter(point, this.zoom);
          var marker = new google.maps.Marker(point);
          this.map.addOverlay(marker);
          marker.openInfoWindowHtml(address);
        }
      }
    );
  },
  
  showMap: function(){
    this.street_view.hide();
    this.similar_view.hide();
    this.map_canvas.show();
    $('map-tab').addClassName('current');
    $('similar-tab').removeClassName('current');
    $('street-tab').removeClassName('current');
  },
  
  showStreet: function(){
    this.street_view.show();
    this.map_canvas.hide();
    this.similar_view.hide();
    $('map-tab').removeClassName('current');
    $('similar-tab').removeClassName('current');
    $('street-tab').addClassName('current');
  },
  
  showSimilar: function(){
    this.similar_view.show();
    this.street_view.hide();
    this.map_canvas.hide();
    $('map-tab').removeClassName('current');
    $('street-tab').removeClassName('current');
    $('similar-tab').addClassName('current');
  },
  
  toggle: function(){
    this.street_view.toggle();
    this.map_canvas.toggle();
  },
  
  panToProperty: function(property){
    latlng = new google.maps.LatLng(property.Property.lat, property.Property.lng);
    this.map.openInfoWindow(
      latlng,
      this.setPropertyInfoWindow(property)
    );
    this.panoClient.getNearestPanorama(latlng, this.showPanoData.bind(this));
  },
  
  panToPropertyById: function(propertyId){
    if(property = this.getPropertyById(propertyId)){
      this.panToProperty(property);
    }
  },
  
  getPropertyById: function(id){
    for(var i = 0; i < this.properties.length; i++){
      if(this.properties[i].Property.id == id){
        return this.properties[i];
      }
    }
    return false;
  },
  
  setLabel: function(string){
    this.map.openInfoWindow(
      new GLatLng(this.center.lat, this.center.lng), 
      new Element('div').update(string)
    );
  },
  
  formatPropertyAddress: function(property){
    var prop = property.Property;
    var retval = '';
    if(this.cleanString(prop.address) != ''){
      retval += prop.address + '<br />';
    }
    if(this.cleanString(prop.city != '')){
      retval += prop.city + ', ';
    }
    if(this.cleanString(prop.state != '')){
      retval += prop.state + ' ';
    }
    if(this.cleanString(prop.zipcode != '')){
      retval += prop.zipcode;
    }
    return retval;
  },
  
  formatBedBath: function(property){
    var prop = property.Property;
    var retval = '';
    if(this.cleanString(prop.bedrooms) != ''){
      retval += "Bedrooms: " + prop.bedrooms;
    }
    if(this.cleanString(prop.bathrooms) != ''){
      retval += " Bathrooms: " + prop.bathrooms;
    }
    return retval;
  },
  
  cleanString: function(string){
    if(string == 'null' || string == null){
      return '';
    }
    return string;
  }
});
