
jQuery(document).ready(function() {

	$(".sch_save_list2").hover(
		function() {
//			showTooltipWin();
		},
		function() {
			hideTooltipWin();
		}
	);
});

function showTooltipWin(){
	var oldTooltipWin = $("#dv_notice_dtl");
	if (oldTooltipWin.length > 0)
	{
		//oldTooltipWin.hide();
		//oldTooltipWin.remove();
		oldTooltipWin.show();
	} else {

		var tooltipWin = read_rptnotice(this);

		tooltipWin.attr("id", "dv_notice_dtl");
		tooltipWin.show();
	}
}

function hideTooltipWin(){
	var tooltipWin = $("#dv_notice_dtl");
	if (!tooltipWin.isMouseOver)
	{
		tooltipWin.hide();
	}
}

function read_rptnotice(p_lnk)
{
	//var lnkPos = dvf_getPosition(p_lnk);
	//var winTop = String((lnkPos.y > 200 ? lnkPos.y : 210) - 200) + "px";
	//var winLeft = String((lnkPos.x > 410 ? lnkPos.x : 420) - 410) + "px";

	var tooltipWin = $("<div></div>").css({width:"200px",
										   height:"80px",
										   top:"145px",
										   left:"780px",
										   position:"absolute",
										   backgroundColor:"#fff"});
	$("body").append(tooltipWin);

	tooltipWin.load("/scripts/templet/iae_tooltip.asp", function() {
		$("#help_title").html("알림");
		$("#help_contents").html("관심학교 보관함은 로그인 후에<br /> 이용하실 수 있습니다.<br />");
	});

	return tooltipWin;
}

//관심학교목록 변할때 처리
function procPickChange(p_schIndex){
	//관심학교 개수
	//$(".saveList > div").length;
	var cartTotal = $(".outDiv").length;
	if (cartTotal > 0){
		$(".saveList").empty();
		$(".sch_save_list2").css("height", "145px");
		$(".saveList").css("background-color","#F7F7F3");
		//$(".saveBottom").show();
		//$("#del_storage_row").show();
		$("#storage_btn_row").show();
		if (cartTotal > 1){
			//$(".outDiv#" + p_schIndex).prepend("<div style='height:1px;background-image:url(http://image.eduhouse.net/iaeweb/bg/bg_dotline_gray.gif);'></div>")
			$(".outDiv#" + p_schIndex).prepend("<div><table width=\"90%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td background=\"http://image.eduhouse.net/iaeweb/bg/bg_dotline_gray.gif\"></td></tr></table></div>");
		}
	} else {
		$(".saveList").append('<ul><li><span>담기를 누르면 관심학교<br>리스트에 저장됩니다.</span></li><li><img src="http://image.eduhouse.net/2009_iae/image/sch_img.gif"></li></ul>');
		$(".sch_save_list2").css("height", "178px");
		$(".saveList").css("background-color","#F3F3F3");
		//$(".saveBottom").hide();
		//$("#del_storage_row").hide();
		$("#storage_btn_row").hide();
	}
}

function DBprocPickSch(p_proc, p_schIndex, p_schName, del_pDiv){
	var param = "proc="+ p_proc +"&schIndex="+ p_schIndex;
	$.ajax({
		url: '/service/study_info/svc_keepsch_ok.asp',
		type: 'POST',
		data: param,
		dataType: 'json',
		error: function() {
			alert("Error loading");
		},
		success: function(p_data) {
			// Login 여부 체크
			if (p_data.is_login != undefined && p_data.is_login == "false")
			{
				// Login하지 않은 경우 로그인 popup을 띄워 Login 확인한 후
				// 다시 수행해야 할 부분을 aftLogin callback 함수에 정의한다.
				//jQuery.extend({
				//	aftLogin: function() {
				//		DBprocPickSch(p_proc, p_schIndex, p_schName, del_pDiv);
				//	}
				//});

				// Login popup open
				// 세션 끊기거나 로그아웃시 보관함 리스트에 있는 항목 삭제
				showTooltipWin();
				$(".outDiv").remove();
				procPickChange(p_schIndex);
				//open_loginPop();
			}
			else
			{

				if (p_data.result == "Success"){
					if (p_proc == 'add'){
						var appendOutDiv = $("<div></div>")
							.attr("id", p_schIndex)
							.addClass("outDiv")
							.html("<p><a href='javascript:open_sch_view_popup_for_keepsch("+ p_schIndex +");'>"+ p_schName +"</a><img src='http://image.eduhouse.net/2009_iae/image/ico/ico_r_del.gif' alt='지우기' class='linklike' onclick='delPick(this);' /></p>");

						$('.saveList').append(appendOutDiv);
						//appendOutDiv.Draggable({
						//		revert: true,
						//		ghosting: true,
						//		opacity: 0.9
						//});

					}else if (p_proc == 'del'){
						$(del_pDiv).parent("span").parent("div").remove();
					}
					procPickChange(p_schIndex);
				}else if(p_data.result == "Fail1"){
					alert("DB저장 실패했습니다.");
				}
				else if(p_data.result == "Fail2"){
					alert("DB삭제 실패했습니다.");
				}
				else if(p_data.result == "Fail3"){
					alert("세션정보가 없습니다.");
				}
				else if(p_data.result == "Fail4"){
					alert("이미 보관되어 있습니다.");
				}

			}
		}
	});
}


//관심학교목록 추가
var afterDrop = function(dragged){

	//var appendHtml = "<div class='outDiv' id='"+ schIndex +"' class='r_storage' style='padding:7 0 1 7'><b>"+ schName +"</b><span onclick='delPick(this);'>삭제</span></div>";
//	var isInCart = $(".sch_save_list2 #"+ schIndex);
//	if (isInCart.length > 0){
//		alert("이미 저장되어 있습니다");
//	}else{
//	}
//	procPickChange('add',schIndex,schName);
};

//관심학교목록 삭제
function delPick(pDiv){
	var schIndex = $(pDiv).parent("p").parent("div").attr("id");
	var schName = $(pDiv).parent("p").text();
	$(pDiv).parent("p").parent("div").remove();
	procSession('del', schIndex, schName, pDiv);
//	DBprocPickSch('del', schIndex, schName, pDiv);
//	procPickChange("del", schIndex, schName);
}

//var outProc = function(dragged){
//	delPickDrag(dragged);
//};

//관심학교목록 삭제(드래그로)
//function delPickDrag(pDiv){
//	var schIndex = $(pDiv).attr("id");
//	var schName = $(pDiv).children("b").html();
//	$(pDiv).remove();
//	procPickChange("del", schIndex, schName);
//}

function open_multiReq()
{
	//showModalDialog("/user_req/pop_multisch_req_dm_write.asp", "", "dialogWidth:670px; dialogHeight:700px");
	var pop_Qwin = window.open('/user_req/pop_sch_catalogue_req.asp?mthd=mt','Qwin','width=640,height=760,scrollbars=yes');
    pop_Qwin.focus();

}

function open_multiQNA()
{
	//showModalDialog("/counsel/pop_qna_write.asp?qna_type=5", "", "dialogWidth:670px; dialogHeight:700px");
	var pop_Qwin = window.open('/counsel/pop_qna_write.asp?qna_type=5','Qwin','width=640,height=760,scrollbars=yes');
    pop_Qwin.focus();
}

function afterDrop2(clicked){
	//$("#keepSchToolTip").hide();
	var schName = $(clicked).prev("#dragInform").children("#pickSchName").html();
	var schIndex = $(clicked).prev("#dragInform").children("#pickSchIndex").html();
	//DBprocPickSch('add', schIndex, schName);
	var isInCart = $(".sch_save_list2 #"+ schIndex);
	var tx = dvf_getPosition(clicked).x;
	var ty = dvf_getPosition(clicked).y;
	$("#keepSchToolTip").css({"top":(ty-40),"left":(tx-70),"z-index":"100"}).show();
	if (isInCart.length > 0){
		$("#KeepSchhelp_contents").text("이미 보관되어 있는 학교입니다")
		//alert("보관되어 있는 학교입니다");
	}else{
		var cartTotal = $(".outDiv").length;
		if (cartTotal == 0) {
			$(".saveList").empty();
		}
		//저장하면 전역변수에 체크값 저장
		firstMsgKeepSch = 1;
		$("#KeepSchhelp_contents").text("관심학교 리스트에 담았습니다");
		var appendOutDiv = $("<div></div>")
			.attr("id", schIndex)
			.addClass("outDiv")
			.html("<p><a class='outDivText' href='javascript:open_sch_view_popup_for_keepsch("+ schIndex +");'>"+ schName +"</a><img src='http://image.eduhouse.net/2009_iae/image/ico/ico_r_del.gif' alt='지우기' class='linklike' onclick='delPick(this);' /></p>");
		$(".saveList").append(appendOutDiv);
		scrollAnimate($("#scrollSchList"), $("#scrollSchList").scrollHeight-5);
		procSession('add',schIndex,schName);
	}
	//툴팁 사라지게...
	$("#keepSchToolTip").fadeOut(400);
}

function afterDropPop(p_schIndex, p_schName){
	var isInCart = $(".sch_save_list2 #"+ p_schIndex);
	if (isInCart.length > 0){
		return "보관되어 있는 학교입니다";
	}else{
		var cartTotal = $(".outDiv").length;
		if (cartTotal == 0) {
			$(".saveList").empty();
		}
		var appendOutDiv = $("<div></div>")
			.attr("id", p_schIndex)
			.addClass("outDiv")
			.html("<p><a class='outDivText' href='javascript:open_sch_view_popup_for_keepsch("+ p_schIndex +");'>"+ p_schName +"</a><img src='http://image.eduhouse.net/2009_iae/image/ico/ico_r_del.gif' alt='지우기' class='linklike' onclick='delPick(this);' /></p>");
		$(".saveList").append(appendOutDiv);
		scrollAnimate($("#scrollSchList"), $("#scrollSchList").scrollHeight-5);
		procSession('add',p_schIndex,p_schName);
		return "관심학교 리스트에 담았습니다";
	}
}

var prevCart=false;
var prevCartTotal;
function procSession(p_proc, p_schIndex, p_schName, del_pDiv){

	//alert('보관');
	var param = "proc="+ p_proc +"&schIndex="+ p_schIndex;
	$.ajax({
		url: '/service/study_info/svc_keepsch_ok.asp',
		type: 'POST',
		data: param,
		dataType: 'json',
		error: function() {
			alert("Error loading");
		},
		success: function(p_data) {
			if (p_data.result == "Success"){
				//alert(p_data.sess_val);
			}else if(p_data.result == "Fail1"){
				alert("DB저장 실패했습니다.");
			}
			else if(p_data.result == "Fail2"){
				alert("DB삭제 실패했습니다.");
			}
			else if(p_data.result == "Fail3"){
				alert("세션정보가 없습니다.");
			}
			else if(p_data.result == "Fail4"){
				alert("보관되어 있는 학교입니다.");
			}

		}
	});
	//관심학교 개수
	//처음으로 카트를 불러올때(prevCart가 false 일때) prevCartTotal 에 현재 카트 개수 저장
	if (!prevCart){
		var cartTotal = $(".outDiv").length;
		prevCart = true;
		prevCartTotal = cartTotal;
	} else {
		if (p_proc == 'add'){
			prevCartTotal += 1;
		}else if(p_proc == 'del'){
			prevCartTotal -= 1;
		}
	}

	if (prevCartTotal == 0){
        $("#upScrolSaveList").hide();
        $("#downScrolSaveList").hide();
        $(".saveList").hide();
        $("#storage_btn_row").hide();
        $("#empty_save_list").show();
	} else if((prevCartTotal == 1) && (p_proc == 'add')){
        $("#empty_save_list").hide();
        $("#upScrolSaveList").show();
        $("#downScrolSaveList").show();
        $(".saveList").show();

        $("#storage_btn_row").show();
	}

}

