// global variables
var $docId = "1";
var $secId = "1";


$(document).ready(function() {
    if ($("#icc_Menu").length != 0) {
        LoadMenu();
        LoadTOC();

	if($("#icc_Menu").attr("secId").length == 0)
	{
		$("#icc_menu_content").css("text-align", "center");
	}
    }
});

$(window).resize(function() {
    if ($("#icc_Menu").length != 0) {
        getESpos('esIcon0', 'divES0');
    }
});



function LoadMenu() {
    $.ajax({
        contentType: "application/json; charset=utf-8",
        data: "{'sDocId':'" + $("#icc_Menu").attr("docId") + "','sSecId':'" + $("#icc_Menu").attr("secId") + "'}",
        dataType: "json",
        error: LoadMenuError,
        success: LoadMenuSuccess,
        type: "POST",
        url: "http://publicecodes.citation.com/ESWebServices/CService.asmx/GetMenu"
    });
}


function LoadMenuError(results) {
    alert(results.status + ' - ' + results.statusText);
}

function LoadMenuSuccess(results) {
    $("#icc_Menu").setTemplateURL("http://publicecodes.citation.com/js/ecodes_template_menu.htm");
    $("#icc_Menu").processTemplate(results);
}

function LoadTOC()
{
    $(".icc_section_item").each(function(i) {

        $.ajax({
            contentType: "application/json; charset=utf-8",
            data: "{'sDocId':'" + $(this).attr("docId") + "','sSecId':'" + $(this).attr("secId") + "'}",
            dataType: "json",
            error: LoadMenuError,
            success: function(msg) {
                if (msg.d != null) {
                    var docItem = "span.icc_section_item[docId='" + msg.d.docId + "'][secId='" + msg.d.secId + "']";
                    $(docItem).html("  (" + msg.d.Category + " Reports)");
                }
            },
            type: "POST",
            url: "http://publicecodes.citation.com/ESWebServices/CService.asmx/IsMenuAvailable"
        });
    });

}











function FindPosX(obj) { var curleft = 0; if (obj.offsetParent) while (1) { curleft += obj.offsetLeft; if (!obj.offsetParent) break; obj = obj.offsetParent; } else if (obj.x) curleft += obj.x; return curleft; }

function FindPosY(obj) { var curtop = 0; if (obj.offsetParent) while (1) { curtop += obj.offsetTop; if (!obj.offsetParent) break; obj = obj.offsetParent; } else if (obj.y) curtop += obj.y; return curtop; }

function getESpos(esThis, esDiv) {
    var esIcon = document.getElementById(esThis);
    var esBox = document.getElementById(esDiv);
    var x = FindPosX(esIcon);
    var y = FindPosY(esIcon);

    esBox.style.left = x + 'px';
    esBox.style.top = y + esIcon.offsetHeight + 5 + 'px';
}

function ShowES(id) {
    var odivES;
    odivES = document.getElementById(id);
    odivES.style.visibility = "visible";
}

function HideES(id) {
    var odivES;
    odivES = document.getElementById(id);
    odivES.style.visibility = "hidden";
}