/** MultiFile
 * Convert a single file-input element into a 'multiple' input list
 *
 * Usage:
 *   1. Create a file input element (no name)
 *      eg. <input type="file" id="first_file_element">
 *   2. Create a DIV for the output to be written to
 *      eg. <div id="files_list"></div>
 *   3. Instantiate a MultiSelector object, passing in the DIV and an (optional) maximum number of files
 *      eg. var multi_selector = new MultiSelector( document.getElementById( 'files_list' ), 3 );
 *   4. Add the first element
 *      eg. multi_selector.addElement( document.getElementById( 'first_file_element' ) );
 *
 *   You might (will) want to play around with the addListRow() method to make the output prettier.
 *
 * Credit:
 *   Class by Stickman -- http://www.the-stickman.com
 *      with thanks to:
 *      [for Safari fixes]
 *         Luis Torrefranca -- http://www.law.pitt.edu
 *         and
 *         Shawn Parker & John Pennypacker -- http://www.fuzzycoconut.com
 *      [for duplicate name bug]
 *         'neal'
 */
function MultiSelector( list_target, max ){
  this.list_target = list_target;
  this.count = 0;
  this.id = 0;
  if( max ){
    this.max = max;
  } else {
    this.max = -1;
  };
  this.addElement = function( element ){
    if( element.tagName == 'INPUT' && element.type == 'file' ){
      element.name = 'file_' + this.id++;
      element.multi_selector = this;
      element.onchange = function(){
        var new_element = document.createElement( 'input' );
        new_element.type = 'file';
        new_element.className = element.className;
        this.parentNode.insertBefore( new_element, this );
        this.multi_selector.addElement( new_element );
        this.multi_selector.addListRow( this );
        // Hide this: we can't use display:none because Safari doesn't like it
        //this.style.display = 'none';
        this.style.position = 'absolute';
        this.style.left = '-1000px';
      };
      if( this.max != -1 && this.count >= this.max ){
        element.disabled = true;
      };
      this.count++;
      this.current_element = element;
    } else {
      // This can only be applied to file input elements!
      alert( 'Error: not a file input element' );
    };
  };
  this.addListRow = function( element ){
    var new_row = document.createElement( 'li' );
    // Delete button
/*
    var new_row_button = document.createElement( 'input' );
    new_row_button.type = 'button';
    new_row_button.value = 'Delete';
    new_row_button.className = 'delete_element';
*/
    var new_row_button = document.createElement( 'img' );
    new_row_button.src = '/img/static/icons/trashcan.png';
    new_row_button.className = 'delete_element';
    new_row_button.title = 'Delete photo';

    new_row.element = element;
    new_row_button.onclick= function(){
      if (!confirm("Are you sure you wish to remove this file?"))
        return;
      this.parentNode.element.parentNode.removeChild( this.parentNode.element );
      this.parentNode.parentNode.removeChild( this.parentNode );
      this.parentNode.element.multi_selector.count--;
      this.parentNode.element.multi_selector.current_element.disabled = false;
      // Appease Safari
      //    without it Safari wants to reload the browser window
      //    which nixes your already queued uploads
      return false;
    };
    new_row.appendChild( new_row_button );
    // Set row value
//    new_row.innerHTML = element.value;
/* Firefox doesn't allow local links
    var image = new Image();
    image.style.width = "100px";
    image.src = 'file://'+element.value;
    image.className = 'image_element';
    new_row.appendChild ( image );
*/
    var span = document.createElement( 'span' );
    span.className = 'file_element';
    var file = document.createTextNode( element.value );
    span.appendChild(file);
    new_row.appendChild(span);
    this.list_target.appendChild( new_row );
  };
};
