/***
@history 2010-04-06 3:10오후 label 인자값을 추가함. 서로 다른 로딩바를 구현할 수 있도록...
@param wrapper 로딩바 넣을 엘리먼트 선택자
***/
function loadingStart(label, wrapper) {
	if (typeof label != 'string') label = '';
	if(!wrapper) wrapper = 'body';

	// 이미 생성된 로딩바가 있다면
	if ($('#'+ label +'loadingBar').length) {
		$('#'+ label +'loadingBar, #'+ label +'loadingBarShadow').show();
	}else{
		$('<div></div>').insertBefore(wrapper)
		.css({'position':'absolute', 'zIndex':'999', 'background-color' : '#ffffff', 'border':'1px solid #dddddd', 'width':'120px', 'height':'70px'})
		.attr({'id': label +'loadingBar', 'class': 'loadingBar'});

		// 그림자
		$('<div></div>').insertBefore(wrapper)
		.css({'position':'absolute', 'zIndex':'998', 'background-color':'#EBEBEB', 'width':'120px', 'height':'70px'})
		.attr({'id': label +'loadingBarShadow'});
	}
	h = (($(window).height() / 2) - ($('#'+ label +'loadingBar').height() / 2)) + $(document).scrollTop();
	w = (($(window).width() / 2) - ($('#'+ label +'loadingBar').width() / 2));
	$('#'+ label +'loadingBar').css({'top': h, 'left': w});
	$('#'+ label +'loadingBarShadow').css({'top': (h + 4), 'left': (w + 4)});
}

function loadingEnd(label) {
	if (typeof label != 'string') label = '';
	$('#'+ label +'loadingBar, #'+ label +'loadingBarShadow').hide();
}

/***
@note 파일사이즈를 보기 쉽게 단위별로 치환해준다.
***/
function byteConvert(bytes) {
	if (bytes <= 0) return "0 Byte";
	convention = 1000; // [1000->10^x|1024->2^x]
	s = new Array('B', 'KB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB');
	e = Math.floor(Math.log(bytes)/Math.log(convention));
	return Math.round(bytes/Math.pow(convention,e),2) +' '+ s[e];
}

/***
@note jquery를 이용하여 xml 노드를 셀렉터하여 출력하는 게시판을 만들었는 데 ie6 에서 너무 느려 대체하여 사용하기 위한 함수
@param inItem 객체(데이터)
@param inName 선택할 노드이름
@param inValue null일 경우 출력값
***/
function node_value(inItem,inName,inValue) {
  if (inItem.getElementsByTagName(inName)[0].firstChild == null) {
    	return inValue;
  }else{
  	var t = inItem.getElementsByTagName(inName)[0].firstChild;
  	var n = "";
  	while (t) {
  		n += t.nodeValue;
  		t = t.nextSibling;
  	}
  	return n;
  }
}

function gnb_init()
{
	// 주메뉴에 서브메뉴 이벤트
	$('#gnb li').each(function()
	{
		$(this).hover(function()
		{
			$(this).find('.lnb').slideDown(0);
		},
		function()
		{
			$(this).find('.lnb').slideUp(0);
		});
	});
}

/**
home 배너 이미지 처리용

사용예 :
	var g = new GalleryMaker({img: ['001.gif','002.gif','003.gif'], links: [1,2,3], box: '#gallery', links_id:'#gallery_link', control:'#gallery_control', img_width:103, img_height:30});
	g.start();
*/
GalleryMaker = function(options)
{
	// 현재 출력 이미지
	this.now = 0;
	// 다음 출력 이미지
	this.idx = 0;
	this.img = [];
	this.control = [];
	this.img_cnt = 0;
	this.stop = false;

	// 릴레이 setTimeout
	this.re = {};

	this.defaults = {
		// 출력 이미지 리스트
		img: [],
		// 갤러리출력 박스 선택자
		box: '',
		control: '',
		// 이미지 변경속도
		duration: 2000,
		// 이미지 크기
		img_width:'',
		img_height:'',
		links: [],
		links_id: '',
		// 마우스 오버시 롤링 멈춤
		hover: false
	}
	this.options = $.extend(this.defaults, options);
};

GalleryMaker.prototype = {
	start: function()
	{
		var gallery = this;
		var op = gallery.options;
		gallery.img_cnt = op.img_src.length;
		last_img = gallery.img_cnt - 1;

		// 제어버튼 박스 생성
		var control = $('<ul class="gallery_control"></ul>');

		$(op.img_src).each(function(idx)
		{
			gallery.img[idx] = $('<img />').attr({src: this, width:op.img_width, height:op.img_height});
			// 이미지 이동 버튼
			gallery.control[idx] = $('<li></li>').click(function(){
				gallery.idx = idx;
				gallery.relay();
				// 클릭 후 텀을 주기 위해 새로 갱신
				clearInterval(gallery.re);
				gallery.re = setInterval(function(){gallery.relay();}, op.duration);
			});

			// 이미지를 갤러리 박스에 넣는 다. 첫번째 이미지만 출력
			if (idx == last_img)
			{
				$(op.box).append(gallery.img[idx]);
				gallery.control[idx].addClass('gallery_bullet_selected');
			}
			else
			{
				$(op.box).append(gallery.img[idx]);
				gallery.control[idx].addClass('gallery_bullet');
			}
			control.append(gallery.control[idx]);
		});

		// 애니메이션 깜박임을 막기 위해
		$(window).blur(function()
		{
			clearInterval(gallery.re);
			$(window).one('focus', function()
			{
				gallery.re = setInterval(function(){gallery.relay();}, op.duration);
			});
		});

		// 이미지 이동 버튼 추가
		$(op.control).append(control);

		// 바로가기 링크 세팅
		$(op.links_id).find('a').attr('href',op.links[last_img]);
		gallery.now = last_img;

		gallery.re = setInterval(function(){gallery.relay();}, op.duration);

		// 이미지에 마우스 오버 시 롤링을 멈춘다.
		if (op.hover)
		{
			$(op.box).children('img').hover(function()
			{
				gallery.stop = true;
				clearInterval(gallery.re);
			},
			function()
			{
				gallery.stop = false;
				// 마지막 이미지라면
				if ((gallery.idx + 1) == gallery.img_cnt) gallery.idx = 0;
				else gallery.idx = gallery.idx + 1;

				gallery.re = setInterval(function(){gallery.relay();}, op.duration);
			});
		}
	},

	relay: function()
	{
		var gallery = this;
		if (gallery.stop) return clearInterval(gallery.re);

		// 현재 이미지가 아니라면
		if (gallery.now != gallery.idx)
		{
    	gallery.img[gallery.now].animate({opacity: 0.0}, 1000);
    	gallery.img[gallery.idx].css({opacity: 0.0}).animate({opacity: 1.0}, 1000);
			gallery.control[gallery.now].removeClass('gallery_bullet_selected').addClass('gallery_bullet');
			gallery.control[gallery.idx].removeClass('gallery_bullet').addClass('gallery_bullet_selected');

			// 바로가기 링크 변경
			$(gallery.options.links_id).find('a').attr('href',gallery.options.links[gallery.idx]);
			gallery.now = gallery.idx;
		}


		// 마지막 이미지라면
		if ((gallery.idx + 1) == gallery.img_cnt) gallery.idx = 0;
		else gallery.idx = gallery.idx + 1;
	}

}
