﻿if (typeof jQuery != 'undefined') {
    $(document).ready(function() {
        //Set ourself a global Timer variable so we can kill it when needed
        var AutoHide = null;

        //Show the the down arrow for the manual page navigation
        $(".PagerPopupFrom img").show();

        //Remove the default cursor by removing the CssClass
        $(".PagerJumpFromAndPopup span").removeClass("RemoveDefaultCursor");

        //PagerPopupFrom = the span that triggers the popup
        $(".PagerPopupFrom").click(function() {
            //Clear old timeout so the popup doesn't repeatedly close when multi-clicks
            clearTimeout(AutoHide);

            //Find the Popup itself
            //$(this).siblings(".PagerPopup").toggle(function() {
            $(this).siblings(".PagerPopup").toggle(function() {
                //Bind the Popup so we can use it in other events
                var DivPopup = $(this);

                //Set a timer to rehide the Popup if no action is taken or if mouse never goes onto the popup
                AutoHide = setTimeout(function() { $(DivPopup).hide(); }, 1500);

                //Kill the timer on mouseenter
                $(DivPopup).bind("mouseenter", function() { clearTimeout(AutoHide); });

                //Hide when leaving and reset textbox
                $(DivPopup).bind("mouseleave", function() { AutoHide = setTimeout(function() { $(DivPopup).hide(); $(".tbxPageToJumpTo", DivPopup).val(""); }, 1500); });

                //Trap the click on the button and Take the PathFormat from the hidden field, replace by the page typed and change location
                $(".PagerPopupSubmit", DivPopup).click(function() {
                //If page asked is 1, we location to page without page pattern
                    if ($(".tbxPageToJumpTo", DivPopup).val() == "1") {
                        document.location = $(this).parents().children(".PathFormatPageOne").val();
                    } else {
                        document.location = $(this).parents().children(".PathFormat").val().replace("{0}", $(".tbxPageToJumpTo", DivPopup).val());
                    }
                });

                //Set focus to textbox upon showing
                $(".tbxPageToJumpTo", DivPopup).focus();

                //User is typing in the Textbox
                $(".tbxPageToJumpTo", DivPopup).keydown(function(e) {
                    //Kill the timer on typing in the textbox
                    clearTimeout(AutoHide);
                    if (e.keyCode == "13") {
                        //ENTER key: Take the PathFormat from the hidden field, replace by the page typed and change location
                        if ($(".tbxPageToJumpTo", DivPopup).val() == "1") {
                            document.location = $(this).parents().children(".PathFormatPageOne").val();
                        } else {
                            document.location = $(this).parents().children(".PathFormat").val().replace("{0}", $(".tbxPageToJumpTo", DivPopup).val());
                        }

                        //Prevent normal form submit to fire if ENTER is pressed
                        return false;
                    }
                });
            });
        });
    });
}
