var map;
var mapElem;
var data = new Array();
var maker = new Array();

var img_path = "img/parking";


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 pointer_man = new GIcon();
	  pointer_man.image ="../img/pointer_man.png";
	  pointer_man.shadow ="../img/pointer_shadow.png";
	  pointer_man.iconSize = new GSize(38,60);
		pointer_man.shadowSize = new GSize(38, 67);
		pointer_man.iconAnchor = new GPoint(19, 60);
		pointer_man.infoWindowAnchor = new GPoint(19, 1);
	  var pointer_aki = new GIcon();
	  pointer_aki.image ="../img/pointer_aki.png";
	  pointer_aki.shadow ="../img/pointer_shadow.png";
	  pointer_aki.iconSize = new GSize(38,60);
		pointer_aki.shadowSize = new GSize(38, 67);
		pointer_aki.iconAnchor = new GPoint(19, 60);
		pointer_aki.infoWindowAnchor = new GPoint(19, 1);
		
		/*	データ読み込み	*/
		ReadData();
		
    /*	エリア毎に中心点を設定	*/
    switch (areaid) {
		case 1:
			//map.setCenter(new GLatLng(35.411665399536396, 136.75296306610107), 16, G_NORMAL_MAP);
			map.setCenter(new GLatLng(35.41390385982164, 136.75013065338135), 17, G_NORMAL_MAP);
			
			break;
		case 2:
			map.setCenter(new GLatLng(35.416282155743815, 136.75013065338135), 17, G_NORMAL_MAP);
			break;
		case 3:
			map.setCenter(new GLatLng(35.41882650490712, 136.7498517036438), 17, G_NORMAL_MAP);
			break;
		case 4:
			map.setCenter(new GLatLng(35.41591492345623, 136.74116134643555), 16, G_NORMAL_MAP);
			break;
		case 5:
			map.setCenter(new GLatLng(35.420601381317816, 136.76377773284912), 15, G_NORMAL_MAP);
			break;
		case 6:
			map.setCenter(new GLatLng(35.406698593754456, 136.75573110580444), 16, G_NORMAL_MAP);
			break;
		case 7:
			map.setCenter(new GLatLng(35.413886372091646, 136.7519760131836), 14, G_NORMAL_MAP);
			break;
		default:
			map.setCenter(new GLatLng(35.413886372091646, 136.7519760131836), 14, G_NORMAL_MAP);
		}
    
    /*	ポインタ表示	*/
		for (i=0; i < data.length; i++) {
			maker[i] = CreateInfoPointer(data[i],pointer_man,pointer_aki);
			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;
  document.getElementById("list").innerHTML = "";
  list_html = "<table><tr><td class='list_new_title'>　</td><td class='list_id_title'>No.</td><td class='list_name_title'>物件名</td><td class='list_space_title'>空き台数</td><td class='list_price_title'>料金</td><td class='list_distance_title'>距離(m)</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);
				
				/*	newアイコンの判断	*/
				//今日の日付を取得
				var now_date = new Date();
				//差が１週間以内ならアイコン表示
				var new_html = "　";
				var tmp = data[i].getAttribute("update").split("/");
				var update_date = new Date(parseInt(tmp[0]), parseInt(tmp[1]) -1, parseInt(tmp[2]));

				var diff = now_date - update_date;
				var diffDay = diff / 86400000; //1日は86400000ミリ秒
				if (diffDay < 14) {
					new_html = "<img src='../img/common/new.jpg'>";
				}
				
				list_html += "<tr><td class='list_new_class' id='list_new_id" + i + "'>" + new_html + "</td><td class='list_id_class' id='list_id_id" + i + "'>" + id + "</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_space_class' id='list_space_id" + i + "'>" + data[i].getAttribute("space") +"</td><td class='list_price_class' id='list_price_id" + i + "'>" + data[i].getAttribute("price") +"</td><td class='list_distance_class' id='list_distance_id" + i + "'>" + interval +"</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_new_id" + i).style.backgroundColor = "#ffccff";
					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", "../parking.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,pointer_man,pointer_aki) {
	var temp_pointer;
	var mk;
	
	
	if (parseInt(rd.getAttribute("space")) <= 0) {
		temp_pointer = pointer_man;
	} else {
		temp_pointer = pointer_aki;
	}
	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);

	/*	newアイコンの判断	*/
	//今日の日付を取得
	var now_date = new Date();
	//差が１週間以内ならアイコン表示
	var new_html = "　";
	var tmp = rd.getAttribute("update").split("/");
	var update_date = new Date(parseInt(tmp[0]), parseInt(tmp[1]) -1, parseInt(tmp[2]));

	var diff = now_date - update_date;
	var diffDay = diff / 86400000; //1日は86400000ミリ秒
	if (diffDay < 14) {
		new_html = "<img src='../img/common/new.jpg'>";
	}
	
	var info_html = "<div class='iw'>" + new_html + "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><br>空台数：<span id='iw_space'>" + rd.getAttribute("space") + "</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_new").innerHTML = new_html;
	document.getElementById("info_id").innerHTML = id;
	document.getElementById("info_name").innerHTML = rd.getAttribute("name");

	document.getElementById("info_price").innerHTML = rd.getAttribute("price");
	document.getElementById("info_space").innerHTML = rd.getAttribute("space");
	if (rd.getAttribute("space") == "0") {
		document.getElementById("info_space").style.color = "red";
	} else {
		document.getElementById("info_space").style.color = "blue";
	}
	
	/*	屋根有り、舗装有りのアイコン	*/
	if (rd.getAttribute("roof_flag") == "1") {
		document.getElementById("info_roof").innerHTML = "<img src='img/common/roof.jpg'>";
	} else {
		document.getElementById("info_roof").innerHTML = "";
	}
	if (rd.getAttribute("pave_flag") == "1") {
		document.getElementById("info_pave").innerHTML = "<img src='img/common/pave.jpg'>";
	} else {
		document.getElementById("info_pave").innerHTML = "";
	}
	
	document.getElementById("info_update").innerHTML = rd.getAttribute("update");
	document.getElementById("info_address").innerHTML = rd.getAttribute("address");
	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;
	
}

/*	住所から検索	*/
function search_address() {
	//alert(document.getElementById("address").value);
	
	var geocoder = new GClientGeocoder();
	geocoder.getLatLng(document.getElementById("address").value, moveTo);
}
function moveTo(latlng){
  if (latlng){
    map.setCenter(latlng, 17);
		ViewList(-1,500);
  }else{
    alert("入力されたキーワードは献策できませんでした。他のキーワードを入力してください。");
  }
}

