var _id = 0, _pid = 0, _lid = 0, _pLayer;
var _mLists = new Array();
var toPrinter = new String(); // versión para imprimir y visualizar en mac
document.lists = _mLists;
var isNav4, isIE4,isNav6,isIE5,isMac;

isMac = (navigator.platform.indexOf("Mac") !=-1) ? true : false;

isNav4 = (document.layers)? true:false
isIE4 = (document.all && !document.getElementById)? true:false
isNav6 = (!document.all && document.getElementById)? true:false
isIE5 = (document.all && document.getElementById)? true:false

var imgTri_on = new Image();
var imgTri_off = new Image();
var imgBola = new Image();
var imgPrint = new Image();

function List(visible, width, height, bgColor) 
{   
  this.setIndent = setIndent;
  this.addItem = addItem;
  this.addList = addList;
  this.build = build;
  this.rebuild = rebuild;
  this.setFont = _listSetFont;
  this._writeList = _writeList;
  this._showList = _showList;
  this._updateList = _updateList;
  this._updateParent = _updateParent;  
  
  this.setImgOn = _setimgon
  this.setImgOff = _setimgoff
  this.setImgInfo = _setimginfo
  this.setImgPrint = _setimgprint
  
  this.onexpand = null; this.postexpand = null;
  this.lists = new Array(); // sublists
  this.items = new Array(); // layers
  this.types = new Array(); // type
  this.strs = new Array();  // content  
  this.x = 0;
  this.y = 0;
  this.visible = visible;
  this.id = _id;
  this.i = 18;
  this.space = true;
  this.parentPath = "../";
  
  imgTri_on.src = this.parentPath + "gif/triangle_on.gif";
  imgTri_off.src = this.parentPath + "gif/triangle_off.gif";
  imgBola.src = this.parentPath + "gif/bola.gif";
  imgPrint.src = this.parentPath + "gif/print.gif";
  
  this.pid = 0;
  this.fontIntro = false;
  this.fontOutro = false;
  this.width = (isNav4)? width || 350 : width + 10 || 350;
  
  this.setParentPath = _setParentPath
  
  /***************************/
  this.getContentHeight = getContentHeight //this is new
  /***************************/
  this.height = height || 22;
  this.parLayer = false;
  this.built = false;
  this.shown = false;
  this.needsUpdate = false;
  this.needsRewrite = false;
  this.parent = null;
  this.l = 0;
  
  if(bgColor) this.bgColor = bgColor;
  else this.bgColor = "#FFFFFF";
  
  _mLists[_id++] = this;   
}

function getContentHeight(layer) 
{		
	if (isNav4) 
		return layer.document.height 
	else if (isNav6) 
	{		
		return layer.offsetHeight
	}
	else
		return layer.scrollHeight	 
}

function _setParentPath(strParentPath)
{	
  this.parentPath = strParentPath;
  imgTri_on.src = this.parentPath + "gif/triangle_on.gif";
  imgTri_off.src = this.parentPath + "gif/triangle_off.gif";
  imgBola.src = this.parentPath + "gif/bola.gif";
  imgPrint.src = this.parentPath + "gif/print.gif";
}

function _setimgon(img)
{
	imgTri_on.src = img;
}

function _setimgoff(img)
{
	imgTri_off.src = img;
}

function _setimginfo(img)
{
	imgBola.src = img;
}

function _setimgprint(img)
{
	if (img!="")
		imgPrint.src = img;
	else
		imgPrint = null;
}

function getHTMLHeight()
{
var longitud=0;

	for (i=0;i<_mLists.length;i++)
	{					
		longitud+=String(_mLists[i].strs).length
	}
	return(longitud);
}

function _listSetFont(i,j) 
{
  this.fontIntro = i;
  this.fontOutro = j;
}

function setIndent(indent) { this.i = indent; if(this.i < 0) { this.i = 0; this.space = false; } }

function setClip(layer, l, r, t, b) 
{
  if(isNav4) 
  {
    layer.clip.left = l; 
    layer.clip.right = r+50;
    layer.clip.top = t;  
    layer.clip.bottom = b;
  }   
  else if(isNav6)
  {		
	//layer.style.width = (r-l);
    //layer.style.height = (b-t);            
  }
  else 
  {		
    layer.style.pixelWidth = (r-l);
    layer.style.pixelHeight = (b-t);            
    //layer.style.clip = "rect("+t+","+r+","+b+","+l+")";        
  }  
  
}

function print()
{
	var newWindow;	
	newWindow = window.open("","Vista_Previa","width="+this.width+",status=yes,toolbar=yes,menubar=no,resizable=yes,scrollbars=yes,location=no");			
	toPrinter="";	
	_PreparePrint(_mLists[0])	
	newWindow.document.open();
	newWindow.document.writeln("<HTML><HEAD><TITLE>Página de impresión</TITLE>");
	newWindow.document.writeln("<STYLE TYPE=\"text/css\">A{text-decoration:none};A:active{color:black};A:visited{color:black};</STYLE></HEAD>");
	newWindow.document.writeln("<BODY bgcolor=#FFFFFF vlink=#000000 link=#000000 >");
	newWindow.document.writeln(toPrinter);	
	newWindow.document.writeln("</BODY>");
	newWindow.document.writeln("</HTML>");
	newWindow.document.close();		
}

function _PreparePrint(lst) 
{
  var layer, str, clip,imgTr;
  for(var i = 0; i < lst.types.length; i++) { 
    layer = lst.items[i];
    if(isNav4) 
    {
		layer.visibility = "visible";
	}
	else if(isNav6)
	{
		layer.style.visibility = "visible";
	}
    else
    { 
		layer.style.visibility = "visible";
	}
    str = "";    
    str += "<TABLE WIDTH="+lst.width+" bgcolor=" + lst.bgColor + " NOWRAP BORDER=0 CELLPADDING=2 CELLSPACING=0><TR>";
       
	if(lst.types[i] == "list") {
		if (_mLists[lst.lists[i].id].visible){imgTr = imgTri_on.src;}else{imgTr = imgTri_off.src;}
		if (lst.parent == null){
			str += "<TD WIDTH=15 NOWRAP VALIGN=TOP><IMG ALT=\"\" BORDER=0 SRC=\"" + imgTr + "\" WIDTH=12 HEIGHT=12 NAME=\"_img"+lst.lists[i].id+"\" vspace=2></TD>";			
		}
		else{
			for (var j=0;j<lst.l;j++){
				str += "<TD WIDTH=18 NOWRAP>&nbsp;</TD>";
			}				
				str += "<TD WIDTH=15 NOWRAP VALIGN=TOP><IMG ALT=\"\" BORDER=0 SRC=\"" + imgTr + "\" WIDTH=12 HEIGHT=12 NAME=\"_img"+lst.lists[i].id+"\" vspace=2></TD>";			
		}
    } 
	else if(lst.space)
      str += "<TD WIDTH=15 NOWRAP>&nbsp;</TD>";
      	
    if(lst.l>0 && lst.i>0 && lst.types[i] != "list") {
		str += "<TD WIDTH="+lst.l*lst.i+" NOWRAP>&nbsp;</TD>";
	}
	
	str += "<TD HEIGHT="+(lst.height-3)+" WIDTH="+(lst.width-15-lst.l*lst.i)+" VALIGN=TOP ALIGN=LEFT>";		
	
    if(lst.fontIntro) str += lst.fontIntro;
    
    var strTemp,index1,index2;
    
    strTemp="";
    
    index1=lst.strs[i].indexOf("<a");
    index2=lst.strs[i].indexOf(">",index1);
    
    if (index1!=-1)
    {
		for (var j=0;j<=lst.strs[i].length;j++)
		{
			if (j==index1){strTemp += "<I>";}
			if (j<index1 || j>index2)
			{
				strTemp += lst.strs[i].charAt(j);
			}		
		}
    }
    else
    {
    	strTemp = lst.strs[i];
    }
        
	strTemp = strTemp.replace(/a>/gi,"I>");
    str += strTemp;  
        
    if(lst.fontOutro) str += lst.fontOutro;
    str += "</TD></TR></TABLE>";
    toPrinter += str;  	
    if(lst.types[i] == "list" && lst.lists[i].visible)
      _PreparePrint(lst.lists[i]);		  
  }
}

function _writeList() {		 // this function is updated
  //self.status = "List: Writing list...";
  var layer, str, clip;
  for(var i = 0; i < this.types.length; i++) { 
    layer = this.items[i];
    if(isNav4) 
    {
		layer.visibility = "hidden";
	}
	else if(isNav6)
	{
		layer.style.visibility = "hidden";
	}
    else 
    {
		layer.style.visibility = "hidden";
	}
	
    str = "";
    if (_pid==0)
	{
		if (imgPrint)
		{		
			str = "<TABLE bgcolor=" + this.bgColor +  " cellspacing=0 cellpadding=0 border=0><TR><TD WIDTH=" + this.width + "px HEIGHT=22px><A href=\"javascript: print();\"><img alt=\"\" src=\"" + imgPrint.src + "\" border=0 width=123px height=22px></A></TD></TR></TABLE>"
		}
	}

    if(isNav4) layer.document.open();
    
    str += "<TABLE NOWRAP BORDER=0 CELLPADDING=2 CELLSPACING=0 bgcolor="+ this.bgColor +" ><TR>";       
	if(this.types[i] == "list") {
		if (this.parent == null){
			str += "<TD WIDTH=15 NOWRAP VALIGN=TOP><A TARGET='_self' HREF=\"javascript:expand("+this.lists[i].id+",'');\"><IMG ALT=\"\" BORDER=0 SRC=\"\" WIDTH=12 HEIGHT=12 NAME=\"_img"+this.lists[i].id+"\" vspace=2></A></TD>";
			_pid++;
		}
		else{
			for (var j=0;j<this.l;j++){
				str += "<TD WIDTH=18 NOWRAP>&nbsp;</TD>";
			}
			str += "<TD WIDTH=15 NOWRAP VALIGN=TOP><A TARGET='_self' HREF=\"javascript:expand("+this.lists[i].id+",'');\"><IMG ALT=\"\" BORDER=0 SRC=\"\" WIDTH=12 HEIGHT=12 NAME=\"_img"+this.lists[i].id+"\" vspace=2></A></TD>";
			_pid++;
		}
    } 
	else if(this.space)
      str += "<TD WIDTH=15 NOWRAP>&nbsp;</TD>";
	
    if(this.l>0 && this.i>0 && this.types[i] != "list") {
		str += "<TD WIDTH="+this.l*this.i+" NOWRAP>&nbsp;</TD>";
	}
	if (this.types[i] == "list")
	{
		str += "<TD HEIGHT="+(this.height-3)+" WIDTH="+(this.width-15-this.l*this.i)+" VALIGN=TOP ALIGN=LEFT><A TARGET='_self' HREF=\"javascript:expand("+this.lists[i].id+",'');\">";		
	}
	else
	{
		str += "<TD HEIGHT="+(this.height-3)+" WIDTH="+(this.width-15-this.l*this.i)+" VALIGN=TOP ALIGN=LEFT>";		
	}
	
    if(this.fontIntro) str += this.fontIntro;
    str += this.strs[i];    
    if(this.fontOutro) str += this.fontOutro;
    str += "</A></TD></TR></TABLE>";
	
	if(isNav4) 
	{
      layer.document.writeln(str);
      layer.document.close();
    } 
    else 
		layer.innerHTML=str;
        
    this.heigth = this.getContentHeight(layer);
    if(this.types[i] == "list" && this.lists[i].visible)
      this.lists[i]._writeList();		  
  }
  this.built = true;
  this.needsRewrite = false;
  self.status = '';
}

function _showList() {
  var layer;
  var layer_ant;
  for(var i = 0; i < this.types.length; i++) { 	
    layer = this.items[i];
	setClip(layer,0, this.width, 0, this.getContentHeight(layer)-1); // this is updated
    var bg = layer.oBgColor || this.bgColor;
    
    if(isIE4 || isIE5) 
    {
      if((bg == null) || (bg == "null")) bg = "";
      layer.style.backgroundColor = bg;
    } 
    else if (isNav4) 
    {
		layer.document.bgColor = bg;
	}
	else if (isNav6)
	{
		layer.style.backgroundColor = bg;
	}
	
    if(this.types[i] == "list" && this.lists[i].visible)
      this.lists[i]._showList();
  }
  this.shown = true;
  this.needsUpdate = false;
}

function _updateList(pVis, x, y) 
{
  var currTop = y, layer;
  var antTop=0;
  for(var i = 0; i < this.types.length; i++) 
  { 
    layer = this.items[i];
    if(this.visible && pVis) 
    {
      if(isNav4) 
      {
        layer.visibility = "visible";
        layer.top = currTop ;
        layer.left = x;
      } 
      else if(isNav6)
      {	
		layer.style.visibility = "visible";			
		layer.style.top = currTop;
        layer.style.left = x;                
      }             
      else
      {		
		layer.style.visibility = "visible";
		layer.style.pixelTop = currTop;
        layer.style.pixelLeft = x;
      }           	  
      
      currTop += this.getContentHeight(layer); // this is updated      
    } 
    else 
    {
      if(isNav4) 
		layer.visibility = "hidden";
      else if (isNav6)
		layer.style.visibility = "hidden";
      else
		layer.style.visibility = "hidden";
    }
    
    if(this.types[i] == "list") 
    {
      if(this.lists[i].visible) 
      {
        if(!this.lists[i].built || this.lists[i].needsRewrite) this.lists[i]._writeList();
        if(!this.lists[i].shown || this.lists[i].needsUpdate) this.lists[i]._showList();        
        if(isNav4) layer.document.images["_img"+this.lists[i].id].src=imgTri_on.src;
        else eval('document.images._img'+this.lists[i].id+'.src=imgTri_on.src');
      } 
      else 
      {
        if(isNav4) layer.document.images["_img"+this.lists[i].id].src=imgTri_off.src;
        else eval('document.images._img'+this.lists[i].id+'.src=imgTri_off.src');
      }      
      if(this.lists[i].built)
      {		
        currTop = this.lists[i]._updateList(this.visible && pVis, x, currTop);        
      }
    }
  }
  return currTop;  
}

function _updateParent(pid, l) 
{
  var layer;
  if(!l) l = 0;
  this.pid = pid;
  this.l = l;
  for(var i = 0; i < this.types.length; i++)
    if(this.types[i] == "list")
      this.lists[i]._updateParent(pid, l+1);
}

function expand(i,all) 
{
	if (!(isMac && (isIE4 || isIE5))) 
	{		
		if (all == '')
		{
			_mLists[i].visible = !_mLists[i].visible;
			if(_mLists[i].onexpand != null) _mLists[i].onexpand(_mLists[i].id);
			_mLists[_mLists[i].pid].rebuild();
			if(_mLists[i].postexpand != null) _mLists[i].postexpand(_mLists[i].id);
		}	
		else 
		if (all == "expand")
		{
			for (i=1;i<_mLists.length;i++)
			{
				if (i == 1)
				{
					_mLists[i].visible = false
				}
				else 
				{
					_mLists[i].visible = true
				}				
			}
			expand(1,'')
		}
		else if (all == "compress")
		{
			for (i=1;i<_mLists.length;i++)
			{
				if (i == 1)
				{
					_mLists[i].visible = true
				}
				else 
				{
					_mLists[i].visible = false
				}				
			}
			expand(1,'')		
		}
	}                           
}

/*
function _MacVersion(obj,x,y)
{

	obj._updateParent(obj.id);	
	obj._showList();
	obj.x = x;
	obj.y = y;
	for (i=1;i<_mLists.length;i++)	
	{		
		_mLists[i].visible = true
	} 	
	_PreparePrint(_mLists[0])		

	window.document.body.innerHTML += '<DIV id="listaMac" name="listaMac" STYLE="position:absolute">&nbsp;</DIV>';	
	
    listaMac.innerHTML = toPrinter;
	listaMac.style.overflow="auto";
	listaMac.style.left=_mLists[0].x;
	listaMac.style.top=_mLists[0].y;	
}
*/

function _MacVersion(obj,x,y)
{
	obj._updateParent(obj.id);
	obj._showList();
	obj.x = x;
	obj.y = y;
	for (i=1;i<_mLists.length;i++)	
	{		
		_mLists[i].visible = true;
	} 
	_PreparePrint(_mLists[0]);
	
	window.setTimeout('_MacPrint("window.document",' + _mLists[0].x + ',' + _mLists[0].y + ')',2000);
}

function _MacPrint(id,x,y)
{		
	var obj=eval(id + ".lItem0");
	
	if (typeof(obj)=="undefined")
	{		
		obj=eval("lItem0");
	}
		
	obj.innerHTML=toPrinter;	
	obj.style.overflow="auto";
	obj.style.left=x;
	obj.style.top=y;
}

function build(x, y) 
{
  if (isMac && (isIE4 || isIE5))
  {			
	  _MacVersion(this,x,y);	
  }  
  else
  { 	
	this._updateParent(this.id);
	this._writeList();
	this._showList();
	this._updateList(true, x, y);
	this.x = x;
	this.y = y;	
  } 
}

function rebuild() {this._updateList(true, this.x, this.y);}

function addItem(str, bgColor, layer) 
{
  var testLayer = false;
  if(isNav4) document.all = document.layers;
  
  if(!layer) 
  {    
  		
    if (isIE4 || isIE5) 
	{		
		testLayer = eval('document.all.lItem'+_lid);		
	}
	else if(isNav6)
	{		
		testLayer = document.getElementById("lItem" + _lid);		
	}
	else if(isNav4)
	{		
		if (this.parLayer)
		{
			_pLayer = this.parLayer;			
			testLayer = eval('_pLayer.document.layers.lItem'+_lid);		
		}
		else
		{
			testLayer = eval('document.layers.lItem'+_lid);		
		}
	}
    
    if(testLayer) 
    {
		layer = testLayer;
    }
    else 
    {
      if(isNav4) 
      {
        if(this.parLayer) 
        {
			layer = new Layer(this.width, this.parLayer);		
		}
        else 
        {
			layer = new Layer(this.width);
		}
      } 
      else 
      {
		return;
	  }
    }
  }
  
  if(bgColor) 
	layer.oBgColor = bgColor;
	
  this.items[this.items.length] = layer;
  this.types[this.types.length] = "item";
  
  if (str != null)
  {
	this.strs[this.strs.length] = str;
  }   
  _lid++;
  
}

function addList(list, str, bgColor, layer) 
{ 
  var testLayer = false;
  	
  if(!layer) 
  {
    
    if (isIE4 || isIE5) 
	{		
		testLayer = eval('document.all.lItem'+_lid);		
	}
	else if(isNav6)
	{			
		testLayer = document.getElementById("lItem" + _lid);				
	}
	else if(isNav4)
	{		
		if (this.parLayer)
		{
			_pLayer = this.parLayer;			
			testLayer = eval('_pLayer.document.layers.lItem'+_lid);		
		}
		else
		{
			testLayer = eval('document.layers.lItem'+_lid);		
		}
	}		

    
    if(testLayer) 
    {
		layer = testLayer;
	}
    else 
    {
      if(isNav4) 
      {
        if(this.parLayer) 
			layer = new Layer(this.width, this.parLayer);
        else 
			layer = new Layer(this.width);
      } 
      else 
		return;
    }
  }
  
  if(bgColor) 
	layer.oBgColor = bgColor;
	
  this.lists[this.items.length] = list;
  this.items[this.items.length] = layer;
  this.types[this.types.length] = "list";
  this.strs[this.strs.length] = str;    
  list.parent = this;
  _lid++;
}



