var fjGL = {};

fjGL.SearchPanel = Class.create({
  initialize: function() {
    this.panel = "googlebox";
    this.mapbox = "mapbox";
    this.txtbox = "txtbox";
    this.imgbox = "imgbox";
    this.targetsite = "tokyoartbeat.com";
    this.marker = null;
    this.eid = 0;
    this.ename = null;
    this.vname = null;
    this.latitude = this.longitude = 0;
  },
  startup: function() {
    this.map = this.initializeGoogleMap();
    this.gsc = this.initializeAJAXSearch();
  },
  initializeGoogleMap: function() {
    var map = new google.maps.Map2(document.getElementById(this.mapbox));
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    return map;
  },
  initializeAJAXSearch: function() {
    var siteSearch = new GwebSearch();
    siteSearch.setUserDefinedLabel("Tokyo Art Beat");
    siteSearch.setUserDefinedClassSuffix("siteSearch");
    siteSearch.setSiteRestriction(this.targetsite);

    var options = new GsearcherOptions();
    options.setExpandMode(GSearchControl.EXPAND_MODE_OPEN);
    options.setRoot(document.getElementById(this.imgbox));

    var searchControl = new google.search.SearchControl();
    searchControl.addSearcher(siteSearch);
    searchControl.addSearcher(new google.search.ImageSearch(), options);
    searchControl.addSearcher(new google.search.BlogSearch());
    searchControl.addSearcher(new google.search.WebSearch());
    searchControl.addSearcher(new google.search.NewsSearch());
    return searchControl;
  },
  setposition: function() {
    var point = document.documentElement.scrollTop || document.body.scrollTop;
    $(this.panel).style.top = (point + 60) + 'px';
  },
  getkeyword: function(type) {
    var kw = null;
    switch(type) {
    case 1:
      kw = this.vname; break;
    case 2:
      kw = this.ename; break;
    default:
      kw = this.vname + " " + this.ename;
    }
    return kw;
  },
  setp: function(id, lat, lng) {
    this.eid = id;
    this.ename = $("ename" + id).innerHTML;
    this.vname = $("vname" + id).innerHTML;
    this.latitude = lat;
    this.longitude = lng;
  },
  search: function(type) {
    this.keyword = this.getkeyword(type);

    if ($(this.panel).style.visibility != "visible") {
      $(this.panel).hide();
      $(this.panel).style.visibility = "visible";
      new Effect.Appear(this.panel, {duration: 0.50, afterFinishInternal: function(){}});
    }

    //alert("search=" + this.keyword + ", " + this.latitude + ", " + this.longitude);
    this.setposition();
    if (this.marker != null) {
      this.map.removeOverlay(this.marker);
      this.marker = null;
    }
    $(this.imgbox).innerHTML = "";
    this.marker = new google.maps.Marker(new google.maps.LatLng(this.latitude, this.longitude));
    this.map.setCenter(new google.maps.LatLng(this.latitude, this.longitude), 14);
    this.map.addOverlay(this.marker);
    this.gsc.draw(document.getElementById(this.txtbox));
    this.gsc.execute(this.keyword);
  },
  close: function() {
      new Effect.Fade('loadingmsg', {duration: 0.25, afterFinishInternal: function(){ $('loadingmsg').style.visibility = 'hidden';}});
    $(this.panel).style.visibility = "hidden";
  }
});

// 最初だけ。
fjGL.localinit = function setactiongg() {
  var bb = ["ggb1", "ggb2", "ggb3", "gclose1", "gclose2"];
  for (i = 0; i < bb.length; i++) {
    e = $(bb[i]);
    e.observe('mouseover', function(b) {
      b.addClassName('mouseover_ggbutton');
    }.bind(e, e));
    e.observe('mouseout', function(b) {
      b.removeClassName('mouseover_ggbutton');
    }.bind(e, e));
  }
}
