// JavaScript Document
//* Copyright 2007 office Sam Corporation. All Rights Reserved.
//*
//* dsplist.js
//*
//* Update: 2007.01.31
//* Author: M.Arashima

//*** Initialize Section ***

var loc_id = 1;
var plc_id = 0;
var type = 0;
var mode = "php";
var xmlLocation = "locations.php";
var xmlPlace = "places.php?loc_id=" + loc_id + "&plc_id=" + plc_id;

var alatitude = new Array();
var alongitude = new Array();
var ahtmlinfo01 = new Array();
var ahtmlinfo02 = new Array();
var ahtmlinfo03 = new Array();
	
if( document.select_location.mode.value != "") {
    mode = document.select_location.mode.value;
}

if( document.select_location.type.value != "") {
    type = parseFloat(document.select_location.type.value);
}

function startInt() {

  setInt(latitude , longitude , zoom);
  setLocationlist("locations-list");
  setPlacelist("place-list");

}

//*** Initialize Routine ***

function setInt(latitude , longitude , zoom) {

  map.setCenter(new GLatLng( latitude , longitude ), zoom);
  map.clearOverlays();

  switch(mode){
	case "php":
	  xmlLocation = "locations.php";
	  switch(type){
		case 1:
		  xmlPlace = "places.php?loc_id=" + loc_id + "&plc_id=" + plc_id;
		  break;
		case 2:
		case 3:
		  xmlPlace = "places_house.php?loc_id=" + loc_id + "&plc_id=" + plc_id + "&type=" + type;
		  break;
		default:
		  alert("php-type:" + type);
		  break;
	  }
	  break;
	case "xml":
	  xmlLocation = "xml/locations.xml";
	  switch(type){
		case 1:
		  xmlPlace = "xml/place" + loc_id + ".xml";
		  break;
		case 2:
		case 3:
		  xmlPlace = "xml/place_h_" + loc_id + ".xml";
		  break;
		default:
		  alert("xml-type:" + type);
		  break;
	  }
	  break;
	default:
	  alert("mode:" + mode);
	  break;
  }

}

//*** Location Routine ***

function setLocation() {

  if( document.select_location.loc_id.length) { // 複数
	for( var i=0; i < document.select_location.loc_id.length; i++ ) {
	  if(document.select_location.loc_id[i].checked) {
		loc_id = document.select_location.loc_id[i].value;
	  }
	}
  }
  else {
	if(document.select_location.loc_id.checked) {
	  loc_id = document.select_location.loc_id.value;
	}
  }
  plc_id = 0;
//  var obj_latitude = new Object("document.select_location.latitude" + loc_id);
//  var obj_longitude = new Object("document.select_location.longitude" + loc_id);
//  var obj_zoom = new Object("document.select_location.zoom" + loc_id);
//  if( obj_latitude.value != "") {
//    var latitude = parseFloat(obj_latitude.value);
//  }
//  if( obj_longitude.value != "") {
//    var longitude = parseFloat(obj_longitude.value);
//  }
//  if( obj_zoom.value != "") {
//    var zoom = parseFloat(obj_zoom.value);
//  }
  setInt(latitude , longitude , zoom);
  setPlacelist("place-list");
}

//*** Location List Display ***

function setLocationlist(id) {

  var htmllist = "";

  var xmlreq = GXmlHttp.create();
  xmlreq.open("GET", xmlLocation, true);
  xmlreq.onreadystatechange = function() {
  if (xmlreq.readyState == 4) {
	var xmlDoc = xmlreq.responseXML;
	var location = xmlDoc.documentElement.getElementsByTagName("location");
	if (location.length != 0) {
	  for( var i=0; i < location.length ; i++ ) {

		var loc_id = "";
		var name = "";
		var latitude = "";
		var longitude = "";
		var zoom = "";

		var loc_id = parseFloat(location[i].getAttribute("id"));
		if( location[i].getElementsByTagName("name").length != 0 ) {
		  var name = location[i].getElementsByTagName("name")[0].firstChild.nodeValue;
		}
		if( location[i].getElementsByTagName("latitude").length != 0 ) {
		  var latitude = location[i].getElementsByTagName("latitude")[0].firstChild.nodeValue;
		}
		if( location[i].getElementsByTagName("longitude").length != 0 ) {
		  var longitude = location[i].getElementsByTagName("longitude")[0].firstChild.nodeValue;
		}
		if( location[i].getElementsByTagName("zoom").length != 0 ) {
		  var zoom = location[i].getElementsByTagName("zoom")[0].firstChild.nodeValue;
		}

		if( i == 0 ) {
		  var htmllist = "<table border='0' cellspacing='0' cellpadding='0'>";
		  htmllist += "<colgroup class='sellect'></colgroup>";
		  htmllist += "<colgroup class='name'></colgroup>";
		  htmllist += "<thead>";
		  htmllist += "<tr><th>選択</th><th>地域</th></tr>";
		  htmllist += "</thead>";
		  htmllist += "<tbody>";
		}
		htmllist += "<tr><td>";
		htmllist += "<input type='hidden' name='latitude" + loc_id + "' value='" + latitude + "' />";
		htmllist += "<input type='hidden' name='longitude" + loc_id + "' value='" + longitude + "' />";
		htmllist += "<input type='hidden' name='zoom" + loc_id + "' value='" + zoom + "' />";
		htmllist += "<input id='' name='loc_id' type='radio' value='" + loc_id + "' onclick='setLocation();' />" + "</td>";
		htmllist += "<td>" + name + "</td></tr>";
	  }
	  htmllist += "</tbody>";
	  htmllist += "</table>";
	} else {
	  htmllist = "<p>該当する地域がありません。</p>";
	}
	document.getElementById(id).innerHTML = htmllist;
  }
  }
  xmlreq.send(null);

  return xmlreq;
}

//*** Marker Add Display ***

function addMarker(point, htmlinfo01, htmlinfo02, htmlinfo03) {
  var marker = new GMarker(point);
  if( htmlinfo01 != "" ) {
	GEvent.addListener(marker, "click", function() {
	tabList = [
	new GInfoWindowTab("物件", htmlinfo01),
	new GInfoWindowTab("概要", htmlinfo02),
	new GInfoWindowTab("お問い合わせ", htmlinfo03)
	]
	marker.openInfoWindowTabsHtml(tabList);
	});
  }
  map.addOverlay(marker);

  return marker;
}

//*** Marker Change Display ***

function chgMarker(no) {

  var point = new GPoint( alongitude[no] , alatitude[no] );
  var marker = new GMarker(point);
  GEvent.addListener(marker, "click", function() {
  tabList = [
  new GInfoWindowTab("物件", ahtmlinfo01[no]),
  new GInfoWindowTab("概要", ahtmlinfo02[no]),
  new GInfoWindowTab("お問い合わせ", ahtmlinfo03[no])
  ]
  marker.openInfoWindowTabsHtml(tabList);
  });
  map.addOverlay(marker);
  GEvent.trigger(marker, "click" , "" , point);

  return marker;
}

//*** Plasce List Display ***

function setPlacelist(id) {

  var htmllist = "";
  var nflg = 0;

  var xmlreq = GXmlHttp.create();
  xmlreq.open("GET", xmlPlace, true);
  xmlreq.onreadystatechange = function() {
	if (xmlreq.readyState == 4) {
	  var xmlDoc = xmlreq.responseXML;
	  if (xmlDoc != null) {
	  var place = xmlDoc.documentElement.getElementsByTagName("place");
	  if (place.length != 0) {
		for( var i=0; i < place.length ; i++ ) {

		  var latitude = "";
		  var longitude = "";
		  var zoom = "";
		  var point = "";
		  var address = "";
		  var price = "";
		  var area01 = "";
		  var area02 = "";
		  var sum01 = "";
		  var sum02 = "";
		  var yoto = "";
		  var kenpei = "";
		  var yoseki = "";
		  var chimoku = "";
		  var torihiki = "";
		  var note01 = "";
		  var note02 = "";
		  var link = "";
		  var image01 = "";
		  var image02 = "";

		  var kozo = "";
		  var ggyymm = "";

		  var latitude = parseFloat(place[i].getAttribute("latitude"));
		  var longitude = parseFloat(place[i].getAttribute("longitude"));
		  var zoom = parseFloat(place[i].getAttribute("zoom"));
		  if ( type != 1 ) {
			var dbtype = parseFloat(place[i].getAttribute("type"));
		  }

		  if ( type != 1 && type != dbtype ) {
			  continue;
		  }

		  var point    = new GPoint( longitude, latitude );

		  nflg += 1;

		  var htmlinfo01 = "<div id='gmap-tab01'>";
		  if( place[i].getElementsByTagName("address").length != 0 ) {
			if( place[i].getElementsByTagName("image01").length != 0 ) {
			  var image01 = place[i].getElementsByTagName("image01")[0].firstChild.nodeValue;
			  if( place[i].getElementsByTagName("image02").length != 0 ) {
				var image02 = place[i].getElementsByTagName("image02")[0].firstChild.nodeValue;
				htmlinfo01 += "<p id='tab01-01'><a href='img/" + image02 + "' rel='lightbox[gmap-tab01]' title='" + address + "' target='_blank'>";
				htmlinfo01 += "<img src='img/" + image01 + "' alt='" + address + "' />";
				htmlinfo01 += "</a></p>";
			  } else {
				htmlinfo01 += "<p id='tab01-01'><img src='img/" + image01 + "' alt='" + address + "' /></p>";
			  }
			}
			var address = place[i].getElementsByTagName("address")[0].firstChild.nodeValue;
			htmlinfo01 += "<p id='tab01-02'>" + address + "</b><br />";
		  }
		  if( place[i].getElementsByTagName("price").length != 0 ) {
			var price = place[i].getElementsByTagName("price")[0].firstChild.nodeValue;
			htmlinfo01 += price + "<br />";
		  }
		  if( place[i].getElementsByTagName("area01").length != 0 ) {
			var area01 = place[i].getElementsByTagName("area01")[0].firstChild.nodeValue;
			htmlinfo01 += "土地面積：" + area01 + "m<sup>2</sup>";
			var sum01 = parseFloat(area01) * 0.3025 * 100
			var sum01 = Math.floor(sum01)
			var sum01 = sum01 / 100
			htmlinfo01 += "（" + sum01 + "坪）";
		  }

		  if ( type != 1 ) {
			if( place[i].getElementsByTagName("area02").length != 0 ) {
			  var area02 = place[i].getElementsByTagName("area02")[0].firstChild.nodeValue;
			  htmlinfo01 += "<br />" + "述床面積：" + area02 + "m<sup>2</sup>";
			  var sum02 = parseFloat(area02) * 0.3025 * 100
			  var sum02 = Math.floor(sum02)
			  var sum02 = sum02 / 100
			  htmlinfo01 += "（" + sum02 + "坪）";
			}
		  }

		  if( place[i].getElementsByTagName("link").length != 0 ) {
			var link = place[i].getElementsByTagName("link")[0].firstChild.nodeValue;
			htmlinfo01 += "</p><p id='tab01-03'><a href='" + link + "'>詳しくはこちら</a>";
		  }

		  htmlinfo01 += "</p></div>";

		  var htmlinfo02 = "<div id='gmap-tab02'>";
		  htmlinfo02 += "<table border='0' cellspacing='0' cellpadding='0'>";

		  if ( type != 1 ) {
			if( place[i].getElementsByTagName("chimoku").length != 0 ) {
			  var chimoku = place[i].getElementsByTagName("chimoku")[0].firstChild.nodeValue;
			  htmlinfo02 += "<tr><td>地目</td><td>" +  chimoku + "</td></tr>";
}
			if( place[i].getElementsByTagName("kozo").length != 0 ) {
			  var kozo = place[i].getElementsByTagName("kozo")[0].firstChild.nodeValue;
			  htmlinfo02 += "<tr><td>構造</td><td>" +  kozo + "</td></tr>";
			}
			if( place[i].getElementsByTagName("ggyymm").length != 0 ) {
			  var ggyymm = place[i].getElementsByTagName("ggyymm")[0].firstChild.nodeValue;
			  htmlinfo02 += "<tr><td>建築年月</td><td>" +  ggyymm + "</td></tr>";
			}
		  }

		  if( place[i].getElementsByTagName("yoto").length != 0 ) {
			var yoto = place[i].getElementsByTagName("yoto")[0].firstChild.nodeValue;
			htmlinfo02 += "<tr><td>用途地域</td><td>" +  yoto + "</td></tr>";
		  }
		  if( place[i].getElementsByTagName("kenpei").length != 0 ) {
			var kenpei = place[i].getElementsByTagName("kenpei")[0].firstChild.nodeValue;
			htmlinfo02 += "<tr><td>建ぺい率</td><td>" +  kenpei + "%</td></tr>";
		  }
		  if( place[i].getElementsByTagName("yoseki").length != 0 ) {
			var yoseki = place[i].getElementsByTagName("yoseki")[0].firstChild.nodeValue;
			htmlinfo02 += "<tr><td>容積率</td><td>" +  yoseki + "%</td></tr>";
		  }

		  if ( type == 1 ) {
			if( place[i].getElementsByTagName("chimoku").length != 0 ) {
			  var chimoku = place[i].getElementsByTagName("chimoku")[0].firstChild.nodeValue;
			  htmlinfo02 += "<tr><td>地目</td><td>" +  chimoku + "</td></tr>";
			}
		  }

		  if( place[i].getElementsByTagName("torihiki").length != 0 ) {
			var torihiki = place[i].getElementsByTagName("torihiki")[0].firstChild.nodeValue;
			htmlinfo02 += "<tr><td>取引態様</td><td>" +  torihiki + "</td></tr>";
		  }
		  if( place[i].getElementsByTagName("note01").length != 0 ) {
			var note01 = place[i].getElementsByTagName("note01")[0].firstChild.nodeValue;
			htmlinfo02 += "<tr><td>備考</td><td>" +  note01;
		  }
		  if( place[i].getElementsByTagName("note02").length != 0 ) {
			var note02 = place[i].getElementsByTagName("note02")[0].firstChild.nodeValue;
			htmlinfo02 += "<br />" +  note02 + "</td></tr>";
		  }
		  htmlinfo02 += "</table>";
		  htmlinfo02 += "</div>";

		  var htmlinfo03 = "<div id='gmap-tab03'>";
		  htmlinfo03 += "<h2>お問い合わせ先</h2>";
		  htmlinfo03 += "<p><a href='../inquiry/index.html'>こちら</a>をクリックしてください。</p>";
		  htmlinfo03 += "</div>";

		  alatitude[i] = latitude;
		  alongitude[i] = longitude;
		  ahtmlinfo01[i] = htmlinfo01;
		  ahtmlinfo02[i] = htmlinfo02;
		  ahtmlinfo03[i] = htmlinfo03;

		  if( nflg == 1 ) {
			map.setCenter(new GLatLng( latitude , longitude ), zoom);
			map.clearOverlays();
		  }
		  addMarker( point, htmlinfo01, htmlinfo02, htmlinfo03 );

		  if( nflg == 1 ) {
			var htmllist = "<table border='0' cellspacing='0' cellpadding='0'>";
			htmllist += "<colgroup class='address'></colgroup>";
			htmllist += "<colgroup class='price'></colgroup>";
			htmllist += "<colgroup class='area01'></colgroup>";
			if ( type != 1 ) {
			  htmllist += "<colgroup class='area02'></colgroup>";
			}
			htmllist += "<thead>";
			if ( type == 1 ) {
			  htmllist += "<tr><th>所在地</th><th>価格</th><th>土地面積</th></tr>";
			} else {
			  htmllist += "<tr><th>所在地</th><th>価格</th><th>土地面積</th><th>述床面積</th></tr>";
			}
			htmllist += "</thead>";
			htmllist += "<tbody>";
		  }
		  htmllist += "<tr><td>" + "<a href='#' onClick='chgMarker(" + i + ")'>" + address + "</a></td><td>"
		  if ( type == 1 ) {
			htmllist += price + "</td><td>" +  area01 + "m<sup>2</sup><br />（" + sum01 + "坪）" + "</td></tr>";
		  } else {
			htmllist += price + "</td><td>" +  area01 + "m<sup>2</sup><br />（" + sum01 + "坪）" + "</td><td>" +  area02 + "m<sup>2</sup><br />（" + sum02 + "坪）" + "</td></tr>";
		  }
		}
		if ( nflg > 0 ) {
		  htmllist += "</tbody>";
		  htmllist += "</table>";
		} else {
		  htmllist = "<p>該当する不動産情報がありません。</p>";
		}
	  } else {
		htmllist = "<p>該当する不動産情報がありません。</p>";
	  }
	  document.getElementById(id).innerHTML = htmllist;
	  } else {
		htmllist = "<p>該当する不動産情報がありません。</p>";
	  }
	  document.getElementById(id).innerHTML = htmllist;
	}
  }
  xmlreq.send(null);

  return xmlreq;
}

//
