<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%~set page_mode = $in.editor_type%><%if not page_mode or page_mode ne message%><%set page_mode = 'post'%><%endif%>
<html>
<head>
  <title>Insert Image</title>
  <link type="text/css" rel="stylesheet" media="screen" href="<%static_url%>/editor/editor_dialog.css">
  <script type="text/javascript" src="<%static_url%>/js/utils.js"></script>
  <script type="text/javascript" src="<%static_url%>/editor/editor_dialog.js"></script>
  <script type="text/javascript">
//<![CDATA[
var src = {}, activeSource, sourceTypes = ['url', 'file', 'inline'];
var inlineImages = [];
var inlines = opener.getInlineAttachments();
<%~if attachment_uploaded%>
inlines.push(['<%escape_js attachment_uploaded%>', 'temp', '<%attachment_uploaded_id%>']);
<%~endif%>

function checkSource(submitting) {
    if (activeSource && src[activeSource + '-input'] && src[activeSource + '-input'].value && src[activeSource + '-input'].value.match(/\S/)) {
        submit.disabled = false;
        if (submitting) {
            if (activeSource == 'file')
                return true;
            else
                submitWrapper();
        }
    }
    else
        submit.disabled = true;

    return false;
}

function changeElement() {
    activeSource = false;
    for (var i = 0; i < sourceTypes.length; i++) {
        if (src[sourceTypes[i]] && src[sourceTypes[i]].checked) {
            activeSource = sourceTypes[i];
            break;
        }
    }
    if (!activeSource) return;

    for (var i = 0; i < sourceTypes.length; i++) {
        if (src[sourceTypes[i]])
            src[sourceTypes[i] + '-row'].style.display = activeSource == sourceTypes[i] ? 'block' : 'none';
    }
}

function init() {
  <%~if attachment_uploaded%>
    // The upload succeeded: make the parent page refresh the attachment list
    if (opener.refreshAttachmentList)
      opener.refreshAttachmentList();
  <%~endif%>

    // We need to copy the temp_id from the parent document.
    document.getElementById('temp_id').value = opener.document.getElementById('temp_id').value;

    // Also copy over forum_id, parent_post_id, and post_id, if they exist.
    var hidden = [opener.document.getElementById('forum_id'), opener.document.getElementById('parent_post_id'), opener.document.getElementById('post_id')];
    for (var i = 0; i < hidden.length; i++) {
        if (hidden[i]) {
            var h = document.createElement('input');
            h.type = 'hidden';
            h.name = hidden[i].name;
            h.value = hidden[i].value;
            document.getElementById('image_form').appendChild(h);
        }
    }

    var eventType = isIE ? 'propertychange' : 'change';
    for (var i = 0; i < sourceTypes.length; i++) {
        var sourceType = sourceTypes[i];
        src[sourceType] = document.getElementById('src-' + sourceType);
        src[sourceType + '-input'] = document.getElementById('src-' + sourceType + '-input');
        src[sourceType + '-label'] = document.getElementById('src-' + sourceType + '-label');
        src[sourceType + '-value'] = document.getElementById('src-' + sourceType + '-value');
        src[sourceType + '-row'] = document.getElementById('src-' + sourceType + '-row');
        if (src[sourceType]) registerEvent(src[sourceType], eventType, changeElement);
    }

    if (inlines.length > 0) {
        for (i = 0; i < inlines.length; i++) {

            var filename = inlines[i][0];
            var inlineRadio;
            // Hack around a weird IE bug where setting the name attribute
            // below results in the radio button not showing its state properly
            try {
                inlineRadio = document.createElement('<input type="radio" name="inline_image" />');
            }
            catch (e) {
                inlineRadio = document.createElement('input');
            }
            inlineRadio.type = 'radio';
            inlineRadio.name = 'inline_image';
            inlineRadio.id = 'inline_image' + i;
            inlineRadio.value = filename;
          <%~if attachment_uploaded%>
            if (filename == '<%escape_js attachment_uploaded%>') {
                inlineRadio.checked = true;
                src['inline-input'].value = filename;
            }
          <%~endif%>
            var inlineLabel = document.createElement('label');
            inlineLabel.appendChild(document.createTextNode(filename));
            inlineLabel.htmlFor = 'inline_image' + i;
            var box = document.createElement('span');
            box.appendChild(inlineRadio);
            box.appendChild(inlineLabel);
            src['inline-value'].appendChild(box);
            inlineImages[inlineImages.length] = inlineRadio;
            registerEvent(inlineRadio, 'click', function(radioBox) { return function () { src['inline-input'].value = radioBox.value; }; }(inlineRadio));
        }
    }
    else {
        src['inline'].parentNode.removeChild(src['inline']);
        src['inline-label'].parentNode.removeChild(src['inline-label']);
        src['inline-row'].parentNode.removeChild(src['inline-row']);
        delete src['inline'];
        delete src['inline-label'];
        delete src['inline-row'];
    }

    if (!opener.canAttach) {
        src['file'].parentNode.removeChild(src['file']);
        src['file-label'].parentNode.removeChild(src['file-label']);
        src['file-row'].parentNode.removeChild(src['file-row']);
        delete src['file'];
        delete src['file-label'];
        delete src['file-row'];
    }

    submit = document.getElementById('submit');

    changeElement();

    setInterval(function () { checkSource() }, 250);
    if (src['url'] && src['url'].checked) document.getElementById('src-url-input').focus();

  <%~if error_loop.length%>
    alert('Error:\n<%loop error_loop%><%escape_js loop_value%><%unless last%>\n<%endunless%><%endloop%>');
  <%~elsif attachment_uploaded%>
    if (true || confirm("submit form?")) submitWrapper();
  <%~endif%>
}

function submitWrapper () {
    if (activeSource == 'inline') {
        var att_type = 'temp', att_id = 0;
        for (var i = 0; i < inlines.length; i++) {
            if (inlines[i][0] == src['inline-input'].value) {
                att_type = inlines[i][1];
                att_id = inlines[i][2];
                break;
            }
        }
        var url = '<%set url = GForum::SEO::url(params => "do=${page_mode}_attachment")%><%escape_js url%>;' + att_type + 'att_id=' + att_id;
        if (att_type == 'temp')
            url += ';<%if page_mode eq "post"%>post_unique<%else%>temp_id<%endif%>=' + document.getElementById('temp_id').value;
        src['url-input'].value = url;
    }

    submitForm();
}

registerEvent(window, 'load', init);
//]]>
  </script>
</head>
<body id="editor_image">
  <form id="image_form" action="<%GForum::SEO::url()%>" method="post" enctype="multipart/form-data" onsubmit="return checkSource(true)">
  <%hidden_form%>
  <input type="hidden" name="do" value="<%page_mode%>_attachment_upload" />
  <input type="hidden" id="temp_id" name="temp_id" value="" /><%-- populated from parent window by init() --%>
  <input type="hidden" name="redo" value="<%this_do%>" />
  <div id="form">
    <div class="row">
      <label class="name">Insert Image from:</label>
      <input type="radio" id="src-url" name="src-type" class="radio"<%unless attachment_uploaded or error_loop.length%> checked<%endunless%>><label id="src-url-label" for="src-url" accesskey="u"><u>U</u>RL</label>
    <%~unless attachment_uploaded%>
      <input type="radio" id="src-file" name="src-type" class="radio"<%if error_loop.length%> checked<%endif%>><label id="src-file-label" for="src-file" accesskey="f"><u>F</u>ile</label>
    <%~endunless%>
      <input type="radio" id="src-inline" name="src-type" class="radio"<%if attachment_uploaded%> checked<%endunless%>><label id="src-inline-label" for="src-inline" accesskey="i"><u>I</u>nline Attachment</label>
    </div>
    <div id="src-url-row" class="row">
      <label for="src-url-input" class="name" accesskey="s">Image <u>S</u>ource:</label>
      <input type="text" id="src-url-input" value="" class="text">
    </div>
  <%~unless attachment_uploaded%>
    <div id="src-file-row" class="row">
      <label for="src-file-input" class="name" accesskey="m">I<u>m</u>age Upload:</label>
      <input type="file" accept="image/*" id="src-file-input" name="<%if page_mode eq message%>msg<%else%>post<%endif%>_attachment" class="file">
      <input type="hidden" name="<%if page_mode eq message%>message<%else%>post<%endif%>_attachment_inline" value="1">
    </div>
  <%~endunless%>
    <div id="src-inline-row" class="row">
      <input type="hidden" id="src-inline-input" value="">
      <label class="name">I<u>n</u>line Attachment:</label>
      <div id="src-inline-value" class="value"><%-- populated by init() --%></div>
    </div>
  </div>
  <div class="buttons">
    <input type="submit" id="submit" value="OK" class="submit">
    <input type="button" value="Cancel" class="button" onclick="window.close()">
  </div>
  </form>
  <div id="output-box"></div>
</body>

</html>
