/**
 * @file	cblib.js
 * @brief	°øÅëÇÔ¼ö
 * @author          
 * @date			
 * @bug			
 *   
*/


function _getEl(a)
{
	try
	{
		return document.getElementById ? document.getElementById(a) : null
	}
	catch(e)
	{
	}

}

function _innerHtml(o, str)
{
	try
	{
		if(!(typeof(o) == "object" || (o = _getEl(o)))) return null;
		var s = o.innerHTML;
		if(str) o.innerHTML = str;
		return s;
	}
	catch(e)
	{
	}
}

function max(n1, n2)
{
	return n1 > n2? n1: n2;
}

function Byte_Size(nSize)
{
	var szSize = "0B";

	if ( nSize >= 1099511627776 )
	{
		nSize = nSize / (1099511627776);
		szSize = Math.round(nSize) + "TB";
	}
	else if( nSize >= 1073741824 )
	{
		nSize = nSize / (1073741824)
		szSize = Math.round(nSize) + "GB";
	}
	else if( nSize > 1048576 )
	{
		nSize = nSize / (1048576)
		szSize = Math.round(nSize) + "MB";
	}
	else if( nSize >= 1024 )
	{
		nSize = nSize / (1024)
		szSize = Math.round(nSize) + "KB";
	}
	else
	{
		szSize = nSize + "B";
	}

	return szSize;
}

function log(arguments)
{
	//alert("log");
	//alert(_getEl('idLog'));
	if(!_getEl('idLog'))
	{
		oLog = document.createElement('div');
		oLog.id = "idLog";
		oLog.style.height = "auto";
		oLog.style.left = "0px";
		oLog.style.top = "0px";
		oLog.style.position = "absolute";
		oLog.style.zIndex = "1000";
		bodyObj = document.getElementsByTagName('body').item(0);
		bodyObj.appendChild(oLog);
	}
	var len = arguments.length;
	var szLog = new Array();
	for(var i=0; i<len; i++)
	{
		szLog.push(arguments[i]);
		szLog.push(", ");
	}
	szLog = szLog.join('');
	_innerHtml('idLog', _innerHtml('idLog') +'<br />'+szLog);
}

String.prototype.cut = function(len)
{
	var str = this;
	var l = 0;

	for (var i=0; i<str.length; i++) {
		l += (str.charCodeAt(i) > 128) ? 2 : 1;
		if (l > len) return str.substring(0,i);
	}

	return str+"..";
}
/**
	parm:type = num, array
	parm:count = length of array
	parm:array = array;
 */
Math.prandom = function(parm)
{
	var result = new Array();
	var source = new Array();
	var i =0;

	if(parm.type == "num")
	{
		for(i=0; i<parm.num; i++)
			source[i] = i;
	}
	else
	{
		source = parm.array;
	}
	var rdnIdx = 0;
	var count = source.length;
	i=count;
	while(i)
	{
		rdnIdx = parseInt(Math.random() * i,10);

		result[count-i] = source[rdnIdx];
		source[rdnIdx] = source[i-1];
		i--;
	}
	source = null;

	return result;
}

String.prototype.trim = function()
{
	return this.replace(/(^\s*)|(\s*$)/g, "");
}


function isTermCheck(start, end)
{
	var today = new Date();
	var YY = today.getFullYear();
	var MM = ((today.getMonth()+1 < 10)?"0":"") + (today.getMonth()+1);
	var DD = ((today.getDate() < 10)?"0":"") + today.getDate();
	var HH = ((today.getHours() < 10)?"0":"") + today.getHours();
	var MI = ((today.getMinutes() < 10)?"0":"") + today.getMinutes();

	var szNow = String(YY)+String(MM)+String(DD)+String(HH)+String(MI);

	if(start <= szNow && szNow <= end)
	{
		return true;
	}
	else
	{
		return false;
	}
}


/**
 * @function	setPopCenterOpen
 * @brief		ÆË¾÷À» Áß¾Ó¿¡ ¿ÀÇÂ
 * @value		wurl : ¿ÀÇÂÇÒ url
 				wtarget	: Å¸°Ù¸í
 				wwidth	: ÆË¾÷Ã¢ ³ÐÀÌ
 				wheight	: ÆË¾÷Ã¢ ³ôÀÌ
 * @return		void
*/
function setPopCenterOpen(wurl, wtarget, wwidth, wheight)
{
	var szResult;
	if(window.navigator.userAgent.indexOf("SV1") != -1)
		szResult = "SV1";
	else if(window.navigator.userAgent.indexOf("MSIE 7.0") != -1)
		szResult =  "IE7";
	else
		szResult = "";

	try
	{
		if(szResult == "SV1")
			add_sp = 15;
		else if(szResult == "IE7")
			add_sp = 15;
		else
			add_sp = 0;
	}

	catch(e)
	{
		Trace("SelfResize2()¿À·ù:"+e.description);
	}

	wheight		= parseInt(wheight) + add_sp;
	var nleft	= (screen.availWidth / 2) - (wwidth / 2);
	var ntop	= (screen.availHeight / 2) - (wheight / 2);

	window.open(wurl, wtarget, "toolbar=no,menubar=no,location=no,directories=no,scrollbars=no,status=no, width="+ wwidth +", height="+ wheight +",left="+nleft+",top="+ntop);
}


/**
 * @function	shuffle
 * @brief		·£´ýÀ¸·Î ¹è¿­¼¯±â
 * @usage		array.shuffle();
 * @return		array
*/
(function () {	
	var swapper =
	    function (sArr, nLen, e)
	    {
    	    var r 	= Math.floor(Math.random() * nLen);
    	    var x 	= sArr[e];
    	    sArr[e]	= sArr[r];
    	    sArr[r] = x;
    	};

	Array.prototype.shuffle =
    	function ()
    	{
        	var i, nLen;
        	i = nLen = this.length;

        	while(i--)
        		swapper(this, nLen, i);
    	};
})();