/**
 * SOYOUプラグイン
 */
(function($) {

	$.fn.extend({

		// [gif] ふわふわ
		wiggle: function(options) {
			var config = {
					speed: 500,
					top: 0,  // 初期位置
					volume: 4
				},
				self = this,
				t = 0;
			
			$.extend(config, options);
			(function() {
				self.animate({
					marginTop: (t ^= 1) * config.volume + config.top
				}, {
					duration: config.speed,
					complete: arguments.callee
				});
			})();
			return this;
		},
		
		// ページ内遷移
		transfer: function(options) {
			this.each(function() {
				var config = {
						speed: 1000,
						easing: "swing"
					},
					self = this,
					target = $(this.hash).length ? $(this.hash).offset().top : 0;

				$.extend(config, options);
				$(this).bind("click", function(event) {
					$("html, body").stop(true).animate({
						scrollTop: target
					}, {
						duration: config.speed,
						easing: config.easing
					});
					event.preventDefault();
				});
			});
			return this;
		},
		
		// バックナンバー遷移
		slidePage: function(options) {
			var showLoading = function(t) {
					$(".mod-loading").width(screen.width).height($(document).height()).stop(true).show();
				},
				hideLoading = function() {
					$(".mod-loading").stop(true).fadeOut();
				},
				createPage = function(target) {
					var container = $("<div>"),
						page = $(".page"),
						// ページの先頭へ移動
						moveToTop = function() {
							var startFlg = true;
							$("html, body").stop(true).animate({
								scrollTop: 0
							}, {
								duration: 1000,
								complete: function() {
									// 一度だけ実行させる
									if (startFlg) {
										startFlg = false;
										setTimeout(function() {
											hideLoading();
											slideCurrentPage();
										}, 500);
									}
								}
							});
						},
						// 現在のページを横へ移動
						slideCurrentPage = function() {
							page.animate({
								marginLeft: -$(document).width()
							}, {
								complete: function() {
									setTimeout(slideNewPage, 500);
								}
							});
						},
						// 新しいページの出現
						slideNewPage = function() {
							$("body").css({ overflowX: "hidden" });
							page.css({ marginLeft: $(window).width() })
								.empty()
								.prepend($(".page", container[0]).contents())
								.animate({
									marginLeft: 0
								}, {
									complete: function() {
										$("body").css({ overflowX: "auto" });
										setEventListener();
									}
								});
						},
						setEventListener = function () {
							// ページ内遷移
							$("a[href^=#]:not([class$=close])").transfer();
							$("a[href^=#][class$=close]").click(function() { return false; });
							// バックナンバー遷移
							$(".mod-backnumber a").slidePage();
						};
						
					container.load(target + " .page", moveToTop);
				};
			
			this.each(function() {
				var config = $.extend({
					}, options),
					self = this;
				
				$(this).bind("click", function(event) {
					event.preventDefault();
					showLoading();
					createPage(this.href);
				});
			});
			return this;
		},
		
		// ローディングアニメーション
		loadingImg: function(options) {
			var config = $.extend({
					delay: 200
				}, options),
				self = this,
				i = 0,
				len = self.length,
				changeImg = function() {
					i = i == len - 1 ? 0 : i + 1;
					self.removeClass("active").eq(i).addClass("active");
					setTimeout(arguments.callee, config.delay);
				};
			
			setTimeout(changeImg, config.delay);
			return this;
		}
	});

})(jQuery);

/**
 * auto height
 *
 * @copyright	keisuke YAMAMOTO <keisukey@ranadesign.com>
 * @link		http://kaelab.ranadesign.com/
 * @version		1.0
 * @date		Jun 10, 2011
 *
 * コンテンツ高さ自動調整プラグイン
 *
 * min-heightに対応。
 *
 * [オプション]
 * minHeight:		["auto"/integer] min-heightの値を指定。
 * headerHeight:	[integer] ヘッダ分(コンテンツ上)の余白を指定。
 * footerHeight:	[integer] フッタ分(コンテンツ下)の余白を指定。
 * 
 */

(function($) {

	$.fn.extend({
		autoHeight: function(options) {
			var config = {
					minHeight: "auto",
					headerHeight: 0,
					footerHeight: 0
				},
				self = $(this),
				selfHeight = self.height();
			
			$.extend(config, options);
			
			var min = parseInt(config.minHeight, 10);
			config.minHeight =  min > selfHeight ? min : selfHeight;

			$(window).resize(function() {
				var windowHeight = $(window).height(),
					h = windowHeight - (config.headerHeight + config.footerHeight);
				// chrome対策
				if (h > config.minHeight) {
					self.height(h);
					$("body").css("overflow", "hidden");
				} else {
					$("body").css("overflow", "auto");
				}

			}).resize();
		
			return this;
		}
	});

})(jQuery);


/**
 * floater
 * 
 * @category    jQuery plugin
 * @license     http://www.opensource.org/licenses/mit-license.html  MIT License
 * @copyright   2010 RaNa design associates, inc.
 * @author      keisuke YAMAMOTO <keisukey@ranadesign.com>
 * @link        http://kaelab.ranadesign.com/
 * @version     1.2
 * @date        Jun 10, 2011
 *
 * コンテンツをスクロールに追従させ、ドキュメントの上下に接着させるプラグイン。
 *
 * [オプション]
 * marginTop:     上の余白
 * marginBottom:  下の余白
 * agility:       0～1(初期値 0.5)。0はナビが通り過ぎるまで待つ。1はナビの末端が出るとすぐ動きだす。
 * speed:         アニメーションにかける時間。
 * easing:        イージング
 * fixed:         trueでposition: fixedに切り替え。初期値はfalse。
 *
 */

(function($) {

    $.fn.extend({

        floater: function(options) {
            var config = {
                marginTop: 0,
                marginBottom: 0,
                agility: 0.5,  // 0-1
                speed: 1000,
                easing: "swing",
                fixed: false,
                callback: $.noop,
                exclude: [-1, -1]
            };
            $.extend(config, options);
            config.agility = $(this).outerHeight() - $(window).height() * config.agility;
            config.marginTop = parseInt(config.marginTop, 10) || 0;
            config.marginBottom = parseInt(config.marginBottom, 10) || 0;

            $(this).each(function() {
                var self = $(this);
                var d = $(document).height();
                var h = self.outerHeight() + config.marginTop + config.marginBottom;

                if (config.fixed === true) {
                    self._fixed(config);
                    return true;
                }

                self.css("position", "absolute");

                $(window).scroll(function() {
                    var s = $(window).scrollTop();
                    var w = $(window).height();

                    if (Math.abs(s - self.offset().top) < config.agility) {
                        return;
                    }
                    // 除外範囲
                    if (config.exclude[0] <= s && s <= config.exclude[1]) {
                    	return;
                    }

                    self.stop(true).animate({
                        top: (d - h) * s / (d - w) + config.marginTop
                    }, {
                        duration: config.speed,
                        easing: config.easing,
                        complete: config.callback
                    });
                }).scroll();
            });

            return this;
        },
        
        _fixed: function(config) {
            if ($.browser.msie && $.browser.version < 7) {
                $(this).css("position", "absolute");
                this[0].style.cssText = "top: expression(documentElement.scrollTop + " + config.marginTop + " + 'px')";
                document.body.style.background = "url(null) fixed";
            } else {
                $(this).css("position", "fixed").css("top", config.marginTop);
            }
        }
        
    });

})(jQuery);

