// JavaScript Document

if (
		(typeof Prototype=='undefined') ||
		(typeof Element == 'undefined') ||
		(typeof Element.Methods=='undefined') ||
		parseFloat(Prototype.Version.split(".")[0] + "." + Prototype.Version.split(".")[1]) < 1.5
		)
	throw("FenetreGC exige le framework Prototype version >= 1.5.0");

List = Class.create();

List.prototype = {

  initialize: function() {
    this.nextIndex;		
    this.elmList;
    
    this.emptyList();
  },

  elmCreateAtBottom: function(titre){
    this.elmList[this.nextIndex] = {'titre':titre, 'pos':this.nextIndex};
    this.nextIndex++;
    this.posList++;
  },
  
  // Supprime un ou une lise d'élément(s) 
  elmDestructor: function(index) {
    if (!(index instanceof Array)) {
      index = [index];
    }
    var newElmList = new Array();
    var newIndex = 0;
    // !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    for (j=0; j<index.length; j++) {
      for (i=0; i<this.elmList.length; i++) {
        if (i == index[j]){
          continue;
        }
        newElmList[newIndex++] = this.elmList[i];
      }
    }
      
    this.nextIndex = newIndex;
    this.elmList = newElmList;
  },

  elmCreateOnTop: function(titre){
    return this.elmCreateAt(titre, 0);
  },

  elmCreateAtEnd: function(titre,url){
    return this.elmCreateAt(titre, this.nextIndex, url);
  },
  
  elmCreateAt: function(titre, pos, url){
    var newElmList = new Array();
    var added = false;
    
    var newIndex = 0;
    for (i=0; i<this.elmList.length; i++) {
      if (i == pos-1) {
      
        newElmList[newIndex++] = {'titre':titre, 'pos':this.nextIndex, 'url':url};
        added = true;
      }

			newElmList[newIndex++] = this.elmList[i];
      //alert(this.elmList[i].titre + ' i ' + i);
    }
    if (!added) {
      newElmList[newIndex++] = {'titre':titre, 'pos':this.nextIndex, 'url':url};
		}
		
    this.nextIndex = newIndex;
    this.elmList = newElmList;
    return this.nextIndex;
  },
  
  getNumElements: function() {
    return this.elmList.length;
  },
  
  emptyList: function() {
    this.nextIndex = 0;
    this.elmList = new Array();
  },
  
  getPosElements: function(index){
    return this.elmList[index].pos;
  },
  
  getIndex: function(pos){
    pos = parseInt(pos);
    for(i=0; i<this.elmList.length; i++){
   
      if(this.elmList[i].pos == pos){
        return i;
      }
    }
  },
  
  getDetailsFromIndex: function(index){
    index = parseInt(index);
    return this.elmList[index-1];
  }
  
}

