var Oheya110 = Class.create();
Oheya110.prototype = {
  options: {
    q:"",
    zoom: 14,
    map_ratio: 0.04, /* だいたい3km */
    control: "l"
  },
  gmap:null,
    
  initialize: function(){
  
  },
  setMMswap2SearchButton: function(){
      $A(document.getElementsByTagName('INPUT')).each(function(input){
          if (input.type=="image"){
              input["onmouseover"] = function(e){
                  this.src=this.src.replace(".gif", "_o.gif"); // 'this' is input Element
              };
              input["onmouseout"] = function(e){
                  this.src=this.src.replace("_o.gif", ".gif"); // 'this' is input Element
              };
          }
      });
  },
  assignPid: function(to_id){
      var p_id = [];
      var regexp = new RegExp("p_id\\[(.*)\\]", "i");
      $A(document.getElementsByTagName('INPUT')).each(function(input){
          if (input.type=="checkbox"){
              if (input.checked){
                  if (input.name.match(regexp)){
                      p_id.push(RegExp.$1);
                  }
              }
          }
      });
      $(to_id).value = p_id.toString();
      if (p_id.length==0){
          alert('物件を選択してください');
      }
      return null;
  },
  showGmap: function(map, point){
      var self=this;
      if (typeof(map)!='object') return; /// map: {id:"mapid", width:300, height:200}
      if (typeof(point)!='object') return; /// point: {lat:37.4419, long:-122.1419, zoom:13, control: s }
      var map_place = $(map.id);
      map_place.style.width  = map.width;
      map_place.style.height = map.height;
      self.gmap = new GMap2(map_place);
      self.gmap.setCenter(new GLatLng(point.lat, point.long), point.zoom);
      self.gmap.addControl(new GMapTypeControl());
      if (map.ctrl!="mini"){
          self.gmap.addControl(new GSmallZoomControl());
          self.gmap.addControl(new GScaleControl());
      }
      self.gmap.addOverlay(new GMarker(new GPoint(point.long, point.lat)));
      self.loadSchoolInfo(point.long,point.lat);
      self.loadTownInfo(point.long,point.lat);
  },
  
  check_results_flag: true,
  checkResults: function(id){
      var self = this;
      $A($(id).getElementsByTagName('INPUT')).each(function(i){
          if(i.type=='checkbox'){
              var tmp_flag = self.check_results_flag==true?true:false;
              i.checked= tmp_flag;
          }
      });
      self.check_results_flag = self.check_results_flag==true ? false : true;
  },
  
  loadSchoolInfo: function(long, lat){
      var self = this;
      /// 学校情報のロード
      var param = 'action_json_get4map_school=1&'+self.getMapParam(long, lat);
      var school_icon = self.getIcon('school');
      var ajax = new Ajax.Request(
          '/json.html',{
            method: 'get',
            parameters: param,
            onSuccess: function(httpobj,json){
                if (json.school){
                    //                      self.marker.school=json.school.length;
                    for (var i=0,j=json.school.length; i<j; i++){
                        self.addSchoolMarker(json.school[i],school_icon);
                    }
                }
            }
          });
  },
    
  addSchoolMarker: function(school,icon){
      var self = this;
      var m = new GMarker(new GPoint(school.sch_long, school.sch_lat),icon);
      GEvent.addListener(m, "click", function(){
          m.openInfoWindowHtml(school.sch_name+'<br>'+school.sch_address);
      });
      self.gmap.addOverlay(m);
  },
    
  loadTownInfo: function(long, lat){
      var self = this;
      /// 街情報のロード
      var param = 'action_json_get4map_town=1&'+self.getMapParam(long, lat);
      var town_icon = self.getIcon('town');
      var ajax = new Ajax.Request(
          '/json.html',{
            method: 'get',
            parameters: param,
            onSuccess: function(httpobj,json){
                if (json.town){
                    for (var i=0,j=json.town.length; i<j; i++){
                        self.addTownMarker(json.town[i],town_icon);
                    }
                }
            }
          });
  },
  addTownMarker: function(town, icon){
      var self = this;
      var m = new GMarker(new GPoint(town.t_long, town.t_lat),icon);
      GEvent.addListener(m, "click", function(){
          var html = town.t_name+'<br>'+town.t_address;
          m.openInfoWindowHtml(html);
      });
      self.gmap.addOverlay(m);
  },
    
  getMapParam: function(long,lat){
      var self=this;
      var param = 'long_max='+(long+self.options.map_ratio/2)+'&long_min='+(long-self.options.map_ratio/2)+'&lat_max='+(lat+self.options.map_ratio/2)+'&lat_min='+(lat-self.options.map_ratio/2);
      return param;
  },
  openSubgmap: function(lat,long){
      var width = 400;
      var height = 300;
      var url = "/gmap.html";
      var win = window.open(url,"popupgmap","width="+width+",height="+height+",status=1,scrollbars=1,resizable=1,");
  },
  
  getIcon: function(type){
      var icon = new GIcon();
      icon.image = "/admin/img/star_"+type+".png";
      icon.shadow = "/admin/img/star_shadow.png";
      icon.iconSize = new GSize(22, 21);
      icon.shadowSize = new GSize(25, 21);
      icon.iconAnchor = new GPoint(11, 11);
      icon.infoWindowAnchor = new GPoint(13, 10);
      return icon;
  },
  
  appendfn: function(){
      /// 新着物件
      $A($('newProps').getElementsByTagName('img')).each(function(ele){
          if (ele.tagName=='IMG' && ele.id.match(/^new_(\d+)$/)){
              var stationList = $('new_st_'+RegExp.$1);
              ele.style.cursor   = 'pointer';
              ele['onmouseover'] = function(){oheya110.fnOpen(stationList);};
              ele['onmouseout']  = function(){oheya110.fnClose(stationList);};
          }
      });
      /// お勧め
      $A($('recommendProps').getElementsByTagName('img')).each(function(ele){
          if (ele.tagName=='IMG' && ele.id.match(/^recm_(\d+)$/)){
              var stationList = $('recm_st_'+RegExp.$1);
              ele.style.cursor   = 'pointer';
              ele['onmouseover'] = function(){oheya110.fnOpen(stationList);};
              ele['onmouseout']  = function(){oheya110.fnClose(stationList);};
          }
      });
  },
  fnOpen: function(ele){
      Element.show(ele);
      ele.style.color = "black";
      ele.style.position = "absolute";
      ele.style.backgroundColor = "white";
      ele.style.borderStyle = 'solid';
      ele.style.borderWidth = '1px';
      ele.style.borderColor = '#ccc';
      ele.style.padding = '5px';
      ele.style.lineHeight = '120%';
      ele.style.width = '170px';
      ele.className="";
  },
  fnClose: function(ele){
      Element.hide(ele);
      ele.className="list";
  },
  
  send2mobile: function(p_id, email){
      if (email.length==0){
          alert('メールアドレスを入力してください');
          return null;
      }
      if (!confirm(email+'に送信してよろしいですか？')){
          return null;
      }
      var url = '/json.html';
      var param = 'action_json_send2mobile_property1=1&p_id='+p_id+'&email='+email;
      var ajax = new Ajax.Request(url, {
        method: 'post',
        parameters: param,
        onSuccess: function(httpobj,json){
            if (json.result==1){
                alert('送信しました');
                $A(document.getElementsByClassName('emailform')).each(function(input){
                    input.value = email;
                });
            } else {
                alert('送信に失敗しました');
            }
        }
      });
  },
  
  send2favorite: function(email){
      if (email.length==0){
          alert('メールアドレスを入力してください');
          return null;
      }
      if (!confirm(email+'に送信してよろしいですか？')){
          return null;
      }
      var ajax = new Ajax.Request('/json.html',{
        method: 'post',
        parameters: 'action_json_send2mobile_favorite=1&email='+email,
        onSuccess: function(httpobj,json){
            if (json.result==1){
                alert('送信しました');
            }
        }
      });
      
  },
  
  showFavoriteQR: function(button){
      $('favoriteQR').src = '/images/loading_favoriteQR.gif';
      var ajax = new Ajax.Request('/json.html',{
        method: 'post',
        parameters: 'action_json_showfavoriteqr=1',
        onSuccess: function(httpobj,json){
            if (json.result==1){
                $('favoriteQR').src = json.qr_path;
                Element.hide(button);
            }
        }
      });
  }
};

oheya110 = new Oheya110;
Event.observe(window, "load", function(){
    if (typeof(oheya110)=='object'){
        oheya110.setMMswap2SearchButton();
    }
});

