_.onWinReseize = null;
_.mapViewFit = function(){
  if(_.onWinReseize) return;
  _.onWinReseize = 1;
  setTimeout(function(){
    if(_.$('checkSv') && _.$('checkSv').checked){
      _.mapSvFit();
    }else{
      _.mapfit();
    }
    _.onWinReseize = null;
  },500);
}

_.mapfit = function(callback){
  var el = $('#map').offset();
  if(el.top > 220) return;
  var h = $(window).height();
  var nh = h - el.top;
  $('#mapwrapper,#map,#mapside').css('height',nh + 'px');
  if(callback) callback();
}

_.mapSideReload = function(v){
  if(!v){
    var c = _.mapCookie();
    v = c['parse'];
  }
  if(v == 'scr'){
    _.mapReload();
  }else if(v == 'tag'){
    _.get('map|side_tag',null,{ds:'mapside'});
  }else if(v == 'full'){
    return;
  }else{
    mapSidePage(1);
  }
}


_.mapSideChg = function(v){
  _.cookie.grp('map',v,4);
  _.mapSideReload(v)
}

_.mapCookieChg = function(el,no){
  var v = el.value;
  if(v == 'sep') return;
  _.cookie.grp('map',v,no);
  _.mapReload();
  if(no == 5){
    el.className = (!v) ? '' : 'bgF';
  }else if(no == 6){
    el.className = (v == -1) ? 'bgF' : '';
  }
  if(no != 6) _.mapSideReload();
}

_.mapTagChg = function(v){
  if(!v) v = '';
  _.cookie.grp('map',encodeURIComponent(v),7);
  _.mapReload();
  _.mapSideReload();
  if(!v){
    $('#msg').empty();
  }else{
    $('#msg').html('<em>' + decodeURIComponent(v) + '</em> 抽出中(<a href=\"javascript:_.mapTagChg()\">解除</a>)');
  }
}

_.toggleZoom = function(isChecked){
  if(isChecked){
    map.enableScrollWheelZoom()
  }else{
    map.disableScrollWheelZoom()
  }
}

_.mapFullChg = function(full){
  if($('#sidebar').css('display') != 'none' || full){
    _.cookie.grp('map','full',4);
    _.mapFullStyle();
    $('#mapSizeBtn').css('right','0');
    $('#msBtnW').removeClass('→').addClass('←')
  }else{
    _.cookie.grp('map','',4);
    $('#mapbody').css('padding-right','0');
    $('#mapSizeBtn').css('right','10px');
    $('#msBtnW').removeClass('←').addClass('→');
    $('#mainbar').animate({width:'65%'},{duration:300,complete:function(){
      $('#sidebar').fadeIn();
      _.mapSideReload();
      map.checkResize();
      _.centerMarker();
    }});
  }
  if(_.$('checkSv') && _.$('checkSv').checked){
    _.mapSvFit();
  }
}

_.mapFullStyle = function(){
  $('#sidebar').hide();
  $('#mapbody').css('padding-right','20px');
  $('#mainbar').animate({width:'100%'},{duration:300,complete:function(){
    if(map){
      map.checkResize();
      _.centerMarker();
    }
  }});
}

_.mapPrintChg = function(){
  if($('#header').css('display') != 'none'){
    $('#msBtnH').removeClass('↑').addClass('↓');
    _.mapFullChg(1);
  }else{
    $('#msBtnH').removeClass('↓').addClass('↑');
    _.mapFullChg();
  }
  $('#header,#status-menu').slideToggle(300,function(){
    _.mapViewFit();
  });
}

_.mapPrefNavi = function(v){
  v = v.split(',');
  _.mapPan(v[0],v[1]);
}

_.svHeight = 240;

_.svChgHeight = function(v){
  if(!v) return;
  _.svHeight += v;
  $('#pano').height(_.svHeight);
  _.mapSvFit();
}

_.mapSvFit = function(callback){
  var h = $(window).height();
  var m = $('#map').offset();
  var nh = h - m.top - _.svHeight;
  $('#map').css('height',nh + 'px');
  $('#mapside').css('height',(nh + _.svHeight) + 'px');
  $('#mapwrapper').animate({height:nh + 'px'},200,'linear',function(){
    $('#pano').empty().show();
    map.checkResize();
    if(callback){
      callback();
    }else if(_.sv.currentLatLng){
      _.panoClient.getNearestPanorama(_.sv.currentLatLng, _.showPanoData);
    }
  });
}

_.goDataDel = function(){
  var c = map.getCenter();
  location.href = _.BP + 'data/del?lat=' + c.y + '&lng=' + c.x;
}


_.ftip = document.createElement('div');
_.floatTips = function(el,txt,t,l){
  if(!el){
    _.ftip.style.visibility = 'hidden';
    _.ftip.style.width = 'auto';
    _.tglSlt(1);
    return;
  }
  document.body.appendChild(_.ftip);
  _.ftip.innerHTML = txt;
  var os = $(el).offset();
  if(!t) t = 25;
  if(!l) l = 0;
  _.ftip.className = 'tooltip';
  $(_.ftip).css({'position':'absolute','top':(os.top + t) + 'px','left':(os.left + l) + 'px','font-size':'92%','visibility':'visible'});
  var w = $(_.ftip).width();
  if(l != undefined) return;
  if(w > 240){
    $(_.ftip).css({'width':'240px','left':(os.left-210) + 'px'})
  }else{
    $(_.ftip).css({'left':(os.left - w + 30) + 'px'});
  }
  if(_.$('iconSlt').disabled) _.tglSlt();
}

_.circles = new Object();
_.polyCircle = function(point,km,id){
  var plat = parseFloat(point.y)
  var plng = parseFloat(point.x)
  if(!(km > 0)) var km = .3;
  var Cradius = km/1.6093
  var d2r = Math.PI/180
  var r2d = 180/Math.PI
  var Clat = (Cradius/3963) * r2d
  var Clng = Clat/Math.cos(plat*d2r)
  var Cpoints = [] ;
  for (var i = 0 ; i < 35 ; i++){
    var theta = Math.PI * (i/17) ;
    Cx = plng + (Clng * Math.cos(theta)) ;
    Cy = plat + (Clat * Math.sin(theta)) ;
    Cpoints.push(new GPoint(Cx,Cy)) ;
  }
  var circle = new GPolygon(Cpoints,'#7700bb',2,.2,'#990088',.3);
  _.circles[id] = circle;
  map.addOverlay(circle);
}


_.tglMarker = function(isChecked){
  for(var id in _.list){
    if(isChecked){
      map.removeOverlay(_.list[id]);
    }else{
      map.addOverlay(_.list[id]);
    }
  }
}

_.overviewmap;
_.showOverviewmap = function(){
  if(!_.overviewmap){
    _.overviewmap = new GOverviewMapControl(new GSize(140,140));
    map.addControl(_.overviewmap);
  }
}

_.tglMobMemo = function(isChecked){
  var v = (isChecked) ? 1 : '';
  _.cookie.grp('map',v,11);
  _.tglMarkerGroup('memo');
  if(_.CUR_ZOOM > _.WA_Z){
    _.parse_marker();
  }else{
    _.mapload();
  }
}

_.tglMarkerGroup = function(k){
  if(!_.markerGroup[k]) return;
  for (var i = 0; i < _.markerGroup[k].length; i++) {
    var marker = _.markerGroup[k][i];
    if(marker.isHidden()){
      marker.show();
    }else{
      marker.hide();
    }
  } 
}

_.sv = {};
_.sv.currentLatLng;
_.sv.currentYaw = 0;
_.sv.myPano;
_.guyMarker;
_.panoClient;

_.toggleSv = function(isChecked){
  if(isChecked){
    $('#svEx').show();
    _.mapSvFit(function(){
      _.panoClient = new GStreetviewClient();
      var svOverlay = new GStreetviewOverlay();
      map.addOverlay(svOverlay);
      
      _.sv.myPano = new GStreetviewPanorama(_.$('pano'));
      GEvent.addListener(_.sv.myPano,'error',_.handleNoFlash); 
      GEvent.addListener(_.sv.myPano, 'yawchanged', _.handleYawChange);
      GEvent.addListener(_.sv.myPano, 'initialized', _.handleInitialized);

      
      _.sv.clickListener = GEvent.addListener(map,'click',function(overlay,latlng) {
        if(_.guyMarker) map.removeOverlay(_.guyMarker);
        _.create_guyMarker(latlng);
        _.sv.currentLatLng = latlng;
        _.panoClient.getNearestPanorama(latlng, _.showPanoData);
      });
      
      if(_.c_marker) map.removeOverlay(_.c_marker);
    });
  }else{
    $('#svEx').hide();
    GEvent.removeListener(_.sv.clickListener);
    $('#pano').fadeOut(300,function(){
      _.sv.myPano = null;
      _.mapfit();
      _.mapReload();
    });
  }
}

_.create_guyMarker = function(latlng){
  var guyIcon = new GIcon(G_DEFAULT_ICON);
  guyIcon.image = "http://maps.gstatic.com/mapfiles/cb/man_arrow-0.png";
  guyIcon.transparent = "http://maps.gstatic.com/mapfiles/cb/man-pick.png";
  guyIcon.imageMap = [
        26,13, 30,14, 32,28, 27,28, 28,36, 18,35, 18,27, 16,26,
        16,20, 16,14, 19,13, 22,8
     ];
  guyIcon.shadow = null;
  guyIcon.iconSize = new GSize(49, 52);
  guyIcon.iconAnchor = new GPoint(25, 35);
  guyIcon.infoWindowAnchor = new GPoint(25, 5);

  _.guyMarker = new GMarker(latlng, {icon: guyIcon, draggable: true});
  map.addOverlay(_.guyMarker);
  
  GEvent.addListener(_.guyMarker,'dragend',function(){
    var latlng = _.guyMarker.getPoint();
    _.sv.currentLatLng = latlng;
    _.panoClient.getNearestPanorama(latlng, _.showPanoData);
  });
  
  return _.guyMarker;
}


_.showPanoData = function(panoData){
  $('#svEx').hide();
  _.sv.myPano.setLocationAndPOV(panoData.location.latlng, {yaw:_.sv.currentYaw, pitch:0});
}

_.handleInitialized = function(location){
  var latlng = location.latlng;
  if(latlng != _.sv.currentLatLng){
    _.sv.currentLatLng = latlng;
    _.guyMarker.setLatLng(latlng);
    var b = _.mapBounds();
    if(latlng.x < b[3] || latlng.x > b[1] || latlng.y < b[2] || latlng.y > b[0]){
      map.panTo(latlng);
    }
  }
}

_.handleYawChange = function(newYaw){
  var GUY_NUM_ICONS = 16;
  var GUY_ANGULAR_RES = 360/GUY_NUM_ICONS;
  if (newYaw < 0) {
    newYaw += 360;
  }
  _.sv.currentYaw = newYaw;
  guyImageNum = Math.round(newYaw/GUY_ANGULAR_RES) % GUY_NUM_ICONS;
  guyImageUrl = "http://maps.gstatic.com/mapfiles/cb/man_arrow-" + guyImageNum + ".png";
  _.guyMarker.setImage(guyImageUrl);
}


_.handleNoFlash = function(errorCode) {
  if (errorCode == FLASH_UNAVAILABLE) {
    alert('Error: Flashが必要です');
    return;
  }
}


_.mapPro = {
  sarr: function(){
    return ['orsrc','photo','posttime','poffice','pnum','com','num','span','tag'];
  },
  
  chg: function(isChecked){
    if(isChecked){
      _.cookie.grp('map','pro',8);
      _.$('iconSlt').disabled = true;
      //alert($('#probar').html());
      if($('#probar').html() != ''){
        $('#probar').slideDown(400,function(){
          _.mapViewFit();
          _.mapPro.srch(_.$('mapProFrm'));
        });
      }else{
        _.get('temp|proNavi',null,{ds:'probar',ef:'slide'},function(){
          _.mapPro.highlight();
          _.mapViewFit();
          _.mapPro.srch(_.$('mapProFrm'));
        });
      }
    }else{
      $('#mkCnt').hide();
      _.$('iconSlt').disabled = false;
      $('#probar').slideUp(300,function(){
        _.mapViewFit();
        _.cookie.grp('map','',8);
        if(_.$('circleChk')) _.$('circleChk').checked = false ;
        if(_.$('cleOpt')) $('#cleOpt').css('visibility','hidden');
        _.mapReload();
      });
    }
    
  },

  srch: function(f){
    if(!f) return;
    var ic = f.elements['ic'];
    var c = new Array;
    for(i=0; i < ic.length; i++){
      if(ic[i].checked) c.push(ic[i].value);
    }
    var s = this.sarr();
    var pro = [c]
    for(key in s){
      if(f.elements[s[key]]) pro.push(f.elements[s[key]].value);
    }
    
    _.cookie.grp('map',pro.join(':'),9);
    this.highlight();
    _.mapReload();
  },
  
  rst: function(){
    var f = document.proSrc;
    var ic = f.elements['ic'];
    var c = new Array;
    for(i=0; i < ic.length; i++){
      if(ic[i].checked) c.push(ic[i].value);
    }
    var s = this.sarr();
    for(key in s){
      if(f.elements[s[key]]) f.elements[s[key]].value = '';
    }
    _.cookie.grp('map',c,9);
    this.highlight();
    _.mapReload();
  },
  
  highlight: function(){
    var f = document.proSrc;
    if(!f) return;
    var s = _.mapPro.sarr();
    for(key in s){
      if(f.elements[s[key]]){
        var cls = (f.elements[s[key]].value != '') ? 'bgF' : '';
        f.elements[s[key]].className = cls;
      }
    }
  },
  
  allchk: function(clr){
    var f = document.proSrc;
    var ic = f.elements['ic'];
    var c = new Array;
    for(i=0; i < ic.length; i++){
      ic[i].checked = clr ? true : false;
    }
  },
  
  cle: function(clr){
    if(!clr & _.KEYS.length > 300){
      _.mapload();
      return;
    }
    for(k in _.KEYS){
      id = _.KEYS[k];
      if(_.circles[id]){
        map.removeOverlay(_.circles[id]);
        _.circles[id] = undefined;
      }
      if(!clr){
        point = _.list[id].getPoint();
        km = (_.$('circleSlt')) ? _.$('circleSlt').value : 0;
        _.polyCircle(point,km,id);
      }
    }
  }
}

_.tglMapAds = function(isChecked){
  if(isChecked){
    if(!_.ADS_M){
      _.mapAds();
    }else{
      _.ADS_M.enable();
    }
  }else{
    _.ADS_M.disable()
  }
}

_.mapAds = function(){
  _.ADS_M = new GAdsManager(map,_.ADS_ID,{
   maxAdsOnMap: 3,
   style: 'adunit',
   channel: '0896180627'
  });
  _.ADS_M.enable();
}

//google.load('search', '1');
_.goGeocation = function(){
  var g = google.loader.ClientLocation;
  if(!g || !g.latitude || !g.longitude) return;
  _.mapPan(g.latitude,g.longitude);
}