/* $Id: properties.js,v 1.11.2.1 2005/05/04 19:04:15 mathias Exp $
   Authored by Matt Westgate <drupal at asitis dot org>
*/

/**
 * When the thumbnail checkbox changes, enable/disable certain form elements
 */
function use_thumb(form) {
  for (i=0, n=form['edit[thumb]'].length; i<n; i++) {
    if (form['edit[thumb]'][i].checked) {
      var thumb = form['edit[thumb]'][i].value;
      break;
    }
  }

  if (thumb == 1) {
    form['edit[height]'].disabled = true;
    form['edit[width]'].disabled  = true;
    form['edit-aspect'].disabled = true;
  }
  else {
    form['edit[height]'].disabled = false;
    form['edit[width]'].disabled  = false;
    form['edit-aspect'].disabled = false;
  }
}

/**
 * Called when the 'insert image' button is pressed.
 */
function create_image(form) {
  insert_image(generate_image_tag(form));
}

/**
 * Maintain the width aspect ratio.
 */
function resizew(form, orig_width, orig_height) {
  if (form['edit-aspect'].checked == false) return null;
  form['edit[height]'].value = Math.round(orig_height * form['edit[width]'].value / orig_width);
}

/**
 * Maintain the height aspect ratio.
 */
function resizeh(form, orig_width, orig_height) {
  if (form['edit-aspect'].checked == false) return null;
  form['edit[width]'].value = Math.round(orig_width * form['edit[height]'].value / orig_height);
}

/**
 * Build the HTML image tag.
 */
function generate_image_tag(form) {
  for (i=0, n=form['formatType'].length; i<n; i++) {
    if (form['formatType'][i].checked) {
      //type will be html or filter
      var type = form['formatType'][i].value;
      break;
    }
  }

  var hasInputFormat = parent.hasInputFormat;
  var attr = new Array();
  if (form['edit[filepath]'].value != '') {
    for (i=0, n=form['edit[thumb]'].length; i<n; i++) {
      if (form['edit[thumb]'][i].checked) {
        var thumb = form['edit[thumb]'][i].value;
        break;
      }
    }
    attr.push('fid='+form['edit[fid]'].value);
    attr.push('thumb='+ thumb);
  }

  if (!thumb) {
    if (form['edit[width]'].value  != '' && form['edit[width]'].value != form['edit[origWidth]'].value) attr.push('width='+form['edit[width]'].value);
    if (form['edit[height]'].value != '' && form['edit[height]'].value != form['edit[origHeight]'].value) attr.push('height='+form['edit[height]'].value);
  }
  if (form['edit[alt]'].value      != '')   attr.push('alt='+form['edit[alt]'].value);
  if (form['edit[caption]'].value  != '') {
    //Remove newlines and HTML
    form['edit[caption]'].value = form['edit[caption]'].value.replace(/\n|\t/g, ' ').replace(/ +/g, ' ').replace(/<&#91;^>&#93;*>/g, '');
    attr.push('caption='+form['edit[caption]'].value);
  }
  else {
    form['edit[caption]'].value = '&nbsp;';
  }

  if (type == 'filter') {
    img = '[img_assist|'+ attr.join('|') +']';
  }
  // If there no inputs formats, drop in a simple img tag. That's better than the filter not working at all.
  else {
    if (thumb == 1) {
      var src    = form['edit[thumbpath]'].value;
      var width  = form['edit[thumbWidth]'].value;
      var height = form['edit[thumbHeight]'].value;
    }
    else {
      var src    = form['edit[filepath]'].value;
      var width  = form['edit[width]'].value  != '' ? form['edit[width]'].value  : form['edit[origWidth]'].value;
      var height = form['edit[height]'].value != '' ? form['edit[height]'].value : form['edit[origHeight]'].value;
    }
    var alt = form['edit[alt]'].value;

    tpl = img_template; // Make a copy inherited from parent
    tpl = tpl.replace('%image-class', 'image');
    tpl = tpl.replace('%node-link', form['edit[nodePath]'].value);
    tpl = tpl.replace('%img-link', src);
    tpl = tpl.replace('%src', src);
    tpl = tpl.replace('%width', width);
    tpl = tpl.replace('%height', height);
    tpl = tpl.replace('%alt', alt);
    tpl = tpl.replace('%caption', form['edit[caption]'].value);
    img = tpl;
  }

  return "\n" + img + "\n";
}

/**
 * Image preview window.
 */
function img_preview(form) {
  var win_width  = 400;
  var win_height = 250;
  type = 'html';
  if (parent.hasInputFormat) {
    type = 'filter';
  }
  var snippet = generate_image_tag(form);

  win = window.open('', 'img_preview', 'width='+ win_width +',height='+ win_height +',scrollbars=yes,status=no,resizable=yes');
  win.document.write('<html><head><title>Preview snippet</title>'+base_href+'</head><body><textarea wrap="virtual" cols="45" rows="8">'+ snippet +'</textarea><p align="right">[ <a href="javascript:window.close()">X</a> ]</p></body></html>');
  win.document.close();
}

/**
 * Insert the code snippet into the parent textarea.
 */
function insert_image(snippet) {
  /* Inherit these values from the parent frameset */
  var myForm     = parent.myForm;
  var myTextarea = parent.myTextarea;
  var myDoc      = parent.myDoc;

  /* IE */
  if (myDoc.selection) {
    myTextarea.focus();
    cursor = myDoc.selection.createRange();
    cursor.text = snippet;
    //myForm.insert.focus();
  }
  /* Gecko-based engines: Mozilla, Camino, Firefox, Netscape */
  else if (myTextarea.selectionStart || myTextarea.selectionStart == "0") {
    var startPos  = myTextarea.selectionStart;
    var endPos    = myTextarea.selectionEnd;
    var body      = myTextarea.value;

    myTextarea.value = body.substring(0, startPos) + snippet + body.substring(endPos, body.length);
  }
  /* Worst case scenario: browsers that don't know about cursor position, Safari, OmniWeb, Konqueror */
  else {
    myTextarea.value += snippet;
  }
}
