// --------------------------------------------------------
// Gestion de la liste des variables
// --------------------------------------------------------

// --------------------------------------------------------
// Constructeur
var objVariable = function (identifier, shortName, selected) {
	this.identifier = identifier;
	this.shortName = shortName;
	this.selected = selected;
	this.colors = "";
	this.menu = "";
}

// --------------------------------------------------------
// Methodes
objVariable.prototype = {
  create : function () {
    //-----------------------------------
    // Création d'une ligne du menu
    this.menu=document.createElement('option');
    this.menu.innerHTML=this.shortName;
    document.getElementById('selectOutputs').appendChild(this.menu);
  
    //-----------------------------------
    if(this.selected == "yes") {
      this.createLine();
      this.menu.style.display = 'none';
    }
  },

  createLine : function() {
    this.selected = "yes";
    nbLignes++;
    //-----------------------------------
    // Création d'une ligne du tableau
    this.ligne=document.createElement('tr');
    var laLigne = this.ligne;
    this.caseDel=document.createElement('td');
    this.caseColor=document.createElement('td');
    this.caseName=document.createElement('td');

    //-----------------------------------
    // Insertion du HTML
    this.colors = colorsDispo[0];
    this.caseColor.innerHTML= "<span class='legend' style='background:"+this.colors+"'></span>";
    colorsDispo.shift();
    this.caseName.innerHTML=this.shortName;

    //-----------------------------------
    // Création d'un bouton delete
    this.btDelete=document.createElement('a');
    this.btDelete.setAttribute('href','#delete');
    this.btDelete.setAttribute('title','Delete');
    this.btDelete.setAttribute('class','deleteBt');
    this.btDelete.innerHTML='<span>Delete</span>';

    //-----------------------------------
    // Affichage des boutons
    this.caseDel.appendChild(this.btDelete);
    this.ligne.appendChild(this.caseDel);
    this.ligne.appendChild(this.caseColor);
    this.ligne.appendChild(this.caseName);
    document.getElementById('listResultsOutputs').appendChild(this.ligne);
    //addedVariable(this.identifier,this.shortName,this.colors);
    getAllResultsParameters();
    // delete
    var col = this.colors;
    var oo = this;
    this.btDelete.onclick = function() {
      colorsDispo.push(col);
      document.getElementById('listResultsOutputs').removeChild(laLigne);
      oo.selected = "no";
      oo.menu.style.display = 'block';
      nbLignes--;
      buildPlot();
      //deletedVariable(oo.identifier,oo.shortName);
    }
  },

  clearVariable : function() {
    document.getElementById('selectOutputs').innerHTML = "";
   
  }
}

// --------------------------------------------------------
// Variables globales
//var colorsDispo = Array('#F00','#0F0','#00F','#FF0','#0FF','#F0F');
var nbLignes = 0;
var variablesArray = Array();

// --------------------------------------------------------
// fonction de creation d'objets
insertVariable = function (identifier, shortName, selected) {
  for (i=0;i<variablesArray.length;i++)
  {
    if (variablesArray[i].identifier==identifier) return  
  }
  var o2 = new objVariable(identifier,shortName,selected);
  variablesArray.push(o2);
  o2.create();
  // ajout
  o2.menu.onclick = function() {
    if(o2.selected != "yes" && nbLignes < 6) {
      o2.createLine();
      o2.menu.style.display = 'none';
      var outputVariable = document.getElementById('resultsOutputsSelect');
      outputVariable.selectedIndex = 0;
      buildPlot();
    } else {
      alertBox("<h3>Too much selection</h3><p>You can choose up to 6 !</p>","","");
    }
  }
}

getVariablesList = function()
{
	list = Array();
	for (i=0;i<variablesArray.length;i++)
	{
		if (variablesArray[i].selected == 'yes')
			list.push(Array(variablesArray[i].identifier,variablesArray[i].shortName,variablesArray[i].colors));
	}
	return list;
}

