// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// public/javascripts/application.js

$(document).ready(function() {
  $("#button").bind("click", function() {
    /* Generating unique id
    */
    var rand = Math.random().toString().split(".")[1];
    var input = '<input type="file" id="upload_field_'+rand+'" class="images['+rand+']" name="images[]" />'
    $(this).before(input + '<br/>');
  });

  /* Pushing the first input to the DOM
  */
  $("#button").trigger("click");

  /* Enabling Ajax form on #upload_form
  */
  $("#upload_form").ajaxForm();

  $("#upload_form").submit(function() {
    $(this).ajaxSubmit({
      dataType  : 'html',
      iframe      : true,
      success     : function (data) {
        data = eval(data.replace("<pre>", "").replace("</pre>", ""));
      }
    });
    return false;
  });
});

