﻿var map = null;
var geocoder = null;

/**
 * 初始化地圖
 * 
 * @return void
 */
function load() {
	if (GBrowserIsCompatible()) {
		if (!document.getElementById("map")) {
			return;
		}
		map = new GMap2(document.getElementById("map"));
		map.enableScrollWheelZoom();
		geocoder = new GClientGeocoder();

		// 地圖控制列
		map.addControl(new GLargeMapControl()); // 左側控制列
		map.addControl(new GMapTypeControl()); // 右上控制列
		map.setCenter(new GLatLng(25.009162, 121.505646), 15);

		GEvent.addListener(map, 'move', function() {
			$("#latlng").html(map.getCenter().lat() + ':' + map.getCenter().lng());
		});
		
		// var icon = new GIcon();
		// icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
		// icon.shadowSize = new GSize(21, 29);
		// icon.iconAnchor = new GPoint(10, 30);
		// icon.infoWindowAnchor = new GPoint(9, 5);
		// icon.infoShadowAnchor = new GPoint(9, 5);
		// icon.image = "http://www.misuisui.com/images/21_29_s5s5.png";
		// icon.iconSize = new GSize(21, 29);
		//
		// var html = '<s>test</s>';
		// var point = new GPoint(121.504114, 25.008322);
		// var marker = createMarker(point, icon ,html);
		// map.addOverlay(marker);
	}
}

function createMarker(point, html) {
	 var icon = new GIcon();
//	 icon.shadow = "http://www.google.com/mapfiles/shadow50.png";
//	 icon.shadowSize = new GSize(21, 29);
	 icon.iconAnchor = new GPoint(10, 30);
	 icon.infoWindowAnchor = new GPoint(9, 5);
	 icon.infoShadowAnchor = new GPoint(9, 5);
	 icon.image = "images/trash.gif";
	 icon.iconSize = new GSize(21, 29);
	var marker = new GMarker(point, icon);

	GEvent.addListener(marker, 'click', function() {
		marker.openInfoWindowHtml(html);
	});

	return marker;
}




/**
 * 判斷時間字串
 * 
 * @param str
 * @return
 */
function validTime(value){	
	var a = value.match(/^(\d{0,2}):(\d{0,2})$/);
	if (a == null) { 
		return false;
	}
	if (a[1]>=24 || a[2]>=60) {
		return false;
	}
	return true;
} 




/**
 * 在地圖上標示出地址，並可以新增
 * 
 * @param address
 * @return
 */
function queryNewAddress(address) {
	if (!geocoder) {
		return;
	}
	geocoder.getLatLng(address, function(point) {
		if (!point) {
			alert("有這個地方嗎？");
			return;
		}
		// 新增 Point 的 HTML
			var html = "<div class=\"highlight\">" + address + "</div><br/>" + 
				"<form id='timeform' method='post' action='/newpoint' onsubmit='" + 
				"if (time.value == \"\") {" + 
				"  $(\"div.errormsg\").text(\"時間必填喔\"); " + 
				"  $(\"div.errormsg\").show();  " + 
				"  return false; " +
				"} " +
				"var re = /^([0-1][0-9]|[2][0-3])(:([0-5][0-9])){1,2}$/;" +
				//" alert(re);alert(re.test(\"12:30\"));alert(re.test(time.value));" +
				"if (!re.test(time.value)) {" +
				"  $(\"div.errormsg\").text(\"時間格式好像不太對ㄝ\");" +
				"  $(\"div.errormsg\").show();" +
				"  return false;" +
				"}"
					+ "'>"
					+ "時間(hh:ss): <input type='text' name='time' size='10'>"
					+ "<input type='hidden' name='address' value='" + address
					+ "'>" + "<input type='hidden' name='lat' value='"
					+ point.lat() + "'>"
					+ "<input type='hidden' name='lng' value='" + point.lng()
					+ "'>" + "<input type='submit' value='新增'>" + "<br/><div class=\"errormsg\"></div>" + "</form>";
			
			// 定位
			map.clearOverlays();
			map.setCenter(point, map.getZoom());
			var marker = new GMarker(point);
			map.addOverlay(marker);
			marker.openInfoWindowHtml(html);
			
		});
}





/**
 * 在地圖上標示出地址
 * 
 * @param address
 * @return
 */
function queryAddress(address) {
	if (!geocoder) {
		return;
	}
	geocoder.getLatLng(address, function(point) {
		if (!point) {
			alert("有這個地方嗎？");
			return;
		}

		// 定位
		map.clearOverlays();
		map.setCenter(point, map.getZoom());
		var marker = new GMarker(point);
		map.addOverlay(marker);

		// 取出附近的點標示在地圖上
		$.getJSON("/closehere?lat=" + point.lat() + "&lng=" + point.lng(),
				function(points) {
					$.each(points, function(i, p) {
						var gp = new GPoint(p.lng, p.lat);
//						var marker = new GMarker(gp);
						var html = '<div class="highlight">' + address + ' @ ' + p.point_time + '</div>';
						var marker = createMarker(gp, html);
						map.addOverlay(marker);
					});
				});
		});
}


function validBatchText(textarea) {
	var lines = textarea.value.split('\r\n');
	var result = '';
	var hasError = false;
	var count = 0;
	var errorcount = 0;
	for (var i = 0; i < lines.length; i++) {
		var line = jQuery.trim(lines[i]);
		if (line == null || line == '') {
			continue;
		}
		
		result += line;
		
		count++;
		var part = line.split(',');
		if (part.length != 2) {
			result += ' <-格式錯誤(' + part.length + ')\r\n';
			hasError = true;
			errorcount++;
			continue;
		}
		if (!validTime(part[1])) {
			result += ' <-時間格式錯誤\r\n';
			hasError = true;
			errorcount++;
			continue;
		}
		result += '\r\n';
	}
	if (count > 200) {
		$("div.errormsg").text('超過 200 行');
		$("div.errormsg").show();
	}
	if (hasError) {
		$("div.errormsg").text('有 ' + errorcount + ' 行格式錯誤');
		$("div.errormsg").show();
		textarea.value = result;
		return false;
	}
	return true;
}
