
// IE6 Flicker Bug Fix
try {document.execCommand("BackgroundImageCache", false, true);} catch(err){}
if ($.browser.msie==true && $.browser.version < 8) { var isIE = true; }

$(document).ready(function() {

	// js 실행되지 않을 때 스타일 제거
	$('body *').removeClass('jsOff');

    // first,last child
    $('table tr > *:first-child').addClass('first-child');
    $('table.table-a tr > *:first-child').addClass('first-child');
    $('.bbs-photo div.section:even').addClass('even');
	$('.bbs-photo div.section div.img').append('<span></span>');
    $('.paginate a.prev').next().addClass('first-child');
    $('table.table-a tr > *:last-child').addClass('last-child');



    // 롤 오버
    $('img.rollover, .tab-a a img').each(function() {
        $(this).mouseover(function() {
            if ($(this).attr('src').match('_off.')) {
                $(this).css('cursor', 'pointer');
                $(this).attr('src', $(this).attr('src').replace('_off.', '_on.'));
                $(this).mouseout(function() {
                    $(this).attr('src', $(this).attr('src').replace('_on.', '_off.'));
                });
            }
        });
    });	
	
	// 팝업창일때
	if ($('body').attr('class').match('popup')){
		// 닫기
		$('a[href=#close]').bind('click', function(){
			window.close();
		});
	}

	// input box 스타일링
	$('input[type=text], input[type=password]').addClass('txt');
	$('input[type=file]').addClass('file');
	$('input[type=image]').addClass('image');
	$('input[type=submit]').addClass('button');
	$('input[type=checkbox]').addClass('check');
	$('input[type=radio]').addClass('radio');
	$('textarea').addClass('textarea');

	// 스카이스크래퍼 초기화
	if($('#skyscraper').val()!=null){
		var skyscraperPos = $('#skyscraper').position();
		var inskysrpTop = Math.round(skyscraperPos.top);
		$(window).bind('scroll', function(){
			var top = $(window).scrollTop();
			var pos = top + 50; // 최소 여백
			if (pos < inskysrpTop){
				pos = inskysrpTop;
			}
			$('#skyscraper').animate({
					top: pos
				},{
					queue: false,
					duration: 300,
					easing: 'swing'
				}
			);
		});
	};
	
	// FAQ
	$('table.faq tr.toggle').hide(); // 답변 모두 닫기
	$('table.faq td.subject a').bind('click', function(event){
		$('table.faq tr').removeClass('current');
		$(this).parent().parent().addClass('current');
		$('table.faq tr.toggle').each(function(){
			if($(this).prev().attr('class').match('current')){
				$(this).toggle();
			} else {
				$(this).hide();
			}
			
		});return false;
	});

	// 바로가기 서비스
	$('div#qservice h2 a').bind('click', function() {
		var target = $('div#qservice');
		var img = $(this).parent();
		if (img.attr('class').match('close')) {
				target.animate({
				right: '0px'
			}, 500)
			img.attr('class', img.attr('class').replace('close', 'open'));
		} else {
				target.animate({
				right: '-355px'
			}, 500)
			img.attr('class', img.attr('class').replace('open', 'close'));
		}
	});

	// Lnb 
	initLnb();

	
	// 외부인
	$('div#utility li.outsider ul').hide();
	$('div#utility li.outsider').hover(function() {
		$(this).children().next().show();
	} , function() {
		$(this).children().next().hide();
	});

	
	// GNB sub
	$('.gnbsub .inner').animate({opacity: 0.0}, 300);
	$('div#gnb li').hover(function() {
		var exception = $(this).attr('class');
		if(exception.match('etclnk') || exception.match('webzine')) {  } // 웹진, 퀵링크는 제외
		else gnbOpen($(this));
	} , function() {
		gnblCose($(this));
	});
	$('div#gnb li a.d1').focus(function() {
		var obj = $('div#gnb li');
		obj.find('.gnbsub').hide();
		var exception = $(this).parent().attr('class');
		if(exception.match('etclnk') || exception.match('webzine') ) {  } // 웹진, 퀵링크는 제외
		else gnbOpen($(this).parent());
	});
	$('div#gnb div.section:last-child ul > *:last-child a').blur(function() {
		var obj = $('div#gnb li');
		obj.find('.gnbsub').hide();
		obj.find('img').each(function() {
			$(this).attr('src', $(this).attr('src').replace('_on', '_off'));
		});
	});

});

// gnb hover
function gnbOpen(obj) {
	// IE6 width 100%
	if(navigator.userAgent.match('MSIE 6')) {
		var winw = document.documentElement.offsetWidth - 17;
		obj.find('.gnbsub').css('width',winw);
	}
	if (navigator.userAgent.match('MSIE 6') && navigator.userAgent.match('MSIE 7')) {
		$(".gnbsub").append('<iframe id="iframe" title="빈프레임"></iframe>');
	} 
	obj.find('.gnbsub').show();
	//obj.find('.gnbsub').animate({height: '320px'}, 500, function() {});
	obj.find('.gnbsub').css('height','230px');
	obj.find('.gnbsub .inner').animate({opacity: 1.0}, 300);
	obj.find('img').each(function() {
		$(this).attr('src', $(this).attr('src').replace('_off', '_on'));
	});
}
// gnb out
function gnblCose(obj) {
	obj.find('.inner').animate({opacity: 0.0}, 300);
	setTimeout(function(){ obj.find('.gnbsub').hide(); },300); 
	obj.find('img').each(function() {
		$(this).attr('src',$(this).attr('src').replace('_on', '_off'));
	});
	if (navigator.userAgent.match('MSIE 6') && navigator.userAgent.match('MSIE 7')) {
		$(".gnbsub").find('#iframe').remove();
	} 
}

// LNB
function initLnb() {
	//$('div#lnb li ul').hide();
	$('div#lnb li a').click ( function() {
		var target = $(this).next();
		var img = $(this).children();
		var condition = $(this).attr('class');
		
		if((target.is('ul')) && (!target.is(':visible'))) {
			switch(condition){
				case 'd1':
					var targetReset = $('div#lnb ul.depth2');
					changeImg();
					img.attr('src', img.attr('src').replace('_off', '_on'));
					break;
				case 'd2':
					var targetReset = $('div#lnb ul.depth3');
					$('div#lnb li a.d2').removeClass('on');
					$(this).addClass('on');
					break;
			}
			targetReset.slideUp('fast');
			target.slideDown('fast').show();
		} // return false;
	});
	function changeImg() {
		var allimg = $('div#lnb a img');
		for(i=0; i< allimg.length; i++) {
			allimg[i].src = allimg[i].src.replace('_on', '_off');
		}
	}
}

// 팝업창 리사이즈 임시
function resizePop(w,h){
	var marginHeight = parseInt($('body.popup #head').height()) + parseInt($('body.popup #foot').height());
	var containH = (parseInt(h)-marginHeight);
	$('html, body.popup, body.popup #head, body.popup #contain').css('width',w)
	$('body.popup #contain').css('height',containH);
	if($('body.popup #wrap').height() > containH && isIE == true){ // 스크롤바 생성시 여백 조절
		$('body.popup #wrap').css('padding-right','20px');
	}
}


// 폰트사이즈 조절
var currentFontSize = 5;
function zoomUtil(state, e){
	var idx;
	var arrFontSize = new Array();

	arrFontSize[0] = "45%";
	arrFontSize[1] = "55%";
	arrFontSize[2] = "65%";
	arrFontSize[3] = "75%";
	arrFontSize[4] = "85%";
	arrFontSize[5] = "100%";
	arrFontSize[6] = "110%";
	arrFontSize[7] = "150%";
	arrFontSize[8] = "200%";

	var e = e || window.event;
	if (e) {
		if (state == "plus") {
			if (currentFontSize < 8 ) {
				idx = currentFontSize + 1;
				currentFontSize = idx;
			}else{
				idx = 8 ;
				currentFontSize = idx;
			}
		} else if (state == "default") {
			idx = 3;
			currentFontSize = idx;
		} else if (state == "minus") {
			if ( currentFontSize >= 1) {
				idx = currentFontSize - 1;
				currentFontSize = idx;
			}else{
				idx = 0;
				currentFontSize = idx;
			}
		}
	}
	document.getElementById('contents').style.fontSize = arrFontSize[idx];
	return false;
}



// toggleTabMenu
// ex) toggleTabMenu('datalatest','a');
//  <a class="tab" href="#data1">
function toggleTabMenu(tabContainerID,tagName) {
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName(tagName);
	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 = tabMenuOn;
		//thismenu.onfocus = tabMenuOn;

		if (!thismenu.container.first) // 첫번째 메뉴가 선택된 상태로 로딩될 때
			thismenu.container.first = thismenu;
	}
	if (tabContainer.first) // 첫번째 메뉴가 선택된 상태로 로딩될 때 
		tabContainer.first.onclick();
}

function tabMenuOn() {
	currentmenu = this.container.current;
	if (currentmenu == this)
		currentmenu.targetEl.style.display = "";
	if (currentmenu) {
		currentmenu.targetEl.style.display = "none";
		if (currentmenu.imgEl) {
			currentmenu.imgEl.src = currentmenu.imgEl.src.replace("_on.gif", "_off.gif");
			currentmenu.className = currentmenu.className.replace(" on", "");
		} else { //이미지가 아닐때
			currentmenu.className = currentmenu.className.replace(" on", "");
		}
	}
	this.targetEl.style.display = "";
	if (this.imgEl && this.imgEl.src.indexOf("_on") == -1) {
		this.imgEl.src = this.imgEl.src.replace("_off.gif", "_on.gif");
		this.className += " on";
	} else { //이미지가 아닐때
		this.className += " on";
	}
	this.container.current = this;
	return false;
};

function tabMenuOut() {
	currentmenu = this.container.current;
	currentmenu.targetEl.style.display = "none";
	this.container.current = this;
	return false;
};



/* ------------------------------
메인
------------------------------ */
// 메인 토글
// mainTabMenu('datalatest','a');
// <a class="tab" href="#data1">
function mainTabMenu(tabContainerID,tagName) {
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName(tagName);
	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.className += " hidden";
		thismenu.imgEl = thismenu.getElementsByTagName("img").item(0);
		thismenu.onclick = maintabMenuOn;
		//thismenu.onfocus = maintabMenuOn;

		if (!thismenu.container.first) // 첫번째 메뉴가 선택된 상태로 로딩될 때
			thismenu.container.first = thismenu;
	}
	if (tabContainer.first) // 첫번째 메뉴가 선택된 상태로 로딩될 때 
		tabContainer.first.onclick();
}
function maintabMenuOn() {
	currentmenu = this.container.current;
	if (currentmenu == this)
		currentmenu.targetEl.className = currentmenu.targetEl.className.replace(" hidden", " visible"); // 컨텐츠 보이기
	if (currentmenu) {
		currentmenu.targetEl.className = currentmenu.targetEl.className.replace(" visible", " hidden"); // 컨텐츠 감추기
		if (currentmenu.imgEl) {
			currentmenu.imgEl.src = currentmenu.imgEl.src.replace("_on.gif", "_off.gif");
			currentmenu.className = currentmenu.className.replace(" current", "");
		} else { //이미지가 아닐때
			currentmenu.className = currentmenu.className.replace(" current", "");
		}
	}
	this.targetEl.className = this.targetEl.className.replace(" hidden", " visible"); // 컨텐츠 보이기
	if (this.imgEl && this.imgEl.src.indexOf("_on") == -1) {
		this.imgEl.src = this.imgEl.src.replace("_off.gif", "_on.gif");
		this.className += " current";
	} else { //이미지가 아닐때
		this.className += " current";
	}
	this.container.current = this;
	return false;
};
function maintabMenuOut() {
	currentmenu = this.container.current;
	currentmenu.targetEl.className = currentmenu.targetEl.className.replace(" visible", " hidden"); // 컨텐츠 감추기
	this.container.current = this;
	return false;
};

/// 메인 비주얼
function start_Int(){
	if(intval=="")
	    intval=window.setInterval("mainslider()",5000);
	else
	    stop_Int();
}
function stop_Int(){
    if(intval!=""){
      window.clearInterval(intval);
      intval="";
  }
}
var currentMainImage = 1;
var mainImageCnt =  0;


$(document).ready(function() {
	mainImageCnt = $("div.visual ul li").length;
});

function mainImg(num) {
	if(currentMainImage==num) return;

	$(".visual-img"+num).css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
	$(".visual-img"+currentMainImage).animate({opacity: 0.0}, 1000).removeClass('show');
	$(".visual-num"+num).addClass("on");
	$(".visual-num"+currentMainImage).removeClass("on");
	currentMainImage=num;

	stop_Int();
	start_Int();
}
function mainslider() {
	if(currentMainImage+1 > mainImageCnt) mainImg(1);
	else mainImg(currentMainImage+1);
}

/* layer close */
function closelayer(ele) {
	var eid = document.getElementById(ele);
	eid.style.display = "none" ;
}


/* layer open : toggle */
function Showlayer(ele) {
	var eid = document.getElementById(ele);

  if (eid.style.display == "none") {
    eid.style.display = "" ;
  } else {
    eid.style.display = "none" ;
  }

}

/* popup open 
   onclick="openWin(this.href,'new','500','300'); return false;"
   onclick="openWin(this.href,'new','full'); return false;"
*/
function openWin(url, name, width, height) {
	if(width == 'full') { // fullscreen일 때
		var info = 'fullscreen,scrollbars';
	} else {
		var info = 'width=' + width + ',height=' + height + ',resizable=no, scrollbars=yes, menubars=no, status=no';
	}
	window.open(url, name, info);
}
