var mObj = new Array();

function TREEMENU(openAll) {
//----------------------------------------------------------------------------------------------------
// Configuration
//----------------------------------------------------------------------------------------------------

  this.width = 170;                                 // menu width (pixels)
  this.bgColor = "#F0F8FF";                         // menu background color
  this.autoClose = true;                           // close level-one-folders automatically (true or false)

  this.itemMargin = 1;                              // item margin (pixels)
  this.itemPadding = 2;                             // item padding (pixels)
  this.itemColor = "#000099";                       // item font color
  this.itemBGColor = "#E0F0FF";                     // item background color
  this.itemBGColor1 = "#E0F0FF";                    // item background color for level-one-items
  this.itemFont = "Verdana, Arial, Helvetica";      // item font family (CSS spec.)
  this.itemSize = 10;                               // item font size (pixels)
  this.itemBold = false;                            // item bold font (true or false)
  this.itemWrap = false;                            // item word wrap (true or false)
  this.itemActive = "red";                          // active item font color
  this.itemActiveUnderline = true;                  // active item underline (true or false)

  this.iconWidth = 12;                              // icon width (pixels)
  this.iconHeight = 10;                             // icon height (pixels)
  this.iconClosed = "closed.gif";                   // icon closed (path)
  this.iconClosedHilight = "closed_hilight.gif";    // icon closed hilight (path)
  //this.iconOpen = "open.gif";                       // icon open (path)
  this.iconOpenHilight = "open_hilight.gif";        // icon open hilight (path)
  this.iconPoint = "point.gif";                     // icon point (path)
  this.iconPointHilight = "point_hilight.gif";      // icon point hilight (path)

  this.imgBlank = "blank.gif";                      // blank image (path)

//----------------------------------------------------------------------------------------------------
// Functions
//----------------------------------------------------------------------------------------------------

  this.mNr = 0;
  this.curItem = -1;
  this.hilightItem = -1;
  this.targetWindow = 0;
  this.actualmenu=-1;
  this.voyen=-1;
  this.estoyenelmenu=-1;

  this.items = new Array();

  if(openAll == null) openAll = false;
  this.openAll = openAll;

  this.entry = function(level, text, url, target, onClick,img1,img2,estilo1,estilo2,idunicov) {
    var i = this.items.length;
    this.items[i] = new makeItem(level, text, url, target, onClick, this.openAll,img1,img2,estilo1,estilo2,idunicov,idunicov);
  }

  this.getObj = function(id) {
    var obj;
    if(document.getElementById) obj = document.getElementById(id);
    else if(document.all) obj = document.all[id];
    return obj;
  }

  this.setHilight = function(item) {
    if(this.hilightItem >= 0) {
      var i = this.hilightItem;
      if(this.items[i].icon == this.iconOpenHilight) this.items[i].icon = this.iconOpen;
      else if(this.items[i].icon == this.iconClosedHilight) this.items[i].icon = this.items[i].img1;
      else if(this.items[i].icon == this.iconPointHilight) this.items[i].icon = this.items[i].img1;
    }
    this.hilightItem = item;
    if(this.items[item].icon == this.items[item].img2) this.items[item].icon = this.iconOpenHilight;
    else if(this.items[item].icon == this.items[item].img1) this.items[item].icon = this.iconClosedHilight;
    else if(this.items[item].icon == this.items[item].img1) this.items[item].icon = this.iconPointHilight;
  }

  this.jump = function(item) {
    //this.setHilight(item);
    //this.openMenu(item);
	
	
this.createCookie('actualitem',item,7);

    var icon = this.items[item].icon;
    if(icon == this.items[item].img2) this.closeMenu(item);
    else{
	
       this.openMenu(item);
	
	}

    if(this.items[item].onClick) eval(this.items[item].onClick);


    if((this.items[item].url) && (this.estoyenelmenu!=item)) {
	this.pausecomp(1000);
      if(this.items[item].target) {
        if(this.items[item].target.indexOf('parent.') == -1 && this.items[item].target.indexOf('top.') == -1) {
          if(this.targetWindow && !this.targetWindow.closed) this.targetWindow.location.href = this.items[item].url;
          else this.targetWindow = window.open(this.items[item].url, 'targetWindow');
          this.targetWindow.focus();
        }
        else eval(this.items[item].target + '.location.href = "' + this.items[item].url + '"');
      }
      else document.location.href = this.items[item].url;
    }


  }

  this.openMenu = function(item) {

    this.cierrasubMenus(item);
    if(this.items[item].node) {

      this.curItem = item;
      this.items[item].icon = (item == this.hilightItem) ? this.iconOpenHilight : this.items[item].img2;
    }
    this.newMenu();
  }

  
	this.pausecomp = function(millis) 
	{
	date = new Date();
	var curDate = null;
		do { var curDate = new Date(); } 
		while(curDate-date < millis);
	} 

  
  
  
  this.closeMenu = function(item) {

    var este=-1;
    var aux;

    if (this.items[item].node)
    {
	aux= this.items[item].level;


	for(i = 0; i <item; i++) 
	{
		if ((aux>this.items[item-i-1].level) && (this.items[item-i-1].node))
		{
		este = 	item-i-1;
		break;
		}
	}

	this.createCookie('actualitem',este,7);

    }


    this.curItem = -1;
    if(this.items[item].node) {
      this.items[item].icon = (item == this.hilightItem) ? this.iconClosedHilight : this.items[item].img1;
	
    }
    this.newMenu();
  }

 this.cierrasubMenus = function(item)  {
	
	if (item>=0)
	{
	var fin=false;
	var fin2=false;
	var inicio;
	var aux;
	var aux2;
	var indice =0;
	var final =this.items.length;

	
	inicio=0;

			
	var mi_matriz = new Array();

	aux= this.items[item].level;
	aux2= this.items[item].level;

	if ((this.items[item].node))
	{
		aux= this.items[item].level;
		mi_matriz[indice] = item;
		indice=indice+1;
	}

 	for(i = 0; i <item; ++i) 
	{
	
			

			if (((aux>this.items[item-i-1].level) && (this.items[item-i-1].node)) && ((this.items[item-i-1].level<this.items[item].level-1) || (this.items[item-i-1].level==1))) 
			fin=true;
			

			if ((aux>this.items[item-i-1].level) && (this.items[item-i-1].node))
			{
			aux= this.items[item-i-1].level;
			mi_matriz[indice] = item-i-1;
			indice=indice+1;
			
			}	

			if (fin)
			{

					inicio= indice-1;

					//alert("debo verificar cierre desde:"+mi_matriz[inicio]);	
					i=item;
					//Ahora debo verificar el hasta
					
					for (q=item+1;q<this.items.length;++q)
					{
						

						if (((aux2>this.items[q].level) && (this.items[q].node))) 
						{
						final=q;
						q= this.items.length;
						}



					}


					//Ahora debo cerrar ITEMs iguales
					for (z=mi_matriz[inicio]; z<final;++z)
					{
						if (this.items[z].node && item!=z)
						{
							if (this.items[z].level == aux2)
							this.closeMenu(z);
						}
					}
						

					return;
			}
		
		
	}
		
	}
	return;
	
   
  }

 this.pagactual = function(item)  {

    var valitem = this.retornaid(item);
	
	this.estoyenelmenu=valitem;
	
 
	//if (Sacacookie() == "undefined")
	//{
	//this.createCookie('actualitem',valitem,7);
	
	//alert(valitem);
	this.abresubMenus(valitem);
	//}
	this.estoyenelmenu=-1;
	
	
 }	

 
 
 
 this.abresubMenus = function(item)  {
	
	
	//if (Sacacookie() == "undefined")
	//return;
	
  	if (item>=0)
	{var fin=false;
	var inicio;
	var aux;
	var indice =0;
	
	inicio=0;
	if ((this.items[item].node) &&(this.items[item].level==1))
	{
	this.openMenu(item);
	//alert("hace");
	return;
	}	
				
	var mi_matriz = new Array();

	aux= this.items[item].level;

	if ((this.items[item].node))
	{
		aux= this.items[item].level;
		mi_matriz[indice] = item;
		indice=indice+1;
	}

 	for(i = 0; i <item; i++) 
	{
	
			
			if ((aux>this.items[item-i-1].level) && (this.items[item-i-1].node) && (this.items[item-i-1].level==1)) 
			{
			inicio=item-i-1;
			fin=true;
			}


			if ((aux>this.items[item-i-1].level) && (this.items[item-i-1].node))
			{
			aux= this.items[item-i-1].level;

			mi_matriz[indice] = item-i-1;
			indice=indice+1;
			
			}	

			if (fin)
			{

			
					for (t=indice-1;t>=0; --t)
					{
					this.openMenu(mi_matriz[t]);
					}

					return;
			}
		
	}
		
	}
	return;
	
   
  }

  this.viewMenu = function(item) {
    var icon = this.items[item].icon;
    if(icon == this.items[item].img2) this.closeMenu(item);
    else{
	
	this.openMenu(item);
}

  }

  this.content = function(item) {
    var text = '';
    var bgc = (this.items[item].level <= 1) ? this.itemBGColor1 : this.itemBGColor;
    text += '<table border=0 cellspacing=' + this.itemMargin + ' cellpadding=0 width='+ this.width + '><tr>' +
            '<td class='+this.items[item].estilo2 +'>' +
            '<table border=0 cellspacing=0 cellpadding=0><tr valign=middle >';
    if(this.items[item].level > 1) {
      for(i = 1; i < 1; i++) { //this.items[item].level; i++) {
        text += '<td><img' +
                ' src="' + this.imgBlank + '"' +
                '></td>';
      }
    }
    text += '<td>';
    if(this.items[item].node) text += '<a href="javascript:mObj[' + this.mNr + '].viewMenu(' + item + ')">';
    text += '<img src="' + this.items[item].icon + '" border=0' +
            '></a></td>' +
            '<td' + (this.itemWrap ? '>' : ' nowrap>') +
            '<a href="javascript:mObj[' + this.mNr + '].jump(' + item + ')" class="' +
            ((item == this.hilightItem) ? this.items[item].estilo1 : this.items[item].estilo1) +'">' +
            (this.itemBold ? '<b>' + this.items[item].text + '</b>' : this.items[item].text) +
            '</a></td></tr></table></td></tr></table>';
    return text;
  }

  this.newMenu = function() {
    var menu = '';
    var i, j;
    var obj = this.getObj('divTreeMenu' + this.mNr);
    if(obj) {
      if(this.autoClose && this.curItem >= 0) {
        if(this.items[this.curItem].level <= 1) {
          for(i = 0; i < this.items.length; i++) {
            if(i != this.curItem && this.items[i].node && this.items[i].level == 1) {
              this.items[i].icon = this.items[i].img1;
            }
          }
        }
      }
      for(i = 0; i < this.items.length; i++) {
        menu += this.content(i);
        if(this.items[i].icon == this.items[i].img1 || this.items[i].icon == this.iconClosedHilight) {
          for(j = i+1; j < this.items.length && this.items[j].level > this.items[i].level; j++);
          i = j - 1;
        }
      }
      obj.innerHTML = menu;
    }
  }

  this.buildContainer = function() {
    var lnk1 = '.cssLink' + this.mNr;
    var lnk2 = '.cssLinkHilight' + this.mNr;
    document.write('<style> .cssMenu' + this.mNr + ' { ' +
                   'width: ' + this.width + 'px; ' +
                   (this.bgColor ? 'background-color: ' + this.bgColor + '; ' : '') +
                   '} ' + lnk1 + ', ' + lnk1 + ':visited, ' + lnk1 + ':active { ' +
                   'color: ' + this.itemColor + '; ' +
                   'font-family: ' + this.itemFont + '; ' +
                   'font-size: ' + this.itemSize + 'px; ' +
                   'text-decoration: none; ' +
                   '} ' + lnk1 + ':hover { ' +
                   'text-decoration: underline; ' +
                   '} ' + lnk2 + ', ' + lnk2 + ':visited, ' + lnk2 + ':active { ' +
                   'color: ' + this.itemActive + '; ' +
                   'font-family: ' + this.itemFont + '; ' +
                   'font-size: ' + this.itemSize + 'px; ' +
                   (this.itemActiveUnderline ? 'text-decoration: underline; ' : '') +
                   '} </style>' +
                   '<div id="divTreeMenu' + this.mNr + '" ></div>');
  }

  this.createStructure = function() {
    for(i = 0; i < this.items.length; i++) {
      if(!this.items[i].icon) {
        if(i < this.items.length-1 && this.items[i+1].level > this.items[i].level) {
          this.items[i].icon = this.openAll ? this.items[i].img2 : this.items[i].img1;
          this.items[i].node = true;
        }
        else this.items[i].icon = this.items[i].img1;
      }
    }
  }

  this.create = function() {
    this.mNr = mObj.length;
    if(mObj[this.mNr] = this) {
      this.buildContainer();
      this.createStructure();
      this.newMenu();
    }
    else alert("Could not create menu!");
  }

this.readCookie = function(name) {
	if (Sacacookie() != "undefined")
	return Sacacookie();
	
	return null;
}

this.retornaid = function(idunico) {
	for(i = 0; i < this.items.length; i++)
	{
		if (this.items[i].idunico==idunico)
		return i;
	}
	return -1;
}

this.createCookie = function(name,value,days) {
	/*if (days)
	{
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	var ck = name+"="+value+expires+"; path=/";
	//if (days != -1) 
	//alert('Cookie\n' + ck + '\ncreated');
	document.cookie = ck;
	*/
	Ponecookie(value);
	
}

  this.jumpTo = function() {
    var pos, curPos, i;
    var item = 0;
    var level = 1;
    this.curItem = -1;
    if(arguments == null) arguments = this.jumpTo.arguments;
    for(i = 0; i < arguments.length; i++, level++) {
      pos = arguments[i];
      for(curPos = 0; item < this.items.length && curPos < pos; item++) {
        if(this.items[item].level == level) curPos++;
      }
      if(curPos == pos) {
        item -= 1;
        this.openMenu(item);
      }
    }
    if(item) this.jump(item);
  }
  //------------------------------------------------------------------------
}

function makeItem(level, text, url, target, onClick, openAll,img1,img2,estilo1,estilo2,estilo3,idunicoval) {
  this.level = level;
  this.text = text.replace('&bajalinea;','<br>');


  this.url = url;
  this.target = target;
  this.onClick = onClick;
  this.icon = '';
  //this.node = false;
  this.img1 = img1;
  this.img2 = img2;
  this.estilo1 = estilo1;
  this.estilo2 = estilo2;
  this.estilo3 = estilo3;
  this.idunico = idunicoval;
//alert(idunicoval);


}

function desactivamenu()
{
  this.estoyenelmenu=-1;
}

function popup(param1,param2,param3)
{
window.open(param1,param2,param3);

}
//----------------------------------------------------------------------------------------------------
