// need jquery (1.7.1+) to run current page
// need general.js to run current page

function toggleInstruction(buttonId, contentId, docId, formId) {
    if ($("#" + contentId).is(":visible")) {
        $("#" + contentId).slideUp(function () {
            $("#" + buttonId).removeClass("active");
            $("#" + buttonId).attr("title", "Show Instruction");
            resizeParentIframe(docId, window);
            setCookie("InstructionHidden_" + formId, "1");
        });
    } else {
        $("#" + contentId).slideDown(function () {
            $("#" + buttonId).addClass("active");
            $("#" + buttonId).attr("title", "Hide Instruction");
            resizeParentIframe(docId, window);
            setCookie("InstructionHidden_" + formId, "0");
        });
    }
}

function togglePageJumpWindow(buttonId, contentId, docId) {
    // prevent button click from propagation
    // body with class "jumpWindowListener" will have a event listener
    // clicking the button will prevent the event from triggering 
    $("#" + buttonId).click(function (e) {
        e.stopPropagation();
    });

    // prevent popup window content from propagation
    // body with class "jumpWindowListener" will have a event listener
    // clicking the window content will prevent the event from triggering
    $("#" + contentId).click(function (e) {
        e.stopPropagation();
    });

    // If current window is visible, hide the window
    // If the window is hidden, show the window
    if ($("#" + contentId).is(":visible")) {
        hidePageJumpWindow(buttonId, contentId, docId);
    }
    else {
        if ($("#" + contentId).is(":hidden")) {
            // Get button width to offset the content
            // button width retrieved from $("#" + buttonId).width() only holds width of page text
            // Need to add +alpha to offset icon width as well
            var width = $("#" + buttonId).width() + 25;
            $("#" + contentId).css({ top: -5, left: width, position: 'absolute' });

            // Trigger fade in
            $("#" + contentId).fadeIn(function () {
                $("#" + buttonId).addClass("active");
                resizeParentIframe(docId, window);
                
                // Add a class "jumpWindowListener" to current body
                // If the class name is being added (for the first time), add event listener to close the window
                if (!$("body").hasClass("jumpWindowListener")) {
                    // temporary variable to store current window
                    var currentWindow = window;
                    var counter = 0; // fail-safe counter for while loop

                    while (currentWindow != currentWindow.parent) {
                        $("body", currentWindow.document).addClass("jumpWindowListener");
                        $(".jumpWindowListener", currentWindow.document).click(function () {
                            hidePageJumpWindow(buttonId, contentId, docId);
                        });
                        currentWindow = window.parent;
                        counter++;
                    }

                    // as final step, add event listener to current iframe
                    $("body", currentWindow.document).addClass("jumpWindowListener");
                    $(".jumpWindowListener", currentWindow.document).click(function () {
                        hidePageJumpWindow(buttonId, contentId, docId);
                    });
                }
            });
        }
    }
}

function hidePageJumpWindow(buttonId, contentId, docId) {
    if ($("#" + contentId).is(":visible")) {
        $("#" + contentId).fadeOut(function () {
            $("#" + buttonId).removeClass("active");
            resizeParentIframe(docId, window);
        });
    }
}