/**
 * グローバル・ナビゲーション
 */
(function($) {
    var navigate;
    
    $.fn.navigate = navigate = function(options) {
        return this.each(function() {
            var instance = $.data(this, 'navigate');
            if (instance) {
                $.extend(true, instance.options, options)
                instance.init();
            } else {
                $.data(this, 'navigate', new navigate.prototype.create(options, this));
            }
        });
    };
    
    navigate.prototype = {
        /**
         * デフォルト設定
         * 呼び出し時に引数で指定することで上書き可能
         */
        options: {
            navLinksID: 'globalNavLinks'
        },
        
        /**
         * コンストラクタ
         * @param object options
         * @param jQuery element
         */
        create: function(options, element) {
            this.originalElement = element;
            this.options         = $.extend(true, {}, this.options, options);
            this.isMoving        = null;
            
            this.naviDiv = null;
            this.tailDiv = null;
            
            this.init();
        },
        
        /**
         * 初期実行処理
         */
        init: function() {
            this.isMoving = false;
            
            var self = this;
            var here = location.href.split('/')[3];
            var pLeft = null, width = 0, href;
            $('#' + this.options.navLinksID, this.originalElement).children().each(function(i) {
                // 今どこにいる？
                href = $('a', $(this)).attr('href').replace(/\//g, '');
                if (here == href) {
                    pLeft = $(this).position().left;
                    width = $(this).width();
                } else {
                    // マウスオーバー時の挙動を設定
                    $(this).bind('mouseenter', function(event) {
                        return self.mouseEnter(event);
                    }).bind('mouseleave', function(event) {
                        return self.mouseLeave(event);
                    });
                }
                
                // 使い方マニュアルの子メニュースライドを設定
                if (href == 'manual') {
                    // 用語集とFAQも使い方ガイドカテゴリに含む
                	if (here == 'glossary' || here == 'faq') {
                        pLeft = $(this).position().left;
                        width = $(this).width();
                    }
                    $(this).bind('mouseenter', function(event) {
                        return self.manualDown(event);
                    }).bind('mouseleave', function(event) {
                        return self.manualUp(event);
                    });
                    $('a', $(this)).eq(0).bind('click', function(event) {
                        // クリック無効
                        return false;
                    });
                    // サイズ調整＆関数のバインド
                    var subMenu = $(this).children('ul');
                    subMenu.css({
                        top  : $(this).height() + 'px',
                    	left : $(this).position().left + 'px',
                    	width: $(this).innerWidth() + 'px' 
                    }).bind('mouseleave', function(event) {
                        return self.manualSelfUp(event);
                    });
                    subMenu.children('li').css('width', $(this).width() + 'px');
                }
            });
            
            if (pLeft != null) {
                // 現在地マーカーとマウスに追従するマーカーを追加
                var div = document.createElement('div');
                this.naviDiv = $(div);
                this.naviDiv.css({
                    height         : '4px',
                    width          : width + 'px',
                    marginTop      : '-3px',
                    backgroundColor: '#ffc500',
                    position       : 'relative',
                    left           : pLeft + 'px',
                    zIndex         : '1'
                });
                $(this.originalElement).append(this.naviDiv);
                
                div = document.createElement('div');
                this.tailDiv = $(div);
                this.tailDiv.css({
                    height         : '4px',
                    width          : width + 'px',
                    marginTop      : '-4px',
                    backgroundColor: '#ffde27',
                    position       : 'relative',
                    left           : pLeft + 'px',
                    zIndex         : '0'
                });
                $(this.originalElement).append(this.tailDiv);
            } else {
                // 現在地が存在しないとき
                $('#' + this.options.navLinksID, this.originalElement).css({
                    position: 'relative',
                    zIndex: '1'
                });
                var div = document.createElement('div');
                this.tailDiv = $(div);
                this.tailDiv.css({
                    height         : '4px',
                    width          : '0px',
                    marginTop      : '-4px',
                    backgroundColor: '#ffde27',
                    position       : 'relative',
                    top            : '-4px',
                    left           : '0px',
                    zIndex         : '0'
                });
                $(this.originalElement).append(this.tailDiv);
            }
        },
        
        /**
         * メニューにマウスカーソルが乗ったとき
         */
        mouseEnter: function(event) {
            if (this.isMoving) {
                this.tailDiv.stop();
                this.isMoving = false;
            }
            var target = $(event.target).parent();
            var self = this;
            this.isMoving = true;
            if (this.naviDiv != null) {
                // ホームポジションからカーソルが乗ったメニューまで移動
                this.tailDiv.animate({
                    width: target.width() + 'px',
                    left : target.position().left + 'px'
                }, 250, 'easeOutQuint', function() {
                    self.isMoving = false;
                });
            } else {
                if (this.tailDiv.css('top') == '-4px') {
                    // バーが隠れている状態なら、カーソルが乗ったメニューの下に現れる
                    this.tailDiv.css({
                        width: target.width() + 'px',
                        left : target.position().left + 'px'
                    }).animate({top:'0px'}, 400, 'easeOutQuint', function() {
                        self.isMoving = false;
                    });
                } else {
                    // バーが表示されているなら、そのままスライドする
                    this.tailDiv.animate({
                        width: target.width() + 'px',
                        left : target.position().left + 'px',
                        top  : '0px'
                    }, 250, 'easeOutQuint', function() {
                        self.isMoving = false;
                    });
                }
            }
        },
        
        /**
         * メニューからマウスカーソルが離れたとき
         */
        mouseLeave: function(event) {
            if (this.isMoving) {
                this.tailDiv.stop();
                this.isMoving = false;
            }
            this.isMoving = true;
            var self = this;
            if (this.naviDiv != null) {
                // ホームポジションに戻る
                this.tailDiv.animate({
                    width: self.naviDiv.width() + 'px',
                    left: self.naviDiv.position().left + 'px'
                }, 400, 'easeOutBounce', function() {
                    self.isMoving = false;
                });
            } else {
                // 上に引っ込む
                this.tailDiv.animate({top:'-4px'}, 800, 'easeOutQuint', function() {
                    self.isMoving = false;
                });
            }
        },
        
        /**
         * 「使い方ガイド」にマウスホバー時に
         * 使い方マニュアルの子メニューを表示する
         */
        manualDown: function(event) {
            var li = $(event.target).parent();
            $('ul', li).slideDown(200);
        },
        
        /**
         * 「使い方ガイド」からマウスが離れた時に
         * 使い方マニュアルの子メニューを隠す
         */
        manualUp: function(event) {
            var li = $(event.target).parent();
            $('ul', li).slideUp(200);
        },
        
        /**
         * 表示された子メニューからマウスが離れた時に
         * 使い方マニュアルの子メニューを隠す
         */
        manualSelfUp: function(event) {
        	var ul = $(event.target).parent().parent();
        	ul.slideUp(200);
        }
    };
    
    navigate.prototype.create.prototype = navigate.prototype;
    
})(jQuery);

