/* 탭지정 */
function ShowTab(val){ 

	for (i=1; i<=2; i++) { 
		if( i == val ){
			$("#tab_img"+i).attr("src","/images/main/tabs"+i+"_on.gif");
			$("#con_view"+i).fadeIn();
		}else{
			$("#tab_img"+i).attr("src","/images/main/tabs"+i+".gif");
			$("#con_view"+i).hide();
		}
	}
	/*
	for (i=1; i<=3; i++) { 
		var tb = document.getElementById('con_view' + i);
		var tb_img = document.getElementById('tab_img' + i);
		if (i != val) {
			tb.style.display = "none";
			tb_img.src = "/images/main/tabs"+i+".gif"
		}else {
			tb.style.display = "block";
			tb_img.src = "/images/main/tabs"+i+"_on.gif"
		}
	}
	*/
}

/* 즐겨찾기 */
function addfavorites(){
	var favoriteurl="http://www.d4b4.co.kr"
	var favoritetitle="▣우리고장우리신문 동네방네▣"
	
	if (document.all){
		window.external.AddFavorite(favoriteurl,favoritetitle);
	}
}

/* 시작페이지 */
function addhome(obj){

	document.body.style.behavior='url(#default#homepage)';
	document.body.setHomePage('http://www.d4b4.co.kr/');

}

/* 검색 */
function total_searchok(){

	var schar = $("#kw").val().replace(" ","");

	if(schar.length < 2){
		alert("2자이상 입력하세요.");
		$("#kw").focus();
		return false;
	}

}

/* 토글내용 */
function l_toggle(val){

	if( $("#h_"+val).css("display") == "none" ){
		$("#h_"+val).fadeIn();
	}else{
		$("#h_"+val).fadeOut("fast");
	}
	//$("#h_"+val).toggle();	
}


/* 상세창 */
function popup_detail(val, file){

	if( val == ""){
		var url = "/"+file;
	}else{
		var url = "/"+file+"?idx="+val;
	}
	
	var popup_detail = window.open(url,"popup_detail","top=10,left=10,width=480,height=550,scrollbars=yes,resizable=no");
		popup_detail.focus();
}

/* 지도창 */
function popup_map(val){

	var url = "/map/?idx="+val;
	var popup_map = window.open(url,"popup_map","top=100,left=300,width=705,height=605,scrollbars=no,resizable=no");
		popup_map.focus();
}

function save_select(val,qval){

	if( savecount >= maxcount ){
		alert("더이상 찜할수 없습니다.\n최대 "+maxcount+"건까지 찜할수 있습니다.");
	}else{

		var str="";
		var i;
		var select_num=0;
		for(i=0;i<document.listfrm.length;i++){
			if(document.listfrm.elements[i].name.indexOf("pchk_")==0){
				var delfile=eval("document.listfrm." + document.listfrm.elements[i].name);
						
				if(delfile.checked == true){
					str=str + ""+ delfile.value +"|";
					++select_num;
				}else{
					continue;
				}

			}else{
				continue;
			}
		}

		if( select_num >= 1){
			var url = "/save.html?num="+str+"&ref="+val+"&"+qval;
			//alert(url);
			document.location.replace(url);
		}else{
			alert("찜할 내용을 선택하세요.");
		}
	}

}

/* 쿠키만들기 */
function setCookie( name, value, expiredays )
{
		var todayDate = new Date();
		todayDate.setDate( todayDate.getDate() + expiredays );
		document.cookie = name + "=" + escape( value ) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

/* 쿠키가져오기 */
function getCookie( name )
{
		var nameOfCookie = name + "=";
		var x = 0;
		while ( x <= document.cookie.length )
		{
				var y = (x+nameOfCookie.length);
				if ( document.cookie.substring( x, y ) == nameOfCookie ) {
						if ( (endOfCookie=document.cookie.indexOf( ";", y )) == -1 )
								endOfCookie = document.cookie.length;
						return unescape( document.cookie.substring( y, endOfCookie ) );
				}
				x = document.cookie.indexOf( " ", x ) + 1;
				if ( x == 0 )
						break;
		}
		return "";
}

		
/* 선택 */		
		var paper_allchecked = 0;

		function paper_allchk() 
		{
			var i;

			for(i=0;i<document.listfrm.length;i++){
				if(document.listfrm.elements[i].name.indexOf("pchk_")==0){
					var dwn=eval("document.listfrm." + document.listfrm.elements[i].name);
					if(paper_allchecked == 1){
						dwn.checked=false;			
					}else{
						dwn.checked=true;
					}

				}else{
					continue;
				}
			}

			if(paper_allchecked == 1){
				paper_allchecked=0;
			}else{
				paper_allchecked=1;
			}

		}


/* 팝업 */
		function paper_make(val){

			var str="";
			var i;
			var select_num=0;
			for(i=0;i<document.listfrm.length;i++){
				if(document.listfrm.elements[i].name.indexOf("pchk_")==0){
					var delfile=eval("document.listfrm." + document.listfrm.elements[i].name);
					
					if(delfile.checked == true){
						str=str + "p["+select_num+"]="+ delfile.value +"&";
						++select_num;
					}else{
						continue;
					}

				}else{
					continue;
				}

			}

			if(select_num >= 1){
				var str;
				if( val == "print"){
					str="/pop_print.html?use="+ val +"&"+ str +"all_num=" + select_num;
				}
				var win_print = window.open(str,"win_print","width=100,height=100,left=10,top=10,scrollbars=yes");
					win_print.focus();
			}else{
				alert("프린트할 내용을 선택하세요.");	
			}

		}

/* 쿠키삭제 */
	function clearCookie(val)
	{
		setCookie( "Notice"+val, "done" , 1); 
		layerclose(val);
	}

/* 레이어닫기 */
	function layerclose(val)
	{
		$("#layerpopup"+val).fadeOut("slow");
	}

/* 레이어띄우기 */
	function fn_layerpopup(idx, w, h, t, l, title)
	{
		var layeridx = 1000 + parseInt(idx,10);

		str_html = "<div id='layerpopup"+idx+"' class='frames' style='width:"+w+"px;top:"+t+"px;left:"+l+"px;z-index:"+layeridx+";'>";
		str_html += "<ul class='framesbar'><li class='left01'>"+title+"</li><li class='right01'><a href=\"javascript:layerclose('"+idx+"')\"><img src='/images/common/contents/x.gif'/></a></li></ul>";
		str_html += "<div class='iframes'><iframe frameborder='0' width='"+w+"' height='"+h+"'scrolling='no' src='/popup.asp?idx="+idx+"'></iframe></div>";
		str_html += "<div class='framesbottom'><a href=\"javascript:clearCookie('"+idx+"')\">하루 동안 안보기</a></div>";
		str_html += "</div>";

		$("body").prepend(str_html);
		$("#layerpopup"+idx).fadeIn("slow");
	}


	function fwrapper(func, args)
	{
		this.invoke = function(){
			func.apply(null, args);
		}
	}

	function getAvailSize(w, h, mw, mh)
	{
		if(w > mw || h > mh)
		{
			if(h / w > mh / mw)
			{
				return [(mh * w) / h, mh];
			}
			else
			{
				return [mw, (mw * h) / w];
			}
		}
		else
			return [w, h];
	}

	function resizeImgEx(img, maxw, maxh)
	{
		if(img.width)
		{
			var avail = getAvailSize(img.width, img.height, maxw, maxh);
			if(avail[0] != 0 && avail[1] != 0 && !isNaN(avail[0]) && !isNaN(avail[1]))
			{
				img.width = avail[0];
				img.height = avail[1];
			}
			else
			{
				img.width = maxw;
				img.height = maxh;
			}
		}
		else
		{
			window.setTimeout(new fwrapper(resizeImgEx, [img, maxw, maxh]).invoke, 30);
		}
	}


	function resizeImg(img, max)
	{
		if(max){}
		else{max = 700;}
		if(img.width)
		{
			if(img.width > img.height)
			{
				if(img.width > max)
					img.width = max;
			}
			else
			{
				if(img.height > max)
					img.height = max;
			}
		}
		else
		{
			window.setTimeout(new fwrapper(resizeImg, [img, max]).invoke, 30);
		}
	}
