

$(function() {


	// 인기검색어순위 시작
    initHotkeyword = function() {
        var hotkeywordRollUl = $('.hotkeyword .keyword_roll ul'),
            hotkeywordRollLi = hotkeywordRollUl.append(hotkeywordRollUl.html()).children(),
            hotkeywordRollHeight = $('.hotkeyword .keyword_roll').height() * -1,
            scrollSpeed = 600,
            speed = 3000 + scrollSpeed;
        var hotkeywordListAnchor = $('.keyword_list .rank_list div > a:not(.nodata)');
        var curKeywordId = 0;
        var curRank = 0;
        var timer = null;
        if (hotkeywordListAnchor.length <= 10) {
            $('.hotkeyword button').attr('disabled', true);
        }
        if (hotkeywordListAnchor.length >= 1) {
            function sliderText() {
                var hotkeywordRollFoucs = hotkeywordRollUl.position().top / hotkeywordRollHeight;
                hotkeywordRollFoucs = Math.floor((hotkeywordRollFoucs + 1) % hotkeywordRollLi.length);
                curRank = (hotkeywordRollFoucs == hotkeywordListAnchor.length) ? 0 : hotkeywordRollFoucs;
                hotkeywordListAnchor.removeClass('active');
                hotkeywordListAnchor.eq(curRank).addClass('active');
                if (curRank >= 10) {
                    curKeywordId = 1;
                    $(".rank_group").eq(0).removeClass('active');
                    $(".rank_group").eq(1).addClass('active');
                } else {
                    curKeywordId = 0;
                    $(".rank_group").eq(1).removeClass('active');
                    $(".rank_group").eq(0).addClass('active');
                }
                hotkeywordRollUl.animate({
                    top: hotkeywordRollFoucs * hotkeywordRollHeight
                }, scrollSpeed, function() {
                    if (hotkeywordRollFoucs == hotkeywordRollLi.length / 2) {
                        hotkeywordRollUl.css('top', 0);
                    }
                });
                timer = setTimeout(sliderText, speed);
            }
            $('.hotkeyword').hover(function() {
                if (timer != null) {
                    clearTimeout(timer);
                }
                timer = null;
            }, function() {
                $('.hotkeyword').addClass('active');
                timer = setTimeout(sliderText, speed);
            });
            var mediaquery = "(max-width:910px)";
            if (window.matchMedia(mediaquery).matches == false) {
                if (timer == null) {
                    $('.hotkeyword').addClass('active');
                    timer = setTimeout(sliderText, speed);
                }
            }
            $(window).resize(function() {
                if (window.matchMedia(mediaquery).matches == false) {
                    if (timer == null) {
                        $('.hotkeyword').addClass('active');
                        timer = setTimeout(sliderText, speed);
                    }
                } else {
                    $('.hotkeyword').removeClass('active');
                    if (timer != null) {
                        clearTimeout(timer);
                    }
                    timer = null;
                }
            });
        }
        $('.hotkeyword button.btn_prev').click(function() {
            curKeywordId--;
            curKeywordId = curKeywordId == -1 ? 1 : curKeywordId;
            $(".rank_group").removeClass('active');
            $(".rank_group").eq(curKeywordId).addClass('active');
        });
        $('.hotkeyword button.btn_next').click(function() {
            curKeywordId++;
            curKeywordId = curKeywordId == 2 ? 0 : curKeywordId;
            $(".rank_group").removeClass('active');
            $(".rank_group").eq(curKeywordId).addClass('active');
        });
    };
    initHotkeyword();
	// 인기검색어순위 종료


    


});

function goDate(date, stx) {
        document.location.href = '/bbs/search.php?date='+date+'&stx=' + stx;
    }

	$.urlParam = function(name) {
        var results = new RegExp('[\?&]' + name + '=([^&#]*)').exec(window.location.href);
        if (results == null) {
            return null;
        } else {
            return results[1] || 0;
        }
    }

    var pattern = /^([0-9]{4})-([0-9]{2})-([0-9]{2})$/;
    var defaultDate = new Date();

	
    if ($.urlParam('date') && pattern.test($.urlParam('date'))) {
        defaultDate = $.urlParam('date');
    }

	

    $('#datetimepicker').datetimepicker({
        icons: {
            date: 'fa fa-calendar',
            time: 'fa fa-clock-o',
            up: 'fa fa-chevron-up',
            down: 'fa fa-chevron-down',
            previous: 'fa fa-chevron-left',
            next: 'fa fa-chevron-right',
            today: 'fa fa-crosshairs',
            clear: 'fa fa-trash-o',
            close: 'fa fa-times'
        },
        inline: true,
        locale: 'ko',
        dayViewHeaderFormat: 'YYYY년 MMMM',
        format: "YYYYMMDD",
        viewMode: "days",
        minDate: '2018-01-01',
        maxDate: new Date(),
        defaultDate: defaultDate
    }).on("dp.show", function(e) {
        $(".datepicker-months .picker-switch").removeAttr('title');
        $(".datepicker .datepicker-years, .datepicker .datepicker-decades").remove();
    }).on("dp.change", function(e) {
        //goDate(e.date.format('YYYY-MM-DD'))
		goDate(e.date.format('YYYY-MM-DD'), e.date.format('YYMMDD'))
    });
		

    $(".datepicker-months .picker-switch").addClass('disable').on('click', function(e) {
        e.stopPropagation();
    });
    $(".datepicker .datepicker-days").on('click', 'td.day.active', function() {
       // goDate($('#datetimepicker').data('date'),$('#datetimepicker').data('date'))
    });


function sendSns(sns, url, txt) {
    var o;
    var _url = encodeURIComponent(url);
    var _txt = encodeURIComponent(txt);
    var _br = encodeURIComponent('\r\n');
    switch (sns) {
        case 'facebook':
            o = {
                method: 'popup',
                url: 'http://www.facebook.com/sharer/sharer.php?u=' + _url
            };
            break;
        case 'twitter':
            o = {
                method: 'popup',
                url: 'http://twitter.com/intent/tweet?text=' + _txt + '&url=' + _url
            };
            break;
        case 'me2day':
            o = {
                method: 'popup',
                url: 'http://me2day.net/posts/new?new_post[body]=' + _txt + _br + _url + '&new_post[tags]=epiloum'
            };
            break;
        case 'kakaostory':
            o = {
                method: 'popup',
                url: 'https://story.kakao.com/share?url=' + _url
            };
            break;
        case 'band':
            o = {
                method: 'popup',
                url: 'http://www.band.us/plugin/share?body=' + _txt + _br + _url
            };
            break;
        default:
            alert('지원하지 않는 SNS입니다.');
            return false;
    }
    switch (o.method) {
        case 'popup':
            var left = (screen.width / 2) - (500 / 2);
            var top = (screen.height / 2) - (400 / 2);
            window.open(o.url, 'snspopup', 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=500, height=400, top=' + top + ', left=' + left);
            break;
        case 'web2app':
            if (navigator.userAgent.match(/android/i)) {
                setTimeout(function() {
                    location.href = 'intent://' + o.param + '#Intent;' + o.g_proto + ';end'
                }, 100);
            } else if (navigator.userAgent.match(/(iphone)|(ipod)|(ipad)/i)) {
                setTimeout(function() {
                    location.href = o.a_store;
                }, 200);
                setTimeout(function() {
                    location.href = o.a_proto + o.param
                }, 100);
            } else {
                alert('이 기능은 모바일에서만 사용할 수 있습니다.');
            }
            break;
    }
}

function kakaolink_send(text, url, img, img_w, img_h) {
    if (!img) img = '';
    if (!img_w) img_w = 300;
    if (!img_h) img_h = 200;
    if (img) {
        Kakao.Link.sendTalkLink({
            label: String(text),
            image: {
                src: img,
                width: img_w,
                height: img_h
            },
            webButton: {
                text: String('자세히 보기'),
                url: url
            }
        });
    } else {
        Kakao.Link.sendTalkLink({
            label: String(text),
            webButton: {
                text: String('자세히 보기'),
                url: url
            }
        });
    }
};









