$(document).ready(function () {
	$('input#search').click(function (e) {
		e.preventDefault();
		$.get("jumpmap.php", function(data){
			// create a modal dialog with the data
			$(data).modal({
				closeHTML: "<a href='#' title='閉じる' class='modalCloseX simplemodal-close'>x</a>",
				position: ["20%",],
				overlayId: 'search-overlay',
				containerId: 'search-container',
				onOpen: search.open,
				onShow: search.show,
				onClose: search.close
			});
		});
	});

	$('input#login').click(function (e) {
		e.preventDefault();
		if ( x.user == null )
		{
			$.get("login.php", function(data){
				// create a modal dialog with the data
				$(data).modal({
					closeHTML: "<a href='#' title='閉じる' class='modalCloseX simplemodal-close'>x</a>",
					position: ["20%",],
					overlayId: 'login-overlay',
					containerId: 'login-container',
					onOpen: login.open,
					onShow: login.show,
					onClose: login.close
				});
			});
		}
		else
		{
			$.get("menu.php", function(data){
				// create a modal dialog with the data
				$(data).modal({
					closeHTML: "<a href='#' title='閉じる' class='modalCloseX simplemodal-close'>x</a>",
					position: ["20%",],
					overlayId: 'menu-overlay',
					containerId: 'menu-container',
					onOpen: menu.open,
					onShow: menu.show,
					onClose: menu.close
				});
			});
		}
	});

	// preload images
	var img = ['form_bottom.gif', 'form_top.gif'];
	$(img).each(function () {
		var i = new Image();
		i.src = 'img/' + this;
	});
});

var search = {
	message: null,
	open: function (dialog)
	{
		// dynamically determine height
		var h = 100;

		dialog.overlay.fadeIn(100, function ()
		{	dialog.container.fadeIn(100, function ()
			{	dialog.data.fadeIn(100, function ()
				{	$('#search-container .search-content').animate(
					{height: h},
					function ()
					{	$('#search-container form').fadeIn(100, function ()
						{	$('#search-container #search-input').focus();
							if ( $.browser.msie && $.browser.version < 7)
							{	$('#search-container .search-button').each(function ()
								{	if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i))
									{	var src = RegExp.$1;
										$(this).css(
										{	backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										});
									}
								});
							}
						});
					});
				});
			});
		});
	},
	show: function (dialog) {
		$('#search-container #search-submit').click(function (e) {
			e.preventDefault();

			if ( $('#search-container #search-input').val() == '' )
			{
				$('#search-container #search-input').focus();
				return false;
			}

			var geocoder = new google.maps.Geocoder();

			var v = $('#search-container #search-input').val();
			var address = v.split(',');

		    geocoder.geocode(
				{
					'address' : $('#search-container #search-input').val(),
					'country' : ( address.length > 1 ) ? address[1].toLowerCase() : 'jp'
				},
			    function(results, status)
			    {
			    	if ( status == google.maps.GeocoderStatus.OK )
			    	{
						$('#search-container .search-message').fadeOut();
						$('#search-container form').fadeOut(100);
						$('#search-container .search-content').animate({
							height: 10
						}, function () {
							dialog.data.fadeOut(100, function () {
								dialog.container.fadeOut(100, function () {
									dialog.overlay.fadeOut(100, function () {
										x.mapObject.setCenter(new google.maps.LatLng(
								    		results[0].geometry.location.lat(),
								    		results[0].geometry.location.lng())
								    	);
										x.mapObject.setZoom(17);
										if ( x.user != null )
										{
											x.setNewMarker(new google.maps.LatLng(
									    		results[0].geometry.location.lat(),
									    		results[0].geometry.location.lng())
									    	);
										}
										$.modal.close();
									});
								});
							});
						});
					}
					else
					{
						alert('見つかりませんでした。名称を変えて再検索してください');
						$('#search-container #search-input').focus();
					}
					return false;
				}
			);

			return false;
		});
	},
	close: function (dialog) {
		$('#search-container .search-message').fadeOut();
		$('#search-container form').fadeOut(100);
		$('#search-container .search-content').animate({
			height: 10
		}, function () {
			dialog.data.fadeOut(100, function () {
				dialog.container.fadeOut(100, function () {
					dialog.overlay.fadeOut(100, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	showError: function () {
		$('#search-container .search-message')
			.html($('<div class="search-error">').append(search.message))
			.fadeIn(100);
	}
};

var login = {
	message: null,
	open: function (dialog)
	{
		// dynamically determine height
		var h = 220;

		dialog.overlay.fadeIn(100, function ()
		{	dialog.container.fadeIn(100, function ()
			{	dialog.data.fadeIn(100, function ()
				{	$('#login-container .login-content').animate(
					{	height: h	},
					function ()
					{
						$('#login-container form').fadeIn(100, function ()
						{
							$('#login-container #login-user').focus();

							if ( $.browser.msie && $.browser.version < 7)
							{
								$('#login-container .login-button').each(function ()
								{
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i))
									{
										var src = RegExp.$1;
										$(this).css(
										{
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										}
										);
									}
								}
								);
							}
						}
						);
					}
					);
				}
				);
			}
			);
		}
		);
	},
	show: function (dialog) {
		$('#login-container #login-submit').click(function (e) {
			e.preventDefault();

			var self = this;
			if ( $('#login-container #login-user').val() == '' )
			{
				alert('メールアドレスを入力してください');
				$('#login-container #login-user').focus();
				return false;
			}

			if ( $('#login-container #login-password').val() == '' )
			{
				alert('パスワードを入力してください');
				$('#login-container #login-password').focus();
				return false;
			}

			new $.ajax({
				url : 'loginprocess.php',
				type : 'POST',
				data : {
					mailaddr : $('#login-user').val(),
					password : $('#login-password').val(),
					'login-save' : ($("input[name='login-save']:checked").val()) ? 1 : 0
				},
				dataType : 'json',
				success : function(data, dataType)
				{
					if ( data['error'] == 0 )
					{
						$('#login-container .login-message').fadeOut();
						$('#login-container form').fadeOut(100);
						$('#login-container .login-content').animate({
							height: 10
						}, function () {
							dialog.data.fadeOut(100, function () {
								dialog.container.fadeOut(100, function () {
									dialog.overlay.fadeOut(100, function () {
										$.modal.close();
										$('#login').val(data['results'][0]['NICKNAME']);
										$('#login').attr('title', 'メニューを表示します');
										x.user = data.results[0];
										x._setMapClickEvents();
										x.notfit = true;
										x.closeInfoWindow();
										x.allMarkerReget();
									});
								});
							});
						});
					}
					else
					{
						alert('ユーザー・パスワードのいずれかに誤りがあります。');
						$('#login-user').val('');
						$('#login-password').val('');
						$('#login-container #login-user').focus();
						return false;
					}
				},
				error : function(XMLHttpRequest, textStatus, errorThrown)
				{
					alert('エラーが発生しました');
					return false;
				},
				complete : function(XMLHttpRequest, textStatus)
				{
				}
			});

			return false;
		});
	},
	close: function (dialog) {
		$('#login-container .login-message').fadeOut();
		$('#login-container form').fadeOut(100);
		$('#login-container .login-content').animate({
			height: 10
		}, function () {
			dialog.data.fadeOut(100, function () {
				dialog.container.fadeOut(100, function () {
					dialog.overlay.fadeOut(100, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	showError: function () {
		$('#login-container .login-message')
			.html($('<div class="login-error">').append(login.message))
			.fadeIn(200);
	}
};

var menu = {
	message: null,
	open: function (dialog)
	{
		// dynamically determine height
		var h = 190;

		dialog.overlay.fadeIn(100, function ()
		{	dialog.container.fadeIn(100, function ()
			{	dialog.data.fadeIn(100, function ()
				{	$('#menu-container .menu-content').animate(
					{	height: h	},
					function ()
					{
						$('#menu-container form').fadeIn(100, function ()
						{
							$('#menu-container #menu-user').focus();

							if ( $.browser.msie && $.browser.version < 7)
							{
								$('#menu-container .menu-button').each(function ()
								{
									if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i))
									{
										var src = RegExp.$1;
										$(this).css(
										{
											backgroundImage: 'none',
											filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
										}
										);
									}
								}
								);
							}
						}
						);
					}
					);
				}
				);
			}
			);
		}
		);
	},
	show: function (dialog) {

	},
	close: function (dialog) {
		$('#menu-container .menu-message').fadeOut();
		$('#menu-container form').fadeOut(100);
		$('#menu-container .menu-content').animate({
			height: 10
		}, function () {
			dialog.data.fadeOut(100, function () {
				dialog.container.fadeOut(100, function () {
					dialog.overlay.fadeOut(100, function () {
						$.modal.close();
					});
				});
			});
		});
	},
	error: function (xhr) {
		alert(xhr.statusText);
	},
	showError: function () {
		$('#menu-container .menu-message')
			.html($('<div class="menu-error">').append(menu.message))
			.fadeIn(200);
	}
};