function jeu() {
  var n = 0;
  this.n = n;
  var cartes = new Array();
  this.cartes=cartes;
  var curr = 0;
  this.curr=curr;
  var prem = new Array();
  this.prem = prem;
  prem[0] = 1;
  
  function suit() {
    var i, k;
    while (this.n < 2*this.curr+20) {
      for(i=this.n+1;i<= this.n+100;i++)
        this.cartes[i] = i;
      this.n = this.n+100;
    }
    this.curr = this.curr+1;
    var momt = new Array();
    for(i=1;i<=this.curr;i++) {
      k=2*i-1;
      momt[k] = this.cartes[this.curr+i];
      momt[k+1] = this.cartes[i];
    }
    
    for(i=1;i<=2*this.curr;i++) {
      this.cartes[i] = momt[i];
    }
    this.prem[this.curr] = this.cartes[1];
    var s="Étape   "+this.curr+" \n-------------------\n", s1="";
    for(i=1;i<=2*this.curr;i++) {
      s1 = s1+ this.cartes[i]+", ";
      if(s1.length>60) {
        s = s+s1+"\n";
	s1 = "";
      }
    }
    if(s1 != "")
      s = s+s1+"\n";
    s = s+this.cartes[2*this.curr+1]+", "+
        this.cartes[2*this.curr+2]+", "+
        this.cartes[2*this.curr+3]+" ...\n\n";
    s = s+"Premiers éléments :\n-------------------\n";
    s1 = "";
    for(i=0;i<=this.curr;i++) {
      s1 = s1+ this.prem[i]+", ";
      if(s1.length>60) {
        s = s+s1+"\n";
        s1 = "";
      }
    }
    if(s1 != "")
      s = s+s1+"\n";

    document.frm.txt.value = s;
    
  }
  this.suit = suit;
  function init() {
    var s="Étape   "+this.curr+" \n-------------------\n";
    s = s+"1, 2, 3 ...\n";
    document.frm.txt.value = s;
  }
  this.init = init;
  
  function prec() {

  }
  this.prec = prec;
}

var lejeu = new jeu();

function suivant() {
  lejeu.suit();
}
function clr() {
  delete lejeu;
  lejeu = new jeu();
  lejeu.init();
}

function traj() {
  if(isNaN(document.frm.n.value))
    document.frm.n.value=1;
  var i, n = parseInt(document.frm.n.value);
  var cr;
  var t=new Array();
  var max = 10000;
  if (n%2 == 0) cr = n/2;
  else cr = (n+1)/2;
  var p0 = cr-1;
  var v = t[0] = n;
  for(i=1;i<=max;i++) {
//    cr = cr+1;
    if(v<=cr) {
      v = 2*v;
    } else {
      v = 2*(v-cr)-1
    }
    t[i] = v;
     cr = cr+1;
  }
  var s="Trajectoire de "+n+"   (t[k<="+p0+"]="+n+")\n------------------------------------------\n";
  var s1="... ";
  for(i=0;i<=max;i++) {
    if(t[i] == 1) {
       s1 = s1+ "\n-----------------\n"+ "t_"+n+"["+(p0+i)+"]= "+t[i]+",\n-----------------\n";
    } else s1 = s1+ t[i]+", ";
    if(s1.length>60) {
      s = s+s1+"\n";
      s1 = "";
    }
  }
  if(s1 != "")
  s = s+s1+"\n";
//  s.replace(/\n\s*\n+/g,"\n");
  document.frm.txt.value = s;
  if(isNaN(document.frm.n.value))
    document.frm.n.value=1;
  document.frm.n.value = parseInt(document.frm.n.value)+1;
}

function premier() {
  if(isNaN(document.frm.n.value))
    document.frm.n.value=1;
  var n = parseInt(document.frm.n.value);
  var cr;
  var max = 3000000000; // 252992198
  var s="Élément "+n+"\n---------------------\n\n";
  if (n%2 == 0) cr = n/2;
  else cr = (n+1)/2;
  var p0 = cr-1;
  var v = n;
  for(i=1; i<= max; i++) {
    if(v<=cr) {
      v = 2*v;
    } else {
      v = 2*(v-cr)-1;
    }
    cr = cr+1;

    if(v == 1) {
      s= s + "t_"+n+"["+(p0+i)+"]= "+1+"\n\n";
      s= s + "l'élément "+n+" est en 1ère position à l'étape "+(p0+i)+"\n";
      document.frm.txt.value = s;
      break;
    }

  }
  document.frm.n.value = parseInt(document.frm.n.value)+1;
}

var sve;
function ante(k,y) {
  if(y > 2*k) return y;
  if(y%2==0) return y/2;
  return k+(y+1)/2;
}


function deb(n) {
  var  y=1, k;
  for(k=n;k>=1;k--)
    y = ante(k,y);
  return y;
}

function eff() {
  document.f2.n.value="";
  sve=1;
}

function velt(x) {
  document.f2.n.value=x;
  elt();
  
}

function elt() {
  document.f2.n.value=document.f2.n.value.replace(/^\s*/g,"");
  document.f2.n.value=document.f2.n.value.split(/\s+/)[0];
  if(isNaN(document.f2.n.value))
    document.f2.n.value=1;
  var u = document.f2.n.value;
  u=u.replace(/^\s*/g,"");
  u=u.replace(/\s*$/g,"");
  u=u.replace(/\s+/g," ");
  var v=u.split(/\s+/);
  sve=parseInt(v[0]);
  document.f2.n.value = sve+"  "+deb(sve);
}

function svant() {
  document.f2.n.value=document.f2.n.value.replace(/^\s*/g,"");
  document.f2.n.value=document.f2.n.value.split(/\s+/)[0];
  if(isNaN(sve))
    sve = parseInt(document.f2.n.value);
  if(isNaN(sve)) 
    sve = document.f2.n.value = 1;
  else
    document.f2.n.value = sve + 1;
  elt();
}

