/*
 * Menu UI
 */
function showSubMenu() {
	var img = null;
	
	// Image
	$('ul#gnb > li a img').each(function() {
		img = $(this).attr('src');
		$(this).attr('src', img.replace('_on.gif', '.gif'));
	});
	img = $('img', this).attr('src');
	$('img', this).attr('src', img.replace('.gif', '_on.gif'));
	
	// Submenu
	$('ul#gnb ul').hide();
	$(this).siblings('ul').show();
}

/*
 * 통합검색
 */
function validateFullSearch() {
	var $query = $('input[name=query]', this);
	if($query.val() == '') {
		alert('검색어를 입력해주세요.');
		$query.focus();
		return false;
	}
	
	return true;	
}

function printPage() {
	window.print();
	return false;	
}

/* Hangul */
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;
}

 
/*
 * Photo
 */
function updateImages() {
	$.get('/attachments', 
		swfu_settings.post_params,
		function(data) {
			$('#photos').html(data);
			initPhotos();
		}
	);			
} 

function initPhotos() {
	$('#photos li')
		.mouseover(function() { $(this).addClass('hover'); })
		.mouseout(function() { $(this).removeClass('hover'); })
		.click(function() {
			$('#photos li').removeClass('selected');
			$(this).addClass('selected');
		});
	
	$('#photos a.insert').click(insertPhoto);
	$('#photos a.delete').click(deletePhoto);
}
 
function insertPhoto() {
	var id = this.href.split('#')[1];	// Temp_1
	var path = $('#attach_' + id + ' .path').text();
	var target = $(this).attr('rel');
	if(target == '') target = 'editor';

	$('#' + target).paste_html('<p><img id="photo_' + id + '" class="photo" src="' + path + '?cmd=thumb&width=600" /></p>');
	
	return false;
}

function deletePhoto() {
	if(confirm('삭제하실 경우 에디터에서도 삭제됩니다. 삭제하시겠습니까?')) {
		var id = this.href.split('#')[1];

		$.post('/attachments/' + id, {'_method': 'DELETE'},
			function(data) {
				if(data.result == 'OK') {
					$('#photos #attach_' + id).remove();
					
					// HTML에디터에서 사진 삭제.
					$('textarea').each(function() {
						var content = $(this).get_content();
						var regexp = new RegExp('<p><img[^>]+id="?photo_' + id + '"?[^>]+></p>', 'ig');
						content = content.replace(regexp, '');
						$(this).set_content(content);
					});
				} else {
					alert('첨부파일을 삭제하는데 실패하였습니다.');
				}	
			}, 'json'
		);
	}
}

function _insertPhoto(target, id, path) {
	$('#editor').paste_html('<p><img id="photo_' + id + '" class="photo" src="' + path + '?cmd=thumb&width=600" /></p>');
}


/* gnb */
function gnbActive(id) {
	for(num=1; num<=5; num++) document.getElementById('gnbsub'+num).style.display='none';
	document.getElementById(id).style.display='block';
}
/* fnlClass */
function fnlClass(elId, tagName) {
	var el = document.getElementById(elId).getElementsByTagName(tagName);
	el[0].className="first";
	el[el.length-1].className="last";
}

/* tabContents */
function tabContents(tabContainerID, tagName) {
	var tabContainer = document.getElementById(tabContainerID);
	var tabAnchor = tabContainer.getElementsByTagName(tagName);
	var i = 0;
	for(i=0; i<tabAnchor.length; i++) {
		if (tabAnchor.item(i).className == "tablist")
			thismenu = tabAnchor.item(i);
		else
			continue;
		thismenu.container = tabContainer;
		thismenu.targetEl = document.getElementById(tabAnchor.item(i).href.split("#")[1]);
		thismenu.targetEl.style.display = "none";
		thismenu.imgEl = thismenu.getElementsByTagName("img").item(0);
		thismenu.onclick = function tabMenuClick() {
			currentmenu = this.container.current;
			if (currentmenu == this)
				return false;
			if (currentmenu) {
				currentmenu.targetEl.style.display = "none";
				if (currentmenu.imgEl) {
					currentmenu.imgEl.src = currentmenu.imgEl.src.replace(/_on\.gif$/, '.gif');
				} else {
					currentmenu.className = currentmenu.className.replace(" selected", "");
				}
			}
			this.targetEl.style.display = "";
			if (this.imgEl) {
				this.imgEl.src = this.imgEl.src.replace(/\.gif$/, '_on.gif');
			} else {
				this.className += " selected";
			}
			this.container.current = this;
			return false;
		};
		if (!thismenu.container.first) thismenu.container.first = thismenu;
	}
	if (tabContainer.first)	tabContainer.first.onclick();
}
/* rolloverimg */
function rollOverImg(elId, tagName) {
	var el = document.getElementById(elId).getElementsByTagName(tagName);
	for (i=0; i<el.length; i++) {
		  if (el[i].src.substring(el[i].src.indexOf(".gif"))
				||el[i].src.substring(el[i].src.indexOf("_on.gif"))) {
				el[i].onmouseover = changeOver;
				el[i].onmouseout = changeOut;
		}
	}
}
function changeOver() {
	this.src=this.src.replace(".gif", "_on.gif");
}
function changeOut() {
  this.src=this.src.replace("_on.gif", ".gif");
}
/* popup open */
function openWin(url, name, width, height) {
	var info = 'width=' + width + ',height=' + height + ',resizable=no, scrollbars=yes, menubars=no,status=no';
	window.open(url, name, info);
}
