

$(function() {

    //$("#timeout").hide();
    if (!$("#loginform").length)    // don't set the timeout on the login page
    {        
        var timeout = 3600000;   // time in milliseconds
        var userAlive = false;

        $("#timeout").click(function() {
            userAlive = true;
            // run the ajax call
            // hide the div
            // restart the timer?
            $("#timeout").countdown('destroy');
            $("#timeout").html('Your MySchoolDay session will end<br/> in 30 seconds. <br/><a href=#>Click here</a> to continue working<br/>in MySchoolDay.').css({top: '-140px'});
            $.ajax({
                type: "GET",
                url: "/default/index/resetsession",
                dataType: "json",
                complete: function() {                    
                    //$(document).idleTimer('destroy');
                    //$(document).idleTimer(timeout);
                }
            });
        });

        $(document).bind("idle.idleTimer", function() {
            //alert("you're done!");
            timedOut = true;
            $("#timeout").show().animate({
                top: '+=88'
            },2000,function() {
                //$("#timeout").idleTimer(ltimeout);
                $('#timeout').countdown({until: '+30s', significant: 1,
                    layout: 'Your MySchoolDay session will end<br/> in {sn} seconds. <br/><a href=#>Click here</a> to continue working<br/>in MySchoolDay.'});
                setTimeout(function() {
                    if (!userAlive)
                    {
                        // log user out unless they have clicked to reset their session
                        window.location.replace("/login/logout");
                    } else
                    {
                        userAlive = false;
                    }
                }, 31*1000);
            });
            //window.location.replace("http://app.myschoolday.local:8888/login/login");
        });
        $(document).bind("active.idleTimer", function() {
            if (!timedOut)
            {
                // in twenty seconds we need to log them out
                // unless they choose to stay logged in
                $("#timeout").stop(true, false).css({top: '-110px'});
                //$("#timeout").idleTimer('destroy');
            }
        });

        $(document).idleTimer(timeout);
    }



    function setTimers()
    {

    }



    function postfeedback(currentpage, feedbackmsg)
    {
        $.post("/help/help/addfeedback",
               {page: currentpage, message: feedbackmsg},
               function(data)
               {});
    }

    var thankYouMsg = "<div id='thanksMsg' class='feedbackThanks'>Thanks for your feedback.</div>";

    var adjustedWidth = $(window).width() - 265;
    var feedback = $("#feedback").dialog({
        show: "blind",
        hide: "blind",
        position: [adjustedWidth,88],
        height: 260,
        width: 260,
        autoOpen: false,
        buttons: {
            Submit: function() {
                // hide the label and text area
                feedback.find("#feedbackMessage").hide();
                feedback.find("label[for='feedbackMessage']").hide();
                feedback.append(thankYouMsg);
                postfeedback(window.location.pathname, feedback.find('#feedbackMessage').val());
                setTimeout(function() {feedback.dialog('close');},1500);
                
                // get page & message and save to DB
                //$(this).dialog('close');
            },
            Cancel: function() {
                $(this).dialog('close');
            }
        },
        close: function() {

        }
    });




    $(window).resize(function() {
        adjustedWidth = $(window).width() - 265;
    });

    $("#feedbackMessage").focus(function() {$("#feedbackMessage").val("");});

    $("#feedbackpanel").click(function()
        {
            feedback.dialog( "option", "position", [adjustedWidth,88]);
            feedback.find("#thanksMsg").remove();
            feedback.find("#feedbackMessage").show();
            feedback.find("label[for='feedbackMessage']").show();
            feedback.find("#feedbackMessage").val("Type feedback for the MySchoolDay team here.");
            feedback.dialog('open');
        }
    );

   $('#endimpersonate').click(function(event) {
        event.preventDefault();
        $.post("/default/login/endimpersonate",
                function(data) {
                    if (data == "success")
                    { window.location.reload(); }
                },'json');
    });


});


