// HEADER Language
jQuery(function(){	
	var language = $('.language ul');
	language.css('top','0px');
	// click
	$('.language p').click(function(){
		language.slideDown(200);
		return false;
	});		
	// mouse out
	$('.language').mouseleave(function(){
		language.delay(200).slideUp(100);
	});
	// blur
	$('*:not(".language li a")').focus(function(){
		language.slideUp(100);
	});
});

// GNB
jQuery(function(){	
	var gnb_layer = $('ul.gnb_layer');
	
	// mouse over
	$('div#gnb ul li').hover(function(){
		$(this).find('> .gnb_layer').show();
	});		
	// mouse out
	$('div#gnb ul li').mouseleave(function(){
		$(this).find('> .gnb_layer').delay(100).hide();
	});
	// focus
	$("div#gnb ul li a").focus(function(){
		$(this.parentNode).find('> .gnb_layer').show();
	});
	
	var gnb_layerlist = $('ul.gnb_layer li a img');
	gnb_layerlist.hover(function(){
		var img = $(this);
		var img_name = img.attr('src').split('.gif')[0];
		img.attr('src', img_name + '_on.gif');
	});
	gnb_layerlist.mouseleave(function(){
		var img = $(this);
		var img_name = img.attr('src').split('_on')[0];
		img.attr('src', img_name + '.gif');
	});
	
	//blur
/*
$("div#gnb ul li ul li a").focus(function(){
		$(this.parentNode.parentNode).find('ul.gnb_layer').css('display','block');
	});
	$("div#gnb ul li ul li a").blur(function(){
		$(this.parentNode.parentNode).find('ul.gnb_layer').slideUp(0);
	});
*/ 
});

// LNB
jQuery(function(){	
	var lnb = $('#lnb');
	li = $('#lnb .lnb_submenu li').addClass("off");
	var bul = $('#lnb .lnb_submenu li a'); /* 모든 LNB a링크 */
	// mouse over bullet
	bul.hover(function(){
		$(this.parentNode).addClass("over");
	});
	// mouse out bullet
	bul.mouseleave(function(){
		$(this.parentNode).removeClass("over");
	});

	// 2depth 활성화
	var current2li = $('#lnb .lnb_submenu > li > ul > li.current');
	current2li.find('> ul').css('display','block');
	current2li.parent('ul').css('height','auto');
	current2li.parent('ul').parent('li').addClass('on').addClass('open');

	// 3depth 활성화
	var current3li = $('#lnb .lnb_submenu > li > ul > li > ul > li.current');
	current3li.parent('ul').css('display','block');
	current3li.parent('ul').parent('li').addClass('on')
	current3li.parent('ul').parent('li').parent('ul').css('height','auto');
	current3li.parent('ul').parent('li').parent('ul').parent('li').addClass('on').addClass('open');

	var depth1li = $('#lnb .lnb_submenu > li');
	var depth2ul = $('#lnb .lnb_submenu > li > ul').addClass("submenu"); // 2depth ul
	depth2ul.find('> li:last-child').addClass('last-child');

	depth1li.hover(function(){
		if($(this).find('> ul').hasClass('submenu')){
			$(this).addClass("on");
		}
		else {
			$(this).addClass("over");
		};

		var o = $(this).find('> ul > li'); // 2depth li들의 높이 계산
		var tmpH = 0;
		for(var i=0; i<o.length; i++){
			tmpH += o[i].offsetHeight;
		};
		//alert(tmpH);
		$(this).find('> ul').stop(true, false).delay(300).animate({ height:tmpH+"px" },"slow");
	}, function(){
		$(this).removeClass("on").removeClass("over");
		$(this).find('> ul').stop(true, false).delay(300).animate({ height:"0" },"slow");
	});

	lnb.mouseleave(function(){
		var openli = $('#lnb .lnb_submenu > li.open');
		openli.addClass('on');
		var o = openli.find('> ul > li');
		var tmpH = 0;
		for(var i=0; i<o.length; i++){
			tmpH += o[i].offsetHeight;
		};
		openli.find('> ul').stop(true, false).delay(300).animate({ height:tmpH+"px" },"slow");
	});
});

// TOP Btn
$(document).ready(function(){
   var currentPosition = parseInt($("#quick").css("top"));
   $(window).scroll(function() {
	   var position = $(window).scrollTop();
	   $("#quick").stop().animate({"top":position+currentPosition+"px"},500);
   });
});

// FOOTER Affiliated Companies 
jQuery(function(){	
	var affiliated = $('.affiliated ul');
	affiliated.hide();
	affiliated.css('bottom','22px');
	
	//click
	$('.affiliated p').click(function(){
		affiliated.slideDown(200);
		return false;
	});
	// mouse out
	$('.affiliated').mouseleave(function(){
		affiliated.delay(200).slideUp(200);
	});

	var affiliatedlist = $('.affiliated ul li');
	affiliatedlist.hover(function(){
		var img = $(this).find('img');
		var img_name = img.attr('src').split('.gif')[0];
		img.attr('src', img_name + '_on.gif');
	});
	affiliatedlist.mouseleave(function(){
		var img = $(this).find('img');
		var img_name = img.attr('src').split('_on')[0];
		img.attr('src', img_name + '.gif');
	});
});

// 이미지롤링 - 가로
function PhRolling(obj){
	var _root = this;
	var to = 0;
	var from = 0;
	var mvCnt = obj.mvCnt || 1; // 리스트가 움직일 갯수
	var sFlag = true;

	var ele = document.getElementById(obj.ele);
	var eleCvWidth = ele.parentNode.offsetWidth; /* 리스트부모의 총넓이(리스트레이어의 딱 넓이 만큼) */
	var list = ele.getElementsByTagName("LI"); /* 상품리스트들 */
	var listWidth = list[0].offsetWidth;
	var visibleListCnt =  parseFloat(eleCvWidth/listWidth);
	var oldAxis = 0;
	
	var btnLchk = true; // <<, >> 버튼 활성화 관련
	var btnRchk = true;

	ele.style.position = "absolute";
	ele.style.width = (listWidth*list.length) + "px";
	ele.style.left = 0;

	var btnLeft = document.getElementById(obj.btnLeft);
	var btnRight = document.getElementById(obj.btnRight);
	btnLeft.onclick = function(){ _root.leftMove() };
	btnRight.onclick = function(){ _root.rightMove() };
	
	// 리스트이동
	this.gotoNum = function(num){
		var lengths = list.length;
		
		// >> 움직임
		if(oldAxis >= lengths-visibleListCnt && num>lengths-visibleListCnt || num>=lengths)
			num = 0;
		else if(num<lengths && num>lengths-visibleListCnt)
			num = lengths-visibleListCnt;
		
		// << 움직임
		if(num<0 && num>-mvCnt)
			num = 0;
		else if(num<0 && num<=-mvCnt)
			num = lengths-visibleListCnt;

		from = _root.getStyle(ele, "left");
		to = -(listWidth*num);
		oldAxis = num;

		_root.btnVisibility();
		_root.action();
	}
	
	// 리스트의 움직일 갯수변경
	this.mvCnt = function(num){
		mvCnt = Number(num);
	}

	// css스타일을 얻을때 사용하는 함수
	this.getStyle = function(el, what){
		var target = el;
		var value = "";
		if(target.currentStyle){
			value = target.currentStyle[what];
		}else if(window.getComputedStyle){
			value = window.getComputedStyle(target,null)[what];
		}
		return parseFloat(value);
	}

	// 왼쪽방향 << 이미지가 클릭되었을때
	this.leftMove = function(){
		if(!sFlag) return false;
		sFlag = false;
		_root.gotoNum(oldAxis-mvCnt);
	}
	
	// 오른쪽방향 >> 이미지가 클릭되었을때
	this.rightMove = function(){
		if(!sFlag) return false;
		sFlag = false;
		_root.gotoNum(oldAxis+mvCnt);
	}
	
	// 화살표 보이기/가리기
	this.btnVisibility = function(){
		if(btnLeft.alpha != "no"){
			btnLeft.alpha = "no"
			btnLeft.style.filter = "alpha(opacity=40)";
			btnLeft.style.opacity = 0.4;
			
			btnRight.style.filter = "alpha(opacity=40)";
			btnRight.style.opacity = 0.4;
		}else{
			btnLeft.alpha = "yes"
			if(oldAxis!=0){
				btnLeft.style.filter = "alpha(opacity=100)";
				btnLeft.style.opacity = 1.;
			}
			
			if(oldAxis!=list.length-visibleListCnt){
				btnRight.style.filter = "alpha(opacity=100)";
				btnRight.style.opacity = 1.;
			}
		}
	}
	
	// 리스트들 움직이는 동작부분
	this.action = function(){
		var func_PhRolling = setInterval(function(){
			var speed = (to-from)*0.2;
			speed = (to>from) ? Math.ceil(speed) : Math.floor(speed);
			
			from = _root.getStyle(ele, "left");
			ele.style.left = from+speed+"px";
			
			if(to==from){
				_root.btnVisibility();
				clearInterval(func_PhRolling);
				sFlag = true;
			}
		}, 36);
	}
}

// 이미지롤링 - 세로
function PhRolling2(obj){
	var _root = this;
	var to = 0;
	var from = 0;
	var mvCnt = obj.mvCnt || 1; // 리스트가 움직일 갯수
	var sFlag = true;

	var ele = document.getElementById(obj.ele);
	var eleCvHeight = ele.parentNode.offsetHeight; /* 리스트부모의 총높이( */
	var list = ele.getElementsByTagName("LI"); /* 상품리스트들 */
	var listHeight = list[0].offsetHeight;
	var visibleListCnt =  parseFloat(eleCvHeight/listHeight);
	var oldAxis = 0;
	
	var btnUchk = true; // <<, >> 버튼 활성화 관련
	var btnDchk = true;

	ele.style.position = "absolute";
	ele.style.Height = (listHeight*list.length) + "px";
	ele.style.top = 0;

	var btnUp = document.getElementById(obj.btnUp);
	var btnDown = document.getElementById(obj.btnDown);
	btnUp.onclick = function(){ _root.upMove() };
	btnDown.onclick = function(){ _root.downMove() };
	
	// 리스트이동
	this.gotoNum = function(num){
		var lengths = list.length;
		
		// >> 움직임
		if(oldAxis >= lengths-visibleListCnt && num>lengths-visibleListCnt || num>=lengths)
			num = 0;
		else if(num<lengths && num>lengths-visibleListCnt)
			num = lengths-visibleListCnt;
		
		// << 움직임
		if(num<0 && num>-mvCnt)
			num = 0;
		else if(num<0 && num<=-mvCnt)
			num = lengths-visibleListCnt;

		from = _root.getStyle(ele, "top");
		to = -(listHeight*num);
		oldAxis = num;

		_root.btnVisibility();
		_root.action();
	}
	
	// 리스트의 움직일 갯수변경
	this.mvCnt = function(num){
		mvCnt = Number(num);
	}

	// css스타일을 얻을때 사용하는 함수
	this.getStyle = function(el, what){
		var target = el;
		var value = "";
		if(target.currentStyle){
			value = target.currentStyle[what];
		}else if(window.getComputedStyle){
			value = window.getComputedStyle(target,null)[what];
		}
		return parseFloat(value);
	}

	// 위쪽방향 << 이미지가 클릭되었을때
	this.upMove = function(){
		if(!sFlag) return false;
		sFlag = false;
		_root.gotoNum(oldAxis-mvCnt);
	}
	
	// 아래쪽방향 >> 이미지가 클릭되었을때
	this.downMove = function(){
		if(!sFlag) return false;
		sFlag = false;
		_root.gotoNum(oldAxis+mvCnt);
	}
	
	// 화살표 보이기/가리기
	this.btnVisibility = function(){
		if(btnUp.alpha != "no"){
			btnUp.alpha = "no"
			btnUp.style.filter = "alpha(opacity=40)";
			btnUp.style.opacity = 0.4;
			
			btnDown.style.filter = "alpha(opacity=40)";
			btnDown.style.opacity = 0.4;
		}else{
			btnUp.alpha = "yes"
			if(oldAxis!=0){
				btnUp.style.filter = "alpha(opacity=100)";
				btnUp.style.opacity = 1.;
			}
			
			if(oldAxis!=list.length-visibleListCnt){
				btnDown.style.filter = "alpha(opacity=100)";
				btnDown.style.opacity = 1.;
			}
		}
	}
	
	// 리스트들 움직이는 동작부분
	this.action = function(){
		var func_PhRolling = setInterval(function(){
			var speed = (to-from)*0.2;
			speed = (to>from) ? Math.ceil(speed) : Math.floor(speed);
			
			from = _root.getStyle(ele, "top");
			ele.style.top = from+speed+"px";
			
			if(to==from){
				_root.btnVisibility();
				clearInterval(func_PhRolling);
				sFlag = true;
			}
		}, 36);
	}
}

// PRODUCT LIST - SUBMAIN
jQuery(function(){
	var product_list = $('#section_group .section_wrap .section_content');
	var btn = $('#section_group .section_wrap .section a.menu');

	// mouse over
	btn.hover(function(){
		$(this.parentNode).addClass('over');
	});
	// mouse out
	btn.mouseleave(function(){
		$(this.parentNode).removeClass('over');
	});
	// click
	$('.section_wrap .section .menu').click(function(){
		product_list.css('display','none').css('left','-460px');
		$(this.parentNode).find('+ .section_content').fadeIn(150).animate({left: '-122'}, 200);
		return false;
	});
});

// PRODUCT INDEX
jQuery(function(){
	var alink = $('.product_indexlist ul li a');
	// mouse over (alink)
	alink.hover(function(){
		$(this.parentNode).addClass('over');
	});
	// mouse out (alnik)
	alink.mouseleave(function(){
		$(this.parentNode).removeClass('over');
	});
});

// A to Z
jQuery(function(){	
	// click
	$('.atoz_sort li a').click(function(){
		$('.atoz_sort li').removeClass('on');
		$(this.parentNode).addClass('on');
	});
	// click
	$('.atoz_result .unit .btn_more a').click(function(){
		$(this.parentNode.parentNode).find('+ ul.atozsub').slideToggle(0);
		$(this.parentNode).toggleClass('minus');
		return false;
	});
});

// TABLE
$(document).ready(function(){
	$("table.tbltype1 td:last-child").addClass("last-child");
	$("table.tbltype1 th:last-child").addClass("last-child");
	$("table.tbltype3 td:last-child").addClass("last-child");
	$("table.tbltype3 th:last-child").addClass("last-child");
	$("table.bbslist thead th:last-child").addClass("last-child");
	$("table.bbslist tbody tr:last-child").addClass("last-child");
});

// tabwrap
jQuery(function(){	
	// click
	$('.tabwrap ul li').hover(function(){
		$(this).toggleClass('over');
	});
});

// 이미지오버
$(document).ready(function(){
	$(".imgover").hover(function(){
		var img = $(this);
		var img_name = img.attr('src').split('.gif')[0];
		img.attr('src', img_name + '_on.gif');
	});
	$(".imgover").mouseleave(function(){
		var img = $(this);
		var img_name = img.attr('src').split('_on')[0];
		img.attr('src', img_name + '.gif');
	});
});

// PR CENTER 검색
$(document).ready(function(){
	$("div.result_con div.unit:last-child").addClass("last-child");
});

// Download Center
jQuery(function(){	
	// click
	$('#bbs-download .title a').click(function(){
		$(this).toggleClass('on')
		$(this.parentNode.parentNode).find('+ .trdownload').slideToggle(0);
		return false;
	});		
});

// Where to Buy
jQuery(function(){	
	// click
	$('#power_btn a').click(function(){
		$('#power_con1').toggleClass('wheretobuycon');
		$('#power_con2').toggleClass('wheretobuycon');
		return false;
	});
});

// history
jQuery(function(){	
	var history_view = $('.history .history_year li')
	var history_con = $('.history_con')
	$('#history_view4').addClass('on');
	$('#history_con4').show(0);
	// hover
	$('.history .history_year li').hover(function(){
		$(this).toggleClass('over');
	});
	// click
	$('#history_view1 a').click(function(){
		history_view.removeClass('on');
		$(this.parentNode).addClass('on');
		history_con.hide();
		$('#history_con1').show(0);
		return false;
	});
	$('#history_view2 a').click(function(){
		history_view.removeClass('on');
		$(this.parentNode).addClass('on');
		history_con.hide();
		$('#history_con2').show(0);
		return false;
	});
	$('#history_view3 a').click(function(){
		history_view.removeClass('on');
		$(this.parentNode).addClass('on');
		history_con.hide();
		$('#history_con3').show(0);
		return false;
	});
	$('#history_view4 a').click(function(){
		history_view.removeClass('on');
		$(this.parentNode).addClass('on');
		history_con.hide();
		$('#history_con4').show(0);
		return false;
	});
});

// PR Archive
jQuery(function(){	
	// click
	$('#prarchive_movie_btn dl dd ul li > a').click(function(){
		$('#prarchive_movie_btn dl dd ul li').removeClass('on');
		$(this.parentNode).addClass('on');
	});
});


// Product > Drive Solutions > Variable Frequency Drive (AC Drive) > Starvert Series > iH
jQuery(function(){	
	// click
	$('#imglayer > a').click(function(){
		$(this).find("+ span").show()
		return false;
	});
	// click
	$('#imglayer > span > a').click(function(){
		$(this.parentNode).hide()
		return false;
	});
});

// ID로 오브젝트 픽업
function getEi(n) { return document.getElementById(n); }

// 탭 체인지 ( lid 컨트롤 할 레이어 이름 / tno 선택된 탭 번호 / tname 활성화시킬 탭 오브젝트)
function  tabChange(lid,tno) {
 if (!lid || !tno) { alert('탭이 지정되지 않았습니다.'); return; }
 if (!getEi(lid + '_1') || !getEi(lid + '_tab_1')) { alert('지정한 명칭 "' + lid + '" 과(와) 일치하는 오브젝트가 없습니다.'); return; }
 for(var i=1;getEi(lid + '_' + i);i++) { getEi(lid + '_' + i).style.display = "none"; }
 for(var i=1;getEi(lid + '_tab_' + i);i++) { getEi(lid + '_tab_' + i).src = getEi(lid + '_tab_' + i).src.replace("s.gif", "off.gif"); }
 getEi(lid + '_tab_' + tno).src = getEi(lid + '_tab_' + tno).src.replace("on.", "s.");
 getEi(lid + '_tab_' + tno).src = getEi(lid + '_tab_' + tno).src.replace("off.", "s.");
 getEi(lid + '_' + tno).style.display = "block";
}

