;(function (window, global) {
    var op = Object.prototype,
        toString = op.toString,
        hasOwn = op.hasOwnProperty,
        slice = [].slice,
        push = [].push;

    function int(str) {
        return parseInt(str, 10);
    }

    function isFunction(it) {
        return toString.call(it) === '[object Function]';
    }

    function isString(it) {
        return typeof it === "string";
    }

    function isArray(it) {
        return toString.call(it) === '[object Array]';
    }

    //可以emunate的对象，但是没有forEach
    //有length property
    function isArrayLike(it) {
        if (!it && (typeof it.length !== 'number')) {
            return false;
        }
        return true;
    }

    //forEach 的 可能是针对函数也可能是一个对象
    //(1)当是函数时要首先排出函数的prototyp, length, name等属性
    //(2)当传入的对象有forEach函数时，就直接调用forEach
    //(3)当传入的为类数组（有length属性，比如arguments）,则通过for循环的方式
    //(4)传入的为一般的对象，通过for in + hasownproperty
    function forEach(obj, iterator, context) {
        var key;
        if (obj) {
            if (isFunction(obj)) {
                for (key in obj) {
                    if (key !== "prototype" && key !== "length" && key !== "name" && obj.hasOwnProperty(key)) {
                        iterator.call(context, obj[key], key);
                    }
                }
            } else if (obj.forEach && obj.forEach !== forEach) {
                iterator.call(context, obj[key], key);
            } else if (isArrayLike(obj)) {
                for (key = 0; i < obj.length; key ++) {
                    iterator.call(context, obj[key], key)
                }
            } else {
                for (key in obj) {
                    if (obj.hasOwnProperty(key)) {
                        iterator.call(context, obj[key], key)
                    }
                }
            }
        }
        return obj;
    }

    function extend(dist) {
        forEach(arguments, function (obj) {
            if (obj !== dist) {
                forEach(obj, function (key, value) {
                    obj[key] = value;
                })
            }
        });

        return dist;
    }

    function inherit(parent, extra) {
        return extend(new (extend(function () {}, {prototype: parent}))(), extra)
    }

    //去除掉路径中的点
    function trimDots(arr) {
        var i;
        var len = arr.length;
        for (i = 0; i < len; i++) {
            var part = arr[i];
            if (part === ".") {
                arr.splice(i, 1);
                i -= 1;
            }
        }
    }
    //将传入的路径进行normalize
    //将url转换成绝对地址
    //将文件相对路径转换成绝对路径
    function normalize(name, baseName) {
        var baseParts = baseName && baseName.split("/");
        var normalizedBaseParts = baseParts;

        //adjust any relative path
        if (name && name.charAt(0) === ".") {
            if (baseName) {
                name = name.split("/");
                var lastIndex = name[name.length - 1]
                if (lastIndex.indexOf(".js") < 0) {
                    name.push(".js")
                }
                name = normalizedBaseParts.concat(name);
                name = trimDots(name)
            }
        }
    }

    function noop() {}
    var ajaxBaseConfig = {
        type: "GET",
        beforeSend: noop,
        success: noop,
        error: noop,
        complete: noop,
        xhr: function () {
            return new XMLHTTPRequest();
        }
    };

    function ajaxJsonp(config) {
        var temptScript = document.createElement("script");
        var jsonPTimeStamp = (+ new Date());
        var jsonPCallbackName = "jsonp" + jsonPTimeStamp;
        if (config.url.indexOf("?") >= 0) {
            config.url += "&jsonp=" + jsonPCallbackName;
        } else {
            config.url += "?jsonp=" + jsonPCallbackName;
        }

        temptScript.src = config.url;
        window[jsonPCallbackName] = config.callback;
        var bodyTag = document.getElementsByName("body")[0];
        bodyTag.appendChild(temptScript);
    }

    function ajax(config) {
        var cfg = extend(ajaxBaseConfig, config);
        xhr.onreadystatechange = function () {
            if (xhr.state == 4) {
                if ((xhr.status >= 200 && xhr.status < 300) || xhr.status == 304) {
                    var result = xhr.responseText;
                    cfg.callback.call(cfg.context, result);
                }
            }
        }
        xhr.open(config.type, config.url, false);
        xhr.send();
    }

    global = global || {};
    global.connontStr = "http://zhushou.huihui.cn/productSense?onlydata&browser=chrome&version=3.5&vendor=chrome&av=3.0&extensionid=7b043323-7fcd-b182-e68f-52a91e639ec6&email=&pop=&k=00cc00954f044e747fa94e62559e582600857f5454607fa98d8599ee9061ff6000a2009c0086009b00a700a5ff6100856bbb55194fa64f4f305955198d80503596f430598d7f52884f306b96305991a590595422664e30596596601b676552f930598fd367d68d8572c1ff5900b6007e00c3009500c400d000cb00d000b6007e00bc009500c400cb&m=e9a8d75c7cbb68cb2c68fcfcfce9a8d7e9a8d799b8d78ccccc0c&t=1392302764646";
    global.baseName = "http://zhushou.huihui.cn/";
    //关于db的API说明
    // dbInfo = db(function () { console.log("init database")})
    // dbInfo = db("api/pricehub/detail?email=85770253&uid=89076", function () {console.log("filter")})
    // dbInfo = db("quan", "./filter")
    // dbInfo = db("dataSource/data/quan")
    global.dbOp = function (dataSource, filter) {
        var config = {};
        if (isFunction(dataSource)) {
            config.callback = dataSource;
            config.url = global.connontStr;
            ajaxJsonp(config);
            return;
        }

        var propertyList = dataSource.split("/");
        var i;

        var db = global.db;
        for (i = 0; i < propertyList.length; i++) {
            if (db[propertyList[i]]) {
                db =  db[db[propertyList[i]]];
            } else {
                return false;
            }
        }

        if (filter) {
            if (isString(filter)) {
                config.callback = require(filter);
            }

            if (isFunction(filter)) {
                config.callback = filter;
            }
            return config.callback.call(null, db);
        } else {
            return db;
        }
    };
}(window, youdao))
