// The examples are licensed “as-is.” Company bears the risk of using it. 
// Microsoft gives no express warranties, guarantees or conditions. 
// Company may have additional consumer rights under local laws which 
// this license cannot change. To the extent permitted by local laws, 
// Microsoft excludes the implied warranties of merchantability, fitness 
// for a particular purpose and non-infringement. Your use of any code 
// provided in this document (“code”) is subject to the Microsoft Limited 
// Public License (the “license”). If you use the code, you accept the license.

var pinnedSiteDetection = new (function () {

    // ***CUSTOMIZE***
    var trackerName = "pinSiteLastAccess"; 	// specify the name of the pin site actvity tracker 

    // -- Logic to measure the level of pinning support by your OS
    this.supportLevel = { none: 0, upgradeable: 1, limited: 2, full: 3 };
    this.getOSPinSupportLevel = function () {
        try {
            var userAgent = navigator.userAgent;

            if (userAgent.indexOf("Windows NT 6.1") !== -1)
                return this.supportLevel.full;
            else if (userAgent.indexOf("Windows NT 6.0") !== -1)
                return this.supportLevel.limited;
            else if (userAgent.indexOf("Windows NT") !== -1)
                return this.supportLevel.upgradeable;
            else
                return this.supportLevel.none;
        } catch (exc) {
            return this.supportLevel.none;
        }
    };

    // -- Logic to measure the level of pin-ability by your Browser
    this.abilityLevel = { none: 0, limited: 1, full: 2 };
    this.getBrowserPinAbilityLevel = function () {
        try {
            var appVersion = navigator.appVersion;

            if (this.browserCanPin) {
                if (appVersion.indexOf("Windows NT 6.0") !== -1)
                    return this.abilityLevel.limited;
                else
                    return this.abilityLevel.full;
            }
            else
                return this.abilityLevel.none;
        } catch (exc) {
            return this.abilityLevel.none;
        }
    };

    // -- Logic to help you track your user's pin activity
    this.getDaysSincePinAccess = function () {
        if (this.browserCanPin) {
            if ('localStorage' in window && window['localStorage'] !== null) {
                var daysSince = -1;
                try {
                    var storage = window.localStorage;

                    if (storage[trackerName] != undefined) {
                        var lastDate = new Date(storage[trackerName]);

                        var daysSince = Math.round(((new Date()).getTime() - lastDate.getTime()) / 86400000);
                        if (isNaN(daysSince)) {
                            daysSince = -1;
                        }
                    }
                } catch (ex) {
                    daysSince = -1;
                }
                return daysSince;
            }
        } else {
            return -1;
        }
    };

    // we stablish the last time the user access the site in pinned mode
    this.setSiteHasBeenPinnedFlag = function () {
        try {
            if ('localStorage' in window && window['localStorage'] !== null && this.browserCanPin) {
                window.localStorage[trackerName] = new Date();
                return true;
            }
            else {
                return false;
            }
        } catch (ex) {
            return false;
        }
    }

    // if user has been accessing your site in normal mode, you should probably assume 
    // site is no longer pinned. It will always return true, even if it wasn't pinned before
    this.removeSiteHasBeenPinnedFlag = function () {
        try {
            if ('localStorage' in window && window['localStorage'] !== null && this.browserCanPin) {
                window.localStorage.removeItem(trackerName);
                return true;
            } else {
                return true;
            }
        } catch (ex) {
            return false;
        }
    }

    // -- all the "simplified" T/F logic
    // -- all built on the above more complex pieces of logic
    this.browserCanPin = ('external' in window && 'msIsSiteMode' in window.external);
    this.viewingFromPin = ('external' in window && 'msIsSiteMode' in window.external && window.external.msIsSiteMode());
    this.siteHasBeenPinned = (this.getDaysSincePinAccess() > -1);

    this.hasFullPinAbilityBrowser = (this.getBrowserPinAbilityLevel() === this.abilityLevel.full);
    this.hasLimitedPinAbilityBrowser = (this.getBrowserPinAbilityLevel() === this.abilityLevel.limited);
    this.hasNoPinAbilityBrowser = (this.getBrowserPinAbilityLevel() === this.abilityLevel.none);

    this.hasFullPinSupportOS = (this.getOSPinSupportLevel() === this.supportLevel.full);
    this.hasLimitedPinSupportOS = (this.getOSPinSupportLevel() === this.supportLevel.limited);
    this.hasUpgradeablePinSupportOS = (this.getOSPinSupportLevel() === this.supportLevel.upgradeable);
    this.hasNoPinSupportOS = (this.getOSPinSupportLevel() == this.supportLevel.none);

});




