var _IMAGE_BUTTON = 0;
var _TEXT_BUTTON = 1;

var imgDir = "images/buttons/";

var buttonsArray = new Array();
var menuArray = new Array();
var extension = ".gif";
var _down = "_down";
var _over = "_over";
var _down_over = _down + _over;
var lastButtonSelected = null;
var lastMenuSelected = null;
var lastMenuClass = null;

var categorias = new Array();

var html_entities = new Array();
html_entities[html_entities.length] = new Array("'", "&#39;");
html_entities[html_entities.length] = new Array('"', "&#34;");
html_entities[html_entities.length] = new Array('&', "&#38;");
html_entities[html_entities.length] = new Array('<', "&#60;");
html_entities[html_entities.length] = new Array('>', "&#62;");

function htmlEntitiesEncode(srcText) {
  var ret = srcText;
  for (var it=0; it < html_entities.length; it++) {
    eval('ret = ret.replace(/'+html_entities[it][0]+'/,"'+html_entities[it][1]+'");');
  }
  return ret;
}

function htmlEntitiesDecode(srcText) {
  var ret = srcText;
  var a = "'";
  for (var it=0; it < html_entities.length; it++) {
    if (it == 1) a = "'"; else a = '"';
    eval('ret = ret.replace(/'+html_entities[it][1]+'/,'+a+html_entities[it][0]+a+');');
  }
  return ret;
}

function button(code) {
      // declare and initialize
      this.code = code;
      this.capCode = this.code.substring(0,1).toUpperCase() + this.code.substring(1,this.code.length);
      this.selected = false;
      this.buttonType = _IMAGE_BUTTON;

      this.img = new Image();
      this.img.src = imgDir + this.code + extension;
      this.overimg = new Image();
      this.overimg.src = imgDir + this.code + _down + extension;
      this.downimg = new Image();
      this.downimg.src = imgDir + this.code + _down + extension;
      this.downoverimg = new Image();
      this.downoverimg.src = imgDir + this.code+ _down + extension;

      buttonsArray[buttonsArray.length] = this;
      this.arrayIndex = buttonsArray.length-1;

      // accessors
      this.getcode = new Function("return(this.code);");
      this.getSelected = new Function("return(this.selected);");
      this.getArrayIndex = new Function("return(this.arrayIndex);");
      this.toHTML = new Function("return('<img src=\""+this.img.src+"\" alt=\""+this.capCode+"\" name=\""+this.code+"\" border=\"0\"/>');");

      this.setcode = new Function("newcode", "this.code = newcode;");
      this.setSelected = new Function("newSelected", "this.selected = newSelected;");
}

function flag(code, codeDesc) {
      var flagsDir = "../libCommon/imagesLib/flags/";
      // declare and initialize
      this.code = code;
      this.capCode = codeDesc;
      this.selected = false;

      this.img = new Image();
      this.img.src = flagsDir + this.code + extension;
      this.overimg = new Image();
      this.overimg.src = flagsDir + this.code + _down + extension;
      this.downimg = new Image();
      this.downimg.src = flagsDir + this.code + _down + extension;
      this.downoverimg = new Image();
      this.downoverimg.src = flagsDir + this.code+ _down + extension;

      buttonsArray[buttonsArray.length] = this;
      this.arrayIndex = buttonsArray.length-1;

      // accessors
      this.getcode = new Function("return(this.code);");
      this.getArrayIndex = new Function("return(this.arrayIndex);");
      this.toHTML = new Function("return('<img src=\""+this.img.src+"\" width=\"34\" height=\"21\" alt=\""+this.capCode+"\" name=\""+this.code+"\" border=\"0\"/>');");
}

function getButton(code) {
    for (var i=0; i < buttonsArray.length; i++) {
        if (code == buttonsArray[i].code) {
            return buttonsArray[i];
        }
    }
}

function getButtonElement(code) {
    return document.getElementsByName(code)[0];
}

function writeButton(button) {
    document.write("<a ");
    document.write("onmouseover=\"javascript:setAction('"+button.code+"', 'over')\" ");
    document.write("onmouseout=\"javascript:setAction('"+button.code+"', 'out')\" ");
    document.write("onclick=\"javascript:setAction('"+button.code+"', 'click')\" ");
    document.write(">");
    document.write(button.toHTML());
    document.write("</a>");
}

function writeFlag(button, destUrl) {
    document.write("<a ");
    document.write("onmouseover=\"javascript:setAction('"+button.code+"', 'over')\" ");
    document.write("onmouseout=\"javascript:setAction('"+button.code+"', 'out')\" ");
    document.write("href=\""+destUrl+"\">");
    document.write(button.toHTML());
    document.write("</a>&nbsp;");
}

function setAction(code, action) {
    var btn = getButton(code);
    var buttonElement = getButtonElement(code);

    if (btn == null || buttonElement == null) {
      //alert("btn->"+btn+"<->buttonElement->"+buttonElement);
      //alert("code->"+code+"<-action->"+action);
    }

    if (action == "over") {
        if (btn.buttonType == _IMAGE_BUTTON) {
          buttonElement.src = ( btn.selected ? btn.downoverimg.src : btn.overimg.src);
        } else if (btn.buttonType == _TEXT_BUTTON){
          //alert("OVER---buttonElement.className->"+buttonElement.className+"<->btn.selected->"+btn.selected);
          buttonElement.className = ( btn.selected ? btn.downOverClass : btn.overClass);
        }
    } else if (action == "out") {
        if (btn.buttonType == _IMAGE_BUTTON) {
          buttonElement.src = ( btn.selected ? btn.downimg.src : btn.img.src);
        } else if (btn.buttonType == _TEXT_BUTTON){
          //alert("OUT---buttonElement.className->"+buttonElement.className+"<->btn.selected->"+btn.selected);
          buttonElement.className = ( btn.selected ? btn.downClass : btn.normalClass);
        }
    } else if (action == "click") {
        var but = null;

        if (lastButtonSelected!=null) {
            if ((lastButtonSelected.code == code) && (buttonsArray.lenght > 1)) {
                return;
            }
            but = lastButtonSelected;
            if (btn.buttonType == _IMAGE_BUTTON) {
              getButtonElement(but.code).src = but.img.src;
            } else if (btn.buttonType == _TEXT_BUTTON){
              getButtonElement(but.code).className = but.normalClass;
            }
            but.selected = false;
            setButtonArray(but.arrayIndex, but);
        }

        but = btn;

        if (btn.buttonType == _IMAGE_BUTTON) {
          buttonElement.src = but.downimg.src;
        } else if (btn.buttonType == _TEXT_BUTTON){
          buttonElement.className = but.downClass;
        }

        but.selected = true;
        setButtonArray(but.arrayIndex, but);
        lastButtonSelected=but;

        if (but.actionStr == null || but.actionStr == "") {
          setElementSource('body', code+".html");
        } else {
          eval(but.actionStr);
        }
    }
}

function setButtonArray(idx, button) {
    buttonsArray[idx] = button;
}

function catButton(code, desc) {
      // declare and initialize
      this.code = code;
      this.capCode = htmlEntitiesEncode(desc);
      menuArray[menuArray.length] = this;
      this.arrayIndex = menuArray.length-1;
      categorias[categorias.length] = new Array (this.code, this.capCode);
      // accessors
      this.getArrayIndex = new Function("return(this.arrayIndex);");
      this.toHTML = new Function("return(\""+this.capCode+"\");");
      this.isSubCategory = new Function("return (false);");
      this.isCategory = new Function("return (true);");
      this.getClasse = new Function("return ('menuC');");
      this.getClassCode = new Function("return ('C');");
}

function subCatButton(code, desc, catId) {
      // declare and initialize
      this.code = code;
      this.capCode = htmlEntitiesEncode(desc);
      this.catId = catId;
      menuArray[menuArray.length] = this;
      this.arrayIndex = menuArray.length-1;
      // accessors
      this.getArrayIndex = new Function("return(this.arrayIndex);");
      this.toHTML = new Function("return(\""+this.capCode+"\");");
      this.isSubCategory = new Function("return (true);");
      this.isCategory = new Function("return (false);");
      this.getClasse = new Function("return ('menuS');");
      this.getClassCode = new Function("return ('S');");
}

function writeMenu(button) {
    document.write("<tr><td ");
    document.write("class='"+button.getClasse()+"' ");
    document.write("onmouseover=\"javascript:NF_setMenuItem(this, '"+button.getClassCode()+"');\" ");
    document.write("onmouseout=\"javascript:NF_unsetMenuItem(this, '"+button.getClasse()+"');\" ");
    if (button.isCategory()) {
      document.write("onclick=\"javascript:selectedCompanyCategoryId="+button.code+";NF_setBCCatetogy('"+button.toHTML()+"');NF_setNewMenuItem(this, '"+button.getClasse()+"', '"+button.getClassCode()+"');\" >");
    } else {
      var tmp = "onclick=\"javascript:selectedCompanySubCategoryId="+button.code+";NF_setBCSubCatetogy("+button.catId+",'"+button.toHTML()+"');NF_setNewMenuItem(this, '"+button.getClasse()+"', '"+button.getClassCode()+"');\" >";
      document.write(tmp);
    }
    document.write(htmlEntitiesDecode(button.toHTML()));
    document.write("</td></tr>");
}

function writeTextButton(button) {
  var htmlTxt = "<td width='"+button.tdWidth+"' height='30' name='"+button.code+"' class='"+button.normalClass+"' id='"+button.code+"' ";
  htmlTxt += "onmouseover=\"javascript:setAction('"+button.code+"', 'over');\" ";
  htmlTxt += "onmouseout=\"javascript:setAction('"+button.code+"', 'out');\"  ";
  htmlTxt += "onclick=\"javascript:setAction('"+button.code+"', 'click');\"> ";
  htmlTxt += button.toHTML();
  htmlTxt += "</td>";
  document.write(htmlTxt);
}

function textButton(code, desc, tdWidth, actionStr) {
      // declare and initialize
      this.code = code;
      this.capCode = desc;
      this.selected = false;
      this.tdWidth = tdWidth;
      this.normalClass = 'normalButton';
      this.overClass = 'overButton';
      this.downClass = 'downButton';
      this.downOverClass = 'downOverButton';
      this.buttonType = _TEXT_BUTTON;
      this.actionStr = actionStr;

      buttonsArray[buttonsArray.length] = this;
      this.arrayIndex = buttonsArray.length-1;

      // accessors
      this.getcode = new Function("return(this.code);");
      this.getSelected = new Function("return(this.selected);");
      this.getArrayIndex = new Function("return(this.arrayIndex);");
      this.toHTML = new Function("return('"+this.capCode+"');");

      this.setcode = new Function("newcode", "this.code = newcode;");
      this.setSelected = new Function("newSelected", "this.selected = newSelected;");
}
