var popup;

function digitcheck(value) {
	var char1 = new Array("１","２","３","４","５","６","７","８","９","０","　"," ");
	var char2 = new Array(1,2,3,4,5,6,7,8,9,0,'','');
	var data = value;
	while (data.match(/[０-９]/)) {
		var i = 0;
		for(; i < char1.length; i++){
			data = data.replace(char1[i], char2[i]);
		}
	}
	return data;
}

var map;
var E;
var PI_ER;

function init() {

	var param = new Array();
	var x = window.location.search.substring(1);
	var xx = x.split('&');

	var items = new Array();
	var i, j;

	for (i=0; i < xx.length; i++) {
		var vals  = new Array(2);
		vals = xx[i].split('=', 2);
		if ((vals[0] == 'c') || (vals[0] == 'i')){
			items[items.length] = new Array(vals[0], vals[1]);
		} else {
			param[vals[0]] = vals[1];
		}
	}
	delete xx;
	delete x;

	if ('pos' in param) {
		param['pos'] = new YLLPoint(param['pos']);
	} else {
		// Tokyo Station
		param['pos'] = new YLLPoint('35.40.41.976, 139.46.13.598');
	}
	if ('layer' in param) {
		param['layer'] = parseInt(param['layer']);
		if ((param['layer'] < 0)) {
			param['layer'] = 1;
		}
	} else {
		param['layer'] = 6;
	}
	if (('datum' in param) && (param['datum'] == 'WGS84')) {
		param['datum'] == YDatumType.WGS84;
		E = ((2 * 298.257223) -1) / Math.pow(298.257223, 2);
		PI_ER = Math.PI * 6378137;
	} else {
		param['datum'] = YDatumType.TOKYO97;
		E = ((2 * 299.152813) -1) / Math.pow(299.152813, 2);
		PI_ER = Math.PI * 6377397.155;
	}

	if (('mode' in param) && (param['mode'] == 'AERO')) {
		param['mode'] = YMapMode.AERO;
	} else {
		param['mode'] = YMapMode.MAP;
	}
	map = new YahooMapsCtrl("map", param['pos'], param['layer'], param['mode'], param['datum']);

	for (i = 0; i < items.length; i++) {
		var pos;
		var r;
		var item = new Array();
		var isvalid = true;
		var xxx = items[i][1].split(';');

		for (j = 0; j< xxx.length; j++) {
			var v = new Array(2);
			v = xxx[j].split(':', 2);
			item[v[0]] = v[1];
		}
		pos = new YLLPoint(item['pos']);

/*
		if (pos is not valid) {
			invalid = false;
		} else {
			if (items[i][0] == 'c') {
				...
			} else if (items[i][0] == 'i') {
				...
			}
		}
*/
		if (items[i][0] == 'c') {
			if (('r' in item) && (parseFloat(item['r']) > 0)) {
				item['r'] = parseFloat(item['r']);
			} else {
				isvalid = false;
			}
			if (!(('sc' in item) && (item['sc'].match(/^[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]$/i)))) {
				item['sc'] = '000000';
			}
			if (!(('fc' in item) && (item['fc'].match(/^[a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9][a-f0-9]$/i)))) {
				item['fc'] = '000000';
			}

			if (('sw' in item) && (parseInt(item['sw']) > 0)) {
				item['sw'] = parseInt(item['sw']);
			} else if (item.length > 0) {
				item['sw'] = 1;
			}
			if (('so' in item) && ((0 <= parseFloat(item['so'])) && (parseFloat(item['so']) <= 1))) {
				item['so'] = parseFloat(item['so']);
			} else if (item.length > 0) {
				item['so'] = 1;
			}
			if (('fo' in item) && ((0 <= parseFloat(item['fo'])) && (parseFloat(item['fo']) <= 1))) {
				item['fo'] = parseFloat(item['fo']);
			} else if (item.length > 0) {
				item['fo'] = 1;
			}
			if (isvalid) {
				addCircle(pos, item['r'], item['sw'], item['sc'], item['so'], item['fc'], item['fo'], decodeURI(item['label']));
			}

		} else if (items[i][0] == 'i') {
			addIcon(pos);
		}
	}


	map.addEvent(YEventType.ICON_CLICK,function(p){
		var icon_info = map.getIcon(p);
		var R = parseFloat(digitcheck(document.getElementById("radius").value)) * parseFloat(digitcheck(document.getElementById("r_Scale").options[document.getElementById("r_Scale").selectedIndex].value));

		var color = '000000';
		var i;
		for (i = 0; i < document.getElementsByName("color").length; i++) {
			if (document.getElementsByName("color")[i].checked) {
				color = document.getElementsByName("color")[i].value;
				break;
			}
		}
		addCircle(icon_info.pos, R, 1, color, 1, color, document.getElementById("fo").options[document.getElementById("fo").selectedIndex].value, icon_info.pos.toString() + '_' + digitcheck(document.getElementById("radius").value) + document.getElementById("r_Scale").options[document.getElementById("r_Scale").selectedIndex].text);
	});

	map.addEvent(YEventType.MAP_CLICK,function(p){
		addIcon(p);
	});
}

function addCircle(pos, radius, sw, sc, so, fc, fo, label) {
	var id = 'pos:' + pos.toString() + ';r:' + radius;

	if (map.getVectorObject(id)) {
		removeItem(id);
	}

	var pLatRadians = (pos.lat * Math.PI) / 180;
	var TMP = 1 - E * Math.pow(Math.sin(pLatRadians), 2);
	var arc_lat = (PI_ER * (1 - E)) / (180 * Math.pow(TMP, 3/2));
	var arc_lon = (PI_ER * Math.cos(pLatRadians)) / (180 * Math.pow(TMP, 1/2));

	var points = new Array(360);

	var i;
	for (i = 0; i <= 360; i++) {
		var rad = i / 180 * Math.PI;
		var lat = (radius / arc_lat) * Math.sin(rad) + pos.lat;
		var lon = (radius / arc_lon) * Math.cos(rad) + pos.lon;
		points[i] = new YLLPoint(lat, lon);
	}
	var circle_style = { strokeWidth: sw, strokeColor: sc, strokeOpacity: so, fillColor: fc, fillOpacity: fo };
	document.getElementById("list").options[document.getElementById("list").length] = new Option(label, id);
	return map.addPolygon(id, points, '', circle_style);
}

function addIcon(pos) {
	var label = pos.toString() + '_MARK';
	var id = 'icon:' + pos.toString();

	map.addIcon(id, pos, '', 'S1');
	document.getElementById("list").options[document.getElementById("list").length] = new Option(label, id);
}

function removeItem(id) {
	if (id == null) {
		if (document.getElementById("list").selectedIndex < 0) {
			return;
		}
		id = document.getElementById("list").options[document.getElementById("list").selectedIndex].value;
	}
	var done = false;
	if (map.getVectorObject(id)) {
		map.removeVectorObject(id);
		done = true;
	} else if (map.getIcon(id)) {
		map.removeIcon(id);
		done = true;
	}
	if (done) {
		var i;
		for (i = 0; i < document.getElementById("list").options.length; i++) {
			if (document.getElementById("list").options[i].value == id) {
				document.getElementById("list").options[i] =  null;
				break;
			}
		}
	}
}

function showURL() {
	var data = '?layer=' + map.getCurrentLayer() + '&pos='  + map.getCenter().toString();
	if (map.getDatum() == YDatumType.TOKYO97) {
		data += '&datum=TOKYO97';
	} else if (map.getDatum() == YDatumType.WGS84) {
		data += '&datum=WGS84';
	}
	if (map.getCurrentMode() == YMapMode.MAP) {
		data += '&mode=MAP';
	} else if (map.getCurrentMode() == YMapMode.AERO) {
		data += '&mode=AERO';
	}
	var i, c;
	for (i = 0; i < document.getElementById("list").options.length; i++) {
		if (document.getElementById("list").options[i].value) {
			if (document.getElementById("list").options[i].value.match(/^icon:/)) {
				c = map.getIcon(document.getElementById("list").options[i].value);
				data += '&i=pos:' + c.pos.toString();
			} else if (document.getElementById("list").options[i].value.match(/^pos:/)) {
				c = map.getVectorObject(document.getElementById("list").options[i].value);
				data += '&c=' + document.getElementById("list").options[i].value + ';sw:' + c.style.strokeWidth + ';sc:' + c.style.strokeColor + ';so:' + c.style.strokeOpacity + ';fc:' + c.style.fillColor + ';fo:' + c.style.fillOpacity + ';label:' + encodeURI(document.getElementById("list").options[i].text);
			}
		}
	}
	document.getElementById("url").value = location.protocol + '//' + location.host + location.pathname + data;
}


function openURL() {
	if (document.getElementById("url").value.length > 0) {
		window.open(document.getElementById("url").value);
	}
}

function clickIcon(target) {
	document.getElementById(target).checked = true;
}
