// Objeto: DhtmlMenu
// Version: 1.1
// Fecha:    24 julio 2002


function DhtmlMenu(in_name) {
   this.name = in_name;
   this.view = new MenuView();
   this.model = new MenuModel(); 
   this.getView = M_getView; 
   this.getModel = M_getModel;
   this.getHtml = M_getHtml;
   this.getNetscapeHtml = M_getNetscapeHtml;
   this.getDefaultHtml = M_getDefaultHtml;
}

function M_getView() {
   return this.view;
}

function M_getModel() {
   return this.model;
}

function M_getHtml() {
   if (document.layers) {
      return this.getNetscapeHtml();
   }
   else {
      return this.getDefaultHtml();
   }
}

function M_getDefaultHtml() {
   var bounds = this.view.getBounds();
   var html = "<DIV ID='MenuDiv' STYLE='position:absolute;  ";
   html = html +  "font-family: Arial; font-size: 13px; color: black; background-color:";
   html = html +  this.view.getBgColor() + "; layer-background-color:";
   html = html +  this.view.getBgColor() + "; left:" + bounds[0] + "; top:";
   html = html + bounds[1] + "; width:" + bounds[2] + "; height:" + bounds[3] + ";'>";
   html = html + "<br><div id='MenuHeader' STYLE='position:absolute;  background-color:";
   html = html +  this.view.getHeaderBgColor() + "; layer-background-color: " + this.view.getHeaderBgColor();
   html = html + "; left:0; top:0; height:10; width:" + bounds[2] + ";'>";
   var headerClass = "txtmTCT";
   //var lrange = (this.name.length - (this.name.length % 10))/10;
   var lrange = parseInt(this.name.length/27);
   var temporal;
   switch (lrange){
   	case 1:temporal = 1;break;
   	case 2:temporal = 2;break;
   	case 3:temporal = 3;break;
   	case 4:temporal = 4;break;
   	case 5:temporal = 5;break;
   	case 6:temporal = 6;break;
   }
   html = html + "<table border=0><tr><td><img src=img/sp.gif width=3 height=1></td><td><font class=" + headerClass + ">" + this.name + "</font></td></tr></table></div><img src=img/sp.gif width=3 height=3><br>"
    if(lrange>0){
		for(var t = 0; t<temporal; t++) {
		html = html + "<br>";
		}	  
	  }
   for(var i = 0; i < this.model.getSize(); i++) {
      var url = this.model.getItem(i).getUrl();
      var label = this.model.getItem(i).getLabel();
      var ih = this.view.getItemHeight();
      var classStr = "";
      var onColor = "";
      classStr = (this.model.getItem(i).isSelected()) ? this.view.getImageClass() + "on"
                                                      : this.view.getImageClass() + "off";
      label = "<a href='" + url + "' class='txtMen'>" + label + "</a>";
      var numStr = (i + 1);
      numStr = (numStr < 10) ? "0" + numStr : numStr;
      html = html + "<div id='MI" + i + "' onMouseOver=\"this.style.backgroundColor='" + this.view.getSelectionColor();
      html = html + "'\"  onMouseOut=\"this.style.backgroundColor='white'\">";
      html = html + "<table border=0 cellpadding=0 cellspacing=1 width='" + (bounds[2] - 10) + "'><tr><td rowspan=2>";
      html = html + "<img src='img/sp.gif' width='5' height='1'></td><td valign=top></td><td width='100%'><font class="; 
      html = html + classStr + ">&nbsp;" + numStr + "&nbsp;</font>&nbsp;" + label;
      html = html + "</td><tr><td></td><td class='tramg'><img src='img/sp.gif' width='1' height='1'></td></table>";
      html = html + "</div>";
     
  	  
	  if (this.model.getItem(i).isSelected() && this.model.getItem(i).hasChildren()) {
         var children = this.model.getItem(i).getChildren();
         for (var j = 0; j < children.length; j++) {
            var suburl = children[j].getUrl();
            var sublabel = children[j].getLabel();
            var imgUrl = children[j].isSelected() ? "ic_fl2on.gif" : "ic_fl2off.gif";
            sublabel = "<a href='" + suburl + "' class='txtDef'>" + sublabel + "</a>";
            html = html + "<div id='MI" + i + "_" + j + "' onMouseOver=\"this.style.backgroundColor='"; 
            html = html + this.view.getSelectionColor() + "'\"  onMouseOut=\"this.style.backgroundColor='white'\">";
            html = html + "<table border=0 cellpadding=0 cellspacing=1 width='" + (bounds[2] - 10);
            html = html + "'><tr><td rowspan=2><img src='img/sp.gif' width='2' height='1'></td><td rowspan=2>";
            html = html + "<img src='img/sp.gif' width=9 height=1></td><td valign=top></td><td width='100%'>";
            html = html + "<img src='img/" + imgUrl + "' width='9' height='7' hspace=3>" + sublabel; 
            html = html + "</td><tr><td></td><td class='tramg'><img src='img/sp.gif' width='1' height='1'></td></table>";
            html = html + "</div>";            
         }
      }
   }
   html = html + "";
   html = html + "</DIV>";
   return html;
}

function M_getNetscapeHtml() {
   var bounds = this.view.getBounds();
   var html = "<br><div id='MenuHeader'>";
   html = html + "<table border=0 cellpadding=0 cellspacing=0 width='" + bounds[2] + "'><tr><td width='100%' bgcolor='";
   html = html + this.view.getHeaderBgColor() + "' class=txtmTCT><b>" + this.name + "</b></td></tr></table></div>";
   for(var i = 0; i < this.model.getSize(); i++) {
      var url = this.model.getItem(i).getUrl();
      var label = this.model.getItem(i).getLabel();
      var ih = this.view.getItemHeight();
      var classStr = "";
      var onColor = "";
      classStr = (this.model.getItem(i).isSelected()) ? this.view.getImageClass() + "on"
                                                      : this.view.getImageClass() + "off";
      label = "<a href='" + url + "' class='txtMen'>" + label + "</a>";
      var numStr = (i + 1);
      numStr = (numStr < 10) ? "0" + numStr : numStr;
      html = html + "<div id='MI" + i + "'>";
      html = html + "<table border=0 cellpadding=0 cellspacing=1 width='" + (bounds[2] - 10); 
      html = html + "'><tr><td rowspan=2><img src='img/sp.gif' width='5' height='1'></td><td valign=top></td>";
      html = html + "<td width='100%'><font class=" + classStr + ">&nbsp;" + numStr + "&nbsp;</font>&nbsp;" 
      html = html + label+ "</td><tr><td></td><td class='tramg'><img src='img/sp.gif' width='1' height='1'></td></table>";
      html = html + "</div>";
      if (this.model.getItem(i).isSelected() && this.model.getItem(i).hasChildren()) {
         var children = this.model.getItem(i).getChildren();
         for (var j = 0; j < children.length; j++) {
            var suburl = children[j].getUrl();
            var sublabel = children[j].getLabel();
            var imgUrl = children[j].isSelected() ? "ic_fl2on.gif" : "ic_fl2off.gif";
            sublabel = "<a href='" + suburl + "' class='txtDef'>" + sublabel + "</a>";
            html = html + "<div id='MI" + i + "_" + j + "'>";
            html = html + "<table border=0 cellpadding=0 cellspacing=1 width='" + (bounds[2] - 10);
            html = html + "'><tr><td rowspan=2><img src='img/sp.gif' width='2' height='1'></td><td rowspan=2>";
            html = html + "<img src='img/sp.gif' width=9 height=1></td><td valign=top></td><td width='100%'><img src='img/"; 
            html = html + imgUrl + "' width='9' height='7' hspace=3>" + sublabel + "</td><tr><td></td><td class='tramg'>";
            html = html + "<img src='img/sp.gif' width='1' height='1'></td></table>";
            html = html + "</div>";            
         }
      }
   }
   html = html + "";
   return html;
}


///////////////////////////////

function MenuView() {
   this.bounds = new Array(10,10,100,100);
   this.bgColor = "white";
   this.headerBgColor = "white";
   this.selectionColor = "red";
   this.imageClass = "UNDEFINED";
   this.itemHeight = 20;
   this.getBounds = MV_getBounds;
   this.setBounds = MV_setBounds;
   this.getBgColor = MV_getBgColor;
   this.setBgColor = MV_setBgColor;
   this.getHeaderBgColor = MV_getHeaderBgColor;
   this.setHeaderBgColor = MV_setHeaderBgColor;
   this.getSelectionColor = MV_getSelectionColor;
   this.setSelectionColor = MV_setSelectionColor;
   this.getItemHeight = MV_getItemHeight;
   this.setItemHeight = MV_setItemHeight;
   this.getImageClass = MV_getImageClass;
   this.setImageClass = MV_setImageClass;
}

function MV_getBounds() {
   return this.bounds;
}

function MV_setBounds(in_x1, in_y1, in_x2, in_y2) {
   this.bounds[0] = in_x1;
   this.bounds[1] = in_y1;
   this.bounds[2] = in_x2;
   this.bounds[3] = in_y2;
}

function MV_setItemHeight(in_height) {
   this.itemHeight = in_height;
}

function MV_getItemHeight() {
   return this.itemHeight;
}

function MV_getBgColor() {
   return this.bgColor;
}

function MV_setBgColor(in_color) {
   this.bgColor = in_color;
}

function MV_getHeaderBgColor() {
   return this.headerBgColor;
}

function MV_setHeaderBgColor(in_color) {
   this.headerBgColor = in_color;
}

function MV_getSelectionColor() {
   return this.selectionColor;
}

function MV_setSelectionColor(in_color) {
   this.selectionColor = in_color;
}

function MV_getImageClass() {
   return this.imageClass;
}

function MV_setImageClass(in_name) {
   this.imageClass = in_name;
}

///////////////////////////////

function MenuModel() {
   this.items = new Array(); 
   this.addItem = MM_addItem;
   this.getItem = MM_getItem;
   this.getSize = MM_getSize;
   this.setSelectedLevel = MM_setSelectedLevel;
}

function MM_addItem(in_item) {
   this.items[this.items.length] = in_item;
}

function MM_getItem(in_num) {
   return this.items[in_num];
}

function MM_getSize() {
   return this.items.length;
}

function MM_setSelectedLevel(in_level) {
   for (var i = 0; i < this.items.length; i++) {
      this.items[i].setSelected((i == in_level) ? "1" : "0");
   }
}


///////////////////////////////

function MenuItem(in_url, in_label) {
   this.url = in_url;
   this.label = in_label;
   this.selected = 0;
   this.children = new Array();
   this.getUrl = MI_getUrl; 
   this.getLabel = MI_getLabel; 
   this.addChild = MI_addChild; 
   this.hasChildren = MI_hasChildren; 
   this.getChildren = MI_getChildren; 
   this.isSelected = MI_isSelected; 
   this.setSelected = MI_setSelected; 
}

function MI_getUrl() {
   return this.url;
}

function MI_getLabel() {
   return this.label;
}

function MI_addChild(in_child) {
   this.children[this.children.length] = in_child;
}

function MI_getChildren() {
   return this.children;
}

function MI_hasChildren() {
   return (this.children.length > 0);
}

function MI_isSelected() {
   return (this.selected == 1);
}

function MI_setSelected(in_val) {
   this.selected = in_val;
}

