/**
 * Created by stanley-king on 2016/10/28.
 */

function init_ios_bridge(callback)
{
    if (window.WebViewJavascriptBridge) {
        return callback(WebViewJavascriptBridge);
    }

    if (window.WVJBCallbacks) {
        return window.WVJBCallbacks.push(callback);
    }

    window.WVJBCallbacks = [callback];
    var WVJBIframe = document.createElement('iframe');
    WVJBIframe.style.display = 'none';
    WVJBIframe.src = 'wvjbscheme://__BRIDGE_LOADED__';
    document.documentElement.appendChild(WVJBIframe);
    setTimeout(function() {
        document.documentElement.removeChild(WVJBIframe);
    }, 0);
}

function init_android_bridge()
{
    if (window.WebViewJavascriptBridge)
    {
        WebViewJavascriptBridge.init();
    }
    else
    {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function() {
                WebViewJavascriptBridge.init();},
            false);
    }
}

function call_native_handler(handler, data,callback)
{
    if (window.WebViewJavascriptBridge)
    {
        window.WebViewJavascriptBridge.callHandler(handler,
            data,
            callback);
    }
    else
    {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function()
            {
                window.WebViewJavascriptBridge.callHandler(
                    handler,
                    data,
                    callback,
                    false);
            }
        );
    }
}
function register_js_hander(name,callback)
{
    if (window.WebViewJavascriptBridge)
    {
        window.WebViewJavascriptBridge.registerHandler(name,
            callback);
    }
    else
    {
        document.addEventListener(
            'WebViewJavascriptBridgeReady'
            , function()
            {
                window.WebViewJavascriptBridge.registerHandler(name,callback);
            }
        );
    }
}

var isMobile = {
    Android: function() {
        return navigator.userAgent.match(/Android/i);
    },
    BlackBerry: function() {
        return navigator.userAgent.match(/BlackBerry/i);
    },
    iOS: function() {
        return navigator.userAgent.match(/iPhone|iPad|iPod/i);
    },
    Opera: function() {
        return navigator.userAgent.match(/Opera Mini/i);
    },
    Windows: function() {
        return navigator.userAgent.match(/IEMobile/i);
    },
    any: function() {
        return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
    }
};
