(function(bsk){
    bsk.app = {};
	bsk.webView = {};
    bsk.infinite = {};
    bsk.os = {};
    var ua = navigator.userAgent.toLowerCase(), pf = navigator.platform.toLowerCase();
    bsk.os.isandroid = ua.indexOf('android') > -1 &&  pf.indexOf('linux') > -1;
    bsk.os.isiphone = ua.indexOf('iphone') > -1 &&  pf.indexOf('iphone') > -1;
    bsk.os.isxzapp = ua.match(/appbsk/i) == "appbsk";
    bsk.os.isIos = bsk.os.isiphone && bsk.os.isxzapp;
    bsk.os.isAndroid = bsk.os.isandroid && bsk.os.isxzapp;

    bsk.showIndicator = function(){
        $(document.body).append('<div class="preloader-indicator-overlay"></div><div class="preloader-indicator-modal"><span class="preloader preloader-white"></span></div>');
    }
    bsk.hideIndicator = function () {
        $('.preloader-indicator-overlay, .preloader-indicator-modal').remove();
    };

    bsk.webView.setBridge = function (callback) {
        if (window.WebViewJavascriptBridge) {
            callback(window.WebViewJavascriptBridge);
        } else {
            document.addEventListener('WebViewJavascriptBridgeReady', function() {
                callback(window.WebViewJavascriptBridge);
            }, false);
        }
    }

    bsk.webView.wrapperForDecodeJSONString = function(callback){
        if (bsk.os.isIos) {
            return callback;
        }
        var wrapper = function(responseData) {
            var temp = responseData;
            if (typeof(responseData) == 'string') {
                try {
                    temp = JSON.parse(responseData);
                } catch (e) {
                }
            }
            callback(temp);
        }
        return wrapper;
    }

    bsk.webView.setWebView = function(handlerName, info, callback){
        bsk.webView.setBridge(function(bridge){
            if(bridge){
                if(bsk.init){
                    bsk.init = false;
                    bridge.init();
                }
                typeof(info) == "function" ? callback = info : "";
                var wrapper = typeof(callback) == "function" ? bsk.webView.wrapperForDecodeJSONString(callback) : null;
            
                if(bsk.os.isIos){
                    bridge.send(handlerName, info, wrapper);
                }else if(bsk.os.isAndroid){
                    bridge.callHandler(handlerName, info, wrapper);
                }
            }
        });
    }

    bsk.app.open = function(datas, callback){
        if(!datas && datas.key === undefined) return false;
        datas.info = datas.info || "";
        bsk.webView.setWebView(datas.key, datas.info, callback);
    }
    bsk.app.getUserInfo = function(callback){
        bsk.webView.setWebView("getLoginInfo", callback);
    }
    bsk.app.share = function(shareInfo){
        var info = {
            "title"     : shareInfo.title || "币胜客",
            "msg"       : shareInfo.content || "",
            "imageurl"  : shareInfo.imageurl || "",
            "url"       : shareInfo.url || ""
        }
        bsk.webView.setWebView("showSharePage", info);
    }
    bsk.app.showTips = function(msg){
        var info = {
            msg:msg
        };
        bsk.webView.setWebView("showTip", info);
    }

    bsk.template = function(id, renderData){
        return $.Template7.renderTpl(id, renderData);
    }

    bsk.ajax = function(o, data, callBack, load){
        var url = o.url || o,
            type = o.type || "GET",
            data = o.data || (data || ""),
            dataType = o.dataType || "json",
            async = o.async || true,
            loading = o.load == null ? (load != undefined ? load : true) : o.load,
            contentType = o.contentType || "application/json";
        callBack = o.callBack || callBack;
        url = url.length > 0 ? (url.indexOf("?") != -1 ? url+"&t="+Math.random() : url+"?t="+Math.random()) : "";
        typeof callBack == "undefined" ? callBack = ajaxback : function(o) {};
        if (loading) bsk.showIndicator();
        $.ajax({
            url: _api+url,
            type: type,
            data: data,
            contentType: contentType,
            dataType: this.dataType,
            cache: false,
            async:async,
            success: function(data) {
              callBack(data);
            },
            error: function(xml, status) {
                if (status == "error") {
                    try {
                        var json = eval("(" + xml.responseText + ")");
                        bsk.app.showTips(json.status + "\n" + json.error);
                    } catch (e) {
                    }
                }
            },
            complete: function() {
                if (loading) {
                    bsk.hideIndicator();
                }
            }
        });
    }
})(BSKE);