/**
**    File: sonar.util.js
**    Created by: Young Mok, Kim. (mailto::super-nova@beizix.com)
**    Created on: 2005-01-16
**    Last modified: 2008-03-01
**
**
**    License Information:
**    -------------------------------------------------------------------------
**    Copyright (C) 2009 beizix.com
**
**    This script is free library; you can redistribute it and/or modify it
**    under the terms of the GNU General Public License as published by the
**    Free Software Foundation; either version 2 of the License, or (at your
**    option) any later version.
**
**    This library is distributed in the hope that it will be useful, but
**    WITHOUT ANY WARRANTY; without even the implied warranty of
**    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
**    General Public License for more details.
**
**    You should have received a copy of the GNU General Public License along
**    with this program; if not, write to the Free Software Foundation, Inc.,
**    59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
**
*/
if (typeof(sonar) != "object" || sonar == null) sonar = {};

sonar.util = {};

// 주어진 노드의 text 변경.
sonar.util.replaceText = function (el, text) {
	if (el) {
		sonar.util.clearChilds( el );
		el.appendChild( document.createTextNode( text ) );
	}
};

// 모든 자식노드 삭제
sonar.util.clearChilds = function (id) {
    if (id) {
        while( id.hasChildNodes() ){
            id.removeChild( id.childNodes[0] );
        }
    }
};

// 이벤트 버블 방지.
sonar.util.stopBubble = function (event) {
    event = event || window.event;
    if (event.stopPropagation) {
        event.stopPropagation();
    } else {
        event.cancelBubble = true;
    }
};

// 엔터키 이벤트인지 체크
sonar.util.check_enter = function (event) {
    var e = event || window.event;
    if (e && e.keyCode === 13) {
        return true;
    }
};

// 이벤트 등록함수.
sonar.util.addEvent = function (obj, evName, func){
	if (document.addEventListener) {
		obj.addEventListener(evName, func, false);
	} else {
		obj.attachEvent('on'+ evName, func);
	}
};

sonar.util.validateEvent	=	function(thisObj, event){

		var	type, target, currentTarget, relatedTarget, IEtarget;

		if ( !window.event && event)	//	FF, Safari, Chrome, Opera
		{
			type			=	event.type;
			target			=	event.target;
			currentTarget	=	event.currentTarget;
			relatedTarget	=	event.relatedTarget;

		}else if (window.event)	//	IE
		{
			event			=	window.event;
			target			=	event.srcElement;
			switch (event.type)
			{
				case 'mouseover':
					IEtarget	=	event.fromElement;
					break;
				case 'mouseout' :
					IEtarget	=	event.toElement;
					break;
			}
		}

		var	compareTarget, result	=	true;
		var	_innerFinder	=	function( nodez ){

			compareTarget	=	null;

			if (!window.event)			//	FF, Safari, Chrome, Opera
			{
				if (currentTarget === relatedTarget)	result	=	false;
				compareTarget	=	relatedTarget;

			} else if (window.event)	//	IE
			{
				if ( IEtarget == thisObj ) result =	false;

				compareTarget	=	IEtarget;

			}

			if (result)
			{
				for (var i = 0 ; i < nodez.childNodes.length ; i++ )
				{
					if (nodez.childNodes[i] == compareTarget) {
						result = false;
						break;
					}

					if(nodez.childNodes[i].hasChildNodes())
						_innerFinder(nodez.childNodes[i]);
				}
			}

		};

		_innerFinder( (!window.event) ? currentTarget : thisObj );

		var evTarget = (IEtarget)? IEtarget : compareTarget;
		return {
			'isOuter' : result,
			'evTarget' : evTarget
		};

};

sonar.util.clearWhite = function (v) {
	return v.replace(/(^[\s]*)|([\s]*$)/g, '');
};

// 브라우저 정보.
sonar.util.browserInfo = function () {

    var ua   =   navigator.userAgent;

    if (/MSIE/.test(ua)) {
        ua = 'MSIE';
    } else if (/Firefox/.test(ua)) {
        ua = 'Firefox';
    } else if (/Chrome/.test(ua)) {
        ua = 'Chrome';
    } else if (/Opera/.test(ua)) {
        ua = 'Opera';
    } else {
        ua = 'Other';
    }

    return ua;
};

sonar.util.console = function (msg) {
	document.body.appendChild(document.createTextNode(msg));
};

//  ================
//>  전역함수 Util   //>
//  ================
if (!window.e_) {
	e_ = function (i) {
        i = document.getElementById(i);
        return i;
    };
}

if (!window.ce_) {
	ce_ = function (i,attr) {
        i = document.createElement(i);

        if (typeof attr === 'object') {
            for (key in attr) {
                if (key === 'class') {
                    i.className   =   attr[key];
                } else if (key === 'style') {
                    i.style.cssText = attr[key];
                } else {
                    i.setAttribute(key, attr[key]);
                }
            }
        }
        return i;
    };
}

if (!window.ct_) {
	ct_ = function (i) {
        i = document.createTextNode(i);
        return i;
    };
}

if (!window.et_) {
	et_ = function (i) {
        return document.getElementsByTagName(i);
    };
}

if (!window.plus_) {
	plus_ = function (p, s) {
        return p.appendChild(s);
    };
}

if (!window.rm_) {
	rm_ = function (o) {
        o.parentNode.removeChild(o);
    };
}

