/**
 * Application Javascript
 */
var MAX_WIDTH = 600;
var MAX_HEIGHT = 600; 
 
// Search
function validateSearchForm() {
	if(!validateEmpty(this.query, '검색어를 입력해주세요.')) return false;
	return true;
}

// Login
function confirmLogin() {
	if(confirm("로그인 후 이용하실 수 있습니다.\n로그인 페이지로 이동하시겠습니까?")) {
		location.href = '/users/login.asp?return_to=' + escape(location.href);
	} else {
		history.back();
	}
}

/*
 * Form
 */
function addAttachment() {
	if($('#attachments input:file').length >= 10) {
		alert('첨부파일은 10개까지 추가하실 수 있습니다.');
		return false;
	}
	
	$('#attachments').append('<p class="file_add"><input type="file" name="attachment" size="40" class="inbox" style="width:330px; height:19px;" /></p>');
	return false;
}

function countHangul(target) {
	var str = $(target).val();
	var bytes = 0;
	var ch;
	
	for(var i = 0; i < str.length; i++) {
		ch = str.charAt(i);
		bytes += (escape(ch).length > 4 ? 2 : 1);
	}	
	
	return bytes;
}

function truncateHangul(target, max_bytes) {
	var str = $(target).val();
	var bytes = 0, pos = 0;
	var ch;
	
	for(var i = 0; i < str.length; i++) {
		ch = str.charAt(i);
		bytes += (escape(ch).length > 4 ? 2 : 1);
		if(bytes <= max_bytes) pos = i + 1;
	}	
	
	if(bytes > max_bytes) {
		str = str.substr(0, pos);
	}
	
	return str;
}

/* 
 * Resizing
 */
// Resize a image.
function resizeImage() {
	var width = this.width;
	var height = this.height;

	if(width > height && width > MAX_WIDTH) {
		width = MAX_WIDTH;
		height = (MAX_WIDTH / this.width) * height;
	} else if(width <= height && height > MAX_HEIGHT) {
		width = (MAX_HEIGHT / this.height) * width;
		height = MAX_HEIGHT;
	}	
	
	$(this).width(width).height(height);
}

// Resize a iframe.
function resizeFrame(frame) {
	frame.style.height = "auto";
	var contentHeight = frame.contentWindow.document.documentElement.scrollHeight;
	frame.style.height = (contentHeight + 4) + "px";
}

// Resize a window.
function resizeWin(width) {
	var height = document.documentElement.scrollHeight;
	self.resizeTo(width + 10, height + 80);
}

function setLayer(layerHeight) {
	//alert(document.getElementById('gnb').style.height);
	$('#gnb').height(layerHeight + 'px');	
	//document.getElementById('gnb').style.height = layerHeight + 'px';

	//alert(document.getElementById("gnb").style.height);
	document.getElementById("gnb").style.height = layerHeight;
	//alert(document.getElementById("gnb").style.height);
}
