/**
 * Author: Olexandr Tarasiuk
 * Date: 14.04.2009
 * Version: 1.0.0
 *
 * @param onDone function(image[id,src,width,height], thumbnail[id,src,width,height])
 */
function UploadImageDialog(hiddenId, onDone) {
    UploadImageDialog.CURRENT_DIALOG = this;
    this.id = UploadImageDialog.COUNTER++;
    this.hiddenId = hiddenId;
    this.onDone = onDone;
    this.div = document.createElement("div");
    this.div.id = "net_integrio_scripting_ajax_UploadImageDialog_div_" + this.id;
    this.div.style.position = "absolute";
    this.div.style.top = "1";
    this.div.style.left = "1";
    this.div.style.width = "1px";
    this.div.style.height = "1px";
    document.body.appendChild(this.div);
    this.div.innerHTML = "<iframe style=\"display:none\" name=\"net_integrio_scripting_ajax_UploadImageDialog_iframe_" + this.id + "\"></iframe>";
    var dialog;
    this.show = function() {
        var uploadHtml =
                "<input type=\"file\" name=\"file\" onchange=\"this.form.doupload.disabled=this.value==''\"/>" +
                "<input type=\"button\" name=\"doupload\" value=\"Загрузить\" onclick=\"UploadImageDialog.CURRENT_DIALOG.submit(this.form)\" disabled=\"true\"/>";
        dialog = new Dialog(Dialog.TYPE_CUSTOM, "Загрузка картинки", uploadHtml, null, "Отмена");
        dialog.form.action = "/uploadimage.jsp";
        dialog.form.method = "post";
        dialog.form.target = "net_integrio_scripting_ajax_UploadImageDialog_iframe_" + this.id;
        dialog.form.enctype = "multipart/form-data";
        dialog.show();
    };
    this.submit = function(form) {
        form.submit();
    };
    this.commit = function(image, thumbnail) {
        document.getElementById(this.hiddenId).value = image.id;
        if (this.onDone)
            this.onDone(image, thumbnail);
        document.body.removeChild(this.div);
        dialog.close();
        UploadImageDialog.CURRENT_DIALOG = null;
    };
}

UploadImageDialog.COUNTER = 1;
UploadImageDialog.CURRENT_DIALOG = null;