/**
 * SHISEIDO THE GINZA
 *
 * @copyright	2011 RaNa design associates, inc.
 * @link		//www.ranadesign.com/
 * @version		1.4
 * @since		Feb 02, 2011
 * @update		May 10, 2011
 */
(function($) {
	var iphone = /(iPhone)|(iPad)/i.test(navigator.userAgent),
		theginza = {
			timerId: {},
			
			// ドキュメントの高さをグリッドにあわせる。
			setFooter: function() {
				var $container = $("#container"),
					windowHeight = $(window).height(),
					containerHeight = 0;
				

				// IE6-7対策のためボーダ幅を引く → 変更
				containerHeight = $container.height(); // - 3;
				// 現在の高さ＋不足分の高さ＋ボーダ幅
				//$container.height(containerHeight + (180 - containerHeight % 180) + 3);

				// コンテンツの高さを調節する。
				var $section = $(".section, .content"),
					sectionHeight = $section.height(),
					footerHeight = 39; // - 3;
				if ($section.length) {
					$section.height($(document).height() - ($section.offset().top + footerHeight));
				}

			},
			
			// スライダーモジュール
			slider: function(options) {
				var param, $mod, $guide, $first;
				$mod = this;
				param = {
					ms: 2500,
					wait: 5000,
					easing: "swing",
					loop: true,
					guide: ".guide",
					cell: "li"
				};
				$.extend(param, options);
				$guide = $mod.find(param.guide);
				$first = $mod.find(param.cell).eq(0);

				$first.animate({
					marginLeft: -$first.width()
				}, {
					duration: param.ms,
					easing: param.easing,
					complete: function() {
						$first.css("marginLeft", 0).appendTo($guide);
						if (param.loop) {
							setTimeout(function() {
								$mod.slider(options);
							}, param.wait);
						}
					}
				});
				return this;
			},
			
			/**
			 *	フェーダーモジュール
			 *	引数に数値のみ渡した場合は、該当番号の画像から始める。
			 */
			fader: function(options) {
				var defaults, param, $mod, $ctrler, $button, _ctrl;
				defaults = {
					click: true,
					hover: false,
					ctrlId: ".ctrl",
					ctrlButton: "li"
				}
				if (typeof options === "number") {
					param = defaults;
					param.num = options;
				} else {
					param = $.extend({}, defaults, options);
				}
				$mod = this;
				$ctrler = $mod.find(param.ctrlId);
				$button = $ctrler.find(param.ctrlButton);
				$.fn._faderCore = theginza._faderCore;
				
				_ctrl = function() {
					clearTimeout(theginza.timerId.fader);
					param.num = $(this).index();
					$mod._faderCore(param);
				};
				
				param.click && $button.bind("click", _ctrl);
				param.hover && $button.bind("mouseover", _ctrl);
				
				$mod._faderCore(param);

				return this;
			},
			
			_faderCore: function(options) {
				var param, $mod, $guide, $cell, len;
				$mod = this;
				param = {
					ms: 1000,
					wait: 5000,
					num: 0,
					ctrlId: ".ctrl",
					guide: ".guide",
					cell: "li"
				};
				$.extend(param, options);
				$guide = $mod.find(param.guide);
				$cell = $guide.find(param.cell);
				len = $cell.length;
				if (len <= Math.abs(param.num)) {
					throw RangeError("range error");
				}
				
				// クラス名でactive表示
				$mod.find(param.ctrlId)
					.find(param.cell).removeClass("active").mouseout()
					.eq(param.num).addClass("active").mouseover();
					
				$cell.stop(true).fadeTo(param.ms, 0);
				$cell.eq(param.num).stop(true).fadeTo(param.ms, 1, function() {
					clearTimeout(theginza.timerId.fader);
					theginza.timerId.fader = setTimeout(function() {
						param.num = param.num < len - 1 ? ++param.num : 0;
						$mod._faderCore(param);
					}, param.wait);
				});
				return this;
			},

			// スムーズスクロール
			scroller: function() {
				$("a[href^=#]").click(function() {
					var top = this.hash ? $(this.hash).offset().top : 0;
					$("html, body").animate({ scrollTop: top }, {
						duration: 800,
						complete: function() {
							// iPhoneでグローバルナビがついてこないバグ対策
							$(window).scroll();
						}
					});
					return false;
				});
			},
			
			webkitLayoutBug: function() {
				if ($.browser.webkit) {
					var timerId,
						wrapper = $("#wrapper");
					$(window).resize(function() {
						// 背景中央揃えで端数がずれるバグ対応
						var center = Math.round((wrapper.width() - 1323) / 2 - 0.5);
						wrapper.css("backgroundPosition", center + "px 0");
						clearTimeout(timerId);
						timerId = setTimeout(function() {
							var center = Math.round((wrapper.width() - 1323) / 2 - 0.5);
							wrapper.css("backgroundPosition", center + "px 0");
						}, 100);
					});
				}
			},
			
			webkitImgLoadBug: function() {
				if ($.browser.webkit) {
					setInterval(function() {
						theginza.setFooter();
					}, 300);
				}
			}

		};

	$.fn.extend({
		// ホバー処理
		hoverFade: function() {
			var $obj;
			$obj = this;
			
			$obj.each(function() {
				$(this).hover(function() {
					$("img", this).stop(true).fadeTo(600, 0);
				}, function() {
					// faderのコントローラ用
					if ($(this).hasClass("active")) {
						return false;
					}
					$("img", this).stop(true).fadeTo(300, 1);
				}).click(function() {
					$(this).mouseout();
				});
			});
			
			return this;
		},

		// スクロールバー追随モジュール
		follower: function() {
			var $window = $(window),
				$mod = this,
				modHeight = $mod.height() + 180 * 3; // 下に余裕を持つ
				borderWidth = 3;
				
			$window.scroll(function() {
				var modTop = $mod.offset().top,
					scrTop = $window.scrollTop(),
					winHeight = $window.height(),
					docHeight = $(document).height(),
					pos = docHeight - scrTop < modHeight ? docHeight - modHeight - borderWidth : scrTop + 30;

				if (winHeight + scrTop < modHeight + modTop && modTop < scrTop) {
					return false;
				} else if (pos < 0) {
					return false;
				}

				$mod.stop(true).animate({
					// 潜り込まないようにストッパーをつける
					top: pos
				}, {
					duration: 1000,
					easing: "easeOutQuart"
				});
			}).scroll();
			
			return this;
		},
		
		// コンテンツサイズが変わったらフォントサイズが変わったとみなす。
		checkFontSize: function() {
			var $obj, checkSize, checker;
			$obj = this;
			
			checkSize = function() {
				var h = $obj.height();
				return function() {
					var p = $obj.height();
					if (h !== p ){
						h = p;
						theginza.setFooter();
					}
				};
			};
			
			checker = checkSize();
			setInterval(checker, 300);

			return this;
		},
		
		// my account
		myaccount: function() {
			$(this).click(function(event) {
				event.preventDefault();
				document.myAccount.submit();
			});
			return this;
		},

		/**
		 * 予約状況読み込み
		 */
		loadReserve: function() {
			$(this).each(function() {
				var mod = $(this),
					xml = "/sc/xml/reservation.xml?len=7&svc=";

				switch (true) {
					case /(?:\/3f-2\/)/.test(location.pathname) || mod.hasClass("shop3f-2") :
						xml += 56366;
						break;
					case /(?:\/3f-1\/)/.test(location.pathname) || mod.hasClass("shop3f-1") :
						xml += 89100;
						break;
					case /(?:\/2f-4\/)/.test(location.pathname) || mod.hasClass("shop2f-4") :
						// 未実装
						return true;

						xml += 70133;
						break;
					default:
						return true;
				}

				$.ajax({
					url: xml,
					dataType: "xml",
					error: function(xhr, status) {
						mod._errorReserve();
					},
					success: function(result) {
						var dd = mod.find("dd"),
							th = mod.find("th"),
							td = mod.find("td");

						// エラーコード処理
						if ($(result).children("result").attr("code") == 2) {
							mod._errorReserve();
							return false;
						}

						$(result).find("reservation").each(function(i) {
							var state = "",
								span = $("<span>");
							
							switch ($(this).children("week").text()) {
								case "001":
									span.text("SUN");
									break;
								case "002":
									span.text("MON");
									break;
								case "003":
									span.text("TUE");
									break;
								case "004":
									span.text("WED");
									break;
								case "005":
									span.text("THU");
									break;
								case "006":
									span.text("FRI");
									break;
								case "007":
									span.text("SAT");
									break;
							}
							switch ($(this).children("state").text()) {
								case "001":
									state = "○";
									break;
								case "002":
									state = "△";
									break;
								case "003":
									state = "×";
									break;
							}
							th.eq(i).text($(this).children("day").text()).append(span);
							td.eq(i).text(state);
						});
					}
				});
			});

			return this;
		},
		_errorReserve: function() {
			$(this).find("dd")
				.eq(0).text("予約状況の取得に失敗しました。").end()
//				.eq(1).remove().end();
			return this;
		},

		fader: theginza.fader

	});

		
	$(function() {
		if ($.browser.msie && $.browser.version < 8 || iphone) {
			if ($("#wrapper").hasClass("home")) {
				// IE6-7のheight指定が正しく挙動しないために再指定
				$("#wrapper").height(36);
				$("#container").height(0);
			}
			if (iphone) {
				// iPhone/iPadの表示ズレ対応
				$(window).resize(function() {
					$("#container").css("marginLeft", Math.floor(($(window).width() - $("#container").width() - 1) / 2));
				}).resize();
			} else {
				// IE6-7でresize時の1pxズレ対応(中央揃えを動的に変更)
				$(window).resize(function() {
					var wrapper = $("#wrapper"),
						center = Math.round((wrapper.width() - 1323) / 2 - 0.5);
					wrapper.css("backgroundPosition", center + "px 0");
				});
			}
		}
		
		// IE6でflashが読み込まれたあと、失われたコンテンツへのアクセスがエラーとなるため
		if ($("#wrapper").hasClass("home")) {
			return false;
		}

		theginza.setFooter();
		theginza.scroller();
		theginza.webkitLayoutBug();
		theginza.webkitImgLoadBug();

		$(".hover").hoverFade();
		$(".mod-fader").length && $(".mod-fader").fader({ wait: 4000 });
	//	!iphone && $(".section").checkFontSize();
		
		if ($("#container").height() > 1083) {
			$("#area-globalNav").follower();
		}

		$("#myAccount").myaccount();
		
		$(".mod-reserve").loadReserve();
		
		//setSound();
	});


})(jQuery);


/**
 * Global Navigation Setting
 */
function setGlobalNav() {
	var path = [], level = 0, img = "", list = [], dot = "";

	path = location.pathname.replace(/\/(index\.html)?$/, "").split("/");
	level = path.length - 1;

	for (var i = level; i--; ) {
		dot += "";
	}
	img = dot + "img/common/lobalnav1.jpg";
	banner1 = dot + "img/common/banner-mirai.jpg";
	banner2 = dot + "img/common/banner-makeup.jpg";

	// 5.14 launch
	list = [
		'<ul>',
			'<li class="nav-0 hover"><a href="' + dot + 'index.html"><img src="images/common/main_menu01_off.jpg" alt="このお店について" /></a></li>',
			'<li class="nav-1 hover"><a href="' + dot + 'works/index.html"><img src="images/common/main_menu02_off.jpg" alt="お知らせ" /></a></li>',
			'<li class="nav-2 hover"><a href="' + dot + 'salary/index.html"><img src="images/common/main_menu03_off.jpg" alt="このお店の楽しみ方" /></a></li>',
			'<li class="nav-3 hover"><a href="' + dot + 'aboutus/index.html"><img src="images/common/main_menu04_off.jpg" alt="地図・ご案内" /></a></li>',
			'<li class="nav-4 hover"><a href="' + dot + 'detail/index.html"><img src="images/common/main_menu05_off.jpg" alt="フロアガイド" /></a></li>',
			'<li class="nav-5 hover"><a href="' + dot + 'contact/index.html"><img src="images/common/main_menu05_off-07.jpg" alt="フリーペーパー" /></a></li>',
		'</ul>',
	];
	document.write(list.join(''));
}


function setLeftlNav() {
	var path = [], level = 0, img = "", list = [], dot = "";

	path = location.pathname.replace(/\/(index\.html)?$/, "").split("/");
	level = path.length - 1;

	for (var i = level; i--; ) {
		dot += "";
	}
	img = dot + "img/common/lobalnav1.jpg";
	banner1 = dot + "img/common/banner-mirai.jpg";
	banner2 = dot + "img/common/banner-makeup.jpg";

	// 5.14 launch
	list = [
		'<ul>',
			'<li class="nav-0 hover"><a href="' + dot + 'voice/index.html"><img src="images/common/r_menu_btn01.jpg" alt="在籍女子の声 VOICES" /></a></li>',
			'<li class="nav-1 hover"><a href="' + dot + 'faq/index.html"><img src="images/common/r_menu_btn02.jpg" alt="よくある質問 Q&amp;A" /></a></li>',
			'<li class="nav-2 hover"><a href="' + dot + 'treatment/index.html"><img src="images/common/r_menu_btn03.jpg" alt="待遇について" /></a></li>',
			'<li class="nav-3 hover"><a href="' + dot + 'counseling/index.html"><img src="images/common/r_menu_btn04.jpg" alt="チャットでお悩み相談室 COUNSELINGROOM" /></a></li>',			
			'<li ><img src="images/common/r_menu_btn05.jpg" alt="携帯サイト MOBILE SITE" /></li>',			
		'</ul>',
	];
	document.write(list.join(''));
}

function setIndexNav(imgPath,urlStr,alt) {
	var path = [], level = 0, img = "", list = [], dot = "";

	path = location.pathname.replace(/\/(index\.html)?$/, "").split("/");
	level = path.length - 1;

	for (var i = level; i--; ) {
		dot += "";
	}
	img = dot + "img/common/lobalnav1.jpg";
	banner1 = dot + "img/common/banner-mirai.jpg";
	banner2 = dot + "img/common/banner-makeup.jpg";

	// 5.14 launch
	list = [
			'<div class="nav-0 hover left" ><a href="'+urlStr+'"><img src="'+imgPath+'" alt="'+alt+'" name="Image19" width="342" height="127" border="0" id="Image19" /></a></div>',
	];
	document.write(list.join(''));
}



