﻿//<![CDATA

// 이미지인지 판별
var isImage = /^.*?([^\/]+\.(?:jpg|gif|png|bmp))$/i;
var isFileName = /^.*?([^\/]+)$/i;

function FileAttach(arrAttach, strID, strAttachName, intCount) {
	$(strID).find("dd").remove();
	var intExistAmount = ViewAttachedFileList(arrAttach, strID, strAttachName);
	var intMax = intCount - intExistAmount;

	// if (intMax == 0) return;

	var strList = "";
	for (var i = 0; i < intMax; i++) {
		strList += GetAttachBox(strAttachName);
	}
	$(strID)
		.append(strList)
	.show();
}

// 첨부파일 박스 보여주기
function GetAttachBox (strAttachName) {
	return "<dd>" 
		+ "<input type=\"file\" id=\"" + (strAttachName + Math.ceil(Math.random() * 10000 % 100)) +  "\" name=\"" + strAttachName + "\" value=\"\" />"
		+ "</dd>";
}

////
//	ViewAttachedFileList : 글 읽기 / 수정 화면에서 업로드 되어있는 첨부파일 리스트를 노출.
//	<arrAttach> : 첨부파일 정보가 담겨있는 배열
//	<strID>		: 데이터를 노출할 태그의 구분자 (dl 의 id 등, 형식은 jquery 셀렉터에 따름)
function ViewAttachedFileList(arrAttach, strID, strAttachName) {
	if (arrAttach == undefined || arrAttach == null)
		return 0;
	if (BD_INF.pcd != "write" && (BD_INF.opt & 4) == 0)
		return 0;
	var strList = "";
	for (var i = 0; i < arrAttach.length; i++) {
//		alert(arrAttach[i].url + " " + isImage.test(arrAttach[i].url));
		if (!isImage.test(arrAttach[i].url)) continue;
		strList += "<dd>"
//			+ "<label class=\"" + (isImage.test(arrAttach[i].url) ? "ic_img" : "ic_file") + "\"></label>"
			+ "<a href=\"/board/filedownload.asp?bid=" + BD_INF.bid + "&idx=" + QueryString("idx") + "&order=" + i + "\">" 
				+ arrAttach[i].url.replace(isFileName, "$1") + "</a>"
			+ ((BD_INF.pcd == "write") ? "<a href=\"javascript:;\" class=\"btn_file_del\" title=\"삭제\">삭제</a>" 
			+ "<input type=\"hidden\" name=\"" + strAttachName + "\" value=\"" + arrAttach[i].url + "\" />" : "") 
			+ "</dd>";
	}
	$(strID)
		.append(strList)

	if (BD_INF.pcd == "write") {
		$(strID + " a.btn_file_del").click(function() {
			$(this).parent().remove();
			$(strID).append(GetAttachBox(strAttachName));
		});
	}

	return arrAttach.length;
}

// 첨부파일 삭제
function DeleteAttachedFile(strID, intIdx) {
	alert(strID);
	$(strID + " dd:eq(" + intIdx + ")").remove();

	//alert($("obj").attr("tagName"));
	return false;
}

// 글 삭제
function DeleteRow(strID, intIdx) {
	if (!confirm("정말 삭제하시겠습니까?")) return false;
	location.href = "?pcd=write&act=Delete&bid=" + strID + "&idx=" + intIdx;
}

// 조회수 증가
function addReadCount() {
	$.post(
	 "/board/ajax/addReadCount.asp"
	, { bid: BD_INF.bid, idx: QueryString("idx") }
	, function(data) {}
	);
}

// 카테고리 노출, arrCategory 라는 이름으로 카테고리 리스트 Json 이 있어야함.
var arrCategory=[];
function viewCategory() {
	var str = "";
	for (var i = 0; i < arrCategory.length; i++) {
		str += "<option value=\"" + arrCategory[i].idx + "\">" 
			+ arrCategory[i].text + "</option>";
	}
	$("#lstCategory")
		.append(str)
		.bind("change", searchCategory);
	if (QueryString("cat") != "") 
		$("#lstCategory").val(QueryString("cat"));
}

// 카테고리로 검색
function searchCategory() {
	if (BD_INF.pcd == "write") return;
	location.href = "?bid=" + BD_INF.bid 
		+ "&pcd=list" 
		+ "&page=1"
		+ "&cat=" + $("#lstCategory").val();
}

// 플래시
function getFlashObject(url, w, h) {
	return "<object width=\"" + w + "\" height=\"" + h + "\" "
		+ "wmode=\"transparent\" classid=\"clsid:d27cdb6e-ae6d-11cf-96b8-444553540000\" "
		+ "codebase=\"http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0\" "
		+ "id=\"V" + (new Date()).getTime() + "\" >"
		+ "<param name=\"movie\" value=\"" + url + "\" />"
		+ "<param name=\"allowScriptAccess\" value=\"always\" />"
		+ "<param name=\"allowFullScreen\" value=\"true\" />"
		+ "<param name=\"quality\" value=\"high\" />"
		+ "<embed src=\"" + url + "\" quality=\"high\" width=\"" + w + "\" height=\"" + h + "\" "
		+ "name=\"V" + (new Date()).getTime() + "\" allowscriptaccess=\"always\" allowFullScreen=\"true\" "
		+ "type=\"application/x-shockwave-flash\" "
		+ "pluginspage=\"http://www.macromedia.com/go/getflashplayer\"></embed></object>";
}

// 검색
function startSearch(obj) {
	var frm = obj.form;
	if (frm.txtSearchText.value == "") {
		alert("검색어를 입력해주세요!");
		frm.txtSearchText.focus();
		return false;
	}

	var strSearchBound = $("#lstSearchBound").val() ? $("#lstSearchBound").val() : "";
	var strSearchOption = $("#lstSearchOption").val() ? $("#lstSearchOption").val() : "";
	var strSearchText = $("#txtSearchText").val() ? $("#txtSearchText").val() : "";

	location.href = "?bid=" + BD_INF.bid
		+ "&pcd=" + BD_INF.pcd
		+ "&sbnd=" + strSearchBound
		+ "&sop=" + strSearchOption
		+ "&stext=" + escape(strSearchText);

	return true;
}
//]]>