/**
 * Created by Benny on 24.04.2015.
 */

"use strict";

var TB = TB || {}

// Environment
TB.Env =  {

    platform : 'WEB',
    deviceId : null,

    setPlatform : function( platform ) {
        var newPlatform  = platform || 'WEB';
        this.platform = newPlatform.toUpperCase();

        // Perform device specific tasks

        // IOS
        if ( "IOS" == this.platform ) {

            // Open external link in native browser
            $(document).on( "click", 'a[target="_blank"]', function(e) {
                var url = $(this).attr('href');
                window.open(url, '_system');
                e.preventDefault();
            });

        }
    },

    isNativeMobile : function()
    {
        return ( window.top != window.self || TB.Env.platform != 'WEB' );
    },

    isWebMobile : function()
    {
        return ( TB.Env.isWebAndroid() || TB.Env.isWebIOS() );
    }
    ,

    isMobile : function()
    {
        return ( TB.Env.isNativeMobile() || TB.Env.isWebAndroid() || TB.Env.isWebIOS() );
    },

    /**
     * Android using normal web / browser version
     */
    isWebAndroid : function() {
        var ua = navigator.userAgent.toLowerCase();
        return ( 'WEB' == TB.Env.platform && ua.indexOf("android") > -1  );
    },

    /**
     * iOS using normal web / browser version
     */
    isWebIOS : function() {
        // For use within normal web clients
        var isIpad = navigator.userAgent.match(/iPad/i) != null;
        var isIphone = (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i));

        return ( 'WEB' == TB.Env.platform && ( isIpad || isIphone ) );
    }
};

