$(document).ready(function() {
	$('body').append("<link rel='stylesheet' href='/framework/css/layer_popup.css' type='text/css' />");
	$('body').append("<div id='idLayerPopupBg'></div><div id='idLayerPopupContent'></div><div id='idLoadingBg'></div><div id='idLoading' style='width:100px;height:100px;text-align:center;'><img src='/framework/image/loading.gif' alt='loading' /></div>");
});

function FuncRequestByURLAndDataType(url, dataType, successFunc, errorFunc) {
	$.ajax({
		url: url,
		type: "POST",
		data: null,
		dataType: dataType,
		success: function(response) {
			if (successFunc != null) {
				successFunc(response);
			}
		},
		error: function(response) {
			if (errorFunc != null) {
				errorFunc(response.statusText);
			}
		}
	});
}

function FuncRequestByFormAndDataType(form, dataType, successFunc, errorFunc) {
	var param = form.serialize();

	$.ajax({
		url: form.attr("action"),
		type: "POST",
		data: param,
		dataType: dataType,
		success: function(response) {
			if (successFunc != null) {
				successFunc(response);
			}
		},
		error: function(response) {
			if (errorFunc != null) {
				errorFunc(response.statusText);
			}
		}
	});
}

function FuncRequestByURL(url, successFunc, errorFunc) {
	FuncRequestByURLAndDataType(url, "json", successFunc, errorFunc);
}

function FuncRequestByFormID(form, successFunc, errorFunc) {
	FuncRequestByFormAndDataType(form, "json", successFunc, errorFunc);
}

function FuncRequestByURLToLayer(url, layer) {
	layer.fadeOut();

	$.ajax({
		url: url,
		type: "POST",
		data: null,
		success: function(response) {
			if (response != "") {
				layer.html(response);
				layer.fadeIn();
			}
		},
		error: function(response) {
			alert(response.status);
		}
	});
}

function FuncShowPopup(formID) {
	FuncRequestByFormAndDataType(formID, null, FuncPopupSuccess, FuncPopupError);
}

function FuncShowPopupByURL(url) {
	FuncRequestByURLAndDataType(url, null, FuncPopupSuccess, FuncPopupError);
}

function FuncHidePopup(isReload) {
	FuncHideLoading();

	$("#idLayerPopupContent").html("");
	
	$("#idLayerPopupContent").fadeTo('slow', 0.0);
	$("#idLayerPopupBg").hide();
	
	if (isReload) {
		location.reload();
	}
}

function FuncPopupSuccess(response) {
	FuncHideLoading();
	
	$("#idLayerPopupContent").html(response);

	FuncPopupLayerCentering($("#idLayerPopupContent"));
	
	$("#idLayerPopupBg").show();
	$("#idLayerPopupContent").fadeTo('slow', 1.0);
}

function FuncPopupError(response) {
	FuncAlert(response);	
	FuncHidePopup();
}

function FuncPopupLayerCentering(layer) {
	layer.css('left', $(window).scrollLeft() + (($(window).width() - layer.width()) >> 1));
	layer.css('top', $(window).scrollTop() + (($(window).height() - layer.height()) >> 1));
}

function FuncShowLoading() {
	FuncPopupLayerCentering($("#idLoading"));
	
	$("#idLoadingBg").show();
	$("#idLoading").show();
}

function FuncHideLoading() {
	$("#idLoading").fadeOut();
	$("#idLoadingBg").hide();
}

/**
 * 문자열 바이트 수 계산
 * 
 * @param text
 * @returns
 */
function FuncCheckBytes(thisObj, maxLengthByte) {
	var tempByteLength = 0, cutByteLength = 0; 
	for(var i = 0; i < thisObj.value.length; i++) { 
		if(escape(thisObj.value.charAt(i)).length > 4) { 
			tempByteLength += 2;
		} else {
			tempByteLength++; 
		} 

		if(tempByteLength < maxLengthByte) { 
			cutByteLength++; 
		}
	} 
	
	if(tempByteLength > maxLengthByte) { 
		alert("최대 " + maxLengthByte + "byte 까지만 입력 가능합니다.");
		thisObj.value = thisObj.value.substring(0, (cutByteLength%2==1)?cutByteLength:cutByteLength+1);
		
		return false;
	}
	
	return true;
}
function FuncCutString(str, maxLengthByte) {
	var tempByteLength = 0, cutByteLength = 0; 
	for(var i = 0; i < str.length; i++) { 
		if(escape(str.charAt(i)).length > 4) { 
			tempByteLength += 2;
		} else {
			tempByteLength++; 
		} 

		if(tempByteLength < maxLengthByte) { 
			cutByteLength++; 
		}
	} 
	
	if(tempByteLength > maxLengthByte) { 
		str = str.substring(0, (cutByteLength%2==1)?cutByteLength:cutByteLength+1) + "...";
	}
	
	return str;
}

function FuncPopupWindow(url, w, h) {
	w += 20; // scrollbar width
	var sw = screen.availWidth;
	var sh = screen.availHeight;
	var cx = (sw - w) / 2;
	var cy = (sh - h) / 2;
	
	window.open(url, 'popWin', 'left=' + cx + ',top=' + cy + ',width=' + w + ',height=' + h + 'toolbar=no,menubar=no,status=no,scrollbars=yes,resizable=yes');
}

function FuncImagePreview(imagePath) {
	var html = "<a href='javascript:FuncHidePopup()'><img src='" + imagePath + "' border='0' style='padding:5px;background-color:#000000;' /></a>";
	FuncPopupSuccess(html);
}

function FuncJsonString(data) {
	alert(JSON.stringify(data));
}

/**
 * replace all 기능
 */
function FuncReplace(text, targetText, replacement) {
	if (text == null || text.length <= 0) return;
	
	return text.replace(new RegExp(targetText, "g"), replacement);
}

function FuncDayOfTheWeek(sDate) {

	// sDate : YYYY-MM-DD
	var yy = parseInt(sDate.substr(0, 4), 10);
    var mm = parseInt(sDate.substr(5, 2), 10);
    var dd = parseInt(sDate.substr(8, 2), 10);

    var d = new Date(yy,mm - 1, dd);
    var weekday=new Array(7);
    weekday[0]="일";
    weekday[1]="월";
    weekday[2]="화";
    weekday[3]="수";
    weekday[4]="목";
    weekday[5]="금";
    weekday[6]="토";

    return weekday[d.getDay()];
}

