var BMKChk	= false;
var BMKAuto, BMKData, BMKSec	= 0;
var Bookmark	= {

	// 쿠키 생성
	setCookie: function(cName, cValue, sec) {
		var expire = new Date();
		expire.setMilliseconds(1000*sec);
		cookies = cName + '=' + escape(cValue) + '; path=/ '; // 한글 깨짐을 막기위해 escape(cValue)를 합니다.
		if(typeof sec != 'undefined') cookies += ';expires=' + expire.toGMTString() + ';';
		document.cookie = cookies;
	},
 
	// 쿠키 가져오기
	getCookie: function(cName) {
		cName = cName + '=';
		var cookieData = document.cookie;
		var start = cookieData.indexOf(cName);
		var cValue = '';
		if(start != -1){
			start += cName.length;
			var end = cookieData.indexOf(';', start);
			if(end == -1)end = cookieData.length;
			cValue = cookieData.substring(start, end);
		}
		return unescape(cValue);
	},

	onLayer: function(opt) {
		// 실행시 세팅한 값
		BMKData	= opt;
		$("#bookmark_icon").prop("src", BMKData.icon);
		$("#bookmark_site").text(BMKData.site);
		
		// 브라우저체크
		var BMKAgt	= navigator.userAgent.toLowerCase();
		if(BMKAgt.indexOf('naver')!=-1) {
			BMKChk	= true;
		}
		// 기기체크
		if(!navigator.userAgent.match(/Android/)) {
			BMKChk	= false;
		}
		// 쿠키체크하여 실행여부 결정
		var BMKCk	= Bookmark.getCookie('bookmark_cookie');
		if(BMKCk==BMKData.enname) {
			BMKChk	= false;
		}
		// 현재 모드 체크
		if(BMKData.mode == 'test') {
			BMKChk	= true;
		}

		// 조건 만족 시 실행
		if(BMKChk) {
			$("#bookmark_wrap").animate({ 'bottom' : '0px'}, 1000, function() {
				Bookmark.timer();
			});
		}
	},

	timer: function() {
		if(BMKSec >= 0) {
			$("#bookmark_time").text(BMKSec);
			BMKAuto	= setTimeout(Bookmark.timer, 1000);
		} else {
			if(BMKData.mode != 'test') {
				Bookmark.clear();
				Bookmark.getIcon();
			}
		}
		BMKSec	-= 1;

	},
	
	clear: function() {
		// 닫기시 창 안띄움 쿠키설정
		Bookmark.setCookie('bookmark_cookie',BMKData.enname,BMKData.day);

		clearTimeout(BMKAuto);
		$("#bookmark_wrap").animate({ 'bottom' : '-100px'}, 500, function() {
			$("#bookmark_wrap").hide();
		});

	},

	getIcon: function() {
		if(BMKChk){
			// 아이콘 다운로드 진행여부 쿠키설정
			Bookmark.setCookie('bookmark_cookie',BMKData.enname,BMKData.day);
			// 아이콘 다운로드 URL
			var BMKUrl	= "naversearchapp://addshortcut?url="+encodeURIComponent(BMKData.url)+"&icon="+encodeURIComponent(BMKData.icon)+"&title="+encodeURIComponent(BMKData.title)+"&serviceCode="+encodeURIComponent(BMKData.enname)+"&version=7";
			//alert(BMKUrl);
			location.href	= BMKUrl;
		}
	},

	getBookmark: function(url, icon, title, enname) {
		if(!url || !icon || !title || !enname) {
			return;
		}
		// 아이콘 다운로드 URL
		var BMKUrl	= "naversearchapp://addshortcut?url="+encodeURIComponent(url)+"&icon="+encodeURIComponent(icon)+"&title="+encodeURIComponent(title)+"&serviceCode="+encodeURIComponent(enname)+"&version=7";
		location.href	= BMKUrl;
	}
}

