/*
 * Default JavaScript ($Revision: 2780 $)
 *
 */
$(document).ready(function(){	
	
	/*quick menu*/
	var lastScrollY = 0;
	var diffY = document.documentElement.scrollTop;

	$(window).scroll(function(){
		var diffTop = Math.max(document.documentElement.scrollTop, document.body.scrollTop);

		diffTop = diffTop + 10;

		if (diffY != lastScrollY) {
			percent = .10 * (diffY - lastScrollY);
			if (percent > 0) {
				percent = Math.ceil(percent);
			}else{
				percent = Math.floor(percent);
			}

			diffTop = parseInt($("#quick_menu").offset().top) + percent;
			lastScrollY = lastScrollY + percent;
		}

		$("#quick_menu").stop();
		$("#quick_menu").animate({"top": diffTop}, 500);
	});
	/*quick menu end*/

	$("#loginpopup").each(function() {
		$(this).fancybox({
			'titleShow'     : false,
			'opacity' 		: true,
			'autoDimensions'	: false,
			'width'         	: 370,
			'height'			: 210,
			'scrolling'		: 'no'
		});
	});
	
	/*  lost pass */  
	//$("a.lostpass").click(function(){  //ie에서 문제로 제거. by kakoo.
	$("a.lostpass").fancybox({
		'width'				: 340,
		'height'			: 220,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'href'				: '/metapresso/wp-login.php?action=lostpassword',
		'type'				: 'iframe'
	});
	//});

	$("a.youtube").click(function() {
		$.fancybox({
			'padding'             : 0,
			'autoScale'   : false,
			'transitionIn'        : 'none',
			'transitionOut'       : 'none',
			'width'               : 680,
			'height'              : 495,
			'href'                : this.href.replace(new RegExp("watch\\?v=", "i"), 'v/'),
			'type'                : 'swf',    // <--add a comma here
			'swf'                 : {'allowfullscreen':'true'} // <-- flashvars here
		});
		return false;
	}); 	

	$("#loginpopup").each(function() {
		$(this).click(function(){
			$("#user_login").focus();
		});
	});

	 /* popup img */  
	//$("a.zoompopup").fancybox();
	$("a.zoompopup").fancybox({
		'titleShow'     : false,
		'padding'		: 0
	});
	
	$(".loginpopup").each(function() {
		$(this).fancybox({
			'titleShow'     : false,
			'opacity' 		: true,
			'autoDimensions'	: false,
			'width'         	: 370,
			'height'			: 210,
			'scrolling'		: 'no',
			'href'			: '#loginform'
		});
	});

	$(".loginpopup").each(function() {
		$(this).click(function(){
			$("#user_login").focus();
		});
	});

	/*  comment edit & delet*/  
	$("a.comment_modify").click(function(){ 
		var editID = $(this).attr("id");
		$.fancybox({
			'width'				: 375,
			'height'			: 177,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'href'				: template_directory + '/comment-form.php?type=modfiy&cid='+ editID,
			'type'				: 'iframe'
		});
		return false;
	});
	
	$("a.comment_delete").click(function(){ 
		var editID = $(this).attr("id");
		$.fancybox({
			'width'				: 215,
			'height'			: 88,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'href'				: template_directory + '/comment-form.php?type=delete&cid='+ editID,
			'type'				: 'iframe'
		});
		return false;
	});

	// 추천나의 친구 - popup
  	//$("a.young1829").click(function(){ //ie에서 문제로 제거. by kakoo.
	$("a.young1829").fancybox({
		'width'				: 340,
		'height'			: 445,
		'href'				: '/popup_mail?pagetype=young1829',
		'titleShow'     	: false,
		'opacity' 			: true,
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	//});

	// 리더스 추천 - popup
  	//$("a.young1829").click(function(){ //ie에서 문제로 제거. by kakoo.
	$("a.leadersback").fancybox({
		'width'				: 340,
		'height'			: 445,
		'href'				: '/popup_mail?pagetype=leadersback',
		'titleShow'     	: false,
		'opacity' 			: true,
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	//});

	// 오픈강의소식 - popup
  	//$("a.open_lecture_news").click(function(){ //ie에서 문제로 제거. by kakoo.
	$("a.open_lecture_news").fancybox({
		'width'				: 340,
		'height'			: 475,
		'href'				: '/popup_mail?pagetype=open_lecture_news',
		'titleShow'     	: false,
		'opacity' 			: true,
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	//});

	// 숨어있는 작가 추천 - popup
  	//$("a.24th_gallery").click(function(){ //ie에서 문제로 제거. by kakoo.
	$("a.24th_gallery").fancybox({
		'width'				: 340,
		'height'			: 425,
		'href'				: '/popup_mail?pagetype=24th_gallery',
		'titleShow'     	: false,
		'opacity' 			: true,
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	//});
	

	// 운영진에게 제안 - popup
  	//$("a.admin_resiter").click(function(){ //ie에서 문제로 제거. by kakoo.
	$("a.admin_resiter").fancybox({
		'width'				: 340,
		'height'			: 305,
		'href'				: '/popup_mail?pagetype=admin_resiter',
		'titleShow'     	: false,
		'opacity' 			: true,
		'autoDimensions'	: false,
		'scrolling'			: 'no',
		'type'				: 'iframe'
	});
	//});

	// Google Map
	$(".gm-map").hover(function(){
		$("#gm-map-main").width(625).height(363);
		$("#gm-map-energy").width(459).height(346);
	}, function() {
		$("#gm-map-main").width(295).height(180);
		$("#gm-map-energy").width(338).height(186);
	});
	

	// 기자 정보
	$('.author_image_hover').click(function(){
		if ($(this).attr('status') == 'open') {
			$(this).next().css('display','none');
			$(this).attr('status', 'close');
		} else {
			$(this).next().css('display','block');
			$(this).attr('status', 'open');
		}
	});

	// 사이드바 열기/닫기
	$('.side_open_switch').click(function(){
		id = $(this).attr('id');
		if ($(this).attr('status') == 'open') {
			$(this).next().css('display','none');
			$(this).attr('status', 'close');
			$(this).removeClass('open');
			
			return false;
		} else {
			$(this).next().css('display','block');
			$(this).attr('status', 'open');
			$(this).addClass('open');

			return false;
		}
	});

	// 사이드바 모두 열기
	$('.title_category_bottom_more').click(function() {
		id = $(this).attr('id');

		if ($(this).attr('status') == 'open') {
			$(this).removeClass('opened');
			$(this).attr('status','close');
			$('#'+id+'_area .foldingContents').css('display','none');
			$('#'+id+'_area .side_open_switch').removeClass('open');
			$('#'+id+'_area .foldingIcon').attr('status', 'close');
		} else {
			$(this).addClass('opened');
			$('#'+id+'_area .foldingContents').css('display','block');
			$('#'+id+'_area .side_open_switch').addClass('open');
			$('#'+id+'_area .foldingIcon').attr('status', 'open');
			$(this).attr('status','open');
		}
	});

	// GNB
	$('#nav .out').hover(function() {
		$(this).parent().find('.over').show();

		target = $(this).parent().find('.over');
		$(this).parent().find('.over').addClass('show');

		$(this).parent().parent().find('.show').each(function(index, elm) {
			if ($(elm).attr('id') != $(target[0]).attr('id')) {
				$(elm).removeClass('show');
				$(elm).hide();
			}
		});
	});

	$('#nav .over').hover(function() {
	}, function() {
		target = $(this);
		$(this).parent().parent().find('.show').each(function(index, elm) {
			if ($(elm).attr('id') != $(target).attr('id')) {
				$(elm).removeClass('show');
				$(elm).hide();
			}
		});

		$(this).hide();
		$(this).removeClass('show');
	});

	// Footer
	var footer_reporter_left = 0;
	$('#foot_reporter_scroll_left').click(function() {
		if (footer_reporter_left > -1 && footer_reporter_left < 4) {
			$('#foot_reporter_wrapper ul').animate({"left": "-=218px"}, "slow");
			footer_reporter_left++;
		}
		return false;
	});
	$('#foot_reporter_scroll_right').click(function() {
		if (footer_reporter_left > 0 && footer_reporter_left < 5) {
			$('#foot_reporter_wrapper ul').animate({"left": "+=218px"}, "slow");
			footer_reporter_left--;
		}
		return false;
	});
});

// 로딩 시 사이드 바 열기
function sidebarOpenWhenLoading(id) {
	$('#'+id).attr('status', 'open');
	$('#'+id).addClass('open');
	$('#'+id).next().css('display','block');
}

/* 공백제거 함수 */
function trim(str) {
      str = str.replace(/^\s*/,'').replace(/\s*$/, '');
      return str;
}
function reg_patten( regstr , str) {
    if (!regstr.test(str)) {
    	return false;
	}else{
    	return true;
	}
}

function changeUrl(){
	var redirect;
	redirect = document.getElementById('reporter_type').value;
	document.location.href = redirect;	
}
/* 우편검색 */
function PopZipCheck(form,zipcode1,zipcode2,add01){
	addr_zip = window.open(template_directory + "/zipcode.php?form="+form+"&zipcode1="+zipcode1+"&zipcode2="+zipcode2+"&addr01="+add01 ,'_zip','toolbar=no,menubar=no,scrollbars=no,resizable=no,width=390, height=400 ,top=215,left=300');
	addr_zip.opener = self;
}

function check_field(fieldname , fieldvalue, alertvalue, resultvalue){
	if( trim(fieldname.value) == fieldvalue )
	{
		alert(alertvalue);
		fieldname.focus();
		resultvalue
	}
}

function toggleDIV( elemID , arrowID )
{
    var elem = document.getElementById( elemID );
    var arrow = document.getElementById( arrowID );
    if( elem.style.display != 'block' )
    {
        elem.style.display = 'block';
        arrow.className = "list_answer";
  	}
    else
    {
        elem.style.display = 'none';
         arrow.className = "list_subject";
	}
}

function only_member() {
    alert('로그인 후 확인 가능합니다.');
}
function only_writer() {
    alert('글쓴이와 관리자만 볼 수 있습니다.');
}
 
function only_open() {
    alert('로그인 후 이용 가능합니다.');
} 
/* 플레시 호출 함수 */
function swf_obj(src,w,h,swfid){
	swf_html = '';
	swf_html += '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="'+w+'" height="'+h+'" id="'+swfid+'">';
	swf_html += '<param name="allowScriptAccess" value="always">';
	swf_html += '<param name="movie" value="'+src+'">';
	swf_html += '<param name="quality" value="high">';
	swf_html += '<param name="bgcolor" value="#FFFFFF">';
	swf_html += '<PARAM NAME="wmode" VALUE="transparent">';
	swf_html += '<embed src="'+src+'" quality="high" bgcolor="#FFFFFF" width="'+w+'" height="'+h+'" name="'+swfid+'" allowScriptAccess="always" wmode="transparent" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">';
	swf_html += '</object>';
	document.write(swf_html);
}

/*ie에서 enter key로 submit. by kakoo 2010.04.19*/

function submitenter(field,e) {
	var keycode;
	if (window.event) {
		keycode = window.event.keyCode;
	} else if (e) {
		keycode = e.which;
	} else {
		return true;
	}

	if (keycode == 13) {
   		field.form.submit();
   		return false;
   	} else {
   		return true;
   	}
}

function bookmarksite(title,url) {
	if (window.sidebar)	// firefox
		window.sidebar.addPanel(title, url, "");
	else if (window.opera && window.print) {	// opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar'); 
		elem.click();
	} 
	else if(document.all)	// ie
		window.external.AddFavorite(url, title);
}

function loginpop(){
	var theForm = document.loginform;

	if (trim(theForm.log.value) =="") {
		alert("아이디를 입력바랍니다");
		theForm.log.focus();
		return false;
	}

	if (trim(theForm.pwd.value) =="") {
		alert('비밀번호를 입력해주시기 바랍니다');
		theForm.pwd.focus();
		return false;
	}
	return true;
}

// 질문 (400자/<span id="xxx_length">0</span>자)
// <textarea id="xxx" name="xxx" onkeyup="form_length_limit(this,400)"></textarea>
var chk_no = 0;
function form_length_limit(obj, length) {
	if ($(obj).attr('value').length > length) {
		if(chk_no == 0)
		{
			alert (length + "자까지 입력이 가능합니다.");
			chk_no++;
			return false;
		}
		$('#'+$(obj).attr('id')+'_length').css('color', 'red');
	} else {
		$('#'+$(obj).attr('id')+'_length').css('color', 'black');
	}

	$('#'+$(obj).attr('id')+'_length').html($(obj).attr('value').length);
}

function isNumber(s) {
  s += ''; // 문자열로 변환
  s = s.replace(/^\s*|\s*$/g, ''); // 좌우 공백 제거
  if (s == '' || isNaN(s)) return false;
  return true;
}

function isEmail(user_email){
	var reg_email=/^[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[@]{1}[-A-Za-z0-9_]+[-A-Za-z0-9_.]*[.]{1}[A-Za-z]{2,5}$/;	
		
	if(reg_patten(reg_email, user_email) == false ) {
		return false;
	}else{
		return true;
	}
}

