$(document).ready(function() {
    $(".js_notice_dtl_lnk").live("click", function(event) {
		var seq = $(this).attr("href").split("#")[1];
		loadRptNotice(seq, this, event);
		return false;
    });
});

function loadRptNotice(p_seq, p_this, e)
{
    if (0 < $("#p_rpt_notice").length)
    {
        // 새로 조회하려는 통신원 공지사항이 현재 화면에 표시 중이면 그냥 return한다.
        if ($("#p_rpt_notice #rpt_notice_seq").val() == p_seq)
        {
            return;
        }
        else
        {
            $("#p_rpt_notice").hide();
        }
    }

	var param = "seq=" + p_seq;
    $.ajax({
        url: '/service/reporter/svc_rpt_notice_detail.asp',
        type: 'POST',
        data: param,
        dataType: 'json',
        async: true,
        //timeout: 3000,
        error: function() {
            alert("Error loading Reporter Notice");
        },
        success: function(p_data) {
            var pos = dvf_getPosition(p_this);

            if (0 < $("#p_rpt_notice").length)
            {
                setNoticeContents(p_seq, p_data);

                var winTop = pos.y;
                // #p_rpt_notice window의 높이를 찍어보면 19px 정도 작게 나오므로 계산식에 반영
                var winBottom = (winTop + $("#p_rpt_notice").height() + 19) - $(window).scrollTop();
                var tuningY = winBottom - $(window).height();
                if (0 < tuningY)
                {
                    winTop -= tuningY;
                }

                $("#p_rpt_notice").css("right", $("body").width() - pos.x + 2)
                                  .css("top", winTop)
                                  .show();
            }
            else
            {
                $("body").append(
                    $("<div></div>")
                        .attr("id", "p_rpt_notice"));
				$("#p_rpt_notice")
                        .load("/common/widget_skin/rptnotice_widget.htm",
                            function() {
                                $("#p_rpt_notice span.close").live("click", function() {
                                    $("#p_rpt_notice").remove();
                                });

                                setNoticeContents(p_seq, p_data);

                                var winTop = pos.y;
                                // #p_rpt_notice window의 높이를 찍어보면 19px 정도 작게 나오므로 계산식에 반영
                                var winBottom = (winTop + $("#p_rpt_notice").height() + 19) - $(window).scrollTop();
                                var tuningY = winBottom - $(window).height();
                                if (0 < tuningY)
                                {
                                    winTop -= tuningY;
                                }

                                $("#p_rpt_notice").css("right", $("body").width() - pos.x + 2)
                                                  .css("top", winTop)
                            });
//                );
            }
        }
    });
}

function setNoticeContents(p_seq, p_data)
{
    $("#p_rpt_notice #rpt_notice_seq").val(p_seq);

    $("#p_rpt_notice #notice_title").text(f_recievedDataToServiceByDecode(p_data.notice_title));
    if(p_data.eflag_html == "1"){
        $("#p_rpt_notice #notice_contents").html(F_dataToHtml(f_recievedDataToServiceByDecode(p_data.contents)));
    }else{
        $("#p_rpt_notice #notice_contents").html(f_recievedDataToServiceByDecode(p_data.contents));
    }
}

function rptNoticeCallbk(p_fldID, p_val) {
    if (p_fldID == 'title')
    {
        p_val = unescape(p_val).replace(/<\/?[^>]+>/gi, '');
		if (p_val.length > 32)
		{
			p_val = p_val.substr(0, 32) + "...";
		}
		else
		{
			p_val = p_val + "&nbsp;&nbsp;";
		}
    }

	return p_val;
}
function strTwo(str){
    if(str.length == 1)
        return "0"+ str;
    else
        return str;
}
function rptNoticeNewImgCallbk(p_fldID, p_val) {
    if (p_fldID == 'regdate')
    {
        var daygap = 7;
        var now = newday = new Date();
        var newyy, newmm, newdd;
        newday.setDate(now.getDate() - daygap);
        newyy = newday.getYear();
        newmm = newday.getMonth()+1;
        newdd = newday.getDate();

		if (p_val >= (newyy.toString().substr(2, 2)+'.'+strTwo(newmm.toString())+'.'+strTwo(newdd.toString())))
		{
			p_val = "<img src=\"http://image.eduhouse.net/iaeweb/ico/ico_notice_new.gif\" width=\"11\" height=\"10\" align=\"absmiddle\" style=\"margin:-1 0 0 0\">";
		}
		else
		{
			p_val = "";
		}
    }

	return p_val;
}

function read_rpt_notice_summary(p_pageNo, p_pageSize, p_container, p_clearGrid)
{
    var param = 'page_no=' + p_pageNo + '&page_size=' + p_pageSize;

    $(p_container + " #img_rpt_wl").show();   //로딩이미지 쇼
    $.ajax({
        url: '/service/reporter/svc_rpt_notice_list.asp',
        type: 'POST',
        data: param,
        dataType: 'json',
        //timeout: 3000,
        error: function() {
			$(p_container + " #img_rpt_wl").hide();   //로딩이미지 하이드
            alert("Error loading Reporter Notice");
        },
        success: function(p_data) {
			$(p_container + " #img_rpt_wl").hide();   //로딩이미지 하이드
			if ($(p_container).find("#data_not_found").length > 0)
			{
				$(p_container).find("#data_not_found").remove();
			}

			$.rptnotice_sum_dataCnt = p_data.dataCnt;

			if (p_data.dataCnt <= 0)
			{
				dvf_simpleGridClear(p_container);

				var msgDiv = $("<div id='data_not_found'></div>").css("height", "100px").css("vertical-align","middle");
				msgDiv.html("등록된 통신원 공지사항이 없습니다.");
				$(p_container).append(msgDiv);
			}
			else
			{
				if (p_data.noticeList == undefined || p_data.noticeList.length <= 0)
				{
					alert("리스트의 마지막입니다.");
				}
				else
				{
					dvf_simpleGrid(p_data.noticeList, p_container, p_clearGrid);

					//$(".js_notice_dtl_lnk").unbind();
				}
			}
        }
    });
}

//----------------------------------------------------
// 서비스에서 encode되어 받은 데이터를 디코드한다.
//----------------------------------------------------
function f_recievedDataToServiceByDecode(str){
    str = unescape(str);
    return str.replace(/&#43;/ig, "+");
}
function F_dataToHtml(str){
    return  str.replace(/\n/ig, "<BR>");
}
function read_rptnotice(p_seq, p_lnk, e)
{
    var p_boxheight = 400;
    var mouseEvt = e ? e : window.event;
    var y = mouseEvt.clientY;
	var topOffset;
	topOffset = y + document.body.scrollTop;

	var winTop1 = (y > p_boxheight ? topOffset - p_boxheight - 10 : document.body.scrollTop + 50) + "px";

	var lnkPos = dvf_getPosition(p_lnk);
	var winTop = winTop1;//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:"400px",
										   //height:"100",
										   top:lnkPos.y,
										   left:winLeft,
                                           //border:"1px solid #e1e1e1",
										   position:"absolute",
										   backgroundColor:"#fff"});
	$("body").append(tooltipWin);

	var param = "seq=" + p_seq;
    $.ajax({
        url: '/service/reporter/svc_rpt_notice_detail.asp',
        type: 'POST',
        data: param,
        dataType: 'json',
        //timeout: 3000,
        error: function() {
            alert("Error loading Reporter Notice");
        },
        success: function(p_data) {
			tooltipWin.load("/scripts/templet/iae_tooltip.asp", function(et) {
				$("#help_title").html(f_recievedDataToServiceByDecode(p_data.notice_title));
                if(p_data.eflag_html == "1"){
				    $("#help_contents").html(F_dataToHtml(f_recievedDataToServiceByDecode(p_data.contents)));
                }else{
				    $("#help_contents").html(f_recievedDataToServiceByDecode(p_data.contents));
                }
                //alert($("#help_contents").html());
			});
        }
    });

	return tooltipWin;
}

//window.onload = initDHTMLAPI;

var isCSS, isW3C, isIE4, isNN4, isIE6CSS;
// Initialize upon load to let all browsers establish content objects
function initDHTMLAPI() {
    if (document.images) {
        isCSS = (document.body && document.body.style) ? true : false;
        isW3C = (isCSS && document.getElementById) ? true : false;
        isIE4 = (isCSS && document.all) ? true : false;
        isNN4 = (document.layers) ? true : false;
        isIE6CSS = (document.compatMode && document.compatMode.indexOf("CSS1") >= 0) ? true : false;
    }
}

// Convert object name string or object reference
// into a valid element object reference
function getRawObject(obj) {
    var theObj;
    if (typeof obj == "string") {
        if (isW3C) {
            theObj = document.getElementById(obj);
        } else if (isIE4) {
            theObj = document.all(obj);
        } else if (isNN4) {
            theObj = seekLayer(document, obj);
        }
    } else {
        // pass through object reference
        theObj = obj;
    }
    return theObj;
}

function getObjectLeft(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("left");
    } else if (elem.currentStyle) {
        result = elem.currentStyle.left;
    } else if (elem.style) {
        result = elem.style.left;
    } else if (isNN4) {
        result = elem.left;
    }
    return parseInt(result);
}

// Retrieve the y coordinate of a positionable object
function getObjectTop(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (document.defaultView) {
        var style = document.defaultView;
        var cssDecl = style.getComputedStyle(elem, "");
        result = cssDecl.getPropertyValue("top");
    } else if (elem.currentStyle) {
        result = elem.currentStyle.top;
    } else if (elem.style) {
        result = elem.style.top;
    } else if (isNN4) {
        result = elem.top;
    }
    return parseInt(result);
}

// Retrieve the rendered width of an element
function getObjectWidth(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetWidth) {
        result = elem.offsetWidth;
    } else if (elem.clip && elem.clip.width) {
        result = elem.clip.width;
    } else if (elem.style && elem.style.pixelWidth) {
        result = elem.style.pixelWidth;
    }
    return parseInt(result);
}

// Retrieve the rendered height of an element
function getObjectHeight(obj)  {
    var elem = getRawObject(obj);
    var result = 0;
    if (elem.offsetHeight) {
        result = elem.offsetHeight;
    } else if (elem.clip && elem.clip.height) {
        result = elem.clip.height;
    } else if (elem.style && elem.style.pixelHeight) {
        result = elem.style.pixelHeight;
    }
    return parseInt(result);
}

