/**
 * 셀렉트 박스에 필요한 함수
 * @param selfObj
 * @param obj
 * @return
 */
function menuToggle(selfObj,obj){
	if(obj.style.display == "none"){
		selectLayAllClose(selfObj);
		obj.style.display = "block";
	} else {
		obj.style.display = "none";
	}
}

function selectLayAllClose(obj){
	var objL = get(obj.parentNode.parentNode,'ul','sList');
	for (i=0;objL.length>i ;i++ ){
		objL[i].style.display='none';
	}
}

function goTotalSearch() {
	window.open("http://search.daum.net/search?q="+encodeURIComponent(document.getElementById("search_keyword").value));
}
/**
 * 목록에 전체 체크
 * @param checkbox
 * @param wrap
 * @return
 */
function checkedAll(checkbox, wrap) {
	var input = $T("INPUT");
	for (var i=0; i<input.length; i++) {
		if (input[i].type == "checkbox" && input[i].name == "checked") {
			input[i].checked = checkbox.checked;
		}
	}
}

/**
 *  /main.do, /recommend/daum.do 에서 사용하는 추천
 * @param viewday
 * @param bookid
 */
function recommend(viewday, bookid) {
	ajaxRecommendController.getBookToBookRecommendByUserid(viewday, bookid, getCountCallback= function(result){
		if(result > 0){
			alert("이미 투표하셨습니다.");
		}else if(result <0){
			alert("에러가 발생하였습니다.");
		}else{
			ajaxRecommendController.recommendBookTobook(viewday, bookid, recommendCallback = function(result){
				if(result > 0){
					$("recommendCnt" + bookid).innerHTML = result;
				}else{
					alert("에러가 발생하엿습니다."); 
				}
			});
		}
	});
}

/**
 * 이벤트 페이지에 HOT EVENT 컨트롤 하는 함수
 * @param id
 */
function showHotEvent(id){
	var hotEvents = daum.getChildElements(daum.getParent(id));
	hotEvents.each(function(hotEvent){
		daum.removeClassName(hotEvent, "on");
	});
	daum.addClassName(id, "on");
}

function toggleRightMenu(id){
	var category = $(id);
	if(daum.hasClassName(category, "open")){
		daum.removeClassName(category, "open");
	}else{
		daum.addClassName(category, "open");
	}
}

//sendToYozm('[책] 간편 다이어트 : 체온을 높이면 살이 빠진다', 'http://book.daum-img.net/image/BOK0001164679211', 'DBB', 'BOK0001164679211'); 
//message가 false이면 alert창 노출 되게 수정, params.shareYN으로 찜목록이 공유되었는지 검사
var sendToSNS = {
//	api : "http://dev.profile.daum.net:10001/api/popup/Share.daum", //개발테스트
	//api : "http://yozm.daum.net/api/popup/prePost", //기존에 사용하고 있던 API
	api : "http://profile.daum.net/api/popup/Share.daum", //실서비스
//	apiMypeople	: "http://msync.addrbook.daum.net/mypeople/web/share.do", //개발테스트
	apiMypeople	: "http://mypeople.daum.net/mypeople/web/share.do", // 실서비스
//	apiprofileCheck : "http://dev.profile.daum.net:10001/api/popup/JoinProfile.daum?service_name=", //개발테스트
	apiprofileCheck : "http://profile.daum.net/api/popup/JoinProfile.daum?service_name=", //실서비스
	params : {
		service_name : null,
		prefix : null,
		link : encodeURIComponent(location.href),
		source_id : 36,
		message : null,
		meta_type : null,
		meta_key : null,
		image_path : null,
		caption : null,
		description : null,
		callback : null	,
		shareYN : null
	},
	getUrl : function(params){
		var paramString = [];
		daum.extend(params, this.params, false);
		for (var param in params) {
			if(params[param] !== null){
				paramString.push(param + '=' + params[param]);
			}
		}
		return (params.service_name == "mypeople" ? this.apiMypeople : this.api) + '?' + paramString.join('&');
	},
	getLink : function(sns){ //추천도서에 북대북 하일라이트를 위해 추가
		return encodeURIComponent(location.href + (location.href.indexOf('?') > 0 ? '&' : '?') + "sns=" + sns);
	},
	joinSns : function(serviceName){
		var url = this.apiprofileCheck+serviceName;
		daum.popup(url, 800, 565, {name: "snsJoinPopup"});
	},
	popup : function(url, key){
		daum.hide("snsMoreList" + key);
		daum.popup(url, 800, 565, {name : "snsPopup"});
	},
	//찜목록에 등록된 책이 있는지 검사
	checkWish : function(prefix, meta_key){
		if(prefix == "false"){
			alert("찜목록에 책을 등록해 주세요.");
			return false;
		} else {
			//찜목록을 공유하도록 서버에 메시지 보냄
			if( !isNaN(parseInt(meta_key, 10)) && this.params.link.indexOf("/my/wish/shareBookList.do") > -1){
				ajaxWishController.updateBookCategoryShareYn(meta_key, {callback:function(r){
						if(r == 1){
							sendToSNS.params.shareYN = "Y";
						}else{
							sendToSNS.params.shareYN = "N";
						}
					},
					async:false
				});
			}
			if(this.params.shareYN == "Y"){
				return true;
			} else if(this.params.shareYN == "N"){
				alert("찜목록 공유에 실패하였습니다. 나중에 다시 시도해 주세요");
				return false;
			} else {
				return true;
			}
		}
	},
	yozm : function(prefix, image_path, meta_type, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "yozm",
				link : this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix),
				meta_type : meta_type,
				meta_key : meta_key,
				image_path : encodeURIComponent(image_path)
			});
			this.popup(url, meta_key);
		}
	},
	mypeople : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "mypeople",
				link : this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	},
	twitter : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "twitter",
				link : this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	},
	nate : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "nate",
				link : this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	},
	facebook : function(prefix, image_path, meta_type, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "facebook",
				link :this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix),
				meta_type : meta_type,
				meta_key : meta_key,
				image_path : encodeURIComponent(image_path)
			});
			this.popup(url, meta_key);
		}
	},
	me2day : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "me2day",
				link : this.params.shareYN=="Y" ? this.params.link :this.getLink(meta_key),
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	},
	//연동불가
	cafe : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "cafe",
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	},
	//연동불가
	blog : function(prefix, meta_key){
		var hasBook = this.checkWish(prefix, meta_key);
		if(hasBook){
			var url = this.getUrl({
				service_name : "blog",
				prefix : encodeURIComponent(prefix)
			});
			this.popup(url, meta_key);
		}
	}
};

/**
 * URL 복사
 * @param meintext
 * @return
 */
function setClip(meintext){
	if (window.clipboardData){ 
		window.clipboardData.setData("Text", meintext);
	}else if (window.netscape){
		netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
		var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
		if (!clip){return;}
		var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
		if (!trans){return;}
		trans.addDataFlavor('text/unicode');
		var str = new Object();
		var len = new Object();
		var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
		var copytext=meintext;
		str.data=copytext;
		trans.setTransferData("text/unicode",str,copytext.length*2);
		var clipid=Components.interfaces.nsIClipboard;
		if (!clip){return false;}
		clip.setData(trans,null,clipid.kGlobalClipboard);
	}
	alert('URL이 복사되었습니다. 붙여넣기를 원하는곳에 Ctrl+V 또는 마우스 오른쪽 버튼을 클릭하여 붙여넣기를 하세요.');
}

function fixedQuickMenu(){
	if(!$("quickMenu")){ return; }
	if(!daum.ie6){
		$("quickMenu").style.position = "fixed";
	}else{
		var top = $("quickMenu").offsetTop;
		daum.addEvent(window, "scroll", function(){
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			daum.setTop("quickMenu", scrollTop + top, false);
		});
	}
}

function moveQuickMenu(type){
	var target = $("quickMenuList");
	var childNode = type=="up" ? daum.getLastChild(target) : daum.getFirstChild(target);
	var cloneNode = daum.createElement("LI");
	cloneNode.innerHTML = childNode.innerHTML;
	
	if(type=="up"){
		target.insertBefore(cloneNode, daum.getFirstChild(target));	
		target.removeChild(childNode);
		daum.hide($T("LI", target)[3]);
	}else{
		daum.show($T("LI", target)[3]);
		target.appendChild(cloneNode);
		target.removeChild(childNode);
		daum.hide(daum.getLastChild(target));
	}

	//3번째 li에 class 추가
	var list = $T("LI", target);
	for(var i=0, len=list.length; i<len; i++){
		if(i == 2){
			daum.addClassName(list[i], "lst");
		}else{
			daum.removeClassName(list[i], "lst");
		}
	}
}
