var GoogleMaps = Class.create();
GoogleMaps.prototype = {

  initialize : function (){
  },

  initTracer : function(gmap_data_array,artist_name,map_size){
    this.marker=[];
    this.pophtml=[];
    this.createMap('googlemap_'+artist_name,map_size);
    for(i=0; i< gmap_data_array.length; i++){
      if( i==0 ){
        this.map.setCenter(new GLatLng(gmap_data_array[i]['latitude'],gmap_data_array[i]['longitude']),16);
      }
      this.setEvent(artist_name+'_point_'+i);
      var point = new GPoint(gmap_data_array[i]['longitude'],gmap_data_array[i]['latitude']);
      this.pophtml[i] = this.getInfo(gmap_data_array[i],artist_name,true);
      this.marker[i] = this.show_marker(point,this.pophtml[i]);
      this.map.addOverlay(this.marker[i]);
    }
    this.gotoPoint(0);
  },

  initBlogPage : function(gmap_data_array){
    this.createMap('googlemap'+gmap_data_array['inumber'],'medium');
    this.map.setCenter(new GLatLng(gmap_data_array['latitude'],gmap_data_array['longitude']),16);
    var point = new GPoint(gmap_data_array['longitude'],gmap_data_array['latitude']);
    var pophtml = this.getInfo(gmap_data_array,false);
    var marker = this.show_marker(point, pophtml);
    this.map.addOverlay(marker);
    marker.openInfoWindowHtml(pophtml);
  },

  show_marker : function (point,pophtml){
    var marker = new GMarker(point,{icon: this.createIcon()});

    GEvent.addListener(marker, "click", function() {
      marker.openInfoWindowHtml(pophtml);
    });
    return marker;
  },

  setEvent : function(id){
    Event.observe(document.getElementById(id), 'click', this.gotoPointEvent.bindAsEventListener(this),false)
  },

  createMap : function(id,map_size){
    this.map = new GMap2(document.getElementById(id));
    switch(map_size){
    case 'large':
      this.map.addControl(new GLargeMapControl());
      this.map.addControl(new GScaleControl());
      break;
    case 'medium':
      this.map.addControl(new GSmallMapControl());
      this.map.addControl(new GScaleControl());
      break;
    case 'small':
      this.map.addControl(new GSmallZoomControl());
      break;
    default:
      this.map.addControl(new GSmallZoomControl());
    }

  },

  createIcon : function(){
    var icon = new GIcon();
    icon.image = "/tracer/img/seppa_logo_mini.gif";
    icon.iconSize = new GSize(70,34);
    icon.iconAnchor = new GPoint(35,17);
    icon.infoWindowAnchor = new GPoint(30,10);
    return icon;
  },

  getInfo : function(gmap_data,artist_name,isTracerPage){
    var nextday='';
    if(gmap_data['roll_up_time_from'].substring(0,10) != gmap_data['roll_up_time_to'].substring(0,10)){
      nextday='翌日';
    }
    var blog_name = '';
    switch(artist_name){
    case 'ikeda'   : blog_name = 'IkedaBlog.php';    break;
    case 'sato'    : blog_name = 'SatoBlog.php';     break;
    case 'yamamoto': blog_name = 'YamamotoBlog.php'; break;
    }
    var  html ='<div class="mapTxt">';
    html +='<h6>'+gmap_data['ititle']+'</h6>';
    html +='<img src="/tracer/img/tit_date.gif" alt="date/time" width="41" height="5" /><br />';
    html +=gmap_data['ru_date']+'   /   '+gmap_data['ru_time_from']+'から'+ nextday + gmap_data['ru_time_to'] + '<br />';
    html +='<img src="/tracer/img/tit_shop.gif" alt="shop" width="20" height="5" /><br />';
    html +=gmap_data['name']+'<br />';
    if(isTracerPage){
      html +='<img src="/tracer/img/tit_arrow.gif" alt="" width="10" height="9" align="absmiddle" /><a href="/blog/' + blog_name + '?itemid='+gmap_data['inumber']+'">詳細はこちら</a>';
    }
    html += '</div>';
    return html;
  },

  gotoPointEvent : function(evt){
    var point_number=Event.element(evt).id.replace(/.*point_/g,"");
    this.gotoPoint(point_number);
  },

  gotoPoint : function(point_number){
    this.marker[point_number].openInfoWindowHtml(this.pophtml[point_number]);
  }

}

