
//neungyule main object
var nym={};

//basic properties
nym={

	//use white interface elements id
	whiteinterface : 'content_buildngrow, content_tomato, content_bizservice',

	//wrap
	wrap : null,

	//header
	header : null,

	//footer
	footer : null,

	//visual contents container
	vcontainer : null,

	//fake article for last article
	fakearticle : null,

	//main visual contents array
	vcontents : [],
	vcontentslength : 0,

	//event object
	event : {},

	//main visual contents height
	vheight : 0,

	//main visual total contents height
	vtotalheight : 0,

	//document height
	docheight : 0,

	//check visual contents moving
	nowmoving : false,

	//timer to fix position to closer visual
	fixpositiontimer : null,

	//check content has recruit info
	hasrecruitcontent : false,

	//timerto main visual if recruit info exist
	tomainvisualtimer : null,

	//check user can view footer
	footerviewable : false,

	//check in footer view mode
	footervierwmode : false,

	//base top for footer view setting
	basetopforfooter : 0

};

//set relative position
nym.setrelposition={

	time : 0.3,
	delay : 0.05,
	easing : 'easeOutCirc',

	//isclick - 메뉴에서 클릭한 경우인지 여부(true|false)
	action : function(isclick){

		var newtop=0;
		//var targetvalue=(isclick)? nym.vcontainer.offsetTop*-1 : (ismobile)? nym.wrap.scrollbar.scrollTop() : util.scroll.get()[1];
		//var targetvalue=(ismobile)? nym.wrap.scrollbar.scrollTop() : nym.vcontainer.offsetTop*-1;
		var targetvalue=nym.vcontainer.offsetTop*-1;

		for(var i=0; i<nym.vcontentslength; i++){

			//get new top
			newtop=(nym.vcontents[i].offsetTop-targetvalue)*-1;

			//set position and opacity to visual items
			for(var j=0, sv=0, jmax=nym.vcontents[i].item.length; j<jmax; j++){
				sv=newtop*nym.vcontents[i].item[j].relativepos+nym.vcontents[i].item[j].originalpos;
				nym.vcontents[i].item[j].style.top=sv+'px';
				if(nym.vcontents[i].item[j].useopacity){
					util.opacity.set(nym.vcontents[i].item[j], (nym.vheight-Math.abs(newtop))/nym.vheight);
				}
			}

			//reposition visual wrapper
			nym.vcontents[i].wrapper.style.top=newtop+'px';

		}

	}

}

//tab menu
nym.tab={

	dot : null,
	item : null,

	nowno : 0,
	resettimer : null,

	over : function(){
		clearTimeout(nym.tab.resettimer);
		nym.tab.focus(this.no);
	},

	out : function(){
		nym.tab.resettimer=setTimeout(function(){
			nym.tab.focus(nym.tab.nowno);
		}, 500);
	},

	focus : function(no){
		var bgtop, bgleft;
		for(var i=0; i<nym.vcontents[0].tab.item.length; i++){
			bgtop=(i==no)? (nym.vcontents[0].tab.item[i].bgy-nym.vcontents[0].tab.item[i].height) : nym.vcontents[0].tab.item[i].bgy;
			bgleft=(i==no)? 0 : -110;
			ani.set({target:nym.vcontents[0].tab.item[i].a, to:'background-position:0 '+bgtop+'px', time:0.75, easing:'easeOutQuart', onupdate:function(tg){
				if(tg.vs.backgroundPosition){
					for(var i=1; i<nym.vcontentslength-1; i++) nym.vcontents[i].tab.item[tg.parentNode.no].a.style.backgroundPosition='0 '+tg.vs.backgroundPosition[1][tg.step]+'px';
				}
			}});
			ani.set({target:nym.vcontents[0].tab.item[i], to:'background-position:'+bgleft+'px '+nym.vcontents[0].tab.item[i].linebgtop+'px', time:0.5, easing:'easeOutQuart', onupdate:function(tg){
				if(tg.vs.backgroundPosition){
					for(var i=1; i<nym.vcontentslength-1; i++) nym.vcontents[i].tab.item[tg.no].style.backgroundPosition=tg.vs.backgroundPosition[0][tg.step]+'px '+tg.linebgtop+'px';
				}
			}});
		}
		ani.set({target:nym.vcontents[0].tab.dot, to:'top:'+(nym.vcontents[0].tab.item[no].offsetTop+nym.vcontents[0].tab.item[no].offsetHeight-22), time:0.75, easing:'easeOutQuart', onupdate:function(tg){
			if(tg.vs.top){
				for(var i=1; i<nym.vcontentslength-1; i++) nym.vcontents[i].tab.dot.style.top=tg.vs.top[tg.step]+'px';
			}
		}});
	},
	
	click : function(){

		nym.auto.clear();
		clearTimeout(nym.vcontainer.timer);
		clearTimeout(nym.tomainvisualtimer);
	
		//if(nym.nowmoving) return false;

		nym.nowmoving=true;
		nym.tab.nowno=this.no;
		
		if(nym.footervierwmode){
			ani.set({target:nym.wrap, to:'top:0', time:0.75, easing:100});
		}

		ani.set({target:nym.vcontainer, to:'margin-top:'+((nym.tab.nowno*nym.vheight)*-1), time:1, easing:'easeInOutExpo',
			onupdate : function(){
				nym.setrelposition.action(true);
			},
			onend : function(tg){
				nym.nowmoving=false;
				nym.auto.set();
			}
		});

		return false;

	},

	initialize : function(){
		for(var i=0, j=0, k=0, lis; i<nym.vcontentslength-1; i++){
			nym.vcontents[i].tab.dot=nym.vcontents[i].tab.getElementsByTagName('span')[0];
			nym.vcontents[i].tab.item=nym.vcontents[i].tab.getElementsByTagName('li');
			for(j=0; j<nym.vcontents[i].tab.item.length; j++){
				nym.vcontents[i].tab.item[j].no=nym.vcontents[i].tab.item[j].parentNode.no=j;
				nym.vcontents[i].tab.item[j].a=nym.vcontents[i].tab.item[j].getElementsByTagName('a')[0];
				nym.vcontents[i].tab.item[j].height=nym.vcontents[i].tab.item[j].a.offsetHeight;
				nym.vcontents[i].tab.item[j].linebgtop=nym.vcontents[i].tab.item[j].height+3;
				nym.vcontents[i].tab.item[j].bgy=parseInt(ani.getstyle(nym.vcontents[i].tab.item[j].a, 'backgroundPosition').split(' ')[1]);
				nym.vcontents[i].tab.item[j].a.onclick=function(){
					return false;
				};
				if(isie) nym.vcontents[i].tab.item[j].a.hideFocus=true;
				nym.vcontents[i].tab.item[j].onmouseover=nym.tab.over;
				nym.vcontents[i].tab.item[j].onmouseout=nym.tab.out;
				nym.vcontents[i].tab.item[j].onclick=nym.tab.click;
			}
		}
		nym.tab.focus(nym.tab.nowno);
	}

}

//career
nym.career={

	obj : null,
	item : null,
	itemheight : 0,
	opened : false,
	closetimer : null,

	open : function(){
		clearTimeout(nym.career.closetimer);
		if(ismobile && nym.career.opened){
			return true;
		}
		for(var i=1; i<nym.career.item.length; i++){
			ani.set({target:nym.career.item[i], to:'margin-top:1', time:0.6, easing:'easeOutExpo', delay:0.1*(i-1)});
		}
		if(ismobile && !nym.career.opened){
			nym.career.opened=true;
			return false;
		}
	},

	close : function(){
		nym.career.closetimer=setTimeout(function(){
			nym.career.closeaction();
		}, 100);
	},

	closeaction : function(){
		for(var i=1, max=nym.career.item.length; i<max; i++){
			ani.set({target:nym.career.item[i], to:'margin-top:-'+nym.career.itemheight, time:0.6, easing:'easeOutExpo', delay:0.1*(max-i)});
		}
	},

	closeinmobile : function(e){
		var etarget=e.target;
		while(!(/body/i).test(etarget.nodeName)){
			etarget=etarget.parentNode;
			if(etarget==nym.career.obj){
				return false;
			}
		}
		if(nym.career.opened){
			nym.career.opened=false;
			nym.career.closeaction();
		}
	},

	initialize : function(){

		nym.career.obj=document.getElementById('careers');
		nym.career.item=nym.career.obj.getElementsByTagName('li');

		if(nym.career.item.length==1){
			nym.career.item[0].className='last';
			return;
		}

		nym.career.itemheight=nym.career.item[0].offsetHeight;

		for(var i=0, max=nym.career.item.length; i<max; i++){
			if(i) nym.career.item[i].style.marginTop=-nym.career.itemheight+'px';
			nym.career.item[i].style.zIndex=(max-i);
		}

		if(ismobile){
			nym.career.obj.ontouchend=nym.career.open;
			util.event.add(document, 'touchend', nym.career.closeinmobile);
		}else{
			nym.career.obj.onmouseover=nym.career.open;
			nym.career.obj.onmouseout=nym.career.close;
		}

	}

}

//product
nym.product={

	obj : null,
	movesize : 176,

	nowpage : 0,
	totalpage : 0,

	click : function(){
		nym.product.nowpage=(this.flag=='prev')? nym.product.nowpage-1 : nym.product.nowpage+1;
		ani.set({target:nym.vcontents[0].product.moveobj, to:'margin-left:-'+(nym.product.nowpage*nym.product.movesize), time:0.75, easing:'easeOutExpo', onupdate:function(tg){
			for(var i=1; i<nym.vcontentslength; i++) nym.vcontents[i].product.moveobj.style.marginLeft=tg.vs.marginLeft[tg.step]+'px';
		}});
		nym.product.setbtn();
	},

	setbtn : function(){
		for(var i=0; i<nym.vcontentslength; i++){
			if(nym.product.nowpage==0){
				nym.vcontents[i].product.btnprev.onclick=null;
				nym.vcontents[i].product.btnprev.style.cursor='default';
				util.opacity.set(nym.vcontents[i].product.btnprev, 0.3);
			}else{
				nym.vcontents[i].product.btnprev.onclick=nym.product.click;
				nym.vcontents[i].product.btnprev.style.cursor='pointer';
				util.opacity.set(nym.vcontents[i].product.btnprev, 1);
			}
			if(nym.product.nowpage==nym.product.totalpage-1){
				nym.vcontents[i].product.btnnext.onclick=null;
				nym.vcontents[i].product.btnnext.style.cursor='default';
				util.opacity.set(nym.vcontents[i].product.btnnext, 0.3);
			}else{
				nym.vcontents[i].product.btnnext.onclick=nym.product.click;
				nym.vcontents[i].product.btnnext.style.cursor='pointer';
				util.opacity.set(nym.vcontents[i].product.btnnext, 1);
			}
		}
	},

	initialize : function(){
		var childs;
		for(var i=0, j=0, k=0, lis; i<nym.vcontentslength; i++){
			nym.vcontents[i].product.moveobj=nym.vcontents[i].product.getElementsByTagName('ul')[0];
			lis=nym.vcontents[i].product.moveobj.getElementsByTagName('li');
			for(j=1; j<lis.length; j+=2){
				lis[j].className='even';
			}
			if(!i) nym.product.totalpage=Math.ceil(lis.length/2);
			childs=nym.vcontents[i].product.childNodes;
			for(j=0; j<childs.length; j++){
				if(childs[j].nodeType==1 && (/span/i).test(childs[j].nodeName)){
					if(!k){
						childs[j].flag='prev';
						nym.vcontents[i].product.btnprev=childs[j];
					}else{
						childs[j].flag='next';
						nym.vcontents[i].product.btnnext=childs[j];
					}
					k++;
				}
			}
			k=0;
		}
		nym.product.setbtn();
	}

}

//recent articles
nym.recents={

	over : function(){
		for(var i=0; i<nym.vcontentslength; i++){
			nym.vcontents[i][this.flag].item[this.no].className='over';
		}
	},

	out : function(){
		for(var i=0; i<nym.vcontentslength; i++){
			nym.vcontents[i][this.flag].item[this.no].className='';
		}
	},

	setting : function(flag){
		for(var i=0, j=0; i<nym.vcontentslength; i++){
			nym.vcontents[i][flag].item=(nym.vcontents[i][flag].getElementsByTagName('ul')[0])? 
				nym.vcontents[i][flag].getElementsByTagName('ul')[0].getElementsByTagName('a') :
				nym.vcontents[i][flag].getElementsByTagName('div')[0].getElementsByTagName('a');
			for(j=0; j<nym.vcontents[i][flag].item.length; j++){
				nym.vcontents[i][flag].item[j].no=j;
				nym.vcontents[i][flag].item[j].flag=flag;
				nym.vcontents[i][flag].item[j].onmouseover=nym.recents.over;
				nym.vcontents[i][flag].item[j].onmouseout=nym.recents.out;
			}
		}
	},

	initialize : function(){
		nym.recents.setting('news');
		nym.recents.setting('notice');
		nym.recents.setting('blognews');
	}

}

//auto
nym.auto={

	timer : null,

	set : function(){
		nym.auto.clear();
		nym.auto.timer=setTimeout(function(){
			nym.tab.nowno++;
			if(nym.tab.nowno>=nym.vcontents[0].tab.item.length){
				nym.tab.nowno=(nym.hasrecruitcontent)? 1 : 0;
			}
			nym.tab.focus(nym.tab.nowno);
			nym.vcontents[0].tab.item[nym.tab.nowno].onclick();
		}, 5000);
	},

	clear : function(){
		clearTimeout(nym.auto.timer);
	}

}

//initialize
nym.initialize=function(){

	var i=0, j=0, max;

	if(ismobile) document.getElementById('container').className='mobile';

	nym.wrap=document.getElementById('wrap');
	nym.header=document.getElementById('header');
	nym.footer=document.getElementById('footer');
	nym.header.menuobj=nym.header.getElementsByTagName('div')[0];

	//remove unnecessary items in #header
	nym.header.removeChild(nym.header.getElementsByTagName('h1')[0]);
	var headerlis=nym.header.getElementsByTagName('li');
	for(i=0, max=headerlis.length; i<max; i++){
		if(headerlis[i].className=='english' || headerlis[i].className=='korean'){
			headerlis[i].parentNode.removeChild(headerlis[i]);
			break;
		}
	}

	//set touch disable in mobile
	//if(ismobile) nym.footer.style.pointerEvents='none';

	//remove unnecessary items in #footer
	nym.footer.removeChild(document.getElementById('page_top'));

	var copycontainer=document.getElementById('copycontents');

	nym.vcontainer=document.getElementById('visualcontents');

	//add fake article
	nym.fakearticle=util.create({tagname:'div', id:'content_blank', classname:'article'});
	nym.fakearticle.innerHTML='<div class="visual"></div>';
	nym.vcontainer.appendChild(nym.fakearticle);

	var vcontainerchilds=nym.vcontainer.childNodes;
	for(i=0, j=0, ptags=null, max=vcontainerchilds.length; i<max; i++){

		if(vcontainerchilds[i].nodeType==1){

			if(vcontainerchilds[i].id=='content_recruit'){
				nym.hasrecruitcontent=true;
			}

			if(vcontainerchilds[i].id && nym.whiteinterface.indexOf(vcontainerchilds[i].id)!=-1){
				vcontainerchilds[i].className+=' article_w';
			}

			vcontainerchilds[i].visual=vcontainerchilds[i].getElementsByTagName('div')[0];

			//get moving items
			vcontainerchilds[i].visual.item=[];
			ptags=vcontainerchilds[i].visual.getElementsByTagName('p');
			for(j=0, jmax=ptags.length; j<jmax; j++){
				ptags[j].originalpos=parseInt(ptags[j].style.top);//get opriginal positioni
				ptags[j].relativepos=parseInt(ptags[j].className.match(/rp_(\-?[0-9]+)/)[1])/100;//get relative position from classname
				if((/opacity/).test(ptags[j].className)) ptags[j].useopacity=true;//check use opacity
				if(ptags[j].innerHTML.indexOf('btn_')!=-1){//if item has button, clear auto move timer when mouse over and restart when mouse out
					ptags[j].getElementsByTagName('img')[0].onmouseover=function(){
						nym.auto.clear();
					}
					ptags[j].getElementsByTagName('img')[0].onmouseout=function(){
						nym.auto.set();
					}
				}
				vcontainerchilds[i].visual.item.push(ptags[j]);
			}

			//create wrapper div
			vcontainerchilds[i].visual.wrapper=util.create({tagname:'div', classname:'wrapper'});
			vcontainerchilds[i].visual.wrapper.innerHTML=copycontainer.innerHTML;
			vcontainerchilds[i].visual.appendChild(vcontainerchilds[i].visual.wrapper); 

			vcontainerchilds[i].visual.h1=vcontainerchilds[i].visual.wrapper.getElementsByTagName('h1')[0];
			vcontainerchilds[i].visual.h1.onclick=function(){
				nym.tab.nowno=(nym.hasrecruitcontent)? 1 : 0;
				nym.tab.focus(nym.tab.nowno);
				nym.vcontents[0].tab.item[nym.tab.nowno].onclick();
			}
			vcontainerchilds[i].visual.tab=vcontainerchilds[i].visual.wrapper.getElementsByTagName('div')[0];
			//vcontainerchilds[i].visual.product=vcontainerchilds[i].visual.wrapper.getElementsByTagName('div')[1];
			vcontainerchilds[i].visual.news=vcontainerchilds[i].visual.wrapper.getElementsByTagName('div')[1];
			vcontainerchilds[i].visual.notice=vcontainerchilds[i].visual.wrapper.getElementsByTagName('div')[3];
			vcontainerchilds[i].visual.blognews=vcontainerchilds[i].visual.wrapper.getElementsByTagName('div')[4];

			nym.vcontentslength++;

			if(nym.vcontentslength==8) vcontainerchilds[i].visual.tab.innerHTML='';

			nym.vcontents.push(vcontainerchilds[i].visual);

		}

	}

	//remove copy container
	copycontainer.innerHTML='';
	copycontainer.parentNode.removeChild(copycontainer);

	//remove loading icon and show contents
	document.getElementById('loading').parentNode.removeChild(document.getElementById('loading'));
	nym.wrap.style.display='block';

	//set visual contents height
	nym.vheight=nym.vcontents[0].offsetHeight;

	//initialize main items
	nym.tab.initialize();
	//nym.career.initialize();
	//nym.product.initialize();
	nym.recents.initialize();

/*
	//use fakescroll in mobile instead of window scroll
	if(ismobile){
		fakescroll('wrap', {
			wrapper : 'visualcontents',
			x  : {hidden : true},
			onscrollstart : function(){
				util.opacity.set(this.scrollbar.y.track, 0.45);
			},
			onscroll : nym.onscroll,
			onscrollend : function(){
				ani.set({target:this.scrollbar.y.track, to:'opacity:0', time:0.5});
			}
		});
		util.opacity.set(nym.wrap.scrollbar.y.track, 0);
		util.event.add(nym.header, 'touchmove', function(e){
			e.preventDefault();
			return false;
		});
	}
*/

	if(isie6 || isie7){
		var atags=document.body.getElementsByTagName('a');
		for(i=0, max=atags.length; i<max; i++){
			atags[i].hideFocus=true;
		}
	}

	nym.onresize();

	nym.setrelposition.action(false);

	if(nym.hasrecruitcontent){
		nym.tomainvisualtimer=setTimeout(function(){
			nym.tab.nowno=1;
			nym.tab.focus(nym.tab.nowno);
			nym.vcontents[0].tab.item[nym.tab.nowno].onclick();
			nym.auto.set();
		}, 2000);
	}else{
		nym.auto.set();
	}

}

//on load
window.onload=nym.initialize;

//on scroll
nym.onscroll=function(){

/*
	nym.auto.clear();
	clearTimeout(nym.fixpositiontimer);
	clearTimeout(nym.vcontainer.timer);

	var scroll=(ismobile)? [0, nym.wrap.scrollbar.scrollTop()] : util.scroll.get();
	var newno=Math.round(scroll[1]/nym.vheight);
	if(newno>nym.vcontentslength-2) newno=nym.vcontentslength-2;

	if(nym.tomainvisualtimer && scroll[1]>nym.vheight){
		clearTimeout(nym.tomainvisualtimer);
		nym.tomainvisualtimer=null;
	}

	//if(!nym.nowmoving){
		if(nym.tab.nowno!=newno){
			nym.tab.nowno=newno;
			if(nym.tab.nowno>-1 && nym.vcontentslength>nym.tab.nowno) nym.tab.focus(nym.tab.nowno);
		}
		if(!ismobile){
			nym.vcontainer.style.marginTop=-scroll[1]+'px';
		}
		nym.setrelposition.action(false);
		nym.fixpositiontimer=setTimeout(function(){
			if(scroll[1]%nym.vheight===0) return;
			nym.tab.nowno=newno;
			if(nym.tab.nowno>-1 && nym.vcontentslength>nym.tab.nowno){
				nym.tab.focus(nym.tab.nowno);
				nym.vcontents[0].tab.item[nym.tab.nowno].onclick();
			}
		}, 200);
	//}

	if(!ismobile && !nym.footerviewable){
		if(scroll[1]>nym.basetopforfooter){
			if(isie6) nym.wrap.style.top=(scroll[1]+(nym.basetopforfooter-scroll[1]))+'px';
			else nym.wrap.style.top=(nym.basetopforfooter-scroll[1])+'px';
			if(!nym.footervierwmode){
				nym.vcontainer.style.marginTop=-(nym.basetopforfooter)+'px';
				nym.setrelposition.action(false);
				nym.footervierwmode=true;
			}
		}else{
			if(isie6) nym.wrap.style.top=scroll[1]+'px';
			else nym.wrap.style.top=0;
			nym.vcontainer.style.marginTop=-scroll[1]+'px';
			nym.setrelposition.action(false);
			nym.footervierwmode=false;
		}
	}else{
		if(isie6) nym.wrap.style.top=scroll[1]+'px';
		else nym.wrap.style.top=0;
		nym.vcontainer.style.marginTop=-scroll[1]+'px';
		nym.setrelposition.action(false);
		nym.footervierwmode=false;
	}
	if(ismobile || nym.basetopforfooter>=scroll[1]){
		nym.fixpositiontimer=setTimeout(function(){
			if(scroll[1]%nym.vheight===0) return;
			nym.tab.nowno=newno;
			if(nym.tab.nowno>-1 && nym.vcontentslength>nym.tab.nowno){
				nym.tab.focus(nym.tab.nowno);
				nym.vcontents[0].tab.item[nym.tab.nowno].onclick();
			}
		}, 200);
		nym.auto.set();
	}

	if(scroll[0]) nym.wrap.style.left=-scroll[0]+'px';
*/

}
window.onscroll=nym.onscroll;

//on resize
nym.onresize=function(){

	nym.docheight=document.documentElement.clientHeight;

	nym.wrap.style.height=(769>nym.docheight)? '768px' : '100%';

	nym.header.menuobj.style.height=(nym.wrap.offsetHeight-10)+'px';

	nym.vtotalheight=(nym.vheight*(nym.vcontentslength-1))+(nym.docheight-nym.vheight);
	//if(ismobile) nym.vcontents[nym.vcontentslength-1].parentNode.style.height=(nym.docheight-nym.vheight)+'px';

/*
	if(ismobile){
		nym.wrap.scrollbar.reset();
	}else{
		nym.footerviewable=nym.docheight>768;
		nym.basetopforfooter=nym.vheight*(nym.vcontentslength-2);
	}
*/
}
window.onresize=nym.onresize;
