/**
 * toilet labo
 *
 * @copyright	2011 RaNa design associates, inc.
 * @link		http://http://www.ranadesign.com/
 * @version		1.2
 * @since		Mar 01, 2011
 * @update		Dec 27, 2011
 */
(function($) {

	$(function() {
		$.toiletInit();
		//toilet.scroller(); //.setFooter().hover(".hover");
		$(".scroller").scroller();
		
		// 質問するボタンの自動件名付加
		$(".inquiry").addSubject();
	});

	var toilet = {
	
		// ふんわりホバー
		hover: function(obj) {
			$(obj).hover(function() {
				$(this).stop(true).fadeTo(500, 0);
			}, function() {
				$(this).stop(true).fadeTo(500, 1);
			});
			return this;
		},
	
		// documentが短い時にfooterを画面下部にあわせる。
		setFooter: function() {
			var headerHeight = $(".area-header").innerHeight(),
				footerHeight = $(".footer").innerHeight();
			$(window).resize(function() {
				var h = $(this).height() - headerHeight - footerHeight - 5 * 2 - 88 - 32// 上下border, 上下padding
				$("#container").css("minHeight", h);
				$("#wrapper").css("minHeight", h);
			}).resize();
			return this;
		}
	};
	
	$.fn.extend({
		// スムーズスクロール。
		// class名scrollerのa要素が対象
		scroller: function() {
			this.click(function() {
				var top = this.hash ? $(this.hash).offset().top : 0;
				$("html, body").animate({ scrollTop: top }, 500);
				return false;
			});
			return this;
		},
		
		// 自動件名付加
		// data-subjectの値をエンコードしてパラメータ化する。
		addSubject: function() {
			var subject = this.data("subject"),
				href = this.attr("href");
			if (!subject || subject.length === 0) {
				return this;
			}
			this.attr("href", href + "?c=counseling&t=" + encodeURI(subject));
			return this;
		}
	});
	
	$.extend({
		// ローカルナビ下のアニメGIFのスペースを確保するため、コンテンツ高さを調整。
		toiletInit: function() {
			var container = $(".area-contents"),
				topicpath = $(".nav.topicpath"),
				localnav = $(".nav.local"),
				item = $(".area-item,.area-item_comfort"),
				navHeight = topicpath.outerHeight() + localnav.outerHeight() + item.innerHeight();

			container.height(container.height() < navHeight ? navHeight : container.height());
			return this;
		}
	});

})(jQuery);


/**
 * local navigation
 */
function setLocalNav() {
	var category = location.pathname.split("/")[1],
		navXML = "/xml/navigation.xml",
		bannerXML = "/xml/banner.xml",
		main = $("<ul>", { "class": "main" }),
		sub = $("<ul>", { "class": "sub" }),
		banner = $("<ul>", { "class": "banner" });

	$(".nav.local").append(main).append(sub).append(banner);
		
	$.ajax(navXML, {
		dataType: "xml",
		success: function(data) {
			var icon = new Image();
			icon.src = "/img/common/icon/icon_window.gif";
			icon.className = "icon";

			$("nav[category=" + category + "]", data)
				.find("title a").each(function() {
					var a = $("<a>", { href: $(this).attr("href"), target: $(this).attr("target"), "class": "title" });
					a.css("background", "url(/img/common/nav/" + category + ".png)").insertBefore(main);
				}).end()
				.find("normal a").each(function() {
					var li = $("<li>"),
						a = $("<a>", { href: $(this).attr("href"), target: $(this).attr("target") });
					a.text($(this).text());
					// 別窓アイコン
					if ($(this).attr("target") === "_blank") {
						$(icon).clone().appendTo(a);
					}
					// 現在表示されているページ
					if (location.pathname === $(this).attr("href")) {
						li.addClass("current");
					}
					li.append(a).appendTo(main);
					// for IE 8
					$.toiletInit();

				}).end()
				.find("special a").each(function() {
					var li = $("<li>"),
						a = $("<a>", { href: $(this).attr("href"), target: $(this).attr("target") });
					a.text($(this).text());
					li.append(a).appendTo(sub);
				});
		}
	});
	
	$.ajax(bannerXML, {
		dataType: "xml",
		success: function(data) {
			$("banner", data).each(function() {
				var li = $("<li>"),
					a = $("<a>", { href: $("link", this).text(), target: $("link", this).attr("target") }),
					img = new Image();
				img.src = $("img", this).text();
				a.append(img).appendTo(li);
				banner.append(li);
				// for IE 8
				$.toiletInit();
			});
		}
	});

}

