
var msie=document.all&&navigator.userAgent.indexOf("Opera")==-1

function Objet(num, scr, width, height, nb_tirages, opacite)
{
  this.num = num ;
  this.scr = scr ;
  this.width = width ;
  this.height = height ;
  this.nb_tirages = nb_tirages ;
  this.opacite = opacite ;
}

function Inst_obj(num_obj, num_inst, num_div, width, height)
{
  this.num_obj = num_obj ;
  this.num_inst = num_inst ;
  this.num_div = num_div ;
  this.width = width ;
  this.height = height ;
}
var ok_recup ;
function go()
{
  t_obj = new Array() ;
  prem = true ;
  nb_fois = 0 ;
  ok_recup = false ;
  recup_fic_param() ;
  attend_recup = setInterval("lance_appli()", 100) ;
}

// Récupération des données
// ------------------------

function recup_fic_param()
{
  document.getElementById("txt").innerHTML = "<iframe src=" + fic_param + " id='itxt' name='itxt' onload='recup_txt()' style='display:none'></iframe>" ;
}

function recup_txt()
{
  var txt = window.itxt.document.body.innerHTML ;
  var ind = txt.indexOf(">")
  txt = txt.substring(ind + 1, txt.length) ;
  ind = txt.indexOf("<")
  if (ind != -1) txt = txt.substring(0, ind) ;
  recup_données(txt) ;
}

var t_obj ;
var tempo ;
var cadre ;
var marge_w, marge_h ;
var ecart_w, ecart_h ;
var nb_noeuds_w, nb_noeuds_h ;
function recup_données(txt)
{
  var ind ;
  var t_prov ;
  var i_o = 0 ;
  var i_a = 0 ;
  var t_ligne = txt.split(String.fromCharCode(10)) ;
  var i ;
  for (i = 0; i < t_ligne.length; i++)
  {
    ind = t_ligne[i].indexOf("//")
    if (ind != -1) t_ligne[i] = t_ligne[i].substring(0, ind) ;
    if (t_ligne[i] != "")
    {
      t_prov = t_ligne[i].split(String.fromCharCode(32)) ; 
      switch(t_prov[0])
      {
        case "fenêtre" :
        {
          cadre = t_prov[1] ;
          var elem = document.getElementById(cadre);
          elem.style.width = t_prov[2] + "px";
          elem.style.height = t_prov[3] +"px";
          if (t_prov[4] != "#") elem.style.backgroundColor = t_prov[4] ;
          else if (t_prov[5] != "#")
          {
            elem.style.background = t_prov[5] ;
            elem.style.backgroundRepeat = "no-repeat" ;
          }
          break ;
        }
        case "réseau" :
        {
          nb_noeuds_w = t_prov[1] ;
          nb_noeuds_h = t_prov[2] ;
          ecart_w = t_prov[3] ;
          ecart_h = t_prov[4] ;
          break ;
        }
        case "tempo" :
        {
          tempo = t_prov[1] ;
          break ;
        }
        case "répétitions" :
        {
          nb_repetitions = t_prov[1] ;
          pause_rep = t_prov[2] ;
          if (msie) t_prov[3] = t_prov[3].substr(0, t_prov[3].length - 1)
          if (t_prov[3] != "#") adr_redirection = t_prov[3] ;
          break ;
        }
        case "objet" :
        {
          t_obj[i_o] = new Objet() ;
          t_obj[i_o].num = t_prov[1] ;
          t_obj[i_o].src = t_prov[2] ;
          t_obj[i_o].width = t_prov[3] ;
          t_obj[i_o].height = t_prov[4] ;
          t_obj[i_o].nb_tirages = t_prov[5] ;
          t_obj[i_o].opacite = t_prov[6] ;
          i_o += 1 ;
          break ;
        }

      }
    }
  }
  if (i == t_ligne.length) ok_recup = true ;
  marge_w = (parseInt(document.getElementById(cadre).style.width) - (nb_noeuds_w - 1) * ecart_w) / 2 ;
  marge_h = (parseInt(document.getElementById(cadre).style.height) - (nb_noeuds_h - 1) * ecart_h) / 2 ;
}

// Gestion de la séquence
// ------------------------

var prem ;
var nb_fois ;
var num_img, num_tirage ;

function lance_appli()
{
  if (ok_recup)
  {
    appli() ;
    clearInterval(attend_recup) ;
  }
}

function appli()
{
  num_img = 0 ;
  num_tirage = 0 ;
  if (prem) pause = 1000 ; 
  else pause = 2500 ;
  setTimeout("sequence()", pause) ;
}

function sequence()
{
  document.getElementById(cadre).innerHTML = "" ;
  tab_tirages = new Array() ;
  actif = setInterval("effectue()", tempo) ;
}

function effectue()
{
    if (num_tirage < t_obj[num_img].nb_tirages)
    {
      result_tirage = tirage() ;
      affiche_image(num_img, result_tirage)
      num_tirage += 1 ;
    }
    else if (num_img < t_obj.length-1)
    {
      tab_tirages = new Array() ;
      num_img += 1 ;
      num_tirage = 0 ;
    }
    else
    {
      clearInterval(actif) ;
      prem = false ;
      nb_fois += 1 ;
      if (nb_fois < nb_repetitions || nb_repetitions == 0) appli() ;
//    else setTimeout("change_page()", pause) ;
    }
}

function tirage()
{
  var ok_tirage = false ;
  var resultat ;
  while (!ok_tirage)
  {
    resultat = Math.floor(Math.random() * (nb_noeuds_w * nb_noeuds_h)) ;
    ok_tirage = verifie_tirage(resultat) ;
  }
  tab_tirages.push(resultat)
  return resultat
}

function verifie_tirage(res)
{
  for(var i = 0; i < tab_tirages.length; i++)
  {
    if (res == tab_tirages[i]) return false ;
  }
  return true ;
}

function affiche_image(n_o, r_t)
{
    var noeud_t = Math.floor(r_t / nb_noeuds_w) ;
    var noeud_l = r_t % nb_noeuds_w ;
    var img_l = marge_w +  noeud_l * ecart_w - t_obj[n_o].width / 2 ;
    var img_t = marge_h + noeud_t * ecart_h - t_obj[n_o].height / 2 ;
    var elem = document.getElementById(cadre) ;
    var new_img = document.createElement("img") ;
    new_img.className="new_img" ;
    new_img.style.position = 'absolute' ;
    new_img.style.top = img_t + 'px' ;
    new_img.style.left = img_l + 'px' ;
    new_img.style.opacity = t_obj[n_o].opacite / 100 ;
    new_img.style.filter = 'alpha(opacity=' + t_obj[n_o].opacite + ')';
    new_img.style.MozOpacity = t_obj[n_o].opacite/100 ;
    new_img.style.KhtmlOpacity = t_obj[n_o].opacite/100 ;
    elem.appendChild(new_img) ;
    new_img.setAttribute('src', t_obj[n_o].src) ;
}

function change_page()
{
  location.replace(adr_redirection);
}


