/*
 * Copyright (C) 2011 hagane soft. All rights reserved.
 */
;
(function($) {
    /**
     * DQ風画面を実現するプラグイン
     */
    $.fn.dq = function(options) {
        // 初期値を設定
        var defaults = {
            containerClass : '.dq-container'
        };
        // オプションの初期値を設定
        var setting = $.extend(defaults, options);

        // ループを回すことにより指定されたすべての要素を処理する
        this.each(function(){
                
            // 最初にトップ以外のコンテナを全部隠す
            // thisの中にあるコンテナクラスを指定することによりトップ要素が隠れない
            $(setting.containerClass, this).hide()
            
            $('li', this)
            .hover(
                function(){ // リスト要素がマウスホバーした場合
                    // 三角形を表示する
                    $(this).css('background', 'url(img/allow.png) no-repeat scroll 0 center transparent');
                }
                , function(){ // マウスが要素から外れた場合
                    // 三角形を隠す
                    $(this).css('background', 'none');
                })
            // クリックした場合は直下の子要素だけを表示
            .click(function(event){
                // クリックイベントのバブリングを停止
                event.stopPropagation();
                
                var child = $(this).children(setting.containerClass);
                // 子要素だけを操作
                if (child.length ){ // 子要素にコンテナがある場合
                    // オープンしていることを示すマーキングクラスを追加
                    child.addClass("dq-console-open").show();
                
                    var caption = child.children(".dq-caption");
                    // キャプションがある場合
                    if (caption.length){
                        // サイズの計算に影響があるので先に指定
                        caption.css({
                            "position": "absolute", 
                            "background-color" : "#000",
                            "padding" : "1px 5px"
                        });
                
                        // 横はセンターに来るように配置
                        var left = Math.round(child.outerWidth() / 2 - caption.outerWidth() / 2);
                        // 縦は上側に食い込ませる
                        var top = Math.round( - caption.outerHeight() / 2 );
                        // 計算結果をCSSで反映
                        caption.css({
                            "left" : left + "px", 
                            "top" : top + "px"
                        });
                    } 
                } else { // 子要素にコンテナがない場合
                    //　開いているウィンドウ(マーキングクラスを持つもの)をすべて閉じる
                    $(".dq-console-open").each(function(){
                        // 非表示にした後、マーキングクラスを除去
                        $(this).hide().removeClass("dq-console-open");
                    });
                }
            });
            // マウスが外れたときに実行する関数のタイマ
            var timerId;
            // コンテナとマウスの挙動
            $(setting.containerClass, this).hover(
                function(){ // コンテナ上にある場合は何もしない
                    if (timerId) { // タイマーがセットされていた(ちょっと要素からはずれてスグに戻ってきた)場合はキャンセルする
                        clearTimeout(timerId);
                    }
                },
                function(){　// コンテナからマウスが外れた場合は隠す
                    var sideup = function(element){
                        $(element).hide().removeClass("dq-console-open");
                    }
                    // タイマーで実行を少し遅らせる マウスオーバーした直後すぐには実行させない
                    timerId = setTimeout(sideup, 1000, this);
                });
        });
        return this;
    };
})(jQuery);

