<?

$data = $this->session->userdata;

$ci = &get_instance();
$now_date = date("Y-m-d");
$ci->load->model('web/m_today_word');
$arr_today_word = $ci->m_today_word->today_word_where_display_date_select($now_date);
$today_word 		= '하나님이 세상을 이처럼 사랑하사 </br>독생자를 주셨으니 이는 그를 믿는 자마다 </br>멸망하지 않고 영생을 얻게 하려 하심이라';
$today_word_title 	= '요한복음 3장 16절';
if(count($arr_today_word)>0) {
	$today_word 		= $arr_today_word['word'];
	$today_word_title 	= $arr_today_word['title'];
}

$ci->load->helper('cookie');
$comment_sns_id 		= get_cookie('crossmap_comment_sns_id');
$comment_sns_name 		= get_cookie('crossmap_comment_sns_name');

$comment_sns_type 		= get_cookie('crossmap_comment_sns_type');

if (isset($data['C_Member_Id']) || isset($comment_sns_name)) {
    if($comment_sns_name) {
        $data['C_Member_Profile'] = '/img/mobile/life_profile.png';
        $data['C_Member_Nicname'] = $comment_sns_name;
        $data['C_Member_Group_Name'] = $comment_sns_type;
        if($comment_sns_type != 'cm') {
            $logout_link = "comment_logout();";
        } else {
            $logout_link = "window.location='/m/member/logout_proc'";
        }
    } else if($data['C_Member_Id']) {
        $logout_link = "window.location='/m/member/logout_proc'";
    } else {
        $logout_link = "";
    }
}
?>

<div class="life_menu_l">
	<div class="life_menu_top">
		<h1 class="logo"><a href="javascript:change_slide(0);"><img src="/img/mobile/life_logo.png"></a></h1>
		<span class="life_close"><img src="/img/mobile/close.png" style="cursor:pointer"></span>

		<? if (isset($data['C_Member_Id'])): ?>

			<div class="profile"><a href="/m/mypage/req_password"><img src="<?= $data['C_Member_Profile'] ?>"
												  onerror="this.src='/img/mobile/life_profile.png'"
												  style="max-width:77px;max-height:77px;"></a></div>
			<div class="nickname"><a href="/m/mypage/req_password"><?= $data['C_Member_Nicname'] ?></a></div>
			<div class="member"><a href="/m/mypage/req_password"><?= $data['C_Member_Group_Name'] ?></a></div>
			<div class="life_menu_btn" style="background: #f6f6f6;">
				<button type="button" class="btn btn-default br20" style="width:100px;color:#a7a7a7"
						onclick="<?=$logout_link;?>">로그아웃
				</button>
			</div>


		<? else: ?>

			<div class="profile"><a><img src="/img/mobile/life_profile.png"></a></div>
			<div class="bible"><?=$today_word?><br><span class="chpter"><?=$today_word_title?></span>
			</div>
			<div class="life_menu_btn" style="background: #f6f6f6;">
				<button type="button" class="btn btn-default br20" style="width:100px;color:#a7a7a7" onclick="login()">
					로그인
				</button>
			</div>
		<? endif; ?>
		<div class="life_cate">
			<span style="width:30%;"><img src="/img/mobile/life_icon2.png"><a <?=(isset($data['C_Member_Id']))? 'href="/m/mypage/user_comment"' : 'href="javascript:alert(\'로그인 후 사용 가능합니다.\')" '  ?> >내댓글보기</a></span>
			<span><img src="/img/mobile/life_bar.gif"></span>
			<span style="width:28%;"><img src="/img/mobile/life_icon3.png"> <a <?=(isset($data['C_Member_Id']))? 'href="/m/mypage/bookmark"' : 'href="javascript:alert(\'로그인 후 사용 가능합니다.\')" '  ?>>북마크</a></span>
			<span><img src="/img/mobile/life_bar.gif"></span>
			<span style="width:28%;"><img src="/img/mobile/life_icon1.png"><a <?=(isset($data['C_Member_Id']))? 'href="/m/mypage/delivery"' : 'href="javascript:alert(\'로그인 후 사용 가능합니다.\')" '  ?>>배달함</a></span>
		</div>


	</div>
	<ul class="life_list_top">
		<li><a onclick="change_slide(0)"  style="cursor:pointer;" class="current">은혜</a></li>
		<li><a onclick="change_slide(1);" style="cursor:pointer;">라이프</a></li>
		<li><a onclick="change_slide(2);" style="cursor:pointer;">뉴스</a></li>
		<li><a onclick="change_slide(3);" style="cursor:pointer;">바로가기</a></li>
	</ul>
	<div class="life_app">
		<h4 style="padding: 0 20px 10px 20px;">추천앱</h4>
		<?
		$ci = &get_instance();
		$ci->load->model('web/m_recommend_app');
		$result = $ci->m_recommend_app->recommend_app_list(array("offset" => 0, "limit" => 99));
		?>
		<div class="swiper-container swiper-04">


			<div class="swiper-wrapper">
				<? foreach ($result as $key => $app_data): ?>
					<div class="swiper-slide"><a href="<?= $app_data['market_url'] ?>" target="_blank"><img style="width:66px;height:66px;"
								src="<?= thumb($app_data['icon_img']) ?>"></a><a href="<?= $app_data['market_url'] ?>"
																		  target="_blank"
																		  class="app_txt"><?= $app_data['app_name'] ?></a>
					</div>
				<? endforeach; ?>
			</div>


			<!-- Add Arrows -->
			<div class="swiper-button-next"></div>
			<div class="swiper-button-prev"></div>
			<div class="swiper-pagination"></div>
		</div>
	</div>
	<ul class="life_list_bottom">
		<li><a href="/m/board/notice">공지사항</a> <span class="new" style="display:none;">N</span></li>
		<li><a href="/m/board/faq">FAQ</a></li>
		<?if(isset($data['C_Member_Id'])):?>
		<li><a href="/m/mypage/qna">1:1 문의</a></li>
		<?endif?>
	</ul>
	<div class="life_menu_btn">
		<? if (isset($data['C_Member_Id'])): ?>
			<button type="button" class="btn btn-default br20" style="widh:100px;color:#a7a7a7"
					onclick="window.location='/m/member/logout_proc'">로그아웃
			</button>
		<? else: ?>
			<button type="button" class="btn btn-default br20" style="width:100px;color:#a7a7a7" onclick="login()">로그인
			</button>
		<? endif ?>
	</div>
	<div class="life_menu_btn" style="padding-bottom:80px;">
		<button type="button" class="btn btn-default_bb br20" style="width:100px;" onclick="window.location='/'">PC 버전
		</button>
	</div>
	<div class="dims" id='swipeme'></div>
	<div class="dims2"></div>
</div>
<div class="news_top">
	<span class="news_menu"><img src="/img/mobile/news_menu.png" style="cursor:pointer"></span>
	<span class="news_logo"><a href=""><img src="/img/mobile/news_logo.png"></a></span>
	<div>
			<span class="srch">
					<form name="search_form" id="search_form" method="get" action="/m/search/">
				<span><input type="text" class="window" id="search_keyword" name="search_keyword" autocomplete="off" placeholder="검색"
							 value=""
							 onkeyup="onkeyup_search_keyword(this.value);">

					<a style="float:right;" href="javascript:search_form.submit();"><img src="/img/mobile/mypage_serch_icon.gif" alt="검색하기"></a>
					<a href="javascript:show_toggle()"><img id="show_toggle" src="/img/mobile//arrow_up2.png" alt="검색하기" style="float:right; margin-top:4px;"></a>
					</span>
		</form>

			</span>
	</div>
</div>
<div class="news_nav"><!-- 여기를 아래로 내릴때 픽스시켜준다 -->
	<span class="current" style="width:20%;"><a href="/m#grace">은혜</a></span>
	<span style="width:30%;"><a href="/m#life">라이프</a></span>
	<span style="width:20%;"><a href="/m#newss">뉴스</a></span>
	<span style="width:30%;"><a href="/m#shortcut">바로가기</a></span>
</div>

<div class="open_search" id="open_search" style="display:none;">
	<div class="roll_box" id="prev_keyword_list">


	</div>
	<div class="roll_box" id="search_keyword_list">


	</div>
	<div class="roll_footer">
		<span><a href="javascript:pre_keyword_toggle()">최근 검색어 <span id="pre_on_off"></span></a></span> | <span><a href="javascript:removeAll()">전체삭제</a></span>
		<span class="fr"><a href='javascript:$("#open_search").hide();'>닫기</a></span>
	</div>
</div>

 
<script>

	$(document).ready(function () {

		$.ajax({
			url: "/m/board/ajax_new_notice",
			success: function (data) {
				if (parseInt(data) > 0) $(".new").show();
			}

		})

	});

	function login() {
		window.location = "/m/member/login?redir=" + window.location.href;
	}


	// 검색
	function onkeyup_search_keyword(val) {
		if (window.event.keyCode == 13) {
			search_keyword_proc(val);

		} else {
			if (val == '') {
				$("#search_arrow").removeClass();
				$("#search_keyword_list").empty();
				$("#open_search").hide();
				$("#search_arrow").addClass('src_icon arrow_down_on');
			} else {
				console.log(val);
				var form_data = {
					"keyword": val
				};


				$.ajax({
					url: '/m/search/search_keyword',
					type: 'POST',
					dataType: 'JSON',
					data: form_data,
					success: function (rtnJson) {
						if (rtnJson.code == "0000") {
							var count = rtnJson.data.length;
							var content = '';
							for (var i = 0; i < 8; i++) {
								var result = rtnJson.data[i];
								var result_keyword = result.keyword;
								keyword = result_keyword.replace(val, '<span class="on">' + val + '</span>');
								content += ' <div class="recents"><span><a href="javascript:search_keyword_proc(\'' + result_keyword + '\');">' + keyword + '</a></span><span class="close fr keywords_copy"><img src="/img/mobile/img_pointer.png"></span></div>';
							}
							/*
							* 키워드끼리 비교하여 정확도를 구하고 해당 정확도 순으로 정렬하는 작업이 필요합니다.
							* */



							$("#search_keyword_list").html(content);
							$("#search_arrow").removeClass();
							$("#open_search").show();

							$(".keywords_copy").each(function(i,v){

								a = $(v).prev().text();

								if(a.indexOf(val) < 0){
									$(v).parent().hide();
								}
								else{
									$(v).parent().show();
									window.aa = $(this);
									console.log($(this).html());
									if( a == val ){
										$(this).find("img").hide();
									}
									else{
										$(this).find("img").show();
									}
								}
								return;
							});


						} else {
							$("#search_arrow").removeClass();
							$("#search_keyword_list").empty();
							$("#open_search").hide();
						}
					},
					error: function (rtnJson) {
					}
				});
			}
		}

	}

	function search_keyword_proc(search_keyword) {
		if (search_keyword == '') {
			search_keyword = $("#search_keyword").val();
		}
		var result = true;
		if (search_keyword != '') result = search_keyword_reg(search_keyword);
		if (result == true) {
			$("#search_keyword").val(search_keyword);
			$("#search_form").submit();
		}

	}
	// 스크롤 고정 함수.
	function scroll_fix(news_nav) {

		$( document ).ready( function() {

			var jbOffset = $( '.'+news_nav ).offset(); // 고정하고자 하는 클래스의 offset.

			$( window ).scroll( function() {
				/*스크롤탑(현재스크롤위치) > news_nav : 상단 아래로 내렸을때 상단 고정*/
				if ( $( document ).scrollTop() > jbOffset.top ) {
					$( '.'+news_nav ).addClass( 'jbFixed' );
                    $( '.mainswiper' ).addClass( 'Fixed' );
				}
				/*스크롤탑(현재스크롤위치) <= news_nav : 원래*/
				else {
					$( '.'+news_nav ).removeClass( 'jbFixed' );
                    $( '.mainswiper' ).removeClass( 'Fixed' );
				}
			});




		} );
	}
	//

	function remove(i){

		$k = JSON.parse($.cookie("pre_keyword"));
		$k.splice(i,1);
		$.cookie("pre_keyword",JSON.stringify($k),{ expires: 7, path: '/'});
		$("#keys"+i).remove();
	}

	function removeAll(){

		$.cookie("pre_keyword","[]",{ expires: 7, path: '/'});
		$(".keys").remove();
	}

	function pre_keyword_toggle(){
		toggle = $.cookie('pre_keyword_show') || "Y";

		if(toggle == 'Y'){
			$.cookie('pre_keyword_show',"N",{ expires: 7, path: '/'});
			$("#prev_keyword_list").hide();
			$("#pre_on_off").text("켜기");
		}
		else{
			$.cookie('pre_keyword_show',"Y",{ expires: 7, path: '/'});
			$("#prev_keyword_list").show();
			$("#pre_on_off").text("끄기");
		}
	}


	function show_toggle(){
		toggle = $.cookie('pre_keyword_show') || "Y";
		$(".open_search").toggle();
		if($(".open_search").css("display") == "block"){
				$("#show_toggle").attr("src","/img/mobile/arrow_up2.png");
		}
		else{
			$("#show_toggle").attr("src","/img/mobile/arrow_down2_gray.png");
		}
		console.log($("#show_toggle").attr("src"));
	}




	$(document).ready(function(){

		$("body").on("click",".keywords_copy",function(i,v){

			selected_keyword = $(this).prev().text();
			$("#search_keyword").val(selected_keyword);
			onkeyup_search_keyword(selected_keyword);
		});

		keys = $.cookie("pre_keyword") || "[]";
		keys = JSON.parse(keys);
		$("#prev_keyword_list").html("");

		add = null;
		add = "<span class=\"close fr keywords_copy\" style=\"width: 14px;\"><img src=\"/img/mobile/img_pointer.png\"></span>";

		for(i=0;i<keys.length;i++){
			$("#prev_keyword_list").append("\n" +
				"\t\t<div class='keys' id='keys"+i+"'>\n" +
				"\t\t\t<span><a href=\"javascript:search_keyword_proc('"+keys[i].split("|")[0]+"')\"><span class=\"search_pointer\">"+keys[i].split("|")[0]+"</span></a></span>\n" +
				//"\t\t\t<span class=\"close fr\" onclick='remove("+i+")'><img src=\"/img/mobile/img_x_grey.png\"></span>\n" +
				add+
				"<span class=\"date fr\">"+keys[i].split("|")[1]+"</span>"+
				"\t\t</div>");
		}

		$("#search_keyword").focus(function(){
			$(".open_search").show();
		});

		toggle = $.cookie('pre_keyword_show') || "Y";

		if(toggle == 'Y'){
			$("#prev_keyword_list").show();
			$("#pre_on_off").text("끄기");
		}
		else{
			$("#prev_keyword_list").hide();
			$("#pre_on_off").text("켜기");
		}

	
	});
	scroll_fix('news_nav'); // 상단 고정하고 싶은 클래스 설정.
	var swiper4 = new Swiper('.swiper-04', {
		slidesPerView: 3,
		spaceBetween: 10,
		pagination : { // 페이징 설정
			el : '.swiper-pagination',
			clickable : true, // 페이징을 클릭하면 해당 영역으로 이동, 필요시 지정해 줘야 기능 작동
		},
		navigation: {
			nextEl: '.swiper-button-next',
			prevEl: '.swiper-button-prev',
		},
	});	

</script>
