$.extend({
	initialize: function() {
		/*
		 * Keyword Form
		 */
		$('form.keyword-form').submit(
			function() {
				var keyword = encodeURIComponent($(this).find('.search-keyword').val());
				location.href = '/keyword/'+keyword;
				return false;
			}
		);
		$('form.keyword-form .search-keyword').focus(
			function() {
				$('#keyword-container').css('background-position', '0 -35px');
			}
		);
		$('form.keyword-form .search-keyword').blur(
			function() {
				if ($(this).val() == '') {
					$('#keyword-container').css('background-position', '0 0');
				}
			}
		);

		/*
		 * Custom Search Form
		 */
		$('.search-custom-box .tab a').click(function() {
			if ($(this).attr('class') != 'current') {
				$(this).parent().siblings().find('a').each(function() {
					$(this).removeClass('current');
				});
				$('.search-custom-box .content .tab-content').hide();
				$('.search-custom-box .content '+$(this).attr('rel')).show();
				$(this).addClass('current');
			}
			$('form.custom-search-form .pref').val('');
			$('form.custom-search-form .city').val('').prop('disabled', true).html('');
			$('form.custom-search-form .line').val('').prop('disabled', true).html('');
			$('form.custom-search-form .station').val('').prop('disabled', true).html('');
			return false;
		});

		$.get('/search/prefs', function(data) {
			$('form.custom-search-form .pref').html(data);
		});
		$('form.custom-search-form .city').val('').prop('disabled', true).html('');
		$('form.custom-search-form .line').val('').prop('disabled', true).html('');
		$('form.custom-search-form .station').val('').prop('disabled', true).html('');

		$('form.custom-search-form .pref').change(function() {
			if ($(this).val() != '') {
				if ($('.search-custom-box .tab a.current').attr('rel') == '#area') {
					$.get('/search/cities/'+$(this).val(), function(data) {
						$('form.custom-search-form .city').html(data);
						$('form.custom-search-form .city').prop('disabled', false);
					});
				}
				if ($('.search-custom-box .tab a.current').attr('rel') == '#station') {
					$.get('/search/lines/'+$(this).val(), function(data) {
						$('form.custom-search-form .line').html(data);
						$('form.custom-search-form .line').prop('disabled', false);
					});
					$('form.custom-search-form .station').val('').prop('disabled', true).html('');
				}
			}
			else {
				$('form.custom-search-form .city').val('').prop('disabled', true).html('');
				$('form.custom-search-form .line').val('').prop('disabled', true).html('');
				$('form.custom-search-form .station').val('').prop('disabled', true).html('');
			}
		});

		$('form.custom-search-form .line').change(function() {
			if ($(this).val() != '') {
				$.get('/search/stations/'+$(this).val(), function(data) {
					$('form.custom-search-form .station').html(data);
					$('form.custom-search-form .station').prop('disabled', false);
				});
			}
			else {
				$('form.custom-search-form .station').val('').prop('disabled', true).html('');
			}
		});

		$('form.custom-search-form').submit(function() {
			if ($('form.custom-search-form .city').val()) {
				url = '/area/'+$('form.custom-search-form .city').val();
				location.href = url;
			}
			else if ($('form.custom-search-form .station').val()) {
				url = '/station/'+$('form.custom-search-form .station').val();
				location.href = url;
			}
			return false;
		});

		/*
		 * Plan - Outline - Map
		 */
		$('a.maplink').click(
			function() {
				var link = $(this);
				$(this).parent().next().slideToggle('normal', function() {
					// SlideUp
					if ($(this).is(':hidden')) {
						link.css('background-position', '0 0');
					}
					// SlideDown
					else {
						link.css('background-position', '0 -15px');
						var point = new google.maps.LatLng($(this).attr('lat'), $(this).attr('lng'));
						var options = {
							zoom: 17,
							center: point,
							mapTypeId: google.maps.MapTypeId.ROADMAP,
							scrollwheel: false
						};
						var map = new google.maps.Map(document.getElementById($(this).attr('id')), options);
						var marker = new google.maps.Marker({position:point, map:map});
					}
				});
				return false;
			}
		);
	}
});

