jQuery.extend({
    handleError: function(s, xhr, status, e) {
        if (s.error) {
            s.error.call(s.context || s, xhr, status, e);
        }
        if (s.global) { (s.context ? jQuery(s.context) : jQuery.event).trigger("ajaxError", [xhr, s, e]);
        }
    }
});
jQuery.extend({
    createUploadIframe: function(a, b) {
        var d, c = "jUploadFrame" + a;
        return window.ActiveXObject ? (d = document.createElement('<iframe id="' + c + '" name="' + c + '" />'), "boolean" == typeof b ? d.src = "javascript:false": "string" == typeof b && (d.src = b)) : (d = document.createElement("iframe"), d.id = c, d.name = c),
            d.style.position = "absolute",
            d.style.top = "-1000px",
            d.style.left = "-1000px",
            document.body.appendChild(d),
            d
    },
    createUploadForm: function(a, b) {
        // form的id
        var c = "jUploadForm" + a,
            // file的id
            d = "jUploadFile" + a,
            e = $('<form  action="" method="POST" name="' + c + '" id="' + c + '" enctype="multipart/form-data"></form>');
//        return $(f).attr("id", d),
//        $(f).before(g),
//        $(f).appendTo(e),
        if(typeof(b) == 'string') {
            b = [b];
        };
        for (var i in b) {
            f = $("#" + b[i]);
            g = $(f).clone(true);
            $(f).attr("id", b[i]);
            $(f).before(g);
            $(f).appendTo(e);

        }
        $(e).css("position", "absolute");
        $(e).css("top", "-1200px");
        $(e).css("left", "-1200px");
        $(e).appendTo("body");
        return e
    },
    addOtherRequestsToForm: function(a, b) {
        var d, e, c = $('<input type="hidden" name="" value="">');
        for (d in b) name = d,
            value = b[d],
            e = c.clone(),
            e.attr({
                name: name,
                value: value
            }),
            $(e).appendTo(a);
        return a
    },
    ajaxFileUpload: function(a) {
        var b, c, e, f, g, h, i;
        a = jQuery.extend({},
            jQuery.ajaxSettings, a),
            b = (new Date).getTime(),
            c = jQuery.createUploadForm(b, a.fileElementId),
        a.data && (c = jQuery.addOtherRequestsToForm(c, a.data)),
            jQuery.createUploadIframe(b, a.secureuri),
            e = "jUploadFrame" + b,
            f = "jUploadForm" + b,
        a.global && !jQuery.active++&&jQuery.event.trigger("ajaxStart"),
            g = !1,
            h = {},
        a.global && jQuery.event.trigger("ajaxSend", [h, a]),
            i = function(b) {
                var i, j, d = document.getElementById(e);
                try {
                    d.contentWindow ? (h.responseText = d.contentWindow.document.body ? d.contentWindow.document.body.innerHTML: null, h.responseXML = d.contentWindow.document.XMLDocument ? d.contentWindow.document.XMLDocument: d.contentWindow.document) : d.contentDocument && (h.responseText = d.contentDocument.document.body ? d.contentDocument.document.body.innerHTML: null, h.responseXML = d.contentDocument.document.XMLDocument ? d.contentDocument.document.XMLDocument: d.contentDocument.document)
                } catch(f) {
                    jQuery.handleError(a, h, null, f)
                }
                if (h || "timeout" == b) {
                    g = !0;
                    try {
                        i = "timeout" != b ? "success": "error",
                            "error" != i ? (j = jQuery.uploadHttpData(h, a.dataType), a.success && a.success(j, i), a.global && jQuery.event.trigger("ajaxSuccess", [h, a])) : jQuery.handleError(a, h, i)
                    } catch(f) {
                        i = "error",
                            jQuery.handleError(a, h, i, f)
                    }
                    a.global && jQuery.event.trigger("ajaxComplete", [h, a]),
                    a.global && !--jQuery.active && jQuery.event.trigger("ajaxStop"),
                    a.complete && a.complete(h, i),
                        jQuery(d).unbind(),
                        setTimeout(function() {
                                try {
                                    $(d).remove(),
                                        $(c).remove()
                                } catch(b) {
                                    jQuery.handleError(a, h, null, b)
                                }
                            },
                            100),
                        h = null
                }
            },
        a.timeout > 0 && setTimeout(function() {
                g || i("timeout")
            },
            a.timeout);
        try {
            c = $("#" + f),
                $(c).attr("action", a.url),
                $(c).attr("method", "POST"),
                $(c).attr("target", e),
                c.encoding ? c.encoding = "multipart/form-data": c.enctype = "multipart/form-data",
                $(c).submit()
        } catch(j) {
            jQuery.handleError(a, h, null, j)
        }
        return window.attachEvent ? document.getElementById(e).attachEvent("onload", i) : document.getElementById(e).addEventListener("load", i, !1),
        {
            abort: function() {}
        }
    },
    uploadHttpData: function(r, type) {
        var rx, am, data = !type;
        return data = "xml" == type || data ? r.responseXML: r.responseText,
        "script" == type && jQuery.globalEval(data),
        "json" == type && (data = r.responseText, rx = new RegExp("<pre.*?>(.*?)</pre>", "i"), am = rx.exec(data), data = am ? am[1] : "", eval("data = " + data)),
        "html" == type && jQuery("<div>").html(data).evalScripts(),
            data
    }
});