// ########## FILE NEW ##########

function fileNew(id,modId,pageId) {
  
  // FORM VALUES
  var media_filename = document.getElementById('media_filename').value;
  document.getElementById('media_file').value = '';
  var media_label = document.getElementById('media_label').value;
  document.getElementById('media_label').value = '';
  var media_description = document.getElementById('media_description').value;
  document.getElementById('media_description').value = '';
  if(document.getElementById('media_visible').checked) {
    var media_visible = 1;
  }
  document.getElementById('media_visible').setAttribute('checked','checked');
  
  var parent = window.parent.document.getElementById('file_manager_'+modId);
  var node = window.parent.document.getElementById('file_new_'+modId);
  
  // <tr id="file_{id}" class="dnd">
  var tr = window.parent.document.createElement('tr');
  tr.setAttribute('id','file_'+id);
  tr.setAttribute('class','dnd');
  tr.setAttribute('className','dnd');
  // <td>
  var td_label = window.parent.document.createElement('td');
  // <input type="hidden" id="media_edit{id}" name="media_edit[{modId}][]" value="file" />
    var media_edit = window.parent.document.createElement('input');
    media_edit.setAttribute('type','hidden');
    media_edit.setAttribute('id','media_edit'+id);
    media_edit.setAttribute('name','media_edit['+modId+'][]');
    media_edit.setAttribute('value','file');
    td_label.appendChild(media_edit);
    
    // <input type="hidden" id="media_id{id}" name="media_id[{modId}][]" value="{id}" />
    var media_id = window.parent.document.createElement('input');
    media_id.setAttribute('type','hidden');
    media_id.setAttribute('id','media_id'+id);
    media_id.setAttribute('name','media_id['+modId+'][]');
    media_id.setAttribute('value',id);
    td_label.appendChild(media_id);
    
    //<img alt="Fichier {file_ext}" src="{path}images/{file_ext}.gif"/>
    var ico = window.parent.document.createElement('img');
      ico.setAttribute('alt',lang_file +' '+file_ext);
      ico.setAttribute('src',path+'images/'+file_ext+'.gif');
    td_label.appendChild(ico);
    
    // <input type="text" id="media_label{id}" name="media_label[{modId}][{id}]" value="{media_label}" />
    var input_label = window.parent.document.createElement('input');
    input_label.setAttribute('type','text');
    input_label.setAttribute('id','media_label'+id);
    input_label.setAttribute('name','media_label['+modId+']['+id+']');
    input_label.setAttribute('value',media_label);
    
    td_label.appendChild(input_label);
    
    tr.appendChild(td_label);
  var td_description = window.parent.document.createElement('td');
    // <input id="media_description{id}" name="media_description[{modId}][{id}]" value="{media_description}" />
    var input_description = window.parent.document.createElement('input');
    input_description.setAttribute('id','media_description'+id);
    input_description.setAttribute('name','media_description['+modId+']['+id+']');
    input_description.setAttribute('value',media_description);
    td_description.appendChild(input_description);
    tr.appendChild(td_description);
      
  var td_visible = window.parent.document.createElement('td');
      // <input type="checkbox" id="media_visible{id}" name="media_visible[{modId}][{id}]" value="1" checked = "checked" />
      var input_visible = window.parent.document.createElement('input');
      input_visible.setAttribute('type','checkbox');
      input_visible.setAttribute('id','media_visible'+id);
      input_visible.setAttribute('name','media_visible['+modId+']['+id+']');
      input_visible.setAttribute('value',1);
      if(media_visible) {
        input_visible.setAttribute('checked','checked');
      }
      td_visible.appendChild(input_visible);
    
    tr.appendChild(td_visible);
  
  var td_download = window.parent.document.createElement('td');
  // <a class="download" href="{path}files/file_manager_files/{filename}.{file_ext}">{lang_download} {label}</a>
    var a_download = window.parent.document.createElement('a');
    a_download.setAttribute('class','download');
    a_download.setAttribute('className','download');
    a_download.setAttribute('title',lang_download);
    a_download.setAttribute('href',path+'files/file_manager_files/'+filename);
      var a_download_txt = window.parent.document.createTextNode(lang_download);
      a_download.appendChild(a_download_txt);
    td_download.appendChild(a_download);
  
    tr.appendChild(td_download);
  
  var td_delete = window.parent.document.createElement('td');
    // <a class="delete" href="javascript:fileDelete({lang_mess},{id},{modId});" title="{lang_link}">{lang_link}</a>
    var a = window.parent.document.createElement('a');
    a.setAttribute('class','delete');
    a.setAttribute('className','delete');
    a.setAttribute('href','javascript:fileDelete("'+lang_mess+'",'+id+','+modId+');');
    a.setAttribute('title',lang_link);
      var a_txt = window.parent.document.createTextNode(lang_link);
      a.appendChild(a_txt);
    td_delete.appendChild(a);
    tr.appendChild(td_delete);
  
  tr = parent.insertBefore(tr,node);
  
  window.parent.Sortable.destroy(parent);
  window.parent.dndFile('file_manager_'+modId,modId);
  window.parent.dndModule('field_content',pageId);
  if(window.parent.document.getElementById('file_manager_addon')){
    window.parent.selectFile(modId);
  }
}

// ########## FILE DELETE ##########

function fileDelete(mess,id,modId) {
  if(confirm(mess))
  {
    var node = document.getElementById('file_'+id);
    var parent = node.parentNode;
    parent.removeChild(node);
    
    new Ajax.Request(
      path_post+'file_manager/file_delete.html',
      {
        method:'post',
        postBody:'media_id='+id+'&modules_id='+modId
      }
    );
  }
}

// ########## DND FILE ##########

function dndFile(e,modId) {
  Sortable.create(e,
    {
      tag:'tr',
      only:'dnd',
      constraint:'false',
      onUpdate:function() {
        poststring = Sortable.serialize(e);
        new Ajax.Request(
          path_post+'file_manager/file_ajax,'+e+'.html',
          {
            method:'post',
            postBody:poststring+'&modules_id='+modId,
            onSuccess:function(ajax) {
            }
          }
        );
      }
    }
  )
}
