<?php
if (!defined('_GNUBOARD_')) exit; // 개별 페이지 접근 불가

if(G5_COMMUNITY_USE === false) {
    include_once(G5_THEME_MSHOP_PATH.'/shop.head.php');
    return;
}

include_once(G5_THEME_PATH.'/head.sub.php');
include_once(G5_LIB_PATH.'/latest.lib.php');
include_once(G5_LIB_PATH.'/outlogin.lib.php');
include_once(G5_LIB_PATH.'/poll.lib.php');
include_once(G5_LIB_PATH.'/visit.lib.php');
include_once(G5_LIB_PATH.'/connect.lib.php');
include_once(G5_LIB_PATH.'/popular.lib.php');
?>

<header id="hd">
    <h1 id="hd_h1"><?php echo $g5['title'] ?></h1>

    <div class="to_content"><a href="#container">본문 바로가기</a></div>

    <?php
    if(defined('_INDEX_')) { // index에서만 실행
        include G5_MOBILE_PATH.'/newwin.inc.php'; // 팝업레이어
    } ?>

    <div id="hd_wrapper">

        <div id="logo">
            <a href="<?php echo G5_URL ?>"><img src="<?php echo G5_IMG_URL ?>/m_logo.png" alt="<?php echo $config['cf_title']; ?>"></a>
        </div>

        <button type="button" id="gnb_open" class="hd_opener"><i class="fa fa-bars" aria-hidden="true"></i><span class="sound_only"> 메뉴열기</span></button>

        <div id="gnb" class="hd_div">
            <button type="button" id="gnb_close" class="hd_closer"><span class="sound_only">메뉴 </span>닫기</button>

            <?php
            $sql = " select *
                        from {$g5['menu_table']}
                        where me_mobile_use = '1'
                          and length(me_code) = '2'
                        order by me_order, me_id ";
            $result = sql_query($sql, false);
            $menu_datas = array();

            for($i=0; $row=sql_fetch_array($result); $i++) {
                $menu_datas[$i] = $row;

                $sql2 = " select *
                            from {$g5['menu_table']}
                            where me_mobile_use = '1'
                              and length(me_code) = '4'
                              and substring(me_code, 1, 2) = '{$row['me_code']}'
                            order by me_order, me_id ";
                $result2 = sql_query($sql2);

                for ($k=0; $row2=sql_fetch_array($result2); $k++) {
                    $menu_datas[$i]['sub'][$k] = $row2;
                }
            }
            ?>

            <div class="gnb_quick_wrap">
                <label for="gnb_quick_select" class="sound_only">빠른 메뉴 이동</label>
                <select id="gnb_quick_select" aria-label="빠른 메뉴 이동">
                    <option value="">빠른 이동 메뉴를 선택하세요</option>
                    <?php
                    $quick_items = array();
                    $quick_order = 0;
                    $quick_exact_pinned = array(
                        '대시보드' => 1,
                        '메인' => 1,
                        '홈' => 1,
                        '이용자 현황' => 2,
                        '이용자관리' => 2,
                        '사용자관리' => 2,
                        '아이디 관리' => 3,
                        '아이디관리' => 3,
                        '정산' => 4,
                        '설정' => 5
                    );
                    $quick_pinned = array(
                        '대시보드' => 1,
                        '메인' => 1,
                        '홈' => 1,
                        '이용자' => 2,
                        '사용자' => 2,
                        '아이디' => 3,
                        '정산' => 4,
                        '설정' => 5
                    );
                    $quick_keywords = array(
                        '대시보드', '메인', '홈', '시작',
                        'vpn', '아이디', '이용자', '사용자', '세션',
                        '고객', '회원', '주문', '결제', '정산',
                        '로그', '모니터', '트래픽', '시스템',
                        '관리자', '관리', '설정', '권한', '보안',
                        '문의', '지원', '알림'
                    );

                    foreach( $menu_datas as $quick_row ) {
                        $quick_label = '[메인] ' . $quick_row['me_name'];
                        $quick_label_lc = mb_strtolower($quick_label, 'UTF-8');
                        $quick_name = trim(str_replace(array('[메인]', '└'), '', $quick_label));
                        $quick_name_lc = mb_strtolower($quick_name, 'UTF-8');
                        $quick_pin_rank = 99;
                        foreach($quick_exact_pinned as $pin_name => $pin_rank) {
                            if ($quick_name_lc === mb_strtolower($pin_name, 'UTF-8') && $pin_rank < $quick_pin_rank) {
                                $quick_pin_rank = $pin_rank;
                            }
                        }
                        foreach($quick_pinned as $pin_keyword => $pin_rank) {
                            if (stripos($quick_label_lc, mb_strtolower($pin_keyword, 'UTF-8')) !== false && $pin_rank < $quick_pin_rank) {
                                $quick_pin_rank = $pin_rank;
                            }
                        }
                        $quick_priority = 50;
                        foreach($quick_keywords as $keyword) {
                            if (stripos($quick_label_lc, mb_strtolower($keyword, 'UTF-8')) !== false) {
                                $quick_priority = 10;
                                break;
                            }
                        }

                        $quick_items[] = array(
                            'label' => $quick_label,
                            'link' => $quick_row['me_link'],
                            'target' => $quick_row['me_target'],
                            'pin_rank' => $quick_pin_rank,
                            'priority' => $quick_priority,
                            'order' => $quick_order++
                        );

                        foreach( (array) $quick_row['sub'] as $quick_row2 ) {
                            $quick_sub_label = '└ ' . $quick_row2['me_name'];
                            $quick_sub_label_lc = mb_strtolower($quick_sub_label, 'UTF-8');
                            $quick_sub_name = trim(str_replace(array('[메인]', '└'), '', $quick_sub_label));
                            $quick_sub_name_lc = mb_strtolower($quick_sub_name, 'UTF-8');
                            $quick_sub_pin_rank = 99;
                            foreach($quick_exact_pinned as $pin_name => $pin_rank) {
                                if ($quick_sub_name_lc === mb_strtolower($pin_name, 'UTF-8') && $pin_rank < $quick_sub_pin_rank) {
                                    $quick_sub_pin_rank = $pin_rank;
                                }
                            }
                            foreach($quick_pinned as $pin_keyword => $pin_rank) {
                                if (stripos($quick_sub_label_lc, mb_strtolower($pin_keyword, 'UTF-8')) !== false && $pin_rank < $quick_sub_pin_rank) {
                                    $quick_sub_pin_rank = $pin_rank;
                                }
                            }
                            $quick_sub_priority = 60;
                            foreach($quick_keywords as $keyword) {
                                if (stripos($quick_sub_label_lc, mb_strtolower($keyword, 'UTF-8')) !== false) {
                                    $quick_sub_priority = 20;
                                    break;
                                }
                            }

                            $quick_items[] = array(
                                'label' => $quick_sub_label,
                                'link' => $quick_row2['me_link'],
                                'target' => $quick_row2['me_target'],
                                'pin_rank' => $quick_sub_pin_rank,
                                'priority' => $quick_sub_priority,
                                'order' => $quick_order++
                            );
                        }
                    }

                    usort($quick_items, function($a, $b) {
                        if ($a['pin_rank'] != $b['pin_rank']) {
                            return ($a['pin_rank'] < $b['pin_rank']) ? -1 : 1;
                        }
                        if ($a['priority'] == $b['priority']) {
                            if ($a['order'] == $b['order']) {
                                return 0;
                            }
                            return ($a['order'] < $b['order']) ? -1 : 1;
                        }
                        return ($a['priority'] < $b['priority']) ? -1 : 1;
                    });

                    foreach($quick_items as $quick_item) {
                    ?>
                        <option value="<?php echo $quick_item['link']; ?>" data-target="<?php echo ($quick_item['target'] === 'blank') ? '_blank' : '_self'; ?>"><?php echo $quick_item['label']; ?></option>
                    <?php } ?>
                </select>
                <button type="button" id="gnb_quick_go">이동</button>
            </div>

            <ul id="gnb_1dul">
            <?php foreach( $menu_datas as $row ) { ?>
                <li class="gnb_1dli">
                    <a href="<?php echo $row['me_link']; ?>" target="_<?php echo $row['me_target']; ?>" class="gnb_1da"><?php echo $row['me_name'] ?></a>
                    <?php
                    $k = 0;
                    foreach( (array) $row['sub'] as $row2 ) {
                        if($k == 0)
                            echo '<button type="button" class="btn_gnb_op">하위분류</button><ul class="gnb_2dul">'.PHP_EOL;
                    ?>
                        <li class="gnb_2dli"><a href="<?php echo $row2['me_link']; ?>" target="_<?php echo $row2['me_target']; ?>" class="gnb_2da"><span></span><?php echo $row2['me_name'] ?></a></li>
                    <?php
                        $k++;
                    }

                    if($k > 0)
                        echo '</ul>'.PHP_EOL;
                    ?>
                </li>
            <?php } ?>

            <?php if (count($menu_datas) == 0) {  ?>
                <li id="gnb_empty">메뉴 준비 중입니다.<?php if ($is_admin) { ?> <br><a href="<?php echo G5_ADMIN_URL; ?>/menu_list.php">관리자모드 &gt; 환경설정 &gt; 메뉴설정</a>에서 설정하세요.<?php } ?></li>
            <?php } ?>
                <li class="gnb_1dli"><a href="<?php echo G5_SHOP_URL ?>" class="gnb_1da"> 쇼핑몰</a></li>
            </ul>

            <div id="hd_sch">
                <h2>사이트 내 전체검색</h2>
                <form name="fsearchbox" action="<?php echo G5_BBS_URL ?>/search.php" onsubmit="return fsearchbox_submit(this);" method="get">
                <input type="hidden" name="sfl" value="wr_subject||wr_content">
                <input type="hidden" name="sop" value="and">
                <input type="text" name="stx" id="sch_stx" placeholder="검색어(필수)" required maxLength="32">
                <button type="submit" value="검색" id="sch_submit"><i class="fa fa-search" aria-hidden="true"></i><span class="sound_only">검색</span></button>
                </form>

                <script>
                function fsearchbox_submit(f)
                {
                    if (f.stx.value.length < 2) {
                        alert("검색어는 두글자 이상 입력하십시오.");
                        f.stx.select();
                        f.stx.focus();
                        return false;
                    }

                    // 검색에 많은 부하가 걸리는 경우 이 주석을 제거하세요.
                    var cnt = 0;
                    for (var i=0; i<f.stx.value.length; i++) {
                        if (f.stx.value.charAt(i) == ' ')
                            cnt++;
                    }

                    if (cnt > 1) {
                        alert("빠른 검색을 위하여 검색어에 공백은 한개만 입력할 수 있습니다.");
                        f.stx.select();
                        f.stx.focus();
                        return false;
                    }

                    return true;
                }
                </script>
            </div>
        </div>

        <button type="button" id="user_btn" class="hd_opener"><i class="fa fa-user" aria-hidden="true"></i><span class="sound_only">사용자메뉴</span></button>
        <div class="hd_div" id="user_menu">
            <button type="button" id="user_close" class="hd_closer"><span class="sound_only">메뉴 </span>닫기</button>

            <?php echo outlogin('theme/basic'); // 외부 로그인 ?>

            <ul id="hd_nb">
                <li class="hd_nb1"><a href="<?php echo G5_BBS_URL ?>/qalist.php" id="snb_qa"><i class="fa fa-comments" aria-hidden="true"></i><br>1:1문의</a></li>
                <li class="hd_nb2"><a href="<?php echo G5_BBS_URL ?>/faq.php" id="snb_faq"><i class="fa fa-question-circle" aria-hidden="true"></i><br>FAQ</a></li>
                <li class="hd_nb3"><a href="<?php echo G5_BBS_URL ?>/current_connect.php" id="snb_cnt"><i class="fa fa-users" aria-hidden="true"></i><br>접속자 <span><?php echo connect('theme/basic'); // 현재 접속자수 ?></span></a></li>
                <li class="hd_nb4"><a href="<?php echo G5_BBS_URL ?>/new.php" id="snb_new"><i class="fa fa-history" aria-hidden="true"></i><br>새글</a></li>

            </ul>

            <div id="text_size">
            <!-- font_resize('엘리먼트id', '제거할 class', '추가할 class'); -->
                <button id="size_down" onclick="font_resize('container', 'ts_up ts_up2', '', this);" class="select"><img src="<?php echo G5_URL; ?>/img/ts01.png" width="20" alt="기본"></button>
                <button id="size_def" onclick="font_resize('container', 'ts_up ts_up2', 'ts_up', this);"><img src="<?php echo G5_URL; ?>/img/ts02.png" width="20" alt="크게"></button>
                <button id="size_up" onclick="font_resize('container', 'ts_up ts_up2', 'ts_up2', this);"><img src="<?php echo G5_URL; ?>/img/ts03.png" width="20" alt="더크게"></button>
            </div>
        </div>

        <script>
        $(function () {
            //폰트 크기 조정 위치 지정
            var font_resize_class = get_cookie("ck_font_resize_add_class");
            if( font_resize_class == 'ts_up' ){
                $("#text_size button").removeClass("select");
                $("#size_def").addClass("select");
            } else if (font_resize_class == 'ts_up2') {
                $("#text_size button").removeClass("select");
                $("#size_up").addClass("select");
            }

            $(".hd_opener").on("click", function() {
                var $this = $(this);
                var $hd_layer = $this.next(".hd_div");

                if($hd_layer.is(":visible")) {
                    $hd_layer.hide();
                    $this.find("span").text("열기");
                } else {
                    var $hd_layer2 = $(".hd_div:visible");
                    $hd_layer2.prev(".hd_opener").find("span").text("열기");
                    $hd_layer2.hide();

                    $hd_layer.show();
                    $this.find("span").text("닫기");
                }
            });

            $("#container").on("click", function() {
                $(".hd_div").hide();

            });

            $(".btn_gnb_op").click(function(){
                $(this).toggleClass("btn_gnb_cl").next(".gnb_2dul").slideToggle(300);

            });

            function moveMobileQuickMenu() {
                var $selected = $("#gnb_quick_select option:selected");
                var link = $selected.val();
                if (!link) {
                    return;
                }
                var target = $selected.data("target") || "_self";
                if (target === "_blank") {
                    window.open(link, "_blank");
                    return;
                }
                window.location.href = link;
            }

            $("#gnb_quick_go").on("click", function(){
                moveMobileQuickMenu();
            });

            $("#gnb_quick_select").on("change", function(){
                moveMobileQuickMenu();
            });

            $(".hd_closer").on("click", function() {
                var idx = $(".hd_closer").index($(this));
                $(".hd_div:visible").hide();
                $(".hd_opener:eq("+idx+")").find("span").text("열기");
            });
        });
        </script>

    </div>
</header>



<div id="wrapper">

    <div id="container">
    <?php if (!defined("_INDEX_")) { ?><h2 id="container_title" class="top" title="<?php echo get_text($g5['title']); ?>"><?php echo get_head_title($g5['title']); ?></h2><?php } ?>
