
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//
// Handling various window sizes
// Normal browser, mobile phone (< 600 pixels wide)
//\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\////\\//


var nLastApplicationWindowWidth = 0;
var nLastApplicationWindowHeight = 0;
var nFullAppMinWidth = 800;
var LastAppSelectionDate;
var bFullSizeApp = true;
var bSmallSizeApp = false;
var bForceSmallApp = false;
var bForceFullApp = false;
var bLockAppSize = false;



/**
 * Allow the server to select a partial view that is most appropriate for the application size
 * Examples:
 *          FullApp.cshtml
 *          SmallApp.cshtml
 * @param {any} element
 */
function AjaxSelectAppSizePartialView() {

    try {
        //console.log("AjaxSelectAppSizePartialView Start " + LastAppSelectionDate);

        // Do not call repeatedly, wait between calls
        if (LastAppSelectionDate) {
            var Now = new Date();
            var millisecondsSinceLastSelection = Now.getTime() - LastAppSelectionDate.getTime();
            if (millisecondsSinceLastSelection < 1000) {
                //console.log("AjaxSelectAppSizePartialView Since: " + millisecondsSinceLastSelection);
                return;
            }
        }
        LastAppSelectionDate = new Date();

        //console.log("AjaxSelectAppSizePartialView Find ApplicationDiv");

        var ApplicationDiv = $("#ApplicationDiv");

        //console.log("AjaxSelectAppSizePartialView ApplicationDiv: " + ApplicationDiv.length);

        // Send the size up to the server
        // Remember these for resize comparison
        nLastApplicationWindowWidth = $(window).width();
        nLastApplicationWindowHeight = $(window).height();

        //console.log("AjaxSelectAppSizePartialView Size: " + nLastApplicationWindowWidth + ", " + nLastApplicationWindowHeight);

        //return;

        // Earliest time to load images
        // The most commonly used images should be cached first
        // Get image data from Ajax, then store in ImageCache global dictionary
        AjaxGetImageCacheList();


        var params = {
            ApplicationWindowWidth: nLastApplicationWindowWidth,
            ApplicationWindowHeight: nLastApplicationWindowHeight,
            FullAppMinWidth: nFullAppMinWidth,
            ForceSmallApp: bForceSmallApp,
            ForceFullApp: bForceFullApp,
        };

        $.ajax({
            url: VirtualDirectory + "/Home/AjaxSelectAppSizePartialView",
            type: "POST",
            data: JSON.stringify(params),
            contentType: "application/json", /*type of data sent to server*/
            error: function (xhr, status, error) {
                var errormsg = "Ajax error " + xhr.status + ", " + xhr.statusText;
                ToastError("AjaxSelectAppSizePartialView", errormsg);
                console.log('AjaxSelectAppSizePartialView error: ' + errormsg);
            }
}).done(function (data) {
            //console.log("AjaxSelectAppSizePartialView Set HTML");
            $(ApplicationDiv).html(data);
        });
    }
    catch (e) {
        console.log("AjaxSelectAppSizePartialView try/catch Error: " + e);
    }
}





/**
 * Check the browser window size, and select a new app size based on width and height
 * @param {any} browserwindow
 */
function CheckSelectNewAppSizeFromWindowSize(browserwindow) {

    if (!browserwindow)
        return;
    if (bLockAppSize)
        return;

    var NewWidth = browserwindow.width();
    var NewHeight = browserwindow.height();
    //var browserZoomLevel = Math.round(window.devicePixelRatio * 100);

    // Check window size
    if (NewWidth >= nFullAppMinWidth) {

        // Large window
        // Check app size mode and resize if necesary
        if (!bFullSizeApp)
            AjaxSelectAppSizePartialView();
    }
    else {

        // Small window
        // Check app size mode and resize if necesary
        if (bFullSizeApp)
            AjaxSelectAppSizePartialView();
    }
}


/**
 * Switch the app size from small to full (or vise-versa)
 * */
function ToggleAppSize() {

    if (bSmallSizeApp) {
        bForceSmallApp = false;
        bForceFullApp = true;
    }
    else {
        bForceSmallApp = true;
        bForceFullApp = false;
    }

    // Switch
    AjaxSelectAppSizePartialView();

    // Reset global vars
    bForceSmallApp = false;
    bForceFullApp = false;
}



$(window).on('resize', function () {

    var browserwindow = $(this); //this = window
    CheckSelectNewAppSizeFromWindowSize(browserwindow);

    // Gantt link lines need rebuilding upon resize, if visible on selected content page
    RebuildGanttLinkLinesFromSelectedContentPane();
});

