//주소록
$(()=>{
    //즐겨찾기 추가
    $("#address,#contentAreaHistory,#contentAreaSearch,#contentAreaSearchHistory").on("click", ".btn_favo_no", function(){
        let tgID = $(this).parent("li").data("id");
        console.log('btn_favo_no on click',"tgID"+tgID);
        let contact = $(this).parents("li").data("contact");
        
        let liEl = 
        `<li data-id='${contact.id}' data-contact='${JSON.stringify(contact)}'> \
            <span class='portrait'><img src='${getThumbImage(contact.thumbnail)}'></span>    \
            <a href='javascript:;' onclick='toggleFavorite(${contact.id})' class='btn_favo'></a>    \
            <h4>${contact.displayName}</h4> \
            <button type='button' class='btn_call'></button> \
        </li>`;
        
        $(".list_favorites").append(liEl);
        
        $(".list_all,.list_sch_all,.list_history,.list_sch_history").find("[data-id='"+ tgID +"'] .btn_favo_no").attr('class',"btn_favo");

        switch ($('.ui-page-active').attr('id')) {
            case 'address':
                if(!isIOS12()){
                    clusterizeAddress.refresh(true);
                    let offset = document.querySelector(".article_favorites").offsetHeight+24+31+61;
                    console.log('btn_favo_no on click','toggleFavorite offset = ' + offset);
                    clusterizeAddress.setTopHeightOffset(offset);
                }
                break;
            default:
                break;
        }
    });

    //즐겨찾기 삭제
    $("#address,#contentAreaHistory,#contentAreaSearch,#contentAreaSearchHistory").on("click", ".btn_favo", function(){
        let tgID = $(this).parent("li").data("id");
        console.log('btn_favo on click',"tgID"+tgID);
        $(".list_favorites").find("[data-id='"+ tgID +"']").remove();
        $(".list_all,.list_sch_all,.list_history,.list_sch_history").find("[data-id='"+ tgID +"'] .btn_favo").attr('class',"btn_favo_no");

        switch ($('.ui-page-active').attr('id')) {
            case 'address':
                if(!isIOS12()){
                    clusterizeAddress.refresh(true);
                    let offset = document.querySelector(".article_favorites").offsetHeight+24+31+61;
                    console.log('btn_favo on click','toggleFavorite offset = ' + offset);
                    clusterizeAddress.setTopHeightOffset(offset);
                }
                break;
            default:
                break;
        }
    });

    //주소록 오른쪽 전화 버튼
    $("#address,#contentAreaSearch,#contentAreaHistory,#contentAreaSearchHistory").on("click", ".btn_call", function(){

        let contact = $(this).parents("li").data("contact");

        console.log('btn_call on click',"selectCall"+JSON.stringify(contact));
        let thisNum = $(this).prev("p").text();
        let number = "";
        let type = "";
        if(!nullcheck(thisNum)){
            number=thisNum.replace(/[^0-9]/g,'');
            type=thisNum.replace(/[0-9]/g,'');
        }

        console.log('btn_call on click',"selectCall"+contact.id);

        selectCall(contact,number,type);
    });

    //주소록 정렬
    $('.search_wrap_01>.sort_tool>button.btn_sort').on('click', function(){
        $('.search_wrap_01>.sort_tool>button.btn_sort.on').removeClass('on');
        $(this).addClass('on');
        
        $(".list_all").empty();
        printAddress();
    });
});

