var AutoUploaderStatic = {
    htmlCurtain: null,
    autoUploadFrame: null
};
var AutoUploader = Class.create({
    initialize: function (B, G) {
        if (!Object.isString(G) || G.blank()) {
            throw "ID not given!"
        }
        this.uploadCompleteBound = this.uploadComplete.bind(this);
        if (AutoUploaderStatic.htmlCurtain === null) {
            var D = new Element("div").hide();
            var A = new Element("div");
            A.setStyle({
                position: "fixed",
                width: "100%",
                height: "100%",
                top: 0,
                left: 0,
                zIndex: 200,
                backgroundColor: "#fff"
            });
            A.setOpacity(0.75);
            D.insert({
                bottom: A
            });
            var C = new Element("div");
            C.setStyle({
                position: "fixed",
                border: "1px solid #000",
                top: "45%",
                left: "40%",
                padding: "10px",
                backgroundColor: "#fff",
                zIndex: 201
            });
            D.insert({
                bottom: C
            });
            var F = new Element("div").update("Laen faili serverisse ...".i18n());
            F.setStyle({
                padding: "10px 10px 10px 46px",
                background: "url(/images/uploading.gif) center left no-repeat"
            });
            C.insert({
                bottom: F
            });
            document.body.insert({
                bottom: D
            });
            AutoUploaderStatic.htmlCurtain = D
        }
        if (AutoUploaderStatic.autoUploadFrame === null) {
            AutoUploaderStatic.autoUploadFrame = new Element("iframe", {
                name: "autoUploadFrame"
            });
            AutoUploaderStatic.autoUploadFrame.setStyle({
                width: 0,
                height: 0,
                position: "absolute",
                border: 0
            });
            AutoUploaderStatic.htmlCurtain.insert({
                after: AutoUploaderStatic.autoUploadFrame
            })
        }
        var E = new Element("div");
        E.innerHTML = '<form action="/autoupload.php" method="post" enctype="multipart/form-data" target="autoUploadFrame"></form>';
        this.wrapper = E.down("form").remove();
        this.input = B;
        this.input.replace(this.wrapper);
        this.wrapper.insert({
            top: this.input
        });
        this.input.writeAttribute("name", "autoupload_file");
        this.wrapper.insert({
            top: new Element("input", {
                type: "hidden",
                name: "autoupload_id",
                value: G
            })
        });
        this.input.observe("change", this.uploadFile.bind(this))
    },
    uploadFile: function () {
        if (AutoUploaderStatic.htmlCurtain.visible()) {
            throw "Can't upload more than one file at a time."
        }
        AutoUploaderStatic.htmlCurtain.show();
        Event.observe(AutoUploaderStatic.autoUploadFrame, "load", this.uploadCompleteBound);
        this.wrapper.submit()
    },
    uploadComplete: function () {
        AutoUploaderStatic.htmlCurtain.hide();
        Event.stopObserving(AutoUploaderStatic.autoUploadFrame, "load", this.uploadCompleteBound);
        var A;
        if (Object.isUndefined(AutoUploaderStatic.autoUploadFrame.contentDocument)) {
            A = AutoUploaderStatic.autoUploadFrame.contentWindow.document.getElementById("resp")
        } else {
            A = AutoUploaderStatic.autoUploadFrame.contentDocument.getElementById("resp")
        }
        if (A !== null) {
            A = A.innerHTML
        }
        if (Object.isString(A) && !A.empty()) {
            alert("Tekkis viga!".i18n() + "\n" + A)
        }
    }
});
