// Copyright (c) 2008 Andris Valums, http://valums.com
// Licensed under the MIT license (http://valums.com/mit-license/)
(function(C) { if (!C) { return } C.ajax_upload = function(E, D) { E = C(E); if (E.size() != 1) { console.error("You passed ", E.size(), " elements to ajax_upload at once"); return false } return new A(E, D) }; var B = function() { var D = 0; return function() { return D++ } } (); var A = function(E, D) { this.button = E; this.wrapper = null; this.form = null; this.input = null; this.iframe = null; this.disabled = false; this.submitting = false; this.settings = { action: "upload.php", name: "userfile", data: {}, onSubmit: function(F, G) { }, onComplete: function(G, F) { }, onSuccess: function(F) { }, onError: function(G, F) { } }; C.extend(this.settings, D); this.create_wrapper(); this.create_input(); if (jQuery.browser.msie) { this.make_parent_opaque() } this.create_iframe() }; A.prototype = { set_data: function(D) { this.settings.data = D }, disable: function() { this.disabled = true; if (!this.submitting) { this.input.attr("disabled", true) } }, enable: function() { this.disabled = false; this.input.attr("disabled", false) }, create_wrapper: function() { var E = this.button, F; F = this.wrapper = C("<div></div>").insertAfter(E).append(E); setTimeout(function() { F.css({ position: "relative", display: "block", overflow: "hidden", height: E.outerHeight(true), width: E.outerWidth(true) }) }, 1); var D = this; F.mousemove(function(G) { if (!D.input) { return } D.input.css({ top: G.pageY - F.offset().top - 5 + "px", left: G.pageX - F.offset().left - 170 + "px" }) }) }, create_input: function() { var D = this; this.input = C('<input type="file" />').attr("name", this.settings.name).css({ position: "absolute", margin: 0, padding: 0, width: "220px", heigth: "10px", opacity: 0 }).change(function() { if (C(this).val() == "") { return } D.submitting = true; D.submit(); D.submitting = false }).appendTo(this.wrapper).hover(function() { D.button.addClass("hover") }, function() { D.button.removeClass("hover") }); if (this.disabled) { this.input.attr("disabled", true) } }, create_iframe: function() { var D = "iframe_au" + B(); this.iframe = C('<iframe name="' + D + '"></iframe>').css("display", "none").appendTo("body") }, submit: function() { var D = this, G = this.settings; var E = this.file_from_path(this.input.val()); if (G.onSubmit.call(this, E, this.get_ext(E)) === false) { if (D.disabled) { this.input.attr("disabled", true) } return } this.create_form(); this.input.appendTo(this.form); this.form.submit(); this.input.remove(); this.input = null; this.form.remove(); this.form = null; this.submitting = false; this.create_input(); var F = this.iframe; F.load(function() { var H = F.contents().find("body").html(); G.onComplete.call(D, E, H); if (H == "success") { G.onSuccess.call(D, E) } else { G.onError.call(D, E, H) } setTimeout(function() { F.remove() }, 1) }); this.create_iframe() }, create_form: function() { this.form = C('<form method="post" enctype="multipart/form-data"></form>').appendTo("body").attr({ action: this.settings.action, target: this.iframe.attr("name") }); for (var D in this.settings.data) { C('<input type="hidden" />').appendTo(this.form).attr({ name: D, value: this.settings.data[D] }) } }, file_from_path: function(E) { var D = E.lastIndexOf("\\"); if (D !== -1) { return E.slice(D + 1) } return E }, get_ext: function(E) { var D = E.lastIndexOf("."); if (D !== -1) { return E.slice(D + 1) } return "" }, make_parent_opaque: function() { this.button.add(this.button.parents()).each(function() { var D = C(this).css("backgroundColor"); var E = C(this).css("backgroundImage"); if (D != "transparent" || E != "none") { C(this).css("opacity", 1); return false } }) } } })(jQuery);

