﻿/*
*    NBoard ability's ( 2009-07-21 )
*    pRix present
*/

var NBoard =
{
	init : function()
	{
		this.board = document.getElementById('n-wrap');
		if (this.board) window.onload = function() {
			NBoard.Ellipsis();
			NBoard.Tooltip();
			NBoard.Gallery();
		}
		//if (this.board) Ns.ready(function() { });
	},

	initUploader : function(data)
	{
		// param to serialize
		var params = [], html = '', input = document.getElementById(data.name);

		// name 지정시 문서내 해당 요소 검사 후 새로생성 또는 value 치환
		if (input) {
			data.value = input.value;
		} else {
			html += '<input type="hidden" name="'+data.name+'" id="'+data.name+'" />';
		}

		// Uploader html 생성
		for ( var p in data ) params.push(p + '=' + escape(data[p]));
		html += this.getSwfHTML( '/NBoard/nflash.uploader.swf', '100%', '110', params.join('&') );

		// 디버그모드일시 디버그 전용 텍스트필드 생성
		if (data.debug) {
			html += '<textarea id="nflashDeberger" cols="74" rows="13"></textarea>';
		}

		document.write( html );
	},

	Gallery : function()
	{
		var targets = this.board.getElementsByTagName('ul');
		for (var i=0, el; el = targets[i]; i++) {
			if (el.className == 'n-gallery') NBoard.initGallery(el);
		}
	},

	Ellipsis : function()
	{
		var match = 'ellipsis', target;
		for (var i = 0, targets = this.board.getElementsByTagName('P'), obj; obj = targets[i]; i++)
		{
			if (obj.clientWidth == 0) obj.style.zoom = "1";
			if (obj.className.indexOf(match) != -1 && obj.scrollWidth > obj.clientWidth )
			{
				for ( target = obj.firstChild; target && (target.nodeType == 3 || target.nodeName == 'IMG' || !(target.innerText || target.textContent) ); target = target.nextSibling );

				target = target || obj;
				var text = target.innerHTML;
				var half = parseInt(text.length * ( obj.clientWidth / obj.scrollWidth)) - 2;
				target.innerHTML = text.substr(0, half) + '... ';

				while (obj.clientWidth < obj.scrollWidth) {
					target.innerHTML = text.substr(0, half-=2) + '... ';
				}
			}
		}
	},

	Tooltip : function()
	{
		this.tooltipDiv = document.createElement('div'),
		this.tooltipDiv.className = 'n-pretip-box';
		document.body.insertBefore(this.tooltipDiv , document.body.firstChild);

		var match = 'pretip', tip = this.tooltipDiv;
		for (var i=0, targets = this.board.getElementsByTagName('A'), obj; obj = targets[i]; i++)
		{
			if (obj.className.indexOf(match) != -1)
			{
				obj.tooltipWidth = obj.className == 'pretip-desc' ? '234px' : '150px';
				obj.onmouseover = function() {
					var pos = NBoard.getOffset(this);
					tip.style.top = (pos.y + 16) + 'px';
					tip.style.left = (pos.x - 1) + 'px';
					tip.innerHTML = '<div class="n-pretip-shadow"></div><div class="n-pretip-txt" style="width:'+this.tooltipWidth+';">'+this.tooltipTxt+'</div>';
					tip.style.display = 'block';
				}
				obj.onmouseout = function() {
					tip.innerHTML = '';
					tip.style.display = 'none';
				}
				obj.tooltipTxt = obj.title;
				obj.title = '';
			}
		};
	},

	toClipboard : function(txt)
	{
		document.write( this.getSwfHTML( '/NBoard/nflash.clipboard.swf', 52, 16, 'txt='+escape(txt) ) );
	},

	getOffset : function(obj)
	{
		var par = obj.offsetParent ? this.getOffset(obj.offsetParent) : { x:0, y:0 };
		return {
			x : obj.offsetLeft + par.x,
			y : obj.offsetTop + par.y
		}
	},

	getSwfHTML : function(file, width, height, vars)
	{
		var id = 'fid_'+parseInt(Math.random() * 100000);
		return !document.all
			// Mozilla
			? '<embed type="application/x-shockwave-flash" src="'+file+'" width="'+width+'" height="'+height+'" id="'+id+'" name="'+id+'" Menu="false" wmode="transparent" flashVars="'+(vars||'')+'" />'
			// MSIE
			: '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="'+width+'" height="'+height+'" id="'+id+'" name="'+id+'">'
			+ '<param name="movie" value="'+file+'" />'
			+ '<param name="Menu" value="false">'
			+ '<param name="wmode" value="transparent">'
			+ '<param name="flashVars" value="'+(vars||'')+'">'
			+ '</object>';
	}
};

// Gallery instance
NBoard.initGallery = function(obj)
{
	var d = document;
	var lists = obj.getElementsByTagName('li');
	var maxWidth = lists[0].clientWidth;
	var maxHeight = lists[0].clientHeight;
	var maxPage = lists.length;
	var nowPage = 0;
	var oldPage = 0;
	var zIndex = 99;

	// 갤러리 생성
	for (var i=0, img, list; list = lists[i]; i++)
	{
		img = list.getElementsByTagName('img')[0];
		img.style.cursor = 'pointer';
		img.title = "클릭하시면 확대해서 보실 수 있습니다.";
		if (img.width > img.height && img.width > maxWidth) img.width = maxWidth;
		if (img.height > img.width && img.height > maxHeight) img.height = maxHeight;

		list.style.left = "0";
		list.style.zIndex = zIndex--;
		list.style.visibility = i == 0 ? 'visible' : 'hidden';
		//list.style.visibility = 'visible';
		list.innerHTML = '<table cellpadding="0" cellspacing="0" style="background:none;border:0;width:'+maxWidth+'px;height:'+maxHeight+'px;ext-align:center;"><tr><td style="padding:0;margin:0;border:0;">' +  list.innerHTML + '</td></tr></table>';
		list.compact = compact;
		list.zoom = zoom;

		(function(s) { list.onclick = function() {
			view(s);
		}})(img.src);
	}

	// 갤러리 슬라이더 - 숨기기
	function compact() {
		var _ = this, width = -_.clientWidth, left;
		_.nextSibling.style.visibility = 'visible';	// 다음 이미지를 활성화
		clearInterval(_.timer);
		this.timer = setInterval(function() {
			left = parseInt(_.style.left, 10);
			if (left > width) {
				_.style.left = Math.floor(left - (left - width)/3) + "px"
			} else {
				_.style.visibility = 'hidden';	// 숨기기가 완료된 이미지 비활성화
				clearInterval(_.timer);
			}
		}, 20);
	}

	// 갤러리 슬라이더 - 보이기
	function zoom() {
		var _ = this, width = -_.clientWidth, left;;
		_.style.visibility = 'visible';	// 현재 이미지를 활성화
		clearInterval(_.timer);
		this.timer = setInterval(function() {
			left = parseInt(_.style.left, 10);
			if (left < 0) {
				_.style.left = Math.ceil(left - (left)/3) + "px"
			} else {
				_.nextSibling.style.visibility = 'hidden';	// 다음 이미지를 비활성화
				clearInterval(_.timer);
			}
		}, 20);
	}

	function addEvent(obj, handler, fn) {
		if (obj.attachEvent) obj.attachEvent("on"+handler, fn);
		else obj.addEventListener(handler, fn, false);
	}

	function root() {
		return (/chrome/i.test(navigator.userAgent) || !document.documentElement.clientHeight) ? document.body : document.documentElement
	}

	// 이미지 확대 뷰 레이어 생성
	var divArea = d.createElement('div'), Img;
	divArea.className = "n-gallery-drop";
	divArea.onclick = hide;
	d.body.insertBefore(divArea, d.body.firstChild);

	// 이미지 확대 뷰 사용시 ESC 키로 hide 기능 연결
	addEvent(d, 'keydown', function(e) {
		var evt = e || window.event;
		if ( evt.keyCode == 27 ) hide();
	})

	function view(src) {
		var r = root();
		divArea.style.width = root().scrollWidth + 'px';
		divArea.style.height = root().scrollHeight+ 'px';
		divArea.style.display = 'block';
		Img = d.createElement('img');
		Img.className = 'n-gallery-view';
		Img.title = '그림을 클릭하시거나 ESC버튼을 누르시면 원래 화면으로 돌아갑니다.';
		Img.onclick = hide;
		Img.onload = function() {
			if (this.width > this.height && this.width > r.clientWidth) this.width = r.clientWidth - 10;
			if (this.height > this.width && this.height > r.clientHeight) this.height = r.clientHeight - 10;
			this.style.top = (r.scrollTop + (r.clientHeight - this.height)/2) + 'px';
			this.style.left = (r.scrollLeft + (r.clientWidth - this.width)/2) + 'px';
		}
		Img.src = src;
		d.body.appendChild(Img);
	}

	function hide() {
		divArea.style.display = 'none';
		d.body.removeChild(Img);
		Img = null;	// IE 메모리누수 방지
	}

	// 이미지가 1개일경우 네비게이션 삽입안함
	if (lists.length == 1) return;

	var prevBtn = d.createElement('li');
	prevBtn.className = 'n-g-prev';
	obj.appendChild(prevBtn);
	prevBtn.onclick = function() {
		if (nowPage == 0) return;
		oldPage = nowPage--;
		lists[nowPage].zoom();
		paging();
	}

	var nextBtn = d.createElement('li');
	nextBtn.className = 'n-g-next';
	obj.appendChild(nextBtn);
	nextBtn.onclick = function() {
		if (nowPage == maxPage - 1) return;
		oldPage = nowPage++;
		lists[oldPage].compact();
		paging();
	}

	var pageNo = d.createElement('li');
	pageNo.className = 'n-g-page';
	obj.appendChild(pageNo);
	function paging() {
		pageNo.innerHTML = '<strong>'+(nowPage+1) + '</strong> / <em>' + maxPage + '</em>';
	};
	paging();

	// IE 메모리누수 방지;
	prevBtn = null;
	nextBtn = null;
}