// JavaScript Document

// hasClass
function hasClass(element,value) {
	var re = new RegExp("(^|\\s)" + value + "(\\s|$)");
	return re.test(element.className);
}

// addClass
function addClass(element,value) {
	if (!element.className) {
			element.className = value;
	}else{
		var new_class_name = element.className;
		if (!hasClass(element,value)) {
			element.className += " " + value;
		}
	}
}

// removeClass
function removeClass(element,value) {
	if (element.className && hasClass(element,value)) {
		var re = new RegExp("(^|\\s)" + value);
		element.className = element.className.replace(re,"");
	}
}

// getElementsByClass('노드/생략[윈도우]', '태그/생략[*]', '클래스명');
function getElementsByClass(node, tagName, srchClass) {
    node = node || window.document;
    tagName = tagName ? tagName.toUpperCase() : "*";
    var eles = node.getElementsByTagName(tagName);
	
	if(!srchClass) return eles;

    var arr = new Array;
    for (var i=0; i<eles.length; i++) {
        if (hasClass(eles[i],srchClass)) arr.push(eles[i]);
    }
    return arr;
}

// 자신을 덮고있는태그 선택하기(클래스명 분류가능)
function parent(ele, tagName, srchClass){
	if(typeof tagName=="number"){
		for(var i=0; i<tagName; i++){
			if(ele!=null) ele = ele.parentNode;
		}
		return ele;
	}	
	
	tagName = tagName ? tagName.toUpperCase() : "*";
	if(srchClass){
		if(tagName!="*"){
			while((ele.nodeName!=tagName || !hasClass(ele,srchClass)) && ele.nodeName!="BODY")
				ele = ele.parentNode;
		}else{
			while(!hasClass(ele,srchClass) && ele.nodeName!="BODY")
				ele = ele.parentNode;
		}
	}else{
		if(tagName!="*"){
			while(ele.nodeName!=tagName && ele.nodeName!="BODY")
				ele = ele.parentNode;
		}else{
			ele = ele.parentNode;
		}
	}
	return ele;
}

/* insertBefore = target의 형제레벨로 앞쪽에 삽입 */
function insertBefore(source, target){
    target.parentNode.insertBefore(source, target);
}

/* insertAfter - target의 형제레벨로 뒤쪽에 삽입 */
function insertAfter(source, target) {
    var parent = target.parentNode;
    if(parent.lastChild == target)
        parent.appendChild(source);
    else
        parent.insertBefore(source, target.nextSibling);
}

// tabList
function tabList(tabContainerID, index) {
	index = index || 0;
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName("A");
	var i = 0;

	var tabA = new Array;
	for(i=0; i<tabAnchor.length; i++) {
		if (hasClass(tabAnchor[i], "tab")){
			var alink = tabAnchor.item(i);
			tabA.push(tabAnchor[i]);
		}else{
			continue;
		}

		alink.container = tabContainer;
		alink.targetEl = document.getElementById(tabAnchor[i].href.split("#")[1]);
		alink.targetEl.style.display = "none";
		alink.imgEl = alink.getElementsByTagName("img")[0];
		
		if(getElementsByClass(alink.targetEl, "P", "more")[0])
			getElementsByClass(alink.targetEl, "P", "more")[0].style.position = "absolute";
			
		alink.onmouseover = function(){
			oldActive = this.container.current;
			if(oldActive == this) return false;

			if(oldActive){
				oldActive.targetEl.style.display = "none";
				if(tabContainerID!="btshoptab")
				if(oldActive.imgEl) oldActive.imgEl.src = oldActive.imgEl.src.replace("_on.gif", ".gif");
				removeClass(oldActive.parentNode, "current");
			}
			
			this.targetEl.style.display = "";
			if(tabContainerID!="btshoptab")
			if(this.imgEl) this.imgEl.src = this.imgEl.src.replace(".gif", "_on.gif");
			addClass(this.parentNode, "current");

			this.container.current = this;
			return false;
		}

		if(!alink.container.first) alink.container.first = tabA[index];
	}

	if(tabContainer.first) tabContainer.first.onmouseover();
	
	/* 탭a태그 노드들을 배열로 반환 */
	return tabA;
}

// tabList
function tabSetList(tabContainerID, index) {
	index = index || 0;
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName("A");
	var i = 0;
	var setTimeCheck;

	var tabA = new Array;
	for(i=0; i<tabAnchor.length; i++) {
		if (hasClass(tabAnchor[i], "tab")){
			var alink = tabAnchor.item(i);
			tabA.push(tabAnchor[i]);
		}else{
			continue;
		}
		alink.container = tabContainer;
		alink.targetEl = document.getElementById(tabAnchor[i].href.split("#")[1]);
		alink.targetEl.style.display = "none";
		alink.imgEl = alink.getElementsByTagName("img")[0];
		
		if(getElementsByClass(alink.targetEl, "P", "more")[0])
			getElementsByClass(alink.targetEl, "P", "more")[0].style.position = "absolute";
			
		alink.onmouseover = function(){
			oldActive = this.container.current;
			if(oldActive == this) return false;

			if(oldActive){
				oldActive.targetEl.style.display = "none";
				if(oldActive.imgEl) oldActive.imgEl.src = oldActive.imgEl.src.replace("_on.gif", ".gif");
				removeClass(oldActive.parentNode, "current");
			}

			this.targetEl.style.display = "";
			if(this.imgEl) this.imgEl.src = this.imgEl.src.replace(".gif", "_on.gif");
			addClass(this.parentNode, "current");
//			if(hasClass(this,"first")) index = 0;
//			else index = 1;

			this.container.current = this;
			return false;
		}
		alink.container.onmouseover = function(){
			clearInterval(setTimeCheck);
		}
		alink.container.onmouseout = function(){
			setInterCheck();
		}

		if(!alink.container.first) alink.container.first = tabA[index];
	}

	if(tabContainer.first) tabContainer.first.onmouseover();
	
	function setInterCheck(){
		setTimeCheck = setInterval(function(){
			++index;
			index = index%3;
			
			alink.container.first = tabA[index];
			
			if(tabContainer.first) tabContainer.first.onmouseover();
		}, 3000);
	}
	setInterCheck();

	/* 탭a태그 노드들을 배열로 반환 */
	return tabA;
}

// 툴팁
function Tooltip(obj, target){
	var obj = document.getElementById(obj);
	var objAlink = obj.getElementsByTagName("A");
	var target = document.getElementById(target);

	for(i=0; i<objAlink.length; i++){
		if(objAlink[i].className.indexOf("tip") != -1){
			objLink = objAlink[i];
		}
		target.style.position = "absolute";
		target.style.display = "none";
		
		objLink.onmouseover = function(e){
			var e = e || event;
			var leftPosition = e.clientX + document.documentElement.scrollLeft + document.body.scrollLeft + 2 + "px";
			var topPosition = e.clientY + document.documentElement.scrollTop + document.body.scrollTop + 2 + "px";
			
			target.style.left = leftPosition;
			target.style.top = topPosition;
			target.style.display = "";
		}
		objLink.onmouseout = function(){
			timeOut = setTimeout(function() {
				target.style.display = "none";
			}, 0);
		}
		target.onmouseover = function(){
			clearTimeout(timeOut);
		}
		target.onmouseout = function(){
			timeOut = setTimeout(function() {
				target.style.display = "none";
			}, 0);
		}
	}
}

// listRotation
function listRotation(eleID){
	var obj = document.getElementById(eleID);
	var btn = getElementsByClass(obj, 'BUTTON', 'rolling-button');
	var list = getElementsByClass(obj, 'DIV', 'rolling-bnnr');
	
	var loop=0;
	var _root = this;
	var intervalFunc_action=null;

	for(var i=0; i<btn.length; i++){
		if(i!=0) list[i].style.display = "none";
		
		btn[i].cnt = i;
		btn[i].onmouseover = function(){
			_root.change(this.cnt);
		}
	}

	// 동작
	this.change = function(num){
		if(loop == num) return; // 활성화된 버튼을 클릭시 return
		
		list[loop].style.display = "none";
	
		++loop;
		if(num!=undefined) loop=num;
		else loop=loop%btn.length;
	
		list[loop].style.display = "block";
	}
}

// 상하 롤링
function VRolling(eleID, num){
	var num = num || 1;
	var obj = document.getElementById(eleID);
	var objUl = obj.getElementsByTagName("UL")[0];
	var objLi = obj.getElementsByTagName("LI");
	var objA = obj.getElementsByTagName("A");
	var course; // 움직일 방향의 조건
	
	var objLiWidth = objLi[0].offsetWidth;
	objUl.style.width = (objLiWidth * objLi.length) + "px";
	
	if(objLi.length <= num) return false;
	
	this.getStyle = function(ele, value){
		if(typeof ele == 'string') var ele = document.getElementById(ele);
		
		if(ele.currentStyle){
			Svalue = ele.currentStyle[value];
		} else if(document.defaultView.getComputedStyle) {
			Svalue = document.defaultView.getComputedStyle(ele, null)[value];
		}
		return Svalue;
	}
	
	var moveInterVal;
	this.move = function(){
		moveInterVal = setInterval( function() {
		var speed = (course == "left") ? Math.floor((parseFloat(-objLiWidth) - parseFloat(_root.getStyle(objUl, "left"))) * 0.2) : Math.ceil((0 - parseFloat(_root.getStyle(objUl, "left"))) * 0.2);
		objUl.style.left = (parseFloat(_root.getStyle(objUl, "left")) + speed) + "px";
		}, 30)
	}
	
	var objButtonPrev = getElementsByClass(obj, "BUTTON", "button-left")[0]; // 이전버튼
	var objButtonNext = getElementsByClass(obj, "BUTTON", "button-right")[0]; // 다음버튼

	objButtonPrev.onclick = function(){ _root.btnPrev(); }
	objButtonNext.onclick = function(){ _root.btnNext(); }
	
	// 이전
	this.btnPrev = function(){
		course = "left";
		_root.requisite();
	}
	// 다음
	this.btnNext = function(){
		course = "right";
		_root.requisite();
	}
	
	this.requisite = function(){
		for(i=0; i<num; i++){
			(course == "up") ? insertAfter(objLi[0], objLi[objLi.length - 1]) : insertBefore(objLi[objLi.length - 1], objLi[0]);
		}
		
		if(course == "left"){
			objUl.style.left = 0;
		} else {	
			objUl.style.left = -parseFloat(objLiWidth) + "px";
		}
	
	}
	
	var _root = this;
	_root.move();
} 

//layer view
function layerView(obj){
	var obj = document.getElementById(obj);
	var objLink = obj.getElementsByTagName("A")[0];
	var timeOut;
	var current;

	objLink.targetLayer = document.getElementById(objLink.href.split("#")[1]);
	objLink.targetLayer.style.display = "none";
	
	objLink.onmouseover = function(){
		this.targetLayer.style.display = "";
		clearTimeout(timeOut);
	}
	objLink.onmouseout = function(){
		timeOut = setTimeout(function() { objLink.targetLayer.style.display = "none"; }, 50);
	}
	objLink.targetLayer.onmouseover = function(){
		clearTimeout(timeOut);
	}
	objLink.targetLayer.onmouseout = function(){
		timeOut = setTimeout(function() {
			objLink.targetLayer.style.display = "none";
		}, 50);
	}
	var etcAllTag = objLink.targetLayer.getElementsByTagName("*");
	for(j=0; j<etcAllTag.length; j++){
		etcAllTag[j].onmouseover = function(){
			current = "check";
			clearTimeout(timeOut);
		}
	}
}

/* 이미지 체인지(중국관) */
function imgChange(eleID, index){
	index = index || 0;
	var tabContainer = document.getElementById(eleID);
	var tabAnchor = tabContainer.getElementsByTagName("A");
	var i = 0;

	var tabA = new Array;
	for(i=0; i<tabAnchor.length; i++) {
		if (hasClass(tabAnchor[i], "sover")){
			var alink = tabAnchor.item(i);
			tabA.push(tabAnchor[i]);
		}else{
			continue;
		}

		alink.container = tabContainer;
		alink.targetEl = document.getElementById(tabAnchor[i].href.split("#")[1]);
		alink.targetEl.style.display = "none";
		alink.imgEl = alink.getElementsByTagName("img")[0];
		
		alink.onmouseover = function(){
			oldActive = this.container.current;
			if(oldActive == this) return false;

			if(oldActive){
				oldActive.targetEl.style.display = "none";
				if(oldActive.imgEl) oldActive.imgEl.src = oldActive.imgEl.src.replace("_on.gif", ".gif");
			}
			
			this.targetEl.style.display = "";
			if(this.imgEl) this.imgEl.src = this.imgEl.src.replace(".gif", "_on.gif");

			this.container.current = this;
			return false;
		}

		if(!alink.container.first) alink.container.first = tabA[index];
	}

	if(tabContainer.first) tabContainer.first.onmouseover();
}

// 플래쉬
function SWFLoader() {
	this.id = "";
	this.title = undefined;
	this.wmode = "window";
	this.flashvars = "";
	this.classId = 'clsid:d27cdb6e-ae6d-11cf-96b8-444553540000';
	this.codeBase = 'http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0';
	this.pluginSpage = 'http://www.macromedia.com/go/getflashplayer';
	this.embedType = 'application/x-shockwave-flash';
}
SWFLoader.prototype.setting = function(obj){
	for(var property in obj){
		this[property] = obj[property];
	}
	if(this.alternate && document.getElementById(this.alternate)){
		var node = document.getElementById(this.alternate);
		this.alternate = node.innerHTML;
		node.style.display = "none";
	}
	this.parameter = "";
	this.parameter+="<param name='wmode' value='Opaque'/>";
	this.parameter += "<param name='allowScriptAccess' value='always' />";
	this.parameter += "<param name='allowFullScreen' value='false' />";
	this.parameter += "<param name='movie' value='"+this.url+this.flashvars+"' />";
	this.parameter += "<param name='wmode' value='"+ this.wmode +"' />";
	this.parameter += "<param name='quality' value='best'/>";
	this.parameter += "<param name='base' value='.'>";
	this.parameter += "<param name='scale' value='noscale'/>";
	this.parameter += "<param name='expressinstall' value='Scripts/expressInstall.swf' />";
}
SWFLoader.prototype.addParameter = function(name, value){
	this.parameter += "<param name='"+name+"' value='"+value+"'/>";
}
SWFLoader.prototype.show = function(){
	var str = "";
	var title = (this.title) ? ' title="'+this.title+'"' : "";
	str += '<object id="'+this.id+'" width="'+this.width+'" height="'+this.height+'" classid="'+this.classId+'" codebase="'+this.codeBase+'"'+ title+'>';
		str += this.parameter;
		str += '<!--[if !IE]>-->';
		str += '<object id="'+this.id+'" type="application/x-shockwave-flash" data="'+this.url+this.flashvars+'" width="'+this.width+'" height="'+this.height+'" name="'+this.id+'"'+ title +'>';
		str += this.parameter;
		str += '<!--<![endif]-->';
			str += this.alternate;
		str += '<!--[if !IE]>-->';
		str += '</object>';
		str += '<!--<![endif]-->';
	str += '</object>';
	document.write(str);
}

// brnitemToogle
function brnitemToogle(){
	var ele = document.getElementById("mn_brnitem");
	
	if( hasClass(ele, "mn_brnitem_unlock") ){
		removeClass(ele, "mn_brnitem_unlock");
	}else{
		addClass(ele, "mn_brnitem_unlock");
	}
}


