// JavaScript Document


function addPictureUpload(picture_id, parentDivId, outDivId, innerDivId, outputDivId, onChange, nextValue, nextPicUploadValueEl, maxPicUploadValueEl) {

  var maxValue = 5;
  var maxValue = document.getElementById(maxPicUploadValueEl).value;
 // alert('next value = ' + nextValue + ' and max value = ' + maxValue);
  if(nextValue <= maxValue)
  { 
   
  var parent = document.getElementById(parentDivId);
  
  var temp_upload_form = document.createElement('form'); 
  temp_upload_form.setAttribute('id', 'tempUploadForm' + picture_id + '_' + nextValue); 
  temp_upload_form.setAttribute('action', '/item_pictures/add'); 
 // buttons = document.createElement('input'); 
  //buttons.setAttribute('type', 'submit'); 
  //buttons.setAttribute('value', 'test'); 
  //temp_upload_form.appendChild(buttons);
  
  var outerDiv = document.createElement("div");
  outerDiv.setAttribute("id", outDivId + "_" + nextValue);
  outerDiv.setAttribute("class", "upload");
  
  var innerDiv = document.createElement("div");
  innerDiv.setAttribute("id", innerDivId + "_" + nextValue);
  //innerDiv.setAttribute("class", "upload");
  
  var outputDiv = document.createElement("div");
  outputDiv.setAttribute("id", outputDivId + "_" + nextValue);
  outputDiv.setAttribute("class", "upload");
  
  var inputEl = document.createElement("input");
  inputEl.setAttribute("id", "ItemPicturePicture" + picture_id + "_" + nextValue);
  inputEl.setAttribute("name", "data[ItemPicture][Picture]");
  inputEl.setAttribute("value", "test");
  inputEl.setAttribute("type", "file");  
  onChange = "nextValue = '" + nextValue + "';" + onChange;
  inputEl.setAttribute("onChange", onChange);
  
  var hiddenInputEl = document.createElement("input");
  //hiddenInputEl.setAttribute("visible", "false");
  hiddenInputEl.setAttribute("type", "hidden");
  hiddenInputEl.setAttribute("value", nextValue);
  hiddenInputEl.setAttribute("name", "data[ItemPicture][order]");
  hiddenInputEl.setAttribute("id", "ItemPictureOrder");
  hiddenInputEl.setAttribute("class", "upload");
  
  var hiddenInputEl2 = document.createElement("input");
  //hiddenInputEl2.setAttribute("visible", "false");
  hiddenInputEl2.setAttribute("type", "hidden");
  hiddenInputEl2.setAttribute("value", picture_id);
  hiddenInputEl2.setAttribute("name", "data[ItemPicture][picture_id]");
  hiddenInputEl2.setAttribute("id", "ItemPicturePictureId");
  hiddenInputEl2.setAttribute("class", "upload");
  
  innerDiv.appendChild(inputEl);
  innerDiv.appendChild(hiddenInputEl);
  innerDiv.appendChild(hiddenInputEl2);
  temp_upload_form.appendChild(innerDiv);
  outerDiv.appendChild(outputDiv);
  outerDiv.appendChild(temp_upload_form);
  parent.appendChild(outerDiv);
  
  //update next value
  document.getElementById(nextPicUploadValueEl).value = document.getElementById(nextPicUploadValueEl).value -1 + 2; 
  }
  else {
	  	 return;
  }
}

function delete_pic(update, pic_id, id) {

	new Ajax.Updater(update,'/item_pictures/delete/' + id, {asynchronous:false, evalScripts:true,  onCreate:function(request, xhr) { $(update).innerHTML = '<img class=\'margin:22px;\' src=\'/img/spinner_small.gif\'>';}, onFailure:function(request) {if (403 == request.status) window.location.href="/users/login";}, parameters:{"max_upload":"maxUploadValue" + pic_id}, requestHeaders:['X-Update', update]});


}
