var gmap;
$(document).ready(function()
{
	if(GBrowserIsCompatible())
	{
		gmap = new GMap2(document.getElementById('gmap'));
		gmap.addMapType(G_PHYSICAL_MAP);
		gmap.addMapType(G_SATELLITE_3D_MAP);
		gmap.addControl(new GHierarchicalMapTypeControl());
		gmap.addControl(new GSmallMapControl());
		gmap.enableScrollWheelZoom();
		gmap.setMapType(G_HYBRID_MAP);
		gmap.setCenter(new GLatLng(36.39069760048364,139.06041383743286), 2);
	}
	$('select[name=country]').change(function()
	{
		$('input[name=photography]').val('').attr('disabled',false);
		$('input[name=p_ruby]').val('').attr('disabled',false);
		$('input[name=location]').val('').attr('disabled',false);
		$('input[name=l_ruby]').val('').attr('disabled',false);
		$('input[name=location]').attr('disabled',false);
		$('input[name=l_ruby]').attr('disabled',false);
		$('input[name=architect]').attr('disabled',false);
		$('input[name=a_ruby]').attr('disabled',false);
		$('input[name=lat]').val(0).attr('disabled',false);
		$('input[name=lon]').val(0).attr('disabled',false);
		$('#getxy').attr('disabled',false);
		$('#loadxy').attr('disabled',false);
		$('#keyword').attr('disabled',false);
		gmap.closeInfoWindow();
		gmap.setCenter(new GLatLng(36.39069760048364,139.06041383743286), 2);
	});
	$('#getxy').click(function(){getXY();});
	$('#loadxy').click(function(){loadXY();});
	$('input[name=photography]').click(function()
	{
		loadlist('photography',$(this),$('input[name=p_ruby]'),'--- 登録済み建造物から選択 ---');
	});
	$('input[name=location]').click(function()
	{
		loadlist('location',$(this),$('input[name=l_ruby]'),'--- 登録済み都市名から選択 ---');
	});
	$('input[name=architect]').click(function()
	{
		loadlist('architect',$(this),$('input[name=a_ruby]'),'--- 登録済み建築家から選択 ---');
	});
	$('form').submit(function()
	{
		$('input[name=photography]').attr('disabled',false);
		$('input[name=p_ruby]').attr('disabled',false);
		$('input[name=location]').attr('disabled',false);
		$('input[name=l_ruby]').attr('disabled',false);
		$('input[name=location]').attr('disabled',false);
		$('input[name=l_ruby]').attr('disabled',false);
		$('input[name=architect]').attr('disabled',false);
		$('input[name=a_ruby]').attr('disabled',false);
		$('input[name=lat]').attr('disabled',false);
		$('input[name=lon]').attr('disabled',false);
		$('#getxy').attr('disabled',false);
		$('#loadxy').attr('disabled',false);
		$('#keyword').attr('disabled',false);
	});
});
function loadXY()
{
	var str = $('#keyword').val();
	if(str)
	{
		var gLocalSearch = new GlocalSearch();
		gLocalSearch.setCenterPoint(gmap);
		gLocalSearch.setSearchCompleteCallback(this,
		function()
		{
			if(gLocalSearch.results.length > 0)
			{
				var first = gLocalSearch.results[0];
				var point = new GLatLng(parseFloat(first.lat), parseFloat(first.lng));
				gmap.setCenter(point, 16);
				gmap.openInfoWindowHtml(point,str);
			}
			else
			{
				window.alert("入力されたキーワードに該当する座標がみつかりませんでした");
			}
		});
		gLocalSearch.execute(str);
	}
}
function getXY()
{
	var point = gmap.getCenter();
	$('input[name=lon]').val(point.x);
	$('input[name=lat]').val(point.y);
}
function loadlist(target,item,ruby,top)
{
	var country = $('select[name=country]').val();
	// コンテナ
	var offset = item.offset();
	var container = $('<div id="list_div" />')
	.css(
	{
		'top': parseInt(offset.top+item.height()+2)+'px',
		'left': offset.left
	})
	.hide()
	.appendTo('body');
	item.blur(function()
	{
		container.hide();
	});
	// リスト
	var lister = $('<ul id="list_ul" />');
	$.ajax(
	{
		type: 'post',
		url: 'loadlist.json',
		data: {
			'country':country,
			'target':target},
		dataType: 'json',
		success: function(request)
		{
			var cnt = 0;
			$.each(request, function(k,v)
			{
				$('<li />')
				.attr({'class':'target','title':v})
				.append(k)
				.mouseover(function()
				{
					$('li.selected',lister).removeClass('selected');
					$(this).addClass('selected');
				})
				.mousedown(function()
				{
					item.val($(this).text());
					if(item.attr('name') == 'photography')
					{
						var info = $(this).attr('title').split('@');
						ruby.val(info.shift())
						.attr('disabled',ruby.val() ? true : false);
						$('input[name=location]').val(info.shift())
						.attr('disabled',$('input[name=location]').val() ? true : false);
						$('input[name=l_ruby]').val(info.shift())
						.attr('disabled',$('input[name=l_ruby]').val() ? true : false);
						$('input[name=architect]').val(info.shift())
						.attr('disabled',$('input[name=architect]').val() ? true : false);
						$('input[name=a_ruby]').val(info.shift())
						.attr('disabled',$('input[name=a_ruby]').val() ? true : false);
						$('input[name=lat]').val(info.shift()).attr('disabled',true);
						$('input[name=lon]').val(info.shift()).attr('disabled',true);
						$('#getxy').attr('disabled',true);
						$('#loadxy').attr('disabled',true);
						$('#keyword').attr('disabled',true);
						var point = new GLatLng(parseFloat($('input[name=lat]').val()), parseFloat($('input[name=lon]').val()));
						gmap.setCenter(point, 16);
						gmap.openInfoWindowHtml(point,$(this).text());
					}
					else
					{
						ruby.val($(this).attr('title')).attr('disabled',ruby.val() ? true : false);
					}
				})
				.appendTo(lister);
				cnt++;
			});
			if(cnt > 0)
			{
				lister.prepend($('<li />').append(top));
				cnt++;
				var fs = parseInt($('html').css('font-size').match(/\d+/g).shift())+1;
				container
				.width(((300>item.width())?300:parseInt(item.width()+2))+'px')
				.height(((200>parseInt(fs*cnt))?parseInt(fs*cnt):200)+'px')
				.append(lister)
				.show();
			}
		}
	});
}

