// JavaScript Document
function fp_formatxtmapcfg (nomgenerico) {
    this.nomgenerico = nomgenerico;
    this.idceldas = nomgenerico;
    this.side = "L";
	this.maxX=0;
	this.maxY=0;
	this.idvalinput="";
	this.bgcolormarca="#000000";
	this.bgcolormarcao="#000000";
	this.bgcolormarcaT1="#ff0000";
	this.bgcolormarcaT2="#0000ff";

	this.bgcolormarcaA="#ff0000";
	this.bgcolormarcaI="#0000ff";


	this.valstr="";
	this.marscaraopacity=0.4;
	this.activo=false;
	this.debug=false;
	this.debugmsg="";
	this.rows=new Array;
	this.setdebugmsgdiv=function(debugid){
		this.bebugdiv=fp_get_element_by_id_siexiste(debugid);
		if (this.bebugdiv){
			this.debug=true;	
		}
	}
	this.debugmsgwrite=function(){
		if (this.debug){
			this.bebugdiv.innerHTML=this.debugmsg;
		}
	}
	this.debugmsgadd=function(msg){
		if (this.debug){
			this.debugmsg+=msg;
		}
	}
	this.loadrows=function(){
		vala=this.valstr.split("|");
		for(x=0;x<this.maxX;x++){
			this.rows[x]=new Array;
			this.rows[x][0]=-1;
			this.rows[x][1]=-1;
			this.rows[x][2]=new Array;
			if (vala[x]){
				valaa=vala[x].split(",");
				if (valaa[0]){
					
					this.rows[x][0]=valaa[0];
				}
				if (valaa[1]){
					this.rows[x][1]=valaa[1];
				}
			}
			for(y=0;y<this.maxY;y++){
				this.rows[x][2][y]=fp_get_element_by_id_siexiste(this.idceldas+"_"+x+"_"+y);
				if (this.rows[x][2][y]){
					set_opacity(this.rows[x][2][y], this.marscaraopacity);
					//this.debugmsgadd(this.idceldas+"_"+x+"_"+y+ "OK <br>");
				}else{
					//this.debugmsgadd(this.idceldas+"_"+x+"_"+y+ "NO EXISTE <br>");
				}
				
			}
			//this.debugmsgwrite();
			this.marcar_row(x);
		}
	}
	this.marcar_rows=function(){
		for(x=0;x<this.maxX;x++){
			this.marcar_row(x);
		}
	}
	this.set_side=function(side){
		if (side){
			if (side!=this.side){
				if (side=="R"){
					this.side="R";
					this.bgcolormarcaT1=this.bgcolormarcaI;
					this.bgcolormarcaT2=this.bgcolormarcaA;
					this.marcar_rows();
				}else if  (side=="L"){
					this.side="L";
					this.bgcolormarcaT1=this.bgcolormarcaA;
					this.bgcolormarcaT2=this.bgcolormarcaI;
					this.marcar_rows();
				}
					
			}
		}
	}
	this.test=function(){
		this.bgcolormarcaO=this.bgcolormarca;
		for(x=0;x<this.maxX;x++){
			for(y=0;y<this.maxY;y++){
				this.bgcolormarca=this.bgcolormarcaT1;
				this.bgcolormarcaT1=this.bgcolormarcaT2;
				this.bgcolormarcaT2=this.bgcolormarca;
				this.marcar(x,y);
			}
		}
		this.bgcolormarca=this.bgcolormarcaO;
	}
	this.set_val=function(){
		var val="";
		for(x=0;x<this.maxX;x++){
			val+=this.rows[x][0]+","+this.rows[x][1]+"|";
		}
		fp_setvalue(this.idvalinput,val);
	}
	this.set_row_siactivo=function(row,pos,side){
		if (this.activo){
			this.set_row(row,pos,side);
		}
	}
	this.set_row_mdown=function(row,pos,side){
		if (mw_mouseDown){
			this.set_row(row,pos,side);
		}
	}
	this.set_row=function(row,pos,side){
		this.debugmsg="";
		if (side){
			this.set_side(side);
		}
		if (this.side=="L"){
			var index=0;	
		}else{
			var index=1;	
		}
		
		this.rows[row][index]=pos;
		
		this.evitarchoque(row,row);
		this.marcar_row(row);
		limiteX=parseInt(this.maxX);
		if (row>1){
			rowvecina=parseInt(row)-1;
			if (this.evitarchoque(row,rowvecina)){
				this.marcar_row(rowvecina);
			}
		}
		rowvecina=parseInt(row)+1;
		if (rowvecina<(limiteX)){
			
			if (this.evitarchoque(row,rowvecina)){
				this.marcar_row(rowvecina);
			}
		}
		this.debugmsgwrite();

		this.set_val();
	}
	
	this.evitarchoque=function(row,rowv){
		//alert(rowv);
		this.rows[row][0]=parseInt(this.rows[row][0]);
		this.rows[row][1]=parseInt(this.rows[row][1]);
		this.rows[rowv][0]=parseInt(this.rows[rowv][0]);
		this.rows[rowv][1]=parseInt(this.rows[rowv][1]);

		limite=parseInt(this.maxY);
		
		this.debugmsgadd("PROBANDO CHOQUE ROW "+row+" - "+rowv+" LIM "+limite+"<br>");
		this.debugmsgadd("R -- LEFT: "+this.rows[row][0]+" RIGHT: "+this.rows[row][1]+"<br>");
		this.debugmsgadd("RV -- LEFT: "+this.rows[rowv][0]+" RIGHT: "+this.rows[rowv][1]+"<br>");
		
		if (this.side=="L"){
			posL=this.rows[row][0];
			posR=this.rows[rowv][1];
		}else{
			posL=this.rows[rowv][0];
			posR=this.rows[row][1];
		}
		this.debugmsgadd("POS L "+posL+" R"+posR+"<br>");
		
		espaciosL=posL+1;
		if (posR<0){
			espaciosR=0;	
		}else{
			espaciosR=limite-posR;
		}
		var suma=espaciosL+espaciosR;
		this.debugmsgadd("ESPACIOS LEFT: "+espaciosL+" RIGHT: "+espaciosR+" SUM: "+suma+" <br>");
		if ((suma)>limite){
			this.debugmsgadd(" CORRIGIENDO...<br>");
			dif=suma-limite;
			this.debugmsgadd("DIF: "+dif+"<br>");
			if (this.side=="L"){
				this.rows[rowv][1]=this.rows[rowv][1]+dif;
				if(this.rows[rowv][1]>=limite){
					this.rows[rowv][1]=-1;	
				}
			}else{
				this.rows[rowv][0]=this.rows[rowv][0]-dif;
			}
			this.debugmsgadd("RV -- LEFT: "+this.rows[rowv][0]+" RIGHT: "+this.rows[rowv][1]+"<br>");
			return true;
		}
	}
	this.marcar_row=function(row){
		for(y=0;y<this.maxY;y++){
			var desmarmar=true;
			if (y<=this.rows[row][0]){
				var desmarmar=false;
				this.bgcolormarca=this.bgcolormarcaT1;
			}else if ((y>=this.rows[row][1])&&(this.rows[row][1]>=0)){
				var desmarmar=false;
				this.bgcolormarca=this.bgcolormarcaT2;
			}else{
				var desmarmar=true;
			}
			this.marcar(row,y,desmarmar);
		}
	}
	this.marcar=function(x,y,desmarmar){
		
		if (this.rows[x][2][y]){
			if (desmarmar){
				this.rows[x][2][y].style.backgroundColor="transparent";
			}else{
				this.rows[x][2][y].style.backgroundColor=this.bgcolormarca;
			}
		}
	}
	
}