// GNB
function initNavigation(seq, target) {
	var nav = document.getElementById(target);
	nav.menu = new Array(); // 메뉴의 1뎁스 a링크
	nav.current = null; // 활성화되었던 1뎁스 a링크
	nav.menuseq = 0; // 메뉴의 고유index번호 (0, 1, 2, 3...)
	nav.initActiveImg = null; // 1뎁스 문서초기 활성화된 이미지기억

	// 이미지 프리로딩
	/*
	$(window).ready(function(){
		var menuImages = nav.getElementsByTagName("IMG");
		for(i = 0; i<menuImages.length; i++){
			if(menuImages[i].src.indexOf("_on.gif") != -1)
				(new Image()).src = menuImages[i].src;
			else
				(new Image()).src = menuImages[i].src.replace("_on.gif", "_off.gif");
		}
	});
	*/

	// 2뎁스 a링크 이벤트
	var allA = nav.getElementsByTagName("a");
	for(k = 0; k < allA.length; k++) {
		allA.item(k).onmouseover = allA.item(k).onfocus = function(){
			nav.isOver = true;
			if(this.getElementsByTagName("img")[0]){
				this.getElementsByTagName("img")[0].src = this.getElementsByTagName("img")[0].src.replace("_off.gif", "_on.gif");
			}
			
		}
		allA.item(k).onmouseout = allA.item(k).onblur = function(){
			nav.isOver = false;
			if(this.getElementsByTagName("img")[0] && !this.depth1img ){
				this.getElementsByTagName("img")[0].src = this.getElementsByTagName("img")[0].src.replace("_on.gif", "_off.gif");
			}
			setTimeout(function () {
				if (nav.isOver == false) {
					if (nav.menu[seq])
						// 메뉴가 고정형식으로 활성화 될때
						nav.menu[seq].onmouseover();
					else if(nav.current){
						// nav.current 는 활성화되었던 a링크가 저장되어 있음.
						if(nav.initActiveImg != menuImg)
							menuImg.src = menuImg.src.replace("_on.gif", "_off.gif");
						if (nav.current.submenu)
							nav.current.submenu.style.display = "none";
						nav.current = null;
					}
				}
			}, 500);
		}
	}

	// 1뎁스 a링크 이벤트
	var navLen = nav.childNodes.length;
	var menuImg = null; // 활성화된 1메뉴의 링크에 포함된 이미지 저장
	for(var i = 0; i < navLen; i++) {
		var navItem = nav.childNodes.item(i);
		if (navItem.tagName != "LI") continue;

		var navAnchor = navItem.getElementsByTagName("a").item(0);
		navAnchor.submenu = navItem.getElementsByTagName("div").item(0);
		
		// 문서초기 활성화된 이미지는 src경로 안바꾸기
		navAnchor.depth1img = true;
		navAnchor.img = navAnchor.getElementsByTagName("img").item(0);
		if(navAnchor.img.src.indexOf("_on.gif") != -1)
			nav.initActiveImg = navAnchor.img;
		
		navAnchor.onmouseover = navAnchor.onfocus = function() {
			if(nav.current){
				menuImg = nav.current.childNodes.item(0);
				if(nav.initActiveImg != menuImg)
					menuImg.src = menuImg.src.replace("_on.gif", "_off.gif");
				if (nav.current.submenu)
					nav.current.submenu.style.display = "none";

				nav.current = null;
			}
			if(nav.current != this){
				menuImg = this.childNodes.item(0);
				menuImg.src = menuImg.src.replace("_off.gif", "_on.gif");
				if(this.submenu)
					this.submenu.style.display = "block";
				if(nav.current){
					smenuImg = this.submenu.childNodes.item(0);
					sMenuImgt = smenuImg.getElementsByTagName("img").item(0);
					sMenuImgt.src = sMenuImgt.src.replace("_off.gif", "_on.gif");
				} 
				nav.current = this;
			}
			nav.isOver = true;
		}

		nav.menuseq++;
		nav.menu[nav.menuseq] = navAnchor;
	}
	if (nav.menu[seq]) nav.menu[seq].onmouseover();
}


function imgMenuOver(containderID) {
	var objwrap = document.getElementById(containderID);
	var imgMenu = objwrap.getElementsByTagName("a");

	for (i=0; i<imgMenu.length; i++) {
		if(imgMenu[i].getElementsByTagName("img").length == 0) continue;

		if (imgMenu[i].getElementsByTagName("img")[0].src.indexOf("_over.gif") != -1 ) {
			continue;
		}
		imgMenu[i].onmouseover = function() {
			subImage = this.getElementsByTagName("img")[0];
			if (subImage.src.indexOf("_over.gif") != -1) return false;
			subImage.src = subImage.src.replace("_out.gif","_over.gif");
		}
		imgMenu[i].onfocus = function() {
			subImage = this.getElementsByTagName("img")[0];
			if (subImage.src.indexOf("_over.gif") != -1) return false;
			subImage.src = subImage.src.replace("_out.gif","_over.gif");
		}
		imgMenu[i].onmouseout = function() {
			subImage = this.getElementsByTagName("img")[0];
			subImage.src = subImage.src.replace("_over.gif", "_out.gif");
		}
		imgMenu[i].onblur = function() {
			subImage = this.getElementsByTagName("img")[0];
			subImage.src = subImage.src.replace("_over.gif", "_out.gif");
		}
	}
}

// 탭이미지변환
function initTab(tabContainerID, targetName, index){
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName("a");
	var tabImages = tabContainer.getElementsByTagName("img");

	var arrEle = new Array();
	for(var i=0; i<tabAnchor.length; i++) {
		if (tabAnchor.item(i).className == "tab") arrEle.push(tabAnchor.item(i));
	}

	for(var i=0; i<arrEle.length; i++) {
		if(document.getElementById(targetName + i)){
			document.getElementById(targetName + i).style.display ="none";
		}
		if(tabImages[i].src.indexOf('_c.gif') != -1){
			tabImages[i].src = tabImages[i].src.replace("_c.gif", ".gif");
		}
	}
	if(tabImages[index].src.indexOf('_c.gif') == -1)
		tabImages[index].src = tabImages[index].src.replace(".gif", "_c.gif");

	if(document.getElementById(targetName + index))
		document.getElementById(targetName + index).style.display = "block";
}

// Tab Content
function initTabMenu(tabContainerID) {
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName("a");
	var i = 0;

	for(i=0; i<tabAnchor.length; i++) {
		if (tabAnchor.item(i).className == "tab")
			thismenu = tabAnchor.item(i);
		else
			continue;

		thismenu.container = tabContainer;
		thismenu.targetEl = document.getElementById(tabAnchor.item(i).href.split("#")[1]);
		thismenu.targetEl.style.display = "none";
		thismenu.imgEl = thismenu.getElementsByTagName("img").item(0);
		thismenu.onclick = function tabMenuClick() {
			currentmenu = this.container.current;
			if (currentmenu == this)
				return false;

			if (currentmenu) {
				currentmenu.targetEl.style.display = "none";
				if (currentmenu.imgEl) {
					currentmenu.imgEl.src = currentmenu.imgEl.src.replace("_c.gif", ".gif");
				} else {
					currentmenu.className = currentmenu.className.replace(" c", "");
				}
			}
			this.targetEl.style.display = "";
			if (this.imgEl) {
				this.imgEl.src = this.imgEl.src.replace(".gif", "_c.gif");
			} else {
				this.className += " on";
			}
			this.container.current = this;

			return false;
		};

		if (!thismenu.container.first)
			thismenu.container.first = thismenu;
	}
	if (tabContainer.first)
		tabContainer.first.onclick();
}

//팝업존  선택
$(document).ready(function(){
	$("#popContent1").css("display","");
});
function showSubMenu(menu_num) {
	var menuSize = new Array("1", "2", "4", "5", "7", "9");
	for(var i=0; i<menuSize.length; i++) {
		if(menu_num == menuSize[i]) {
			//하위 메뉴 레이어를 보이게 한다
			$("#subMenu"+menu_num).css("display", "inline");
			//메뉴의 on이미지로 교체시킨다
			$("#mainMenu"+menu_num).attr("src", IMG_PATH + "local/mainmenu"+menu_num+"on.gif");
		} else {
			hideSubMenu(menuSize[i]);
		}
	}
}

function hideSubMenu(i) {
	$("#subMenu"+i).css("display","none");
	$("#mainMenu"+i).attr("src", IMG_PATH + "local/mainmenu"+i+".gif");
}

//팝업존  선택
function popupZoneChange(choice,popsize) {
	$("p.img").each(function() {
		$(this).css("display","none");
	});
	$("#popContent"+choice).css("display","");
	for (var i=1; i<=popsize; i++){
		$("#popupNumber"+i).css("color","#fff");
	}
	$("#popupNumber"+choice).css("color","#f46302");
	return false;
}

//팝업 오픈
var TOP_POSITION = 10;
var LEFT_POSITION = 10;
function popOpen(contentId) {
	if(getCookie("individualNotice_"+contentId) != "done") {
		var options = "scrollbars=1, width=500,height=400,top=" + TOP_POSITION + ",left=" + LEFT_POSITION;
		window.open('/main/individualPopup?content_id='+contentId,'individualNotice'+contentId,options);
		if(LEFT_POSITION >= 510) {
			if(TOP_POSITION >= 410) {
				TOP_POSITION = 10;
			} else {
				TOP_POSITION = TOP_POSITION + 400;
			}
			LEFT_POSITION = 10;
		} else {
			LEFT_POSITION = LEFT_POSITION + 500;
		}
	}
}

//신착도서 컨텐츠 이동
var smCount = parseInt(newBLength);
var smViewNo = 1;
var smWidth = 330;

$("#smContentPrev").click(
	function() {
		if(smViewNo == 1) {
			alert(newBfirstMSG);
			return false;
		} else {
			smViewNo--;
		}
		var rightPositoin = smWidth*smViewNo;
		var leftPosition = smWidth*(smViewNo-1);
		$("#newBContentSlide").animate({left: '+='+smWidth,'clip':'rect(0px '+rightPositoin+'px 200px '+leftPosition+'px)'});
		return false;
	}
);
$("#smContentNext").click(
	function() {
		if(smViewNo == smCount || smCount == 0) {
			alert(newBlastMSG);
			return false;
		} else {
			smViewNo++;
		}
		var rightPositoin = smWidth*smViewNo;
		var leftPosition = smWidth*(smViewNo-1);
		$("#newBContentSlide").animate({left: '-='+smWidth,'clip':'rect(0px '+rightPositoin+'px 200px '+leftPosition+'px)'});
		return false;
	}
);

/**
 * 썸네일 호출
 * @param id
 * @param isbn
 * @return
 */
function callThumbnailM(id, isbn) {
	ajaxLoader("#"+id, true, "small");
	$.ajax({
		type: "POST",
		url: "/openapi/thumbnail",
		dataType: "json",
		data: "isbn="+isbn,
		success: function(msg) {
			//썸네일 생성.
			ajaxLoader("#"+id, false);
			viewThumnailM(msg, id);
		},
		error: function() {
			ajaxLoader("#"+id, false);
			$("<img src=\"/image/ko/local/main/nocover.gif\" width=\"61\" height=\"85\" alt=\""+bookImageMsg+"\" />").appendTo("#"+id);
			$("#"+id).css("display","");
			//결과를 찾지 못해도 error로 떨어짐.
		}
	});
}

function callDlThumbnailM(id, sysdiv, ctrl) {
	ajaxLoader("#"+id, true, "small");
	$.ajax({
		type: "POST",
		url: "/search/dl/thumbnailAjax/"+sysdiv+ctrl,
		dataType: "json",
		success: function(msg) {
			//썸네일 생성.
			ajaxLoader("#"+id, false);
			viewThumnail(msg, id);
		},
		error: function() {
			ajaxLoader("#"+id, false);
			
			//결과를 찾지 못해도 error로 떨어짐.
		}
	});
}

function viewThumnailM(json, id) {
	var images = json.smallUrl;
	if(images != undefined && images != '' && images != null) {
		//alert("isbn=================>"+$("isbn", xmlObj)[0].firstChild.nodeValue);
		$("<img src=\""+images+"\" width=\"61\" height=\"85\" alt=\""+bookImageMsg+"\" />").appendTo("#"+id);
		$("#"+id).css("display","");
	} else {
		$("<img src=\"/image/ko/local/main/nocover.gif\" width=\"61\" height=\"85\" alt=\""+bookImageMsg+"\" />").appendTo("#"+id);
		$("#"+id).css("display","");
	}
}

function callDlThumbnailUrlM(id, sysdiv, ctrl) {
	ajaxLoader("#"+id, true, "small");
	$.ajax({
		type: "POST",
		url: "/search/dl/thumbnailAjaxUrl/"+sysdiv+ctrl,
		dataType: "json",
		success: function(msg) {
			//썸네일 생성.
			ajaxLoader("#"+id, false);
			viewThumnailUrl(msg, id);
		},
		error: function() {
			ajaxLoader("#"+id, false);
			//결과를 찾지 못해도 error로 떨어짐.
		}
	});
}

function viewThumnailUrlM(json, id) {
	var images = json.thumbnailUrl;
	if(images != undefined && images != '' && images != null) {
		//alert("isbn=================>"+$("isbn", xmlObj)[0].firstChild.nodeValue);
		$("<img src=\""+images+"\" width=\"61\" height=\"85\" alt=\""+bookImageMsg+"\" />").appendTo("#"+id);
		$("#"+id).css("display","");
	}
}


function popOpen(obj){
	window.open(obj.href, '', 'width=420, height=460, resizable=yes, left=400, top=300, statusbar=0, scrollbars=1');
}


