// ########## PICTURE NEW ##########

function pictureNew(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 = '';
  var media_alt = document.getElementById('media_alt').value;
  document.getElementById('media_alt').value = '';
  if(document.getElementById('media_visible').checked) {
    var media_visible = 1;
  }
  document.getElementById('media_visible').setAttribute('checked','checked');
  
  var parent = window.parent.document.getElementById('module_'+modId);
  var node = window.parent.document.getElementById('picture_new_'+modId);
  
  // <fieldset id="picture_{id}" class="field_picture dnd">
  var fieldset = window.parent.document.createElement('fieldset');
  fieldset.setAttribute('id','picture_'+id);
  fieldset.setAttribute('class','field_picture dnd');
  fieldset.setAttribute('className','field_picture dnd');
  // <input type="hidden" id="media_edit{id}" name="media_edit[{modId}][]" value="picture" />
    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','picture');
    fieldset.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);
    fieldset.appendChild(media_id);
    // <p class="center p_picture">
    var p_img = window.parent.document.createElement('p');
    p_img.setAttribute('class','center p_picture');
    p_img.setAttribute('className','center');
      // <img id="media_file{id}" src="/images/thumbs/gallery_files/150x100/{media_filename}" />
      var img = window.parent.document.createElement('img');
      img.setAttribute('id','media_file'+id);
      img.setAttribute('src',path+'images/thumbs/gallery_files/150x100/'+media_filename);
      img.setAttribute('alt',media_alt);
      p_img.appendChild(img);
    // </p>
    fieldset.appendChild(p_img);
    // <p>
    var p_label = window.parent.document.createElement('p');
      // <label for="media_label{id}">{lang_label} :</label>
      var label_label = window.parent.document.createElement('label');
      label_label.setAttribute('for','media_label'+id);
        var label_txt = window.parent.document.createTextNode(lang_label+' : ');
        label_label.appendChild(label_txt);
      p_label.appendChild(label_label);
      // <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);
      p_label.appendChild(input_label);
    // </p>
    fieldset.appendChild(p_label);
    // <p>
    var p_description = window.parent.document.createElement('p');
      // <label for="media_description{id}">{lang_description} :</label>
      var label_description = window.parent.document.createElement('label');
      label_description.setAttribute('for','media_description'+id);
        var description_txt = window.parent.document.createTextNode(lang_description+' : ');
        label_description.appendChild(description_txt);
      p_description.appendChild(label_description);
      // <textarea id="media_description{id}" name="media_description[{modId}][{id}]" cols="20" rows="5">{media_description}</textarea>
      var textarea_description = window.parent.document.createElement('textarea');
      textarea_description.setAttribute('id','media_description'+id);
      textarea_description.setAttribute('name','media_description['+modId+']['+id+']');
      textarea_description.setAttribute('cols',20);
      textarea_description.setAttribute('rows',5);
        var description_value = window.parent.document.createTextNode(media_description);
        textarea_description.appendChild(description_value);
      p_description.appendChild(textarea_description);
    // </p>
    fieldset.appendChild(p_description);
    // <p>
    var p_alt = window.parent.document.createElement('p');
      // <label for="media_alt{id}">{lang_alt} :</label>
      var label_alt = window.parent.document.createElement('label');
      label_alt.setAttribute('for','media_alt'+id);
        var alt_txt = window.parent.document.createTextNode(lang_alt+' :');
        label_alt.appendChild(alt_txt);
      p_alt.appendChild(label_alt);
      // <input type="text" id="media_alt{id}" name="media_alt[{modId}][{id}]" value="{media_alt}" />
      var input_alt = window.parent.document.createElement('input');
      input_alt.setAttribute('type','text');
      input_alt.setAttribute('id','media_alt'+id);
      input_alt.setAttribute('name','media_alt['+modId+']['+id+']');
      input_alt.setAttribute('value',media_alt);
      p_alt.appendChild(input_alt);
    // </p>
    fieldset.appendChild(p_alt);
    // <p>
    var p_visible = window.parent.document.createElement('p');
      // <label for="media_visible{id}">{lang_visible} :</label>
      var label_visible = window.parent.document.createElement('label');
      label_visible.setAttribute('for','media_visible'+id);
        var visible_txt = window.parent.document.createTextNode(lang_visible+' : ');
        label_visible.appendChild(visible_txt);
      p_visible.appendChild(label_visible);
      // <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');
      }
      p_visible.appendChild(input_visible);
    // </p>
    fieldset.appendChild(p_visible);
    // <a class="delete" href="javascript:pictureDelete({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:pictureDelete("'+lang_mess+'",'+id+','+modId+');');
    a.setAttribute('title',lang_link);
      var a_txt = window.parent.document.createTextNode(lang_link);
      a.appendChild(a_txt);
    fieldset.appendChild(a);
  // </fieldset>
  
  fieldset = parent.insertBefore(fieldset,node);
  
  window.parent.Sortable.destroy(parent);
  window.parent.dndPicture('module_'+modId,modId);
  window.parent.dndModule('field_content',pageId);
}

// ########## PICTURE DELETE ##########

function pictureDelete(mess,id,modId) {
  if(confirm(mess))
  {
    var parent = document.getElementById('module_'+modId);
    var node = document.getElementById('picture_'+id);
    
    parent.removeChild(node);
    
    new Ajax.Request(
      path_post+'gallery/picture_delete.html',
      {
        method:'post',
        postBody:'media_id='+id+'&modules_id='+modId
      }
    );
  }
}

// ########## DND PICTURE ##########

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

