// script.js
(function (window, $) {

    var document = window.document,
        location = window.location;

    var iOS = /iPad|iPod|iPhone/.test(navigator.userAgent);

    // https://gist.github.com/527683
    var ie = (function () {
            var v = 3,
                div = document.createElement('div'),
                all = div.getElementsByTagName('i');
            do {
                div.innerHTML = '<!--[if gt IE ' + (++v) + ']><i></i><![endif]-->';
            } while (all[0]);
            return (v > 4)? v: void 0;
        }());

    //$('html').removeClass('no-js').addClass('js');

    $(function () {

        $('#header a, #showcase a').smoothScroll( { duration: 1500, easing: 'easeOutExpo'} );

        $('#contents').find('img').lazyLoad({
            threshold: -200,
            duration: 1000,
            easing: 'jswing'
        });

        $('#ctrl').each(function () {
            var $window = $(window),
                $this = $(this),
                $links = $this.find('a[href*="#"]').filter(function () {
                    return $($(this)[0].hash).length;
                }),
                $linkWrappers = $links.parent(),
                containerWidth = $('#container').width(),
                offsetTop = $this.offset().top,
                
                // for iOS
                delay = 0,            // アニメーションが始まるまでの時間
                duration = 0,         // アニメーションにかかる時間
                easing = 'swing'; // イージングタイプ

            $window.on('scroll', function () {
                var x = $window.scrollLeft(),
                    y = $window.scrollTop() + 50,
                    winHeight = $window.height(),
                    docHeight = $(document).height();
                if (iOS) {
                    if (y > offsetTop) {
                        setTimeout(function () {
                            $this.stop().animate({ marginTop: y - offsetTop - 50 }, duration, easing);
                        }, delay);
                    } else {
                        setTimeout(function () {
                            $this.stop().animate({ marginTop: 0 }, duration, easing);
                        }, delay);
                    }
                } else {
                    if (x > 0) {
                        $this.css({ marginLeft: - x });
                    } else {
                        $this.css({ marginLeft: 0 });
                    }
                }
                $links.each(function (i, link) {
                    var $link = $(link),
                        $linkWrapper = $($linkWrappers[i]),
                        gte = (i === 0)? 0: $(link.hash).offset().top,
                        lt = (i < $links.length - 1)? $($links[i + 1].hash).offset().top: docHeight;
                    if (y + winHeight > docHeight) {
                        $linkWrappers.removeClass('current');
                        $linkWrappers.last().addClass('current');
                        return false;
                    } else if ((y >= gte) && (y < lt)) {
                        $linkWrappers.removeClass('current');
                        $linkWrapper.addClass('current');
                        return false;
                    }
                });
            });

            $window.trigger('scroll');

            if (!iOS) {
                $this.floatingWidget();
            }

            $links.smoothScroll({
                duration: 1500,
                easing: 'easeOutExpo'
            });

        });

        if (location.hash) {
            $(window).trigger('scroll');
        }

    });

})(window, jQuery);
