// DOM 형성후 바로 실행되는 스크립트
var tmp = new Date();

// 본문 쓰기
window.doWrite = function(e,bn,cn) {
	var urls = 'write.php?bn='+bn+'&cn='+cn;
	document.location.href = urls;
}

// 본문 목록으로
window.doList = function(bn) {
	var urls = 'enti.php?bn='+bn;
	document.location.href = urls;
}


$(document).ready(function() {

	$("#startPage").click(function() {
		window.external.AddFavorite('http://www.wapy.com', '워피닷컴')
	});

	// 메뉴 현재위치 표시
	if(gn) {
		$("#msele_"+gn).addClass("sele"); 
	}
	if(bn) {
		$("#msele2_"+bn).addClass("sele"); 
	}

	//$("#gnb").bind("mouseover",function() { $(".notice").hide() });
	//$("#gnb").bind("mouseout",function() { $(".notice").show() });

	// 검색어 리셋
	$("#keyword").click(function() {
		$("#old").val( $(this).val() );
		$(this).val("");
	}).bind("blur",function() { 
		if( $(this).val() == "") { 
			$(this).val( $("#old").val() ); 
		}
	});
	// 회원레이어 띄우기
	$(".user_function a").click(function(event) { userFunc( $(this) ,event); });
	memo_get_ajax();
	setInterval(memo_get_ajax, 10000);

	// 이벤트 로그인 필요시
	$(".joinEvent").click(function(event) {
		if( $("#myid").text().length > 0) {
			$.get( $(this).attr('rel'), function(data) {
	
				var oj = new String();
				oj = data.split("|");
				if(oj[0] == "confirm") {
					var conf = confirm(oj[1]);
					if(conf == true) {
						window.document.location.href=oj[2];
					}
				} else {
					alert(data);
				}

			});
		} else {
			loginNeed(event);
		}

	});

});

//menu script
$(function(){
	var menu = (".menu");
	$(menu).children("li").each( function() {
		var item = $(this);

		//mouse Action
		$(item).hover( function() {
			menuOverEffect(item);
		}, function() {
			menuOutEffect(item);
		});

		//keyboard Action
		$(item).children("a").focus( function() {
			menuOverEffect(item);
		});
		$(item).children("ul").children("li").children("a:last").blur( function() {
			menuOutEffect(item);
		});

		//Over Event
		function menuOverEffect(target) {
			$(menu).children("li").children("ul").hide();
			$(target).children("ul").show();
		}

		//Out Event
		function menuOutEffect(target) {
			$(target).children("ul").hide();
			$(menu).children("li.sele").children("ul").show();
		}
	});
});

// 쪽지 확인 및 도착 액션
window.memo_get_ajax = function(){
	$.post('/ajax/getNote.php', 'tmp='+tmp, function(data){
		if(data){ 
			var uid = $("#myid").text();
			$("#Layer")
			.removeClass("user")
			.removeClass("login")
			.html('')
			.append($("<div>").text('쪽지가 도착했습니다. 지금 확인하시겠습니까?'))
			.append($("<button>").text('확인').click(function(){ 
				popup('pop'+uid,'/myhome/?id='+uid+'&type=note&mode=&act=read&num='+data,640,600,'yes');}))
			.append($("<button>").text('닫기').click(function(){ $(this).slideUp("fast"); }))
			.css("padding","30px")
			.css("width","100px")
			.css("height","100px")
			.css("bottom","0px")
			.css("right","10px")
			.css("background","#ddd")
			.click(function() { $(this).slideUp("fast"); })
			.slideDown("fast");	
		}
	});
}


window.userFunc = function(obj,e) {
	var userid = $(obj).attr("rel");
	var mouseovers = '';
	var bn = $("#bn").text();

	var layer1 = $("<li>").append($("<a>").addClass("memo").html("쪽지보내기")
		.click(function() {popup('pop'+userid,'/myhome/?id='+userid+'&type=memo',640,600,'yes');$("#Layer").hide();}));
	var layer2 = $("<li>").append($("<a>").addClass("profile").html("회원프로필")
		.click(function() {popup('pop'+userid,'/myhome/?id='+userid,640,600,'yes');$("#Layer").hide();}));
	
	if(bn) {
	var layer3 = $("<li>").append($("<a>").addClass("searchs").html("작성글보기")
		.click(function() {document.location.href='/entiz/enti.php?bn='+$("#bn").text()+'&searchType=search&search1=4&keys='+userid;}));
	} else {
	var layer3 = '';
	}
	var layer4 = $("<li>").append($("<a>").addClass("guestbook").html("방명록")
		.click(function() {popup('pop'+userid,'/myhome/?id='+userid+'&type=guestbook',640,600,'yes');$("#Layer").hide();}));
	var layer5 = $("<li>").append($("<a>").addClass("friend").html("친구추가")
		.click(function() {$.post("/myhome/doAddFriend.php","id="+userid, function(data) {
		if(data) { alert(data) }
		else { alert('친구등록을 신청하였습니다.'); }});})); 

	var ULS = $("<div>")
	.append(
		$("<ul>")
		.append(layer1)
		.append(layer2)
		.append(layer3)
		.append(layer4)
		.append(layer5)
	).bind("mouseover",function() { $("#Layer").show()});
	
	$("#Layer")
		.removeClass("login")
		.addClass("user")
		.css('left', fnIsIETrueBody().scrollLeft+e.clientX-50+'px')
		.css('top', fnIsIETrueBody().scrollTop+e.clientY+'px')
		.show().bind("mouseout",function() {$(this).hide()})
		.html(ULS);

}

window.popup = function(id,url,width,height,scroll) {
	var ids = window.open(url, id, 'width='+width+',height='+height+',scrollbars='+scroll);
	ids.focus();
}

window.gotoMyhome = function(userid) {
	popup('pop'+userid,'/myhome/?id='+userid+'&type=basic',640,600,'yes');
}



window.showAdmin = function(val) {
	if(val == 'system') popup('admin1','/admin/system.php?mode='+val,'750','750','yes')
	else		 popup('admin1','/admin/config.php?bn='+val,'750','750','yes')
}


// 검색
window.Search = function() {
	if(  $("#keyword").val().length < 2 || $("#keyword").val() == "통합검색") {
		alert('검색어는 2글자 이상 입력하셔야 합니다.');
		return false;
	}
	return true;
}

window.fnIsIETrueBody = function() {
	// 스크롤된 실제 BODY 위치 출력용
	return (document.compatMode && document.compatMode!="BackCompat")? document.documentElement : document.body
}

window.flash = function(vWidth, vHeight, flashUri) {
	var _obj_ = "";
	_obj_ = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0" width="' + vWidth + '" height="' + vHeight + '"  align="middle">';
	_obj_ += '<param name="allowScriptAccess" value="always" />';
	_obj_ += '<param name="allowFullScreen" value="true" />';
	_obj_ += '<param name="movie" value="' + flashUri + '" />';
	_obj_ += '<param name="quality" value="high" />';
	_obj_ += '<param name="wmode" value="transparent" />';
	_obj_ += '<embed src="' + flashUri + '" quality="high" wmode="transparent"  width="' + vWidth +'" height="' + vHeight + '" align="middle" allowScriptAccess="always" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></embed>    ';
	_obj_ += '</object>';
	document.writeln( _obj_ );
}

// 로그인 필요시
window.loginNeed = function(e) {
	var dwidth = $(document).width();
	var pLeft = fnIsIETrueBody().scrollLeft+e.clientX-10;
	var pTop = fnIsIETrueBody().scrollTop+e.clientY+25;
	var pArrow = '';
	if( dwidth > pLeft+400 ) {
		var pArrow = $("<img>").attr("src","/images/bg/box_arrow_left.gif").removeClass("pright").addClass("pleft");
	} else {
		var pArrow = $("<img>").attr("src","/images/bg/box_arrow_right.gif").removeClass("pleft").addClass("pright");
		pLeft = pLeft-375;
	}

	$("#Layer")
		.removeClass("user")
		.addClass("login")
		.css('left', pLeft+'px')
		.css('top', pTop+'px');

	$.post("/myhome/login.php","_url="+document.URL,function(data) {
		$("#Layer").html(data).append(pArrow).show();
		document.login2.userid.focus()
	});
}

window.loginAct = function() {
	// 로그인 처리
	if( window.document.login2.userid.value.length < 2) {
		alert('아이디가 입력되지 않았습니다');
		window.document.login2.userid.focus()
		return false;
	}


	if( window.document.login2.passwd.value.length < 2) {
		alert('비밀번호가 입력되지 않았습니다');
		window.document.login2.passwd.focus()
		return false;
	}

	$.ajax( {
		type: "POST",url: "/entiz/doLogin.php",data: "userid="+ document.login2.userid.value + "&passwd=" + document.login2.passwd.value + "&url=" + document.login2.url.value,
		async	: true,success : function(data) {
			var oj = new String();
			oj = data.split("|");
			if(oj[0] == "error") {
				alert(oj[1]);
				window.document.login2.passwd.focus();
			} else {
				if(oj[1] ){ window.document.location.replace(oj[1]); }
				else { window.document.location.replace('/'); }
			}
		},
		error : function(data) {
			alert("로그인시스템 오류입니다.");
		}
	});
	return false;
}

window.logoutAct = function() {
	// 로그아웃 처리
	$.ajax( {
		type	: "POST",
		url		: "/entiz/doLogout.php",
		data	: "tmp="+new Date(),
		async	: false,
		success : function(data) {
			var oj = new String();
			oj = data.split("|");
			alert(oj[0]);
				if(oj[1] ){ window.document.location.replace(oj[1]); }
				else { window.document.location.replace('/'); }
		},
		error : function(data) {
			alert("로그인시스템 오류입니다.");
		}
	});
}

window.findPass = function() {
	// 아이디/비밀번호 찾기
	var itemPOP = window.open("about:blank", "itemPOP", "toolbar=no,location=no,directories=no,status=yes,menubar=no,resizable=no,width=400,height=400,scrollbars=yes"); 
	itemPOP.location.href = "/entiz/findPass.php"; 
	itemPOP.focus();
}

window.closeLayer = function() {
	$("#Layer").removeClass("user").hide();
}

try {document.execCommand('BackgroundImageCache', false, true);} catch(e) {} 




