var Admin = Class.create({
	initialize:function(){
		this.tableau = $('planning_body');
		this.lignes = this.tableau.getElementsByTagName('tr');
		this.form = $('administration');
		this.indiceMax = this.form['indice_max'].value;
		this.add = $$('.bouton_aj');
		this.sup = $$('.bouton_sup');
		this.reinitStyles();
		
		for(var i=0;i<this.add.length;i++){
			$(this.add[i].observe('click',this.addLigne.bindAsEventListener(this)));
			$(this.sup[i].observe('click',this.supLigne.bindAsEventListener(this)));
		}
	},
	reinitStyles:function(){
		for(var i=1;i<this.lignes.length;i++){
			if(i%2 == 0){
				if(!$(this.lignes[i]).hasClassName('ligne_impaire')){
					this.lignes[i].removeClassName('ligne_paire');
					this.lignes[i].addClassName('ligne_impaire');
				}
			}else{
				if(!$(this.lignes[i]).hasClassName('ligne_paire')){
					this.lignes[i].removeClassName('ligne_impaire');
					this.lignes[i].addClassName('ligne_paire');
				}
			}
		};
	},
	addLigne: function(event){
		var bouton = Event.element(event);
		var tr = bouton.up('tr');
		this.displayNewLine(tr);
	},
	displayNewLine: function(tr){
		var obj = this;
		var nouvelleLigne = new Element('tr');
		var td1 = new Element('td').update(new Element('input', {
			'class': 'date calendar',
			'type': 'text',
			'name': 'date[' + obj.indiceMax + ']'
		}));
		var td11 = new Element('td').update('<input type="text" value="20" name="h_start['+obj.indiceMax+']" maxlength="2" class="hinput" /><span style="float:left">:</span><input type="text" value="20" name="m_start['+obj.indiceMax+']" maxlength="2" class="hinput" />');
		var td12 = new Element('td').update('<input type="text" value="22" name="h_end['+obj.indiceMax+']" maxlength="2" class="hinput" /><span style="float:left">:</span><input type="text" value="30" name="m_end['+obj.indiceMax+']" maxlength="2" class="hinput" />');
		var td2 = new Element('td').update(new Element('input', {
			'class': 'libelle',
			'type': 'text',
			'name': 'libelle[' + obj.indiceMax + ']'
		}));
		var td3 = new Element('td').update(new Element('input', {
			'class': 'choeur',
			'type': 'checkbox',
			'name': 'groupe[' + obj.indiceMax + '][hommes]'
		}));
		var td4 = new Element('td').update(new Element('input', {
			'class': 'orchestre',
			'type': 'checkbox',
			'name': 'groupe[' + obj.indiceMax + '][femmes]'
		}));
		var bouton_aj = new Element('input', {
			'class': 'bouton_aj',
			'type': 'button',
			'value': '+'
		});
		var td5 = new Element('td').update(bouton_aj);
		var bouton_sup = new Element('input', {
			'class': 'bouton_sup',
			'type': 'button',
			'value': 'x'
		});
		var td6 = new Element('td').update(bouton_sup);
		nouvelleLigne.appendChild(td1);
		nouvelleLigne.appendChild(td11);
		nouvelleLigne.appendChild(td12);
		nouvelleLigne.appendChild(td2);
		nouvelleLigne.appendChild(td3);
		nouvelleLigne.appendChild(td4);
		nouvelleLigne.appendChild(td5);
		nouvelleLigne.appendChild(td6);
		tr.insert({ after: nouvelleLigne});
		this.reinitStyles();
		popUpCal.init();
		this.indiceMax++;
		bouton_aj.observe('click',this.addLigne.bindAsEventListener(this));
		bouton_sup.observe('click',this.supLigne.bindAsEventListener(this));
	},
	
	supLigne:function(event){
		var bouton = Event.element(event);
		var tr = bouton.up('tr');
		tr.remove();
		this.reinitStyles();
		if((this.lignes.length)-1 ==0){
			this.displayNewLine(this.tableau.select('tr')[0]);
		}
	}
});

Event.observe(window, 'load', function(){
	if($('planning_body')){
		new Admin();
	}
});

