var map;
var mapElem;
var data = new Array();
var maker = new Array();

var img_path = "img/immovables";


function load(itemid,areaid) {
	
	CreateMap(itemid,areaid);
	
}


function CreateMap(itemid,areaid) {
	
  if (GBrowserIsCompatible()) {
    
    mapElem = document.getElementById("map");
    /*	Mapの作成	*/
    map = new GMap2(mapElem);
    //map.setCenter(new GLatLng(35.417399143664035, 136.75156831741333), 19, G_NORMAL_MAP);

    /*	Mapコントロールの設定	*/
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.addControl(new GScaleControl());
    map.addControl(new GOverviewMapControl());

		/*	マーカーアイコン初期設定	*/
	  var pointer2b = new GIcon();
	  pointer2b.image ="../img/pointer2b.png";
	  pointer2b.shadow ="../img/pointer2s.png";
	  pointer2b.iconSize = new GSize(24,38);
		pointer2b.shadowSize = new GSize(24,38);
		pointer2b.iconAnchor = new GPoint(12,38);
		pointer2b.infoWindowAnchor = new GPoint(12,1);
	  var pointer2c = new GIcon();
	  pointer2c.image ="../img/pointer2c.png";
	  pointer2c.shadow ="../img/pointer2s.png";
	  pointer2c.iconSize = new GSize(24,38);
		pointer2c.shadowSize = new GSize(24,38);
		pointer2c.iconAnchor = new GPoint(12,38);
		pointer2c.infoWindowAnchor = new GPoint(12,1);
		
		/*	データ読み込み	*/
		ReadData();
		
    /*	中心点を設定	*/
		map.setCenter(new GLatLng(35.413886372091646, 136.7519760131836), 14, G_NORMAL_MAP);

    /*	ポインタ表示	*/
		for (i=0; i<data.length; i++) {
			maker[i] = CreateInfoPointer(data[i],pointer2b,pointer2c);
			map.addOverlay(maker[i]);
    }
    /*	リストを表示	*/
		ViewList(areaid,-1)
		
		/*	物件IDが指定されている場合は物件を表示	*/
		if (itemid != "-1") {
			OpenListInfo(getUqID(itemid));
		}
    
  }
}

/*	絞り込み処理（）	*/
function LimitArea(distance) {
	ViewList(-1,distance);
}

function ViewList(areaid,distance) {
  
  var list_html;
  list_html = "<table><tr><td class='list_id_title'>No.</td><td class='list_type_title'>種別</td><td class='list_name_title'>物件名</td><td class='list_address_title'>所在地</td><td class='list_price_title'>賃料</td></tr>";
	for (i=0; i<data.length; i++) {
		if (data[i].getAttribute("areaid") == areaid || areaid == -1) {
			
			/*	距離の算出	*/
			var interval = parseInt(maker[i].getPoint().distanceFrom(map.getCenter()));
			
			if (interval <= distance || distance == -1) {
				var id = data[i].getAttribute("areaid") + ("00" + data[i].getAttribute("id")).substr("00" + data[i].getAttribute("id").length,2);
				list_html += "<tr><td class='list_id_class' id='list_id_id" + i + "'>" + id + "</td><td class='list_type_class' id='list_type_id" + i + "'>" + data[i].getAttribute("type") +"</td><td class='list_name_class' id='list_name_id" + i + "'><a href='javascript:OpenListInfo(" + i + ");'>" + data[i].getAttribute("name") +"</a></td><td class='list_address_class' id='list_address_id" + i + "'>" + data[i].getAttribute("address") +"</td><td class='list_price_class' id='list_price_id" + i + "'>" + data[i].getAttribute("price") +"</td></tr>";
			}
		}
	}
	list_html += "</table>";
  document.getElementById("list").innerHTML = list_html;

	/*
	for (i=0; i<data.length; i++) {
		if (data[i].getAttribute("areaid") == areaid || areaid == -1) {
			//	距離の算出
			var interval = parseInt(maker[i].getPoint().distanceFrom(map.getCenter()));
			if (interval <= distance || distance == -1) {
				var id = data[i].getAttribute("areaid") + ("00" + data[i].getAttribute("id")).substr("00" + data[i].getAttribute("id").length,2);
				
				if (data[i].getAttribute("space") <= 0) {
					document.getElementById("list_id_id" + i).style.backgroundColor = "#ffccff";
					document.getElementById("list_name_id" + i).style.backgroundColor = "#ffccff";
					document.getElementById("list_space_id" + i).style.backgroundColor = "#ffccff";
					document.getElementById("list_price_id" + i).style.backgroundColor = "#ffccff";
					document.getElementById("list_distance_id" + i).style.backgroundColor = "#ffccff";
				}
			}
		}
	}
	*/

}

/*	データ読み込み	*/
function ReadData() {
  var temp = new Array();
  
  /*	データ読み込み	*/
	httpObj = GXmlHttp.create();
	httpObj.open("get", "../immovables.xml",false);
	httpObj.send(null);
  
  temp = new Array(httpObj.responseXML.getElementsByTagName("item").length);
	temp = httpObj.responseXML.getElementsByTagName("item");
	
	/*	非表示要素の削除	*/
	for (i=0; i<temp.length; i++) {
		if (temp[i].getAttribute("hidden") == "0") {
			data.push(temp[i]);
		}
	}

	maker = new Array(data.length);
	

}

/*	Infoウィンドウの作成	*/
function CreateInfoPointer(rd,pointer2b,pointer2c) {
	var mk;
	
	switch (rd.getAttribute("areaid").substr(0, 1)) {
	case "B":
		temp_pointer = pointer2b;
		break;
	case "C":
		temp_pointer = pointer2c;
		break;
	default:
		temp_pointer = pointer2b;
		break;
	}
	var lat = parseFloat(rd.getAttribute("lat"));
	var lon = parseFloat(rd.getAttribute("lon"));

	mk = new GMarker(new GLatLng(lat, lon),{icon:temp_pointer});	//マーカーを指定されたポイントに表示
	
	var id = rd.getAttribute("areaid") + ("00" + rd.getAttribute("id")).substr("00" + rd.getAttribute("id").length,2);
	var tmp_photo = new Image();
	var tmp_layout = new Image();
	tmp_photo.src = img_path + "/photo/thumbnail/photo" + id + ".jpg";
	tmp_layout.src = img_path + "/layout/thumbnail/layout" + id + ".jpg";
	
	GEvent.addListener(mk, "click", function() {
		ViewInfo(mk,rd,tmp_photo,tmp_layout);
	});
	/*
	GEvent.addListener(mk, "mouseover", function() {
		ViewInfoWindow(mk,rd);
	});
	*/
	return mk;
}


function ViewInfo(mk,rd,tmp_photo,tmp_layout) {

	var id = rd.getAttribute("areaid") + ("00" + rd.getAttribute("id")).substr("00" + rd.getAttribute("id").length,2);

	/*	Infoウィンドウ	*/
	var info_html = "<div class='iw'>No.<span id='iw_id'>" + id + "</span><br><span id='iw_name'>" + rd.getAttribute("name") + "</span><br>賃料：<span id='iw_price'>" + rd.getAttribute("price") + "</span></div>";
	
	mk.openInfoWindowHtml(info_html);
	/*	詳細情報の作成	*/
	
	
	var from = map.getCenter();
	
	/*
  map.setCenter(new GLatLng(35.4146470848379, 136.750769019126), 19, G_NORMAL_MAP);
	alert(mk.getPoint().distanceFrom(map.getCenter()));
	LimitArea(100);
	*/

	document.getElementById("info_id").innerHTML = id;

	document.getElementById("info_type").innerHTML = rd.getAttribute("type");
	document.getElementById("info_name").innerHTML = rd.getAttribute("name");
	document.getElementById("info_address").innerHTML = rd.getAttribute("address");
	document.getElementById("info_price").innerHTML = rd.getAttribute("price");
	document.getElementById("info_public_cost").innerHTML = rd.getAttribute("public_cost");
	document.getElementById("info_parking").innerHTML = rd.getAttribute("parking");
	document.getElementById("info_deposit").innerHTML = rd.getAttribute("deposit");
	document.getElementById("info_reward").innerHTML = rd.getAttribute("reward");
	document.getElementById("info_brokerage_fee").innerHTML = rd.getAttribute("brokerage_fee");

	document.getElementById("info_comment").innerHTML = rd.getAttribute("comment").replace("\n","<br>");
	document.getElementById("info_nb").innerHTML = rd.getAttribute("nb").replace("\n","<br>");
	
	document.getElementById("info_photo").src = "";
	document.getElementById("info_layout").src = "";
//alert(tmp_photo.width);
	if (tmp_photo.width > 28) {
		document.getElementById("info_photo").src = tmp_photo.src;
		document.getElementById("info_photo_big").href = img_path + "/photo/photo" + id + ".jpg";
	} else {
		document.getElementById("info_photo").src = "img/blank.jpg";
	}
	if (tmp_layout.width > 28) {
		document.getElementById("info_layout").src = tmp_layout.src;
		document.getElementById("info_layout_big").href = img_path + "/layout/layout" + id + ".jpg";
	} else {
		document.getElementById("info_layout").src = "img/blank.jpg";
	}
	
}


function OpenListInfo(uqid) {
	
	if (uqid != -1) {
		var rd = data[uqid];
		var mk = maker[uqid];
		
		var id = rd.getAttribute("areaid") + ("00" + rd.getAttribute("id")).substr("00" + rd.getAttribute("id").length,2);
		var tmp_photo = new Image();
		var tmp_layout = new Image();
		tmp_photo.src = img_path + "/photo/thumbnail/photo" + id + ".jpg";
		tmp_layout.src = img_path + "/layout/thumbnail/layout" + id + ".jpg";

		ViewInfo(mk,rd,tmp_photo,tmp_layout);
		
		window.scrollTo(0, 400);
	}
}

function getUqID(itemid) {
	
	for (i=0; i<data.length; i++) {
		var id = data[i].getAttribute("areaid") + ("00" + data[i].getAttribute("id")).substr("00" + data[i].getAttribute("id").length,2);
		if (id == itemid) {
			return i;
			break;
		}
	}
	
	return -1;
	
}
