$(function() {
	$("#quicknavi").organicTabs({
			"speed": 300
	});

	$("#tabmap").organicTabs({
			"speed": 100
	});

	if ($("#slideBox ul li").length > 1) {
		$("#slideBox").sudoSlider({ 
			auto: true,
			numeric: true,
			autoheight: false,
			prevNext: true,
			pause: "10000"	//待ち時間(ミリ秒)
		});
	}
	
	$(".toggle_container").hide();

	$("h4.trigger").click(function(){
		if ( $(this).hasClass("active") ){
			$(this).next().slideToggle("slow", function() {$(this).prev().removeClass("active")});
		} else {
			$(this).toggleClass("active").next().slideToggle("slow");
		}
		return false;
	});

});

function write_random_banner() {
	document.write('<iframe src="http://www.yonago-navi.jp/cgi-bin/advertise/index.cgi?max=1');
	document.write('&cookie=' + navigator.cookieEnabled);
	document.write('" height="250" width="240" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>');
}

function write_random2_banner() {
	document.write('<iframe src="http://www.yonago-navi.jp/cgi-bin/advertise/index.cgi?max=2');
	document.write('&cookie=' + navigator.cookieEnabled);
	document.write('" height="250" width="400" marginwidth="0" marginheight="0" scrolling="no" frameborder="0"></iframe>');
}
function runMovie(Src, Width, Height, Quality, Scale, flashVars) {
	if (!Scale) {
		Scale = 'noscale';
	}

	paramFlashVars = "";
	embedFlashVars = "";
	if (flashVars) {
		paramFlashVars = "<param name='flashVars' value='" + flashVars + "'>";
		embedFlashVars = " flashVars='" + flashVars + "'";
	}

	document.write(
		"<object classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000'"
		+ " width='" + Width + "'"
		+ " height='" + Height + "'"
		+ " codebase='//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0'>"
		+ "<param name='movie' value='" + Src + "'>"
		+ "<param name='quality' value='" + Quality + "'>"
		+ "<param name='menu' value='false'>"
		+ "<param name='wmode' value='transparent'>"
		+ paramFlashVars
		+ "<embed src='" + Src + "'"
		+ " quality='" + Quality + "'"
		+ " width='" + Width + "'"
		+ " height='" + Height + "'"
		+ " scale='" + Scale + "'"
		+ embedFlashVars
		+ " menu='false' wmode='transparent' pluginspage='//www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash'></embed></object>"
	);
}

// ここからはページ内Google Mapで使う関数
function addMarkerForContents(data, map) {
//  if (data.lat == null || data.lng == null) return;

  var marker = new GMarker(new GLatLng(data.lat, data.lng));

  var content = createContentDivForContents('gmapWindow');

  content.appendChild(
    createContentDivForContents('gmapTitle', document.createTextNode(data.title))
  );

  if (data.description || data.map_text) {
    var t = createContentDivForContents('gmapText');

/*
    if (data.description) {
      var description = data.description;
      t.innerHTML = '<p>' + description + '</p>';
    }
*/

    if (data.map_text) {
      var text = data.map_text;
      text = text.replace(/\r\n/g, "<br />");
      text = text.replace(/(\n|\r)/g, "<br />");
      t.innerHTML = t.innerHTML + text;
    }
    content.appendChild(t);
  }

  if (data.url) {
    var a = document.createElement('a');
    a.setAttribute('href', data.url);
    a.setAttribute('target', '_blank');
    a.appendChild(document.createTextNode('詳細ページ'));
    content.appendChild(createContentDivForContents(
      'gmapUrl',
      a
    ));
  }

  marker.bindInfoWindowHtml(content);
  map.addOverlay(marker);
  return marker;
}

function createContentDivForContents(classname, child) {
  var div = document.createElement('div');
  if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Trident/4.0")!=-1) {
    div.setAttribute('class', classname);
  }
  else {
    div.setAttribute((document.all ? 'className' : 'class'), classname);
  }

  if (child) {
    div.appendChild(child);
  }

  return div;
}

function adjustMapZoom(map, marker) {
  // 1.markerからgetLatLng();
  var point = marker.getLatLng();

  // 2.座標から住所を逆ジオコーディング
  var geocoder = new GClientGeocoder();
  geocoder.getLocations(point, function(res) {
    
  // 3.住所が米子市内かどうかチェック
    var insideYonago = false;
    $(res.Placemark).each(function(e) {
      place = res.Placemark[e];
      if (place.address.match(/鳥取県米子市/)) {
        insideYonago = true;
        return false;
      }
    });

    if (insideYonago) {
  // 4-1.市内なら特定のZoomLevelに変更する
      map.setZoom(11);
    } else {
  // 4-2.市街なら米子を地図の中心地として、markerが表示エリアに納まるまでZoomlevelを下げていく
      var yonagoCenterPoint = new GLatLng(35.42307973123601, 133.3363437652588);
      var yonagoWestPoint = new GLatLng(35.527439165742244, 133.20030212402344);
      var yonagoEastPoint = new GLatLng(35.46135040908259, 133.43891143798828);

      var yonagoPoint = yonagoCenterPoint;
      if (point.lng() < yonagoCenterPoint.lng()) {
        yonagoPoint = yonagoWestPoint;
      } else {
        yonagoPoint = yonagoEastPoint;
      }

      map.setCenter(yonagoPoint);
      while(true) {
        var zoomLv = map.getZoom();
        map.setZoom(zoomLv - 1);
        var bounds = map.getBounds();
        if (bounds.containsLatLng(point)) {
          return false;
        }
      }
    }
  });

  
}
