﻿var JsUtil = {
		
	options : {},		// jQuery 애니메이션 처리용

	//애니메이션 처리용
	callBack : function (){
	},
	
	browserCheck : function (){ 
		var ver = 0; // 브라우저  버전정보
		
		if( navigator.appName.charAt(0) == "N" ){ 
			if( navigator.userAgent.indexOf( "Chrome" ) != -1 ){
				return 0;
				
			} else if( navigator.userAgent.indexOf( "Firefox" ) != -1 ){
				return 1;
				
			} else if( navigator.userAgent.indexOf( "Safari" ) != -1){
				return 2;
				
			}
		} else if( navigator.appName.charAt(0) == "M" ){
			return 3;
			
		} else if( navigator.appName.charAt(0) == "O" ){
			return 4;
			
		}
	}, 

	// 이미지 resize
	imageResize : function( img ) {
		var maxWidth = 400;
		var maxHeight = 400;
		var width;		//  가로
		var height;		// 	세로
		var srcWidth;	//	원래 가로
		var srcHeight;	//	원래 세로
		var ratio;		//	가로 * 세로 비율
		
		srcWidth 	= 	img.attr("width");
		srcHeight 	= 	img.attr("height");
		
		ratio = srcWidth / srcHeight;
		
		if(srcWidth > maxWidth) {
			width = maxWidth;
		} else {
			width = srcWidth;
		}
		
		if(srcHeight > maxHeight) {
			height = maxHeight;
		} else {
			height = srcHeight;
		}
		
		if(ratio > 1) {
			height = srcHeight * width / srcWidth > maxHeight ? srcHeight * maxWidth / srcWidth  : srcHeight * width / srcWidth;
			
		} else {
			width = srcWidth * height / srcHeight > maxWidth ? srcWidth * maxHeight / srcHeight  : srcWidth * height / srcHeight;
		}
		
		var resultArr = new Array(2);
		resultArr[0] = width;
		resultArr[1] = height;
		
		return resultArr;
	},

	cutString : function( str, length ) {
		if( str.length <= length ) {
			return str;
		} else {
			return str.substring( 0, length ) + "...";
		}
	},

	hideDiv : function ( divSelector, effect ) {
		$( divSelector ).hide( effect, this.options, 500, this.callBack );
	},

	// Blind 애니메이션 처리
	showDiv : function ( divSelector, effect ) {
		$( divSelector ).show( effect, this.options, 500, this.callBack);
	},

	// 1000단위 comma
	addComma : function ( obj ) {
		var str = obj.value;
		var Re = /[^0-9]/g;
		var ReN = /(-?[0-9]+)([0-9]{3})/;
		
		str = str.replace(Re, '');
		
		while(ReN.test(str)) {
			str = str.replace(ReN, "$1,$2");
		}
		obj.value = str;
	},

	// 가장 앞 숫자 0으로 오지 못 하게 제한
	checkZeroAtFirst : function ( id ) {
		var value = $(id).val();
		
		if(value.length > 1 && value.charCodeAt(0) == 48) {
			alert("입력한 내용이 형식에 어긋납니다.");
			$(id).focus();
			return false;
		}
		
		return true;
	},

	// 숫자로 입력 제한 걸기
	numeric : function ( id ) {
		$( id ).numeric();
		$( id ).css( "ime-mode", "disabled" );
	},

	// 문자열 길이 알아내는 메서드
	getStringLength : function ( str ) {
		var retCode 	= 0;
		var strLength 	= 0;

		for (i = 0; i < str.length; i++) {
		  	var code 	= str.charCodeAt(i);
		   	var ch 		= str.substr(i,1).toUpperCase();

	    	code = parseInt(code);

	    	if ((ch < "0" || ch > "9") && (ch < "A" || ch > "Z") && ((code > 255) || (code < 0))) {
	    		strLength = strLength + 2;
	    	} else{
	    		strLength = strLength + 1;
	    	}
	   }
	   return strLength;
	},

	/*
	 * 길이 체크
	 * obj 			: 	textarea
	 * targetId 	: 	길이를 표시할 라벨
	 * maxLength 	: 	제한 바이트
	 */

	checkLength : function ( objID, targetID, maxLength ) {
		var size 	= this.getStringLength($(objID).val());
		
		if(size > maxLength) {
			alert(getMsg("limit_byte"));

			var text 	= 	$(objID).val();
			var i 		= 	maxLength / 2;
			var temp 	= 	text.substring(0, i);
			
			
			while( this.getStringLength(temp) < maxLength) {
				temp = temp + text.charAt(i);
				i++;
			}
			
			size = this.getStringLength(temp);
			$(objID).val(temp);
		}
		
		$(targetID).html(size);
	},

	// 브라우저가 서블릿에서 반환한 text에 <pre> 태그를 붙이는 경우 이를 제거
	removePre : function ( text ) {
		var filename = text;
		
		if(filename.indexOf('<pre style="word-wrap:') > -1) {		//	사파리, 크롬	
			var beginIdx = text.indexOf(">") + 1;
			var lastIdx = text.indexOf("</pre>");
			
			filename = text.substring(beginIdx, lastIdx);
			
		} else {		//	파폭
			filename = filename.replace("<pre>", "");
			filename = filename.replace("</pre>", "");
			
		}
		
		return filename;
	},

	// innerHTML 교체 AJAX
	changeHtmlAjax : function ( path, divSelector ) {
		$.post(	path, 
				function(data) {
					$( divSelector ).html(data);
				});
	},

	// jQuery Ajax호출
	doAjax : function ( type, url, data, dataType, callBack, async ) {
		$.ajax({
			type: type,
			url: url,
			data: data,
			dataType:dataType,
			error:this.ajaxError,
			async:async,
			success:callBack
		});
		
	},

	// String을 XML로 파싱
	createXMLFromString : function ( string ) {
		var xmlDocument;
		var xmlParser;
		
		if(window.ActiveXObject) {
			xmlDocument = new ActiveXObject("Microsoft.XMLDOM");
			xmlDocument.async = false;
			xmlDocument.loadXML(string);
			
		} else if(window.XMLHttpRequest) {
			xmlParser = new DOMParser();
			xmlDocument = xmlParser.parseFromString(string, 'text/xml');
		} else {
			return null;
		}
		
		return xmlDocument;
	},

	getParam : function getParam( name ) {
		var parameters = unescape(location.search).substring(1).split(/\&/g);

		if(parameters.length > 0) {
			for(var i = 0; i < parameters.length; i++) {
				if(parameters[i].indexOf("=") > -1) {
					var splitedParam = parameters[i].split("=");

					if(splitedParam[0] == name) {
						return splitedParam[1];
					}
				}
			}
		}
		
		return null;
	},

	// 객체가 배열인지 체크
	isArray : function ( arg ) {
		if(typeof arg == 'object') {
			var criteria = arg.constructor.toString().match(/array/i);
			return (criteria != null);
		}
		
		return false;
	},

	// 이벤트 객체 가져오기
	getActivatedObject : function ( e ) {
		var obj;
		
		if(!e) {
			obj = window.event.srcElement;
			
		} else if(e.srcElement) {
			obj = e.srcElement;
			
		} else {
			obj = e.target;
		}
		return obj;
	},
	
	ajaxError : function() {
		alert("호출에 실패하였습니다.");
	},
	
	encodeSpChar : function( text ) {
		var reg = 	/[äöüßÄÖÜ]/;
		
		var i 	= 	0;
		var char;
		
		while( reg.test( text ) ) {
			char = text.charAt( i );
			
			switch ( char ) {
			case "ä":
				text = text.replace( "ä", "&#228;" );
				break;
			case "ö":
				text = text.replace( "ö", "&#246;" );
				break;
			case "ü":
				text = text.replace( "ü", "&#252;" );
				break;
			case "ß":
				text = text.replace( "ß", "&#223;" );
				break;
			case "Ä":
				text = text.replace( "Ä", "&#196;" );
				break;				
			case "Ö":
				text = text.replace( "Ö", "&#214;" );
				break;
			case "Ü":
				text = text.replace( "Ü", "&#220;" );
				break;				
				
			}
			
			i++;
		}
		
		return text;
	}
};

$(document).ready(function() {
	nowLoading();	// 로딩중 표시 설정
});

//ajax Loading 중 표시
function nowLoading() {
	$("#viewLoading").ajaxStart(function() {
		PopEffect.setCenter( "#viewLoading" );
		$("#viewLoading").fadeIn("fast");
	});
	
	$("#viewLoading").ajaxStop(function() {
		$("#viewLoading").fadeOut(200);
	});
}

