/**
 * @constructor
 */
function lineSelector(hPr,rdonly) {
	
	this._id=lineSelector._getId();

	this._selected=-1;
	this._handle=null;
	this._values=null;
	this._indices=null;
	this._options=null;
	this._rdonly=rdonly;
	
	this.changeCB=null;
	this.clickCB=null;
	this.deletedCB=null;
	this._readOnly=false;
	
	this.unselect=function () {
		var ndoc=document.getElementById(this._id+"itr_main_view");
		var arr=ndoc.getElementsByTagName("div");
		var bc;
		
		for (bc=0; bc<arr.length; bc++) {
			arr[bc].style.background="transparent";
			arr[bc].style.color="black";
		}
		
		this._selected=-1;
	}
	
	this.setReadOnly=function(vl) {
		if (vl) {
			this._readOnly=true;
			document.getElementById(this._id+"itr_go_up").disabled=true;
			document.getElementById(this._id+"itr_go_down").disabled=true;
			document.getElementById(this._id+"itr_go_del").disabled=true;
			document.getElementById(this._id+"itr_go_go").disabled=true;
		} else {
			this._readOnly=false;
		}
	}
	
	this._block=function() {
		UnTip();
		var hUp=document.getElementById(this._id+"itr_go_up");
		var hDown=document.getElementById(this._id+"itr_go_down");
		var hDel=document.getElementById(this._id+"itr_go_del");
		if (hUp)   hUp.disabled=true;
		if (hDown) hDown.disabled=true;
		if (hDel)  hDel.disabled=true;
	}

	this._conv=function(scad) {
		scad=scad.replace("&","&amp;");
		scad=scad.replace("<","&lt;");
		scad=scad.replace(">","&gt;");
		return scad;
	}

	this.move=function(px,py) {
		if (!this._handle) return;
		this._handle.style.left=px+"px";
		this._handle.style.top=py+"px";
	}

	this.resize=function(pw,ph) {
		if (!this._handle) return;
		this._handle.style.width=pw+"px";
		this._handle.style.height=ph+"px";
		var view=document.getElementById(this._id+"itr_main_view");
		if (view) {
			view.style.width=(pw-34)+"px";
			var hOb=document.getElementById(this._id+"itr_show_selector");
			if (hOb.style.visibility=="visible") view.style.height=(ph-24)+"px";
			else view.style.height=ph+"px";
		}
	}

	this.showSelector=function() {
		var hOb=document.getElementById(this._id+"itr_show_selector");
		hOb.style.visibility="visible";
		hOb.style.display="";
	}
	
	this.hideSelector=function() {
		var hOb=document.getElementById(this._id+"itr_show_selector");
		hOb.style.visibility="hidden";
		hOb.style.display="none";
	}

	this.getWidth=function()  { if (!this._handle) return 0; return this._handle.offsetWidth;  }
	this.getHeight=function() { if (!this._handle) return 0; return this._handle.offsetHeight; }
	this.getLeft=function()   { if (!this._handle) return 0; return this._handle.offsetLeft;   }
	this.getTop=function()    { if (!this._handle) return 0; return this._handle.offsetTop;    }

	this.getData=function() {
		var arr=Array();
	
		for (bc=0; bc<this._values.length; bc++) {
			if (this._rdonly) arr.push([this._values[bc],this._indices[bc]]);
			else              arr.push(this._values[bc]);
		}
		
		return arr;
	}

	this._add=function(sval,hind) {
		if (!this._handle) return;
		var hDiv=document.getElementById(this._id+"itr_main_view");
		
		var scad="<div id='"+this._values.length+"-"+this._id+"itr_view' ";
		scad+="onclick='lineSelector._elclick(this);'";
		scad+="style='cursor: pointer; width: 100%;'>"+ this._conv(sval) + "</div>";

		hDiv.innerHTML+=scad;
		var vH=document.getElementById(this._values.length+"-"+this._id+"itr_view");
		if (vH) vH=vH.offsetTop;
		
		this._values.push(sval);
		this._indices.push(hind);
		if (vH!=null) hDiv.scrollTop=vH;
	}
	
	this.changeIndex=function(oldind,newind) {
		var bc;
		
		for (bc=0; bc<this._indices.length; bc++) {
			if ( this._indices[bc]==oldind ) { this._indices[bc]=newind; break; }
		}
	}
	
	this.add=function(sval,hind) {
		this._add(sval,hind);
		if (this.changeCB) this.changeCB(this);
	}

	this._clear=function() {
		this._values=Array();
		this._indices=Array();
		this._options=Array();
		this._block();

		document.getElementById(this._id+"itr_main_view").innerHTML="";
		
		var hEl=document.getElementById(this._id+"itr_edit");
		hEl.value="";
		hEl.backgroundColor="white";
		if ( this._rdonly ) {
			while (hEl.options.length) hEl.options[0]=null;
		}
	}

	this.clear=function() {
		this._clear();
		if (this.changeCB) this.changeCB(this);
	}

	this._drawOptions=function() {
		
		var hOp=document.getElementById(this._id+"itr_edit");
		var hSp=document.getElementById(this._id+"itr_go_go");
		var bfirst=true;
		var bc,bc2,bfound;
		
		hSp.disabled=true;
		while( hOp.options.length ) hOp.options[0]=null;
		for (bc=0; bc<this._options.length; bc++) {
			bfound=false;
			for (bc2=0; bc2<this._indices.length; bc2++) {
				if ( this._indices[bc2]==this._options[bc][1] ) {
					bfound=true;
					break;
				}
			}
			if (bfound) continue;
			hIter=new Option(this._options[bc][0], this._options[bc][1]);
			hOp.options.add(hIter,0);
			if (!this._readOnly) hSp.disabled=false;
			if (bfirst) {
				hOp.value=this._options[bc][1];
				bfirst=false;
			}
		}
	}

	this.setOptions=function(ops) {
		if (!this._rdonly) return;
		var bc;
	
		this._options=Array();
		for (bc=(ops.length-1); bc>=0; bc--) {
			this._options.push([ops[bc][1], ops[bc][0]]);
		}
		
		this._drawOptions();
	}

	var pdata="<table style='background: white; width: 200px; height: 150px; border: 1px solid #e0e0e0'>";

	pdata+="<tr><td>";
	pdata+="<input onblur='lineSelector._mainblur(this)' onfocus='lineSelector._mainfocus(this)' onkeypress='return lineSelector._keymove(this,event);' id='"+this._id+"itr_focus' style='font-size: 0px; width: 0px; height: 0px; border: 0px none;' type='text'></input>";
	pdata+="<div id='"+this._id+"itr_main_view' style='font-size: 11px; font-family: Arial; border: 1px solid #a0a0a0; background: #DFE0E6; width: 176px; height: 126px; overflow: auto;'></div>";	
	pdata+="</td><td style='width: 24px;'><table style='width: 100%; height: 100%; border-collapse: collapse'>";
	pdata+="<tr style='height: 24px;'><td><input onmouseover='Tip(\"Subir opci&oacute;n\")' onmouseout='UnTip()' onclick='UnTip(); lineSelector._moveup(this);' id='"+this._id+"itr_go_up' type='button' style='width: 100%; background: white; border: 1px solid #a0a0a0; background-repeat: no-repeat; background-position: center; background-image: url(imagenes/act-up.gif)'></input></td></tr>";
	pdata+="<tr style='height: 24px;'><td><input onmouseover='Tip(\"Bajar opci&oacute;n\")' onmouseout='UnTip()' onclick='UnTip(); lineSelector._movedown(this);' id='"+this._id+"itr_go_down' type='button' style='width: 100%; background: white; border: 1px solid #a0a0a0; background-repeat: no-repeat; background-position: center; background-image: url(imagenes/act-down.gif)'></input></td></tr><tr><td></td></tr>";
	pdata+="<tr style='height: 24px;'><td>";
	if (!rdonly) pdata+="<input onmouseover='Tip(\"Nueva opci&oacute;n\")' onmouseout='UnTip()' onclick='UnTip(); lineSelector._donew(this);' id='"+this._id+"itr_go_new' type='button' style='width: 100%; background: white; border: 1px solid #a0a0a0; background-repeat: no-repeat; background-position: center; background-image: url(imagenes/act-new.gif)'></input>";
	pdata+="</td></tr><tr><td></td></tr>";
	pdata+="<tr style='height: 24px;'><td style='vertical-align: bottom'><input onmouseover='Tip(\"Borrar opci&oacute;n\")' onmouseout='UnTip()' onclick='UnTip(); lineSelector._delete(this);' id='"+this._id+"itr_go_del' type='button' style='background: white; width: 100%; border: 1px solid #a0a0a0; background-repeat: no-repeat; background-position: center; background-image: url(imagenes/act-del.gif)'></input></td></tr>";
	pdata+="</table></td></tr>";

	pdata+="<tr id='"+this._id+"itr_show_selector' style='height: 24px;'><td><table style='width: 100%; border-collapse: collapse;'>";
	pdata+="<tr><td>";
	if (rdonly)	pdata+="<select onkeypress='return lineSelector._keypress(this,event);' id='"+this._id+"itr_edit' type='text' style='width: 100%' value=''></select>";
	else        pdata+="<input onkeypress='return lineSelector._keypress(this,event);' id='"+this._id+"itr_edit' type='text' style='width: 100%' value=''></input>";
	pdata+="</td>";
	pdata+="<td style='width: 24px;'><input ";
	if (rdonly) pdata+="disabled='true' onmouseover='Tip(\"A&ntilde;adir\")'";
	else        pdata+="onmouseover='Tip(\"Modificar o a&ntilde;adir texto\")'";
	pdata+=" onmouseout='UnTip()' onclick='UnTip(); lineSelector._editnow(this);' id='"+this._id+"itr_go_go' type='button' style='font-size: 11px; font-family: Arial, Helvetica; background: white; width: 100%; border: 1px solid #a0a0a0; background-repeat: no-repeat; background-position: center; background-image: url(";
	if (rdonly) pdata+="imagenes/act-addlist.gif";
	else        pdata+="imagenes/act-go.gif";
	pdata+=")'></input>";
	pdata+="</td></tr></table></td><td></td></tr></table>";

	var idiv=document.createElement("div");
	idiv.innerHTML=pdata;
	this._handle=idiv.childNodes[0];
	this._handle.parentNode.removeChild(this._handle);
	hPr.appendChild(this._handle);
	idiv=null;	

	this._clear();
	lineSelector._allSelectors.push(this);
}


lineSelector._getObject=function(id) {
	var bc;

	for (bc=0; bc<lineSelector._allSelectors.length; bc++) {
		if ( lineSelector._allSelectors[bc]._id==id ) return lineSelector._allSelectors[bc];
	}

	return null;
}

lineSelector._donew=function(el) {
	var vl=parseInt(el.id.substring(0,el.id.length-10));
	var THIS=lineSelector._getObject(vl);
	var ndoc=document.getElementById(vl+"itr_main_view");
	var arr=ndoc.getElementsByTagName("div");
	var bc;
	
	for (bc=0; bc<arr.length; bc++) {
		arr[bc].style.color="black";
		arr[bc].style.backgroundColor="transparent";
	}
	
	THIS._selected=-1;
	document.getElementById(THIS._id+"itr_edit").value="";
	document.getElementById(THIS._id+"itr_edit").style.backgroundColor="white";
	document.getElementById(THIS._id+"itr_edit").focus();
}

lineSelector._mainblur=function(el) {
	var vl=parseInt(el.id.substring(0,el.id.length-9));
	var THIS=lineSelector._getObject(vl);
	
	document.getElementById(THIS._id+"itr_main_view").style.borderColor="#a0a0a0";
}

lineSelector._mainfocus=function(el) {
	var vl=parseInt(el.id.substring(0,el.id.length-9));
	var THIS=lineSelector._getObject(vl);

	document.getElementById(THIS._id+"itr_main_view").style.borderColor="#9090f0";
}

lineSelector._keymove=function (el,ev) {
	var vl=parseInt(el.id.substring(0,el.id.length-9));
	var THIS=lineSelector._getObject(vl);

	if ( THIS._selected == -1 ) return true;

	switch (ev.keyCode) {
		case 38:
			if (THIS._selected>0) {
				THIS._selected--;
				var dc=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
				lineSelector._elclick(dc);
				var vH=dc.offsetTop;
				document.getElementById(THIS._id+"itr_main_view").scrollTop=vH;
			}
			break;
		case 40:
			if ( THIS._selected<(THIS._values.length-1) ) {
				THIS._selected++;
				var dc=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
				lineSelector._elclick(dc);
				var vH=dc.offsetTop;
				document.getElementById(THIS._id+"itr_main_view").scrollTop=vH;
			}
			break;
	}

	return true;
}

lineSelector._keypress=function (el,ev) {
	var vl=parseInt(el.id.substring(0,el.id.length-8));
	var THIS=lineSelector._getObject(vl);
	
	if (ev.keyCode==13) {
		el=document.getElementById(vl+"itr_go_go");
		lineSelector._editnow(el);
		return false;
	}
	return true;
}

lineSelector._editnow=function (el) {
	var vl=parseInt(el.id.substring(0,el.id.length-9));
	var THIS=lineSelector._getObject(vl);

	if (THIS._readOnly) return;

	var sval=document.getElementById(THIS._id+"itr_edit").value;
	if (THIS._rdonly) THIS._selected=-1;
	
	if (THIS._selected==-1) {
		if (THIS._rdonly) {
			var nOb=document.getElementById(THIS._id+"itr_edit");
			THIS._add(nOb[nOb.selectedIndex].text,nOb[nOb.selectedIndex].value);
			THIS._drawOptions();
		} else {
			THIS._add(sval);
			document.getElementById(THIS._id+"itr_edit").value="";
		}
		document.getElementById(THIS._id+"itr_edit").focus();
	} else {
		THIS._values[THIS._selected]=sval;
		var it=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
		it.innerHTML=THIS._conv(sval);
	}
	if (THIS.changeCB) THIS.changeCB(THIS);
}

lineSelector._delete=function (el) {
	var vl=parseInt(el.id.substring(0,el.id.length-10));
	var THIS=lineSelector._getObject(vl);

	//if (THIS._rdonly) return;
	if (THIS._selected==-1) return;
	var sndVal,sndInd;
	var arr=Array();
	var arrind=Array();
	var bc;

	sndVal=THIS._selected;
	for (bc=0; bc<THIS._values.length; bc++) {
		if (bc==THIS._selected) {
			sndInd=THIS._indices[bc];
			continue;
		}
		arr.push(THIS._values[bc]);
		arrind.push(THIS._indices[bc]);
	}

	if (THIS._rdonly) {
		var arrops=Array();
		for (bc=0; bc<THIS._options.length;bc++) arrops.push(THIS._options[bc]);
	}
	THIS._clear();

	for (bc=0; bc<arr.length; bc++) THIS._add(arr[bc],arrind[bc]);
	if (!arr.length) {
		THIS._block();
		THIS._selected=-1;
		if (!THIS._rdonly) {
			document.getElementById(THIS._id+"itr_edit").style.backgroundColor="white";
			document.getElementById(THIS._id+"itr_edit").value="";
		}
	} else {	
		while ( arr.length<=THIS._selected ) THIS._selected--;
		bc=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
		lineSelector._elclick(bc);
	}
	if (THIS._rdonly) { THIS._options=arrops; THIS._drawOptions(); }

	if (THIS.deletedCB) THIS.deletedCB(THIS,sndVal,sndInd);
	if (THIS.changeCB) THIS.changeCB(THIS);
}

lineSelector._movedown=function (el) {
	var vl=parseInt(el.id.substring(0,el.id.length-10));
	var THIS=lineSelector._getObject(vl);
	var bc;

	if (THIS._selected==-1) return;
	if (THIS._selected>=(THIS._values.length-1)) return;

	var arr=Array();
	var arrind=Array();
	for (bc=0; bc<THIS._values.length; bc++) {
		if ( bc==THIS._selected ) {
			arr.push (THIS._values[bc+1]); 
			arrind.push (THIS._indices[bc+1]);
		}
		else if ( bc==(THIS._selected+1) ) {
			arr.push (THIS._values[bc-1]);  
			arrind.push (THIS._indices[bc-1]);
		}
		else {
			arr.push (THIS._values[bc]); 
			arrind.push (THIS._indices[bc]);
		}
	}	

	if (THIS._rdonly) {
		var arrops=Array();
		for (bc=0; bc<THIS._options.length;bc++) arrops.push(THIS._options[bc]);
	}
	THIS._clear();

	for (bc=0; bc<arr.length; bc++) THIS._add(arr[bc],arrind[bc]);
	THIS._selected++;
	bc=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
	lineSelector._elclick(bc);
	if (THIS._rdonly) { THIS._options=arrops; THIS._drawOptions(); } 

	if (THIS.changeCB) THIS.changeCB(THIS);
}


lineSelector._moveup=function (el) {
	var vl=parseInt(el.id.substring(0,el.id.length-8));
	var THIS=lineSelector._getObject(vl);
	var bc;

	if (THIS._selected==-1) return;
	if (THIS._selected<=0) return;

	var arr=Array();
	var arrind=Array();
	for (bc=0; bc<THIS._values.length; bc++) {
		if ( bc==THIS._selected ) {
				arr.push (THIS._values[bc-1]); 
				arrind.push (THIS._indices[bc-1]); 
		}
		else if ( bc==(THIS._selected-1) ) {
			arr.push (THIS._values[bc+1]); 
			arrind.push (THIS._indices[bc+1]);
		}
		else {
			arr.push (THIS._values[bc]);
			arrind.push (THIS._indices[bc]);
		}
	}	

	if (THIS._rdonly) {
		var arrops=Array();
		for (bc=0; bc<THIS._options.length;bc++) arrops.push(THIS._options[bc]);
	}
	
	THIS._clear();

	for (bc=0; bc<arr.length; bc++) THIS._add(arr[bc],arrind[bc]);
	THIS._selected--;
	bc=document.getElementById(THIS._selected+"-"+THIS._id+"itr_view");
	lineSelector._elclick(bc);
	if (THIS._rdonly) { THIS._options=arrops; THIS._drawOptions(); }
	
	if (THIS.changeCB) THIS.changeCB(THIS);
}

lineSelector._elclick=function (el) {
	
	var vl=el.id.substr(el.id.indexOf("-")+1);
	vl=parseInt(vl.substring(0,vl.length-8));

	var THIS=lineSelector._getObject(vl);
	var ndoc=document.getElementById(vl+"itr_main_view");
	var arr=ndoc.getElementsByTagName("div");
	var bc;
	var npos;

	if (THIS._readOnly) return;

	for (bc=0; bc<arr.length; bc++) {
		if ( arr[bc]!=el ) { 
			arr[bc].style.background="transparent"; 
			arr[bc].style.color="black"; 
		}
		else { 
			UnTip();
			arr[bc].style.background="#000088"; 
			arr[bc].style.color="white"; 
			if (bc) { document.getElementById(vl+"itr_go_up").disabled=false; }
			else    { document.getElementById(vl+"itr_go_up").disabled=true; }
			if ( arr.length==1 ) { document.getElementById(vl+"itr_go_down").disabled=true; }
			else {
				if ( bc == (arr.length-1) ) document.getElementById(vl+"itr_go_down").disabled=true;
				else document.getElementById(vl+"itr_go_down").disabled=false;
			}
			npos=bc;
		}
	}

	THIS._selected=npos;
	UnTip();
	
	if (!THIS._rdonly) {
		document.getElementById(vl+"itr_edit").value=THIS._values[npos];
		document.getElementById(THIS._id+"itr_edit").style.backgroundColor="#ffffbb";
	}
	document.getElementById(THIS._id+"itr_go_del").disabled=false;
	document.getElementById(THIS._id+"itr_focus").focus();
	
	if (THIS.clickCB) {
		vl=parseInt(el.id.substring(0,el.id.indexOf("-")));
		THIS.clickCB(THIS,THIS._values[vl],THIS._indices[vl]);
	}
}

lineSelector._count=0;

lineSelector._allSelectors=Array();

lineSelector._getId=function() {
	return lineSelector._count++;
}
