﻿// Version: 1.1
var JRL;
if (typeof(JRL) !== "object" || JRL === null) {
    JRL = {};
}

if (typeof(JRL.UI) !== "object" || JRL.UI === null) {
    JRL.UI = {};
}

JRL.UI.showMessagePopup = function (title, message, callback) {
        if (typeof(JRL.UI.divMessagePopup) === "undefined") {
            JRL.UI.divMessagePopup = document.getElementById("divMessagePopup");
            if (JRL.UI.divMessagePopup === null) {
                delete JRL.UI.divMessagePopup;
                return;
            }
        }
        
        JRL.UI.divMessagePopup.childNodes[1].innerHTML = message.replace(/\n/g, "<br />");
        JRL.UI.divMessagePopup.childNodes[0].innerHTML = title;

        JRL.UI.divMessagePopup.style.display = "block";
        JRL.UI.toggleOverlay(true);
        
        var button = JRL.UI.divMessagePopup.getElementsByTagName("button")[0];
        button.onclick = function() { JRL.UI.hideMessagePopup(callback); };
        button.focus();
    };
JRL.UI.hideMessagePopup = function (callback) {
        JRL.UI.toggleOverlay(false);
        JRL.UI.divMessagePopup.style.display = "none";
        
        if (typeof(callback) == "function") {
            callback();
        }
    };

JRL.UI.showConfirmPopup = function (parameters) {
        if (!parameters.first_button_text) {
            parameters.first_button_text = "Yes";
        }
        if (!parameters.second_button_text) {
            parameters.second_button_text = "No";
        }

        if (typeof(JRL.UI.divConfirmPopup) === "undefined") {
            JRL.UI.divConfirmPopup = document.getElementById("divConfirmPopup");
            if (JRL.UI.divConfirmPopup === null) {
                delete JRL.UI.divConfirmPopup;
                return;
            }
        }
        
        JRL.UI.divConfirmPopup.childNodes[1].innerHTML = parameters.message.replace(/\n/g, "<br />");
        JRL.UI.divConfirmPopup.childNodes[0].innerHTML = parameters.title;

        JRL.UI.divConfirmPopup.style.display = "block";
        JRL.UI.toggleOverlay(true);
        
        var first_button = JRL.UI.divConfirmPopup.getElementsByTagName("button")[0];
        var second_button = JRL.UI.divConfirmPopup.getElementsByTagName("button")[1];
        
        first_button.innerHTML = parameters.first_button_text;
        second_button.innerHTML = parameters.second_button_text;
        
        first_button.onclick = function () {
            JRL.UI.hideConfirmPopup(parameters.first_button_callback);
        };
        second_button.onclick = function () {
            JRL.UI.hideConfirmPopup(parameters.second_button_callback);
        };
        
        first_button.focus();
    };
JRL.UI.hideConfirmPopup = function (callback) {
        JRL.UI.toggleOverlay(false);
        JRL.UI.divConfirmPopup.style.display = "none";
        
        if (typeof(callback) === "function") {
            callback();
        }
    };
JRL.UI.showPromptPopup = function (parameters) {
        //title, message, icon, default_value, continue_callback, cancel_callback
        if (typeof(JRL.UI.divPromptPopup) === "undefined") {
            JRL.UI.divPromptPopup = document.getElementById("divPromptPopup");
            if (JRL.UI.divPromptPopup === null) {
                delete JRL.UI.divPromptPopup;
                return;
            }
        }

        JRL.UI.divPromptPopup.childNodes[1].innerHTML = parameters.message.replace(/\n/g, "<br />");
        JRL.UI.divPromptPopup.childNodes[0].innerHTML = parameters.title;

        JRL.UI.divPromptPopup.style.display = "block";
        JRL.UI.toggleOverlay(true);
        
        var text_box = JRL.UI.divPromptPopup.getElementsByTagName("input")[0];
        var first_button = JRL.UI.divPromptPopup.getElementsByTagName("button")[0];
        var second_button = JRL.UI.divPromptPopup.getElementsByTagName("button")[1];
        
        text_box.value = parameters.default_value;
        
        first_button.onclick = function () {
            JRL.UI.hidePromptPopup(parameters.continue_callback);
        };
        second_button.onclick = function () {
            JRL.UI.hidePromptPopup(parameters.cancel_callback);
        };
        
        first_button.focus();
    };
JRL.UI.hidePromptPopup = function (callback) {

        JRL.UI.toggleOverlay(false);
        JRL.UI.divPromptPopup.style.display = "none";
        
        var text_box = JRL.UI.divPromptPopup.getElementsByTagName("input")[0];


        if (typeof(callback) === "function") {
            callback(text_box.value);
        }
    };


    /* ********************
       Blue overlay handler
       ****************** */
JRL.UI.overlay_toggle_count = 0;
    
JRL.UI.toggleOverlay = function (on) {
        if (typeof(JRL.UI.divMessagePopupBackground) === "undefined") {
            JRL.UI.divMessagePopupBackground = document.getElementById("divMessageBackground");
            if (JRL.UI.divMessagePopupBackground === null) {
                delete JRL.UI.divMessagePopupBackground;
                return;
            }
        }

        if (on) {
            JRL.UI.overlay_toggle_count++;
            JRL.UI.divMessagePopupBackground.style.display = "block";
            JRL.UI.resize(JRL.UI.divMessagePopupBackground);
            
            window.onresize = function () {
                JRL.UI.resize(JRL.UI.divMessagePopupBackground);
            };
        } else {
            JRL.UI.overlay_toggle_count--;

            if (JRL.UI.overlay_toggle_count === 0) {
                JRL.UI.divMessagePopupBackground.style.display = "none";
            
                window.onresize = null;
            }
        }
    };

JRL.UI.resize = function (element) {  
        var window_height = document.documentElement.clientHeight || window.innerHeight || document.body.clientHeight;
        if ( isNaN(parseInt(window_height, 10)) ) {
            element.style.height = '';
        } else {
            element.style.height = window_height + "px";
        }
    };




    /* *******************
    * Fancy acitivty indicator that only shows after
    * the Wait time has elapsed. Once showing it stays showing
    * until the Min time has elapsed. This avoids flashing the activity indicator for a time too short for the user
    * to understand what just happened.
    ********************* */
JRL.UI.toggle_activity_on_timeout = null;
JRL.UI.toggle_activity_off_timeout = null;
JRL.UI.toggle_activity_start_time = false;
JRL.UI.TOGGLE_ACTIVITY_WAIT_TIME = 500;
JRL.UI.TOGGLE_ACTIVITY_MIN_TIME = 1000;
JRL.UI.toggleActivity = function (on) {
        var elapsed_time;
    
        if (on) {
            clearTimeout(JRL.UI.toggle_activity_off_timeout);
            JRL.UI.toggle_activity_on_timeout = setTimeout(JRL.UI.toggleActivityOn_Callback, JRL.UI.TOGGLE_ACTIVITY_WAIT_TIME);
        } else {
            clearTimeout(JRL.UI.toggle_activity_on_timeout);
            clearTimeout(JRL.UI.toggle_activity_off_timeout);
            if (JRL.UI.toggle_activity_start_time) {
                elapsed_time = (new Date()).getTime() - JRL.UI.toggle_activity_start_time;
                if (elapsed_time < JRL.UI.TOGGLE_ACTIVITY_MIN_TIME) {
                    JRL.UI.toggle_activity_off_timeout = setTimeout(JRL.UI.toggleActivityOff_Callback, JRL.UI.TOGGLE_ACTIVITY_MIN_TIME - elapsed_time);
                } else {
                    JRL.UI.toggleActivityOff_Callback();
                }
            } else {
                JRL.UI.toggleActivityOff_Callback();
            }
        }
    };

JRL.UI.toggleActivityOn_Callback = function () {
        //document.getElementById("ifActivity").style.display = "block";
        //document.getElementById("divActivity").style.display = "block";

        JRL.UI.toggle_activity_start_time = (new Date()).getTime();
    };
JRL.UI.toggleActivityOff_Callback = function () {
        //document.getElementById("divActivity").style.display = "none";
        //document.getElementById("ifActivity").style.display = "none";

        JRL.UI.toggle_activity_start_time = false;
    };

