/*
This script sets all but a specific number of rows to display = "none"
Only rows which are parents to input-fields with the specified
class name are affected. (Default is autoHide).
When the last visible row is typed into,
the next is automatically made visible.
*/

/* Version 1.3 - Converted to class, uses c_utilities.js, now handles both TABLE:s and UL:s */
/* Version 1.2 - Handles both INPUT and TEXTAREA-tags */
/* Version 1.1 - Handles multiple autoHide-tables per page */
/* Version 1.0 - Handles only one table per page */


var HideAndReveal = {

  normalFieldClass : "autoHide", //can be edited
  hideAndRevealTableClass : "hideAndReveal",

  //If set to false the will only search for
  //inputFields where normalFieldClass is the only class.
  allowMultipleClassSearch : true, //can be edited
  defaultKeepRows : 1,

  debug : true, //can be edited. Should be set to false on release
  debugPreString : "c_hideandreveal.js: ", //can be edited
  debugPostString : "-error: ", //can be edited

  hideNormalFields : function(keepThisMany, inputClass, multipleClasses) {
    try {
        if(keepThisMany == undefined || isNaN(keepThisMany)) keepThisMany = HideAndReveal.defaultKeepRows;
        if(inputClass != undefined) HideAndReveal.normalFieldClass = inputClass;
        if(multipleClasses != undefined) HideAndReveal.allowMultipleClassSearch = multipleClasses;
        var normalFields = Utilities.getElementsByClassAndTagName(HideAndReveal.normalFieldClass, new Array("INPUT", "TEXTAREA"));
        /*TODO: concat
        var containNormalFields = Utilities.getElementsByClassAndTagName(HideAndReveal.hideAndRevealTableClass, new Array("TABLE", "DIV"));
        for(var x=0; x<containNormalFields.length; x++) {
        var moreNormalFields = containNormalFields[x].getElementsByTagName("INPUT");
        for(var y=0; y<moreNormalFields.length; y++) {
          
        }
        normalFields = normalFields.concat();
        }*/
        
        
        if(normalFields.length > 0) {
          //Gets an array with the unique tables and rows containing the normalFields
          var normalFieldTables = Utilities.getUniqueContainerWithItems(normalFields, "TABLE", "TR", 4);
          //Gets an array with the unique lists and listitems containing normalFields and adds it to normalFieldTables
          normalFieldTables = normalFieldTables.concat(Utilities.getUniqueContainerWithItems(normalFields, "UL", "LI", 5));
          
          for(var i=0; i<normalFieldTables.length; i++) {
            //A fake argument is a fictive argument in the CSS-class. E.g. in the CSS-class autoHide_X where X would be the fake argument.
            var keepThisManyByClassArgument = HideAndReveal.getKeepThisManyFakeArgument(normalFieldTables[i][0]);
            if(keepThisManyByClassArgument != -1) keepThisMany = keepThisManyByClassArgument;
            var actuallyKeepThisMany = keepThisMany;
            
            //The rows before a row with content should not be hidden
            var lastRowWithContent = HideAndReveal.getLastRowWithContent(normalFieldTables[i]);
            if((Number(lastRowWithContent) + 1) >= keepThisMany) actuallyKeepThisMany = (Number(lastRowWithContent) + 2);
            HideAndReveal.hideAllBut(actuallyKeepThisMany, normalFieldTables[i]);
          }
          HideAndReveal.addAutomaticRowEventsTo(normalFields);
        }
      } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "hideNormalFields" + HideAndReveal.debugPostString + err);}
  },
  
  hideAllBut : function(theFirstX, elementsArray) {
    try {
      for(var i=theFirstX; i<elementsArray.length; i++) {
      /* Uncomment to hide all empty rows
      var rowInputs = getElementsByTagAndType("INPUT", "text", elementsArray[i]);
      if(checkIfEmpty(rowInputs)) elementsArray[i].style.display = "none";
      */
        elementsArray[i].style.display = "none";
      }
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "hideAllBut" + HideAndReveal.debugPostString + err);}
  },
  
  revealNextRow : function(normalField) {
    try {
      var row = Utilities.getKin(normalField, "parent", "TR", undefined, 2); //Try to find parent row (max 2 steps up)
      if(!row) row = Utilities.getKin(normalField, "parent", "LI", undefined, 4); //Otherwise try parent listItem (max 4 steps up)
      var nextRow = Utilities.getKin(row, "next");
      if(nextRow !== null && nextRow.style.display == "none") nextRow.style.display = "";
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "revealNextRow" + HideAndReveal.debugPostString + err);}
  },
  
  getLastRowWithContent : function(rows) {
    try {
      var foundAt = -1;
      for(var i=0; i<rows.length; i++) {
            var isEmpty = Utilities.checkIfEmpty(Utilities.getElementsByClassAndTagName(HideAndReveal.normalFieldClass, new Array("INPUT", "TEXTAREA"), rows[i], HideAndReveal.allowMultipleClassSearch));
            if(!isEmpty) foundAt = i; //If row is not empty, update foundAt-variable.
      }
      return foundAt;
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "getLastRowWithContent" + HideAndReveal.debugPostString + err);}
  },
  
  getKeepThisManyFakeArgument : function(fromParentElement) {
    try {
      var keepThisManyByArgument = -1;
      var aNormalField = Utilities.getElementsByClassAndTagName(HideAndReveal.normalFieldClass, new Array("INPUT", "TEXTAREA"), fromParentElement, true, true);
      var fakeArguments = Utilities.getFakeClassArguments(aNormalField[0], HideAndReveal.normalFieldClass);
      if(fakeArguments.length > 1) keepThisManyByArgument = Number(fakeArguments[1]);
      return keepThisManyByArgument;
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "getKeepThisManyFakeArgument" + HideAndReveal.debugPostString + err);}       
  },
  
  addAutomaticRowEventsTo : function(elementsArray) {
    try {
      for(var i=0; i < elementsArray.length; i++) HideAndReveal.addAutomaticRowEvent(elementsArray[i]);
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "addAutomaticRowEventsTo" + HideAndReveal.debugPostString + err);}
  },
  
  addAutomaticRowEvent : function(element) {
    try {
    	element.onkeydown = function(e) {
    		  HideAndReveal.revealNextRow(Utilities.getEventTarget(e));
      };
    } catch(err) {if(HideAndReveal.debug) alert(HideAndReveal.debugPreString + "addAutomaticRowEvent" + HideAndReveal.debugPostString + err);}
  }
};

if(addOnloadEvent) addOnloadEvent(HideAndReveal.hideNormalFields);
else HideAndReveal.hideNormalFields();

