﻿/***************************************************유효성검사 및 사용자 확인용********************************************************/
function delCheck() {
	return confirm("정말 삭제하시겠습니까?");
}


//입력된 값이 사용자 아이디로 사용하기 적절한지(영문+숫자 6~15자 이내) 검사
function checkUsableId(id) {
	var regExp = new RegExp(/^[a-z][0-9a-z]{6,15}/g);
	return regExp.test(id);
}


//라디오버튼용 체크여부 확인 함수
function SomeChecked(name) {
	var target = $("[name='" + name + "']");
	var returnValue = false;
	target.each(function() {
		if (this.checked) {
			returnValue = true;
		}
	});

	return returnValue;
}


//입력된 값이 이메일 주소 형태인지 검사
String.prototype.IsEmail = function() {

	var regExp = new RegExp(/^[A-Za-z0-9]{4,12}@[A-Za-z0-9]{2,12}\.[A-Za-z0-9]{2,12}/g);

	return regExp.test(this) || this == "" || this == null;
}








/**************************************************************************************************************************************/

//해당 객체의 val()메서드로 추출된는 값이 바뀔때마다 숫자가 아니면 초기화한다.
function SetOnlyNumber(obj) {
	$(obj).bind("keyup", function() {
		if (isNaN($(this).val())) {
			$(this).val("");
		}
	});
	$(obj).bind("blur", function() {
		if (isNaN($(this).val())) {
			$(this).val("");
		}
	});
}



//이메일 선택자 기능용 함수 (빈값이면 목표 개체의 readonly속성을 제거)
function setEmail(obj, target) {

	if (!obj.value) {
		$(target).attr("readonly", false);
		$(target).val("");
		target.focus();
	}
	else {
		$(target).attr("readonly", true);
		$(target).val(obj.value);
	}
}



//이미지 리사이징
//페이지 내의 모든 img중 cls값과 일치하는 class값을 가진 이미지를 width, height 영역 안으로 비율에 맞춰 축소시킨다.
//width, height값은 최소 1보다 커야한다.
//로딩중 페이지 틀어짐을 막기위해 div로 감싸주는것이 좋다
function ImageResize(cls, width, height) {
	if (width == null || width <= 1 || height == null || height <= 1) {
		alert("[ImageResize()] width, height값은 반드시 1이상의 값이 들어가야합니다.");
		return false;
	}

	if (cls == null || cls == "") {
		alert("[ImageResize()] cls값이 필요합니다.");
		return false;
	}

	var images = $("." + cls);

	images.each(function() {
		if (this.nodeName.toUpperCase() == "IMG") {
			var imgWidth;
			var imgHeight;
			var widthRatio;
			var heigthRatio;
			var resizeRatio;

			if (this.style.width) {
				imgWidth = parseInt(this.style.width);
			}
			else {
				imgWidth = parseInt(this.width);
			}

			if (this.style.height) {
				imgHeight = parseInt(this.style.height);
			}
			else {
				imgHeight = parseInt(this.height);
			}

			widthRatio = width / imgWidth;
			heigthRatio = height / imgHeight;

			if (widthRatio > heigthRatio) {
				resizeRatio = heigthRatio;
			}
			else {
				resizeRatio = widthRatio;
			}

			this.style.width = (imgWidth * resizeRatio) + "px";
			this.style.height = (imgHeight * resizeRatio) + "px";
		}
	});

}


//객체 속성 지정
function addAttr(obj, attrName, attrValue) {
	$(obj).attr(attrName, $(obj).attr(attrName) + " " + attrValue);
}



//지정한 class값을 가지는 체크박스를 클릭된 체크박스 또는 라디오버튼 상태대로 변경 (.NET 체크박스용)
function checkAll(obj, targetClass) {
	$("." + targetClass).each(function() {
		$($(this).find('input')[0]).attr("checked", obj.checked);
	});
}

//Highslide 레이어팝업 오픈
function layerPop(url, w, objType, position) {
	if (!objType) { objType = "iframe"; }
	if (!position) { position = "center"; }

	var anc = document.createElement("a");
	document.appendChild(anc);
	$(anc).attr("href", url);
	$(anc).click(function() {
		return hs.htmlExpand(this, { objectType: objType, width: w, align: position });
	});

	anc.click();
}



/*********************************이미지 슬라이드 뷰어 스크립트*****************************************/
function ImageSlider(id) {
	this.id = id;
	this.orientation = "horizontal";
	this.animation = true;
	this.objClassName = "jsSlideImages";
	this.prevBtnClass = "jsPrevBtn";
	this.nextBtnClass = "jsNextBtn";
	this.curIdx = 0;
	this.viewCnt = 4;
}

//객체 기본값을 초기화한다.
ImageSlider.prototype.Init = function(viewCnt, orientation, animation, objClassName, prevBtnClass, nextBtnClass) {
	if (viewCnt) {
		this.viewCnt = viewCnt;
	}

	if (orientation) {
		this.orientation = orientation;
	}

	if (animation != null) {
		this.animation = animation;
	}

	if (objClassName) {
		this.objClassName = objClassName;
	}

	if (prevBtnClass) {
		this.prevBtnClass = prevBtnClass;
	}

	if (nextBtnClass) {
		this.nextBtnClass = nextBtnClass;
	}
}

//객체 기능을 활성화한다.
ImageSlider.prototype.Activate = function() {

	var orientation = this.orientation;
	var animation = this.animation;
	var objClassName = this.objClassName;
	var prevBtnClass = this.prevBtnClass;
	var nextBtnClass = this.nextBtnClass;
	var rootObj = this;
	var viewCnt = this.viewCnt;

	var imgs = $("." + objClassName);

	imgs.each(function(i) {
		if (i >= 4) {
			$(this).hide();
		}
	});

	$("." + prevBtnClass).click(function() {
		rootObj.curIdx = rootObj.curIdx - 1;
		if (rootObj.curIdx < 0) {
			rootObj.curIdx = 0;
		}

		if (rootObj.curIdx >= 0) {
			imgs.each(function(i) {
				if (i == rootObj.curIdx) {
					$(this).parent().show();
					if (animation) {
						$(this).slideDown();
					}
					else {
						$(this).show();
					}
				}
				else if (i == rootObj.curIdx + viewCnt) {
					if (animation) {
						$(this).slideUp(function() {
							$(this).parent().hide();
						});
					}
					else {
						$(this).hide();
						$(this).parent().hide();
					}
				}
			});
		}
	});

	$("." + nextBtnClass).click(function() {

		if (rootObj.curIdx < imgs.length - viewCnt) {
			imgs.each(function(i) {
				if (i == rootObj.curIdx) {
					if (animation) {
						$(this).slideUp(function() {
							$(this).parent().hide();
						});
					}
					else {
						$(this).hide();
						$(this).parent().hide();
					}
				}
				else if (i == rootObj.curIdx + viewCnt) {
					$(this).parent().show();
					if (animation) {
						$(this).slideDown();
					}
					else {
						$(this).show();
					}
				}
			});

			rootObj.curIdx = rootObj.curIdx + 1;

			if (rootObj.curIdx == imgs.length - viewCnt) {
				rootObj.curIdx == imgs.length - viewCnt - 1;
			}
		}
	});
}

/********************************************************************************/



/**********************썸네일 크게 보기 스크립트 *********************************************************/
function imageMagnifier(imgClass, areaClass, activeClass, deactiveClass) {
	var objs = $("." + imgClass);
	objs.each(function() {
		$(this).css("cursor", "pointer");
		$(this).click(function() {
			var clickObj = this;
			objs.each(function() {
				var filename = $(this).attr("src");
				var onlyFilename = filename.split("/")[filename.split("/").length - 1].split(".")[0].replace("_off", "").replace("_on", "");
				var fileDir = filename.replace(filename.split("/")[filename.split("/").length - 1], "")
				var fileExt = filename.split("/")[filename.split("/").length - 1].split(".")[1];
				if (this == clickObj) {
					if ($(this).attr("class").indexOf(deactiveClass) != -1) {
						$(this).attr("src", fileDir + onlyFilename + "_on." + fileExt);
					}

					$(this).attr("class", $(this).attr("class").replace(deactiveClass, activeClass));

					$("." + areaClass).html("<img id='imageMagnifierZoomImageId' src='" + $(this).attr("bigImageSrc") + "' alt='" + $(this).attr("bigImageSrc") + "' style='display:none;' />");
					$("#imageMagnifierZoomImageId").fadeIn();
				}
				else {
					if ($(this).attr("class").indexOf(activeClass) != -1) {
						$(this).attr("src", fileDir + onlyFilename + "_off." + fileExt);
					}
					$(this).attr("class", $(this).attr("class").replace(activeClass, deactiveClass));
				}
			});
		});
	});
}
/*********************************************************************************************************/



/************************************** 게시글 롤링 스크립트 *******************************************/

function objRotator(targetClassName, time, animation, step) {
	if (!targetClassName) {
		targetClassName = "jsRotatorItem";
	}
	
	if (step == null) {
		step = 0;
	}
	
	if (!time) {
		time = 3000;
	}
	
	if (animation == null) {
		animation = true;
	}

	var targetObj = $("." + targetClassName);


	targetObj.each(function(i) {
		if (i != step || $(this).attr("display") != "none") {
			$(this).hide();
		}

		if (i == step) {
			if (animation) {
				$(this).fadeIn();
			}
			else {
				$(this).show();
			}
		}
	});
	
	step++;
	if (step == targetObj.length) {step = 0;}
	setTimeout("objRotator('" + targetClassName + "', " + time + ", " + animation + ", " + step + ")", time);
}

/********************************************************************************************************/
