/* gnb Navigation */
$.fn.gnav = function (options) {
	var config = $.extend({
			items: '> li',
			sub: '> div',
			subItems: '> ul > li',
			selectedClass: 'on',
			hasSubClass: 'has-sub',
			hasSubSelectedClass: 'has-sub-on',
				eventType: 'mouseover.gnav focusin.gnav',
			mainIndex: -1,
			subIndex: -1
		}, options);

	return this.each(function () {
		var self = this,
			$self = $(self),
			$items = $self.find(config.items),
			$selected = (config.mainIndex > -1) && $items.eq(config.mainIndex);

		$items
			.each(function (i) {
				var $this = $(this),
					$sub = $this.find(config.sub),
					$subItems = $sub.length && $sub.find(config.subItems);

				if ($sub.length) {
					$this.addClass(config.hasSubClass);

					$.data(this, 'index.gnav', i);
					$.data(this, 'sub.gnav', $sub);
					$.data(this, 'subItems.gnav', $subItems);
				}
			})
			.bind('open.gnav', function () {
				var $item = $(this),
					$sub = $.data(this, 'sub.gnav');
					selectedClass = ($sub && $sub.length) ? config.hasSubSelectedClass : config.selectedClass;

				if ($selected && $selected.trigger) {
					$selected.trigger('close.gnav');
				}

				if ($sub && $sub.length) {
					$sub.slideDown(0, function () {
						$item
							.addClass(selectedClass)
							.data('isOpened.gnav', true);
						$selected = $item;
					});
				} else {
					$item
						.addClass(selectedClass)
						.data('isOpened.gnav', true);
					$selected = $item;
				}
			})
			.bind('close.gnav', function () {
				var $item = $(this),
					$sub = $.data(this, 'sub.gnav');
					selectedClass = ($sub && $sub.length) ? config.hasSubSelectedClass : config.selectedClass;

				if ($sub && $sub.length) {
					$sub.slideUp(100, function () {
						$item
							.removeClass(selectedClass)
							.data('isOpened.gnav', false);
					});
				} else {
					$item
						.removeClass(selectedClass)
						.data('isOpened.gnav', false);
				}
			})
			.bind(config.eventType, function (e) {
				if (!$.data(this, 'isOpened.gnav')) {
					if (e.type === 'mouseover') {
						e.preventDefault();
					}

					$(this).trigger('open.gnav');
				}
			});

		if ($selected && $selected.length) {
			$selected.trigger('open.gnav');
			if ($selected.data('subItems.gnav') && config.subIndex > -1) {
				$selected.data('subItems.gnav').eq(config.subIndex).addClass(config.selectedClass);
			}
		}
	});
};
/* Snb Navigation */
$.fn.nav = function (options) {
	var config = $.extend({
			items: '> li',
			sub: '> ul',
			subItems: '> li',
			selectedClass: 'selected',
			hasSubClass: 'has-sub',
			hasSubSelectedClass: 'selected',
			eventType: 'mouseover.nav',
			mainIndex: -1,
			subIndex: -1
		}, options);

	return this.each(function () {
		var self = this,
			$self = $(self),
			$items = $self.find(config.items),
			$selected = (config.mainIndex > -1) && $items.eq(config.mainIndex);

		$items
			.each(function (i) {
				var $this = $(this),
					$sub = $this.find(config.sub),
					$subItems = $sub.length && $sub.find(config.subItems);

				if ($sub.length) {
					$this.addClass(config.hasSubClass);

					$.data(this, 'index.nav', i);
					$.data(this, 'sub.nav', $sub);
					$.data(this, 'subItems.nav', $subItems);
				}
			})
			.bind('open.nav', function () {
				var $item = $(this),
					$sub = $.data(this, 'sub.nav');
					selectedClass = ($sub && $sub.length) ? config.hasSubSelectedClass : config.selectedClass;

				if ($selected && $selected.trigger) {
					$selected.trigger('close.nav');
				}

				if ($sub && $sub.length) {
					$sub.slideDown(0, function () {
						$item
							.addClass(selectedClass)
							.data('isOpened.nav', true);
						$selected = $item;
					});
				} else {
					$item
						.addClass(selectedClass)
						.data('isOpened.nav', true);
					$selected = $item;
				}
			})
			.bind('close.nav', function () {
				var $item = $(this),
					$sub = $.data(this, 'sub.nav');
					selectedClass = ($sub && $sub.length) ? config.hasSubSelectedClass : config.selectedClass;

				if ($sub && $sub.length) {
					$sub.slideUp(0, function () {
						$item
							.removeClass(selectedClass)
							.data('isOpened.nav', false);
					});
				} else {
					$item
						.removeClass(selectedClass)
						.data('isOpened.nav', false);
				}
			})
			/* .bind(config.eventType, function (e) {
				if (!$.data(this, 'isOpened.nav')) {
					if (e.type === 'click') {
						e.preventDefault();
					}

					$(this).trigger('open.nav');
				}
			}); */

		if ($selected && $selected.length) {
			$selected.trigger('open.nav');
			if ($selected.data('subItems.nav') && config.subIndex > -1) {
				$selected.data('subItems.nav').eq(config.subIndex).addClass(config.selectedClass);
			}
		}
	});
};

// Tabs
$.fn.jsTabs = function (options) {
	var config = $.extend({
			items: 'li',
			selectedItem: 'selected',
			selectedContent: 'tab_content_selected',
			selectedTitle: unescape('%uC120%uD0DD%uB428'),
			selectedIndex: 0,
			subTabItems: null,
			subTabIndex: 0,
			random: false,
			imgRegExp: null,
			normalImage: '.gif',
			selectedImage: 'on.gif',
			images: [],
			mouseOver: false,
			roll: false,
			interval: 5000,
			effect: null
		}, options);

	return this.each(function () {
		var self = this,
			$self = $(self),
			items = $self.find(config.items),
			imgRegExp = config.imgRegExp,
			eventType = config.mouseOver ? 'click.jsTabs mouseenter.jsTabs' : 'click.jsTabs';

		if ( $.data(self, 'init.jsTabs') ) {
			destroy();
		}

		function init() {
			$.data(self, 'selectedIndex.jsTabs', config.random ? getRandom(0, items.length - 1) : config.selectedIndex);
			$.data(self, 'items.jsTabs', items);

			items.each(function (index) {
				var item = this,
					$item = $(item),
					anchor = item.getElementsByTagName('a')[0],
					contentId = anchor && anchor.getAttribute('href', 2),
					content = contentId && (contentId !== '#') && $(document).find(contentId),
					subTabItems = content && content.length && content.find(config.subTabItems),
					image = item.getElementsByTagName('img')[0];

				if ( !config.random && $item.hasClass(config.selectedItem) ) {
					$.data(self, 'selectedIndex.jsTabs', index);
				}

				$.data(item, 'index.jsTabs', index);
				$.data(item, 'image.jsTabs', image);
				$.data(item, 'content.jsTabs', content);
				$.data(item, 'subTabItems.jsTabs', subTabItems);

				if ( config.images.length && config.images[index] ) {
					$.data(item, 'onImage.jsTabs', config.images[index]['on']);
					$.data(item, 'offImage.jsTabs', config.images[index]['off']);
				}

				$item.bind({
					'select.jsTabs': function () {
						select(index);
					},
					'unselect.jsTabs': function () {
						unselect(index);
					}
				});

				$item.bind(eventType, function (e) {
					e.preventDefault();
					$item.trigger('select.jsTabs');
				});

				if (config.roll) {
					$item
						.bind('mouseenter.jsTabs', function () {
							stop();
						})
						.bind('mouseleave.jsTabs', function () {
							roll();
						});
					content
						.bind('mouseenter.jsTabs', function () {
							stop();
						})
						.bind('mouseleave.jsTabs', function () {
							roll();
						});
				}
			});

			select( $.data(self, 'selectedIndex.jsTabs') );

			if (config.roll) {
				roll();
			}

			$.data(self, 'init.jsTabs', true);
		}

		function select(index) {
			unselectAll();

			if ( index < 0 ) {
				return null;
			}

			var selected = items.eq(index),
				image = selected.data('image.jsTabs'),
				content = selected.data('content.jsTabs'),
				subTabItems = selected.data('subTabItems.jsTabs');

			if (image) {
				var src = image.getAttribute('src');

				if ( config.images.length && config.images[index] ) {
					image.setAttribute( 'src', src.replace( config.images[index]['off'], config.images[index]['on'] ) );
				} else if ( config.selectedImage ) {
					if ( imgRegExp && imgRegExp.exec ) {
						var exec = imgRegExp.exec(src),
							match = exec && exec[1];
						if (match && match !== config.selectedImage) {
							image.setAttribute( 'src', src.replace(config.normalImage, config.selectedImage) );
						}
					} else if ( src.indexOf(config.normalImage) > -1 ) {
						image.setAttribute( 'src', src.replace(config.normalImage, config.selectedImage) );
					}
				}
			}

			selected.attr('title', config.selectedTitle);
			selected.addClass(config.selectedItem);

			if (content.length) {
				content.addClass(config.selectedContent);

				if(content.find(".cloud-zoom").html()) {
					$('.cloud-zoom').CloudZoom();
					$('#SZsmallImg').css('opacity', 0.5);
				}
			}

			if (subTabItems && subTabItems.length) {
				subTabItems.eq(config.subTabIndex).trigger('select.jsTabs');
			}

			$.data(self, 'selectedIndex.jsTabs', index);
		}

		function unselect(index) {
			var item = items.eq(index),
				image = item.data('image.jsTabs'),
				content = item.data('content.jsTabs');

			if (image) {
				var src = image.getAttribute('src');

				if (config.images.length && config.images[index]) {
					image.setAttribute( 'src', src.replace( config.images[index]['on'], config.images[index]['off'] ) );
				} else {
					if ( imgRegExp && imgRegExp.exec ) {
						var exec = imgRegExp.exec(src),
							match = exec && exec[1];
						if (match && match === config.selectedImage) {
							image.setAttribute( 'src', src.replace(match, config.normalImage) );
						}
					} else if ( src.indexOf(config.normalImage) === -1 ) {
						image.setAttribute( 'src', src.replace(config.selectedImage, config.normalImage) );
					}
				}
			}

			item.attr('title', '');
			item.removeClass(config.selectedItem);
			if (content) {
				content.removeClass(config.selectedContent);
			}
		}

		function unselectAll() {
			items.each(function () {
				$(this).trigger('unselect.jsTabs');
			});
		}

		function roll() {
			var timer = window.setInterval(function () {
				var index = $.data(self, 'selectedIndex.jsTabs'),
					next = ++index < items.length ? index : 0;
				select( next );
				$.data(self, 'selectedIndex.jsTabs', next);
			}, config.interval);

			$.data(self, 'timer.jsTabs', timer);
		}

		function stop() {
			window.clearInterval( $.data(self, 'timer.jsTabs') );
			$.data(self, 'timer.jsTabs', null);
		}

		init();

		function destroy() {
			items.each(function () {
				var $item = $(this),
					content = $.data(this, 'content.jsTabs');
				$item.removeClass(config.selectedItem);
				content.removeClass(config.selectedContent);
				$.data(this, 'content.jsTabs').unbind('.jsTabs');
				$.removeData(this, 'index.jsTabs');
				$.removeData(this, 'image.jsTabs');
				$.removeData(this, 'content.jsTabs');
				$item.unbind('.jsTabs');
			});
			$self
				.removeData('selectedIndex.jsTabs')
				.removeData('items.jsTabs')
				.removeData('timer.jsTabs');
		}
	});
};


//tabon
function tabOn(a) {
	for (var i=1 ; i <= a ; i++ ){
		var obj_tab = document.getElementById("tab" + a);
		if(i == a) {
			obj_tab.className = "on";
		}
	}
}

// main notice
function fcb(num) {
	for (i=1; i<4; i++) {
		if (i == num)	{
			document.getElementById('cb'+i).className = 'selected';
		}
		else {
			document.getElementById('cb'+i).className = ' ';
		}
	}
}

function notice(tit,obj,num,total) {
	document.getElementById('noticeBox').className="main-notice loadscript";
	for (i=1; i<=total; i++) {
		if (i==num)	{
			document.getElementById(obj+i).className = 'selected';
			document.getElementById(tit+i).className = 'selected';
		}
		else {
			document.getElementById(obj+i).className = '';
			document.getElementById(tit+i).className = '';
		}
	}
}

function re_fr(x){
	var tFrame = eval("document.body");
	var cFrame = eval("parent.document.all."+ x);
	cFrame.style.height = tFrame.scrollHeight + 50;
}

// Viewport
(function (window) {
	var document = window.document,
		documentElement = document.documentElement;

	window.viewport = {
		"width": 0,
		"height": 0,
		"scrollTop": 0,
		"scrollLeft": 0
	};

	function getDimension() {
		viewport.width = window.innerWidth || documentElement.offsetWidth || 0;
		viewport.height = window.innerHeight || documentElement.offsetHeight || 0;
	}

	function getScroll() {
		viewport.scrollLeft = window.pageXOffset || documentElement.scrollLeft || 0;
		viewport.scrollTop = window.pageYOffset || documentElement.scrollTop || 0;
	}

	getDimension();
	getScroll();

	$(window).bind({
		"scroll.viewport": function () {
			getScroll();
		},
		"resize.viewport": function () {
			getDimension();
			getScroll();
		}
	});
}(this));

var MSIE = $.browser.msie && parseInt($.browser.version, 10);

// Layer
function Layer(selector, options) {
	var $layer = $(selector),
		config = $.extend({}, this.defaults, options);

	this.layer = $layer;
	this.config = config;

	this.init();
}
Layer.prototype = {
	"defaults": {
		"curtainId": "layerCurtain",
		"closeButton": ".layer-close",	//2010-12-31 수정
		"zIndex": 100
	},
	"init": function () {
		var that = this,
			$layer = that.layer,
			config = that.config,
			$closeButton = $layer.find(config.closeButton),
			$openers = $("a[href=" + $layer.selector + "]");

		if ($openers.length) {
			$openers.bind("click.Layer", function (e) {
				e.preventDefault();

				that.open();
			});
		}

		$closeButton.bind("click.Layer", function (e) {
			e.preventDefault();

			that.close();
		});
	},
	"open": function () {
		var that = this,
			$layer = that.layer,
			config = that.config,
			curtain = document.getElementById(config.curtainId),
			$curtain = null;

		if (curtain) {
			$curtain = $(curtain);
			$curtain.insertBefore($layer);
		} else {
			$curtain = $("<div id=\"" + config.curtainId + "\"></div>").insertBefore($layer);
		}

		if ($curtain.length) {
			that.curtain = $curtain;

			if (window.MSIE && MSIE < 7) {
				var documentElement = document.documentElement,
					scrollWidth = documentElement.scrollWidth,
					scrollHeight = documentElement.scrollHeight,
					offsetWidth = documentElement.offsetWidth,
					offsetHeight = documentElement.offsetHeight,
					curtainOffset = $curtain.offset(),
					curtainPosition = $curtain.position(),
					width = scrollWidth < offsetWidth ? offsetWidth : scrollWidth,
					height = scrollHeight < offsetHeight ? offsetHeight : scrollHeight;

				$curtain.css({
					"top": curtainPosition.top - curtainOffset.top,
					"left": curtainPosition.left - curtainOffset.left,
					"width": width,
					"height": height
				});
			}
			$curtain
				.css({
					"display": "block",
					"z-index": config.zIndex
				})
				.animate({
					"opacity": 0.5
				}, 300)
				.bind("click.Layer", function () {
					that.close();
				});
		}

		$layer.css({
			"display": "block",
			"opacity": 0,
			"z-index": config.zIndex + 1
		});
		that.center();
		$layer
			.animate({
				"opacity": 1
			}, 300, function () {
				that.status = "opened";
			});
	},
	"close": function () {
		var that = this,
			$layer = that.layer,
			config = that.config,
			$curtain = that.curtain;

		if ($curtain.length) {
			$curtain
				.animate({
					"opacity": 0
				}, 300, function () {
					$curtain
						.css("display", "none")
						.unbind("click.Layer");
				});
		}

		$layer
			.fadeOut(300, function () {
				that.status = "closed";
			});
	},
	"center": function () {
		if (!window.viewport) {
			return null;
		}

		var that = this,
			$layer = that.layer,
			config = that.config,
			viewportWidth = viewport.width,
			viewportHeight = viewport.height,
			layerWidth = $layer.width(),
			layerHeight = $layer.height(),
			layerOffset = $layer.offset(),
			layerPosition = $layer.position(),
			diffTop = layerPosition.top - layerOffset.top,
			diffLeft = layerPosition.left - layerOffset.left,
			layerTop = ((viewportHeight - layerHeight) / 2) + diffTop + viewport.scrollTop,
			layerLeft = ((viewportWidth - layerWidth) / 2) + diffLeft + viewport.scrollLeft;

		if (layerWidth > viewportWidth) {
			$layer.css("left", diffLeft);
		} else {
			$layer.css("left", layerLeft);
		}

		if (layerHeight > viewportHeight) {
			$layer.css("top", diffTop);
		} else {
			$layer.css("top", layerTop);
		}
	}
};

// 2010-12-31 추가
function mxplatform(num) {
	for (i=1; i<3; i++) {
		if (i == num)	{
			document.getElementById('cvm'+i).className = 'on';
		} else {
			document.getElementById('cvm'+i).className = ' ';
		}
	}
}


var TINY={};

function T$(i){return document.getElementById(i)}
function T$$(e,p){return p.getElementsByTagName(e)}

TINY.fader=function(){
	function fade(n,p){this.n=n; this.init(p)}
	fade.prototype.init=function(p){
		var s=T$(p.id), u=this.u=T$$('li',s), l=u.length, i=this.l=this.c=this.z=0;
		if(p.navid&&p.activeclass){this.g=T$$('li',T$(p.navid)); this.s=p.activeclass}
		s.style.overflow='hidden'; this.a=p.auto||0; this.p=p.resume||0;
		for(i;i<l;i++){
			if(u[i].parentNode==s){
				u[i].style.position='absolute'; this.l++; u[i].o=p.visible?100:0;
				u[i].style.opacity=u[i].o/100; u[i].style.filter='alpha(opacity='+u[i].o+')'
			}
		}
		this.pos(p.position||0,this.a?1:0,p.visible)
	},
	fade.prototype.auto=function(){
		this.u.ai=setInterval(new Function(this.n+'.move(1,1)'),this.a*10000)
	},
	fade.prototype.move=function(d,a){
		var n=this.c+d, i=d==1?n==this.l?0:n:n<0?this.l-1:n; this.pos(i,a)
	},
	fade.prototype.pos=function(i,a,v){
		var p=this.u[i]; 
		this.z++; 
		p.style.zIndex=this.z;
		clearInterval(p.si); 
		
		clearInterval(this.u.ai); 
		this.u.ai=0; 
		this.c=i;
		if(p.o>=100&&!v){
			p.o=0; 
			p.style.opacity=0; 
			p.style.filter='alpha(opacity=0)'
		}
		if(this.g){
			for(var x=0;x<this.l;x++){
				this.g[x].className=x==i?this.s:''}
				}
		p.si=setInterval(new Function(this.n+'.fade('+i+','+a+')'),20)
	},
	fade.prototype.fade=function(i,a){
		var p=this.u[i];
		if(p.o>=100){
			clearInterval(p.si); if((a||(this.a&&this.p))&&!this.u.ai){this.auto()}
		}else{
			p.o+=5; p.style.opacity=p.o/100; p.style.filter='alpha(opacity='+p.o+')'
		}
	};
	return{fade:fade}
}();

