/*
* Ce fichier utilise plusieurs fonctions et variables définies dans content.js (défini par le pyc original)
* content.js, ajaxRequestsTools.js, net.js, wz_tooltip.js needed 
*/

/******************************************************************************/
/******************* Fonctions de création de scénario ************************/
/******************************************************************************/

var oldImgElement = false;
var appRoot='undefined';

function reloadDiapMe(idLogin,type,num) {
  var element = ajaxGetElement('diaporama?service=getContentsType&type='+type);
  var elemObj = element.getElementsByTagName('file');
  var len = 0;

  //on vide la table
  if( elemObj ) {
    len = elemObj.length;
    
    document.getElementById(type+"Container").innerHTML='<h3>'+galleryTitle[type]+'</h3>';

  for (var i = 0; i < len; i++) {
      var aElem = elemObj[i];
      var basic = getFirstElementValue(aElem, 'filesystemBaseName');
      var id = getFirstElementValue(aElem, 'id');
      var prettyName = getFirstElementValue(aElem,'prettyName');
      var img = document.createElement('img');
      var link = document.createElement('a');
      var br = document.createElement('br');
      var small = document.createElement('small');
      var span = document.createElement('span');
      
      link.setAttribute("class","thumb"); // for FF, harmless on IE
      link.setAttribute("className","thumb"); // for IE
      link.setAttribute("href","#");
      
      
      var typeAbrej = "Unknown";
      if (type == "image")
        typeAbrej = "I";
      else if (type == "audio")
        typeAbrej = "A";
      else if (type == "video")
        typeAbrej = "V";
      
      link.id = id;
      link.basic = basic;
      link.prettyName = prettyName;
      link.typeAbrej = typeAbrej;
      link.onclick = new Function("addContentToScenario(this.typeAbrej, 'I', this.prettyName, this.id, this.id+'_'+this.basic,scenario);");
      //link.onclick = function() {addContentToScenarioAssumeObjectName(this.typeAbrej, 'I', this.prettyName, this.id, this.id+"_"+this.basic); return false;};
      
      if (type == "image") {
        img.src = "content/"+idLogin+"/thumbnails/"+id+"_"+basic;
      }
      else if (type == "video") {
        img.src = "content/"+idLogin+"/thumbnails/"+id+"_"+basic+".jpg";
      }
      else if (type == "audio") {
        img.src = "images/icones/sound-32.png"
      }
      
      if (prettyName)
        small.innerHTML = prettyName;
      
      link.appendChild(img);
      link.appendChild(br);
      link.appendChild(small);

      block = document.createElement('span');
      block.setAttribute("class","block");
      block.setAttribute("className","block");
      block.appendChild(link);
      
      span.setAttribute("class","overlay");
      span.setAttribute("className","overlay");
      
      link = document.createElement('a');
      link.setAttribute("href","#");
      
      //link.setAttribute("onclick","showPreview(modalPreviewManager, 'content/"+idLogin+"/"+type+"/"+id+"_"+basic+"'); return false;");
      link.idLogin = idLogin;
      link.type = type;
      link.id = id;
      link.basic = basic;
      link.onclick = new Function("showPreview(modalElementPreviewManager, 'content/'+this.idLogin+'/'+this.type+'/'+this.id+'_'+this.basic); return false;");
      img = document.createElement('img');
      if (type == "audio") {
          img.src = "images/icones/speaker.png"
       } else {
          img.src = "images/icones/magnifier.png"
       }
       img.alt="+";
      
      link.appendChild(img);
      span.appendChild(link);
      
      block.appendChild(span);
      document.getElementById(type+"Container").appendChild(block);
      
    }
  
  
  if (type=="image") {
    displaySuccess(INFO_IMAGE_ADD_OK,'msgDiv_'+type+'_'+num);
  }
  else if (type=="video") {
    displaySuccess(INFO_VIDEO_ADD_OK,'msgDiv_'+type+'_'+num);
  }
  else if (type == "audio")
    displaySuccess(INFO_AUDIO_ADD_OK,'msgDiv_'+type+'_'+num);
  
  setTimeout('hide(\'msgDiv_'+type+'_'+num+'\')',3000);
  document.forms['upload'+type]['file_'+num].value='';
  document.forms['upload'+type]['prettyName_'+num].value='';
  
  }
}

function reloadContent(idLogin,type,num)
{

  if (type=="image")
    displaySuccess(INFO_IMAGE_ADD_LIBRARY_OK,'msgDiv_'+num);
  else if (type=="video")
    displaySuccess(INFO_VIDEO_ADD_LIBRARY_OK,'msgDiv_'+num);
  else if (type == "audio")
    displaySuccess(INFO_AUDIO_ADD_LIBRARY_OK,'msgDiv_'+num);
  setTimeout('hide(\'msgDiv_'+num+'\')',3000);
  
  if (num == 1) {
    document.forms.uploadcontent.file_1.value='';
    document.forms.uploadcontent.prettyName_1.value='';
  }
  else if (num == 2) {
    document.forms.uploadcontent.file_2.value='';
    document.forms.uploadcontent.prettyName_2.value='';
  }
  else if (num == 3) {
    document.forms.uploadcontent.file_3.value='';
    document.forms.uploadcontent.prettyName_3.value='';
  }
  else if (num == 4) {
    document.forms.uploadcontent.file_4.value='';
    document.forms.uploadcontent.prettyName_4.value='';
  }
  else if (num == 5) {
    document.forms.uploadcontent.file_5.value='';
    document.forms.uploadcontent.prettyName_5.value='';
  }
  
}

function addContentToTable(type, url, name, prettyName, id)
{
  
  if (type == "video")
    Tip(videoAddLibraryOk, parent.DURATION, 5000);
  else if (type == "audio")
    Tip(audioAddLibraryOk, parent.DURATION, 5000);
}


function setCurrentImage(fileName)
{
  currentImage=fileName;

}

function setCurrentVideo(filename)
{
  currentVideo = filename;
}

function setCurrentAudio(filename)
{
  currentAudio = filename;
}

var processRunning = new Array();
var processRunningErrorDiv = new Array();
var processRunningSpinnerRow = new Array();

/** long process **/

function startLongProcesses(baseSpinnerRow, baseErrorDiv, maxIndex, formName) {
  
  for (var i = 1 ; i < maxIndex+1; i++) {
    if (document.forms[formName]["file_"+i].value != '') {
      startLongProcess(baseSpinnerRow+i,baseErrorDiv+i,i);
    }
  }

}

function startLongProcess(spinnerRow, errorDiv, index) {
  if (processRunning[index]) {
    displayError( errorsLabels_fr["ERROR_PROCESS_RUNNING"] , errorDiv );
  } else {
    processRunning[index] = true;
    processRunningErrorDiv[index] = errorDiv;
    processRunningSpinnerRow[index] = spinnerRow;
    if (document.getElementById(spinnerRow)) {
      document.getElementById(spinnerRow).className = "spinner";
    }
  }
}

function stopLongProcess(processNum) {
  //if (processRunning[processNum]) {}
  processRunning[processNum] = false;
  if (document.getElementById(processRunningSpinnerRow[processNum])) {
    document.getElementById(processRunningSpinnerRow[processNum]).className = "hidden";
  }
  clearError(processRunningErrorDiv[processNum]);
}

/******************************************************************************/
/******************* Fonctions de construction du player **********************/
/******************************************************************************/

function buildPlayer(idScenario,key)
{
  alert('key not yet used');
  var resources = new Array();
  var types = new Array();
  var durations = new Array();

  
}

function wait(duration)
{
  var date = new Date();
  var curDate = null;

  do { 
    curDate = new Date(); 
  } 
  while(curDate-date < duration);
}

