/** EVENTOS POSIBLES EN ESTA PAGINA **/
/**
 * - Carga de la pagina: han de aparecer los temas generales y las faq del tema 'general', si hay: init_default
 * - Carga de la pagina con parametro "id": ha de aparecer los temas de la coleccion y el tema general de la coleccion : init_idcol
 * - Cambio en el desplegable de clecciones: select_change
 * - Click en alguna categoria: click_default -- este es el evento que se asume por defecto
 *
 */

function prepareTemasPfrecuentes(request,json)
{
  // item sobre el que se ha hecho 'click'
  var idcat = json.idcat_padre ;
  var idcol = json.idcol ;
  var event = json.event ; // el evento que ha originado la accion. 'init_default|init_idcol|select_change|click_default
  /**
   * eventos posibles:
   * select_change: cuando se cambia el select, eligiendo una coleccion. No hay que marcar nada como rojo.
   * init_default: cuando se carga la pagina. hay que marcar el 'general' como rojo.
   */

  // div sobre el que se hara el listado de preguntas
  switch(event)
  {
    case 'select_change':
      break;
    case 'init_idcol':
      if($('idcol'))
      {
        $A($('idcol').options).find(function(opt){return opt.value == idcol ;}).selected = true ;
      }
      break;
    case 'init_default':
      if($('idcol'))
      {
        $('idcol').options[0].selected = true ;
      }
      break;
    case 'click_default':
      if($('idcol'))
      {
        $('idcol').options[0].selected = idcol == 0 ? true : false ;
      }
      break;
    default:
      break;
    }

  var idcatcol = 'idcat_'+idcat+'_idcol_'+idcol ;
  var parentIdCat = idcat != 0 ? findParentIdCat ( $('item_'+idcatcol) ) : 0 ;
  var parentIdCatCol = 'idcat_'+parentIdCat+'_idcol_'+idcol ;
  // generamos array con todos los idcat del item y sus ancestros.
  var meAndMyParents = new Array (idcat);
  if(idcat != 0 && parentIdCat == 0 )
  {
    meAndMyParents.push ('0') ;
  }
  else if(idcat != 0)
  {
    meAndMyParents = diggParents (meAndMyParents, $('item_'+parentIdCatCol) ) ;
  }
  var itemDivs = document.getElementsByClassName('item') ;
  itemDivs.each(function(divObj)
		{
		  divObj.down('a').setStyle({color:'',fontWeight:''}) ;
		  var divIdCol = divObj.id.substr(divObj.id.lastIndexOf('_')+1) ;
		  var parentIdCat = findParentIdCat(divObj) ;
		  var isAncestor = false ;
		  var isRoot = divIdCol == 0 && parentIdCat == 0 ? true : false ;
		  meAndMyParents.each(function(cat)
				      {
					isAncestor = cat == parentIdCat && divIdCol == idcol ? true : false ;
					if (isAncestor)
					  {
					    throw $break ;
					  }
				      }
				      );
		  if (!isAncestor && !isRoot )
		    {
		      divObj.remove () ;
		    }
		}
		) ;
  // colores alternos
  alterna('item');
  // el primer nivel lo quieren todo en gris. Caprichos.
  agris('parent_0');
  // si hay item "pinchado" lo marcamos rojo
  if($('item_'+idcatcol) )
  {
    $('item_'+idcatcol).down('a').setStyle({color:'#CB260F',fontWeight:'bold'});
  }
  // ponemos el titulo de la coleccion (si hay)
  $('tituloColeccion').innerHTML = json.tituloCol ;

  // generamos las preguntas
  if(idcol >= 0)
  {
    var op_preguntas = new Ajax.Updater('preguntas',
                                        url_pfrecuentes+'?idcat='+idcat+'&idcol='+idcol+'&event='+event,
                                        {method: 'post'}
                                        ) ;
  }

}

function agris(nombre)
{
  lineas = document.getElementsByClassName(nombre);
  for(var i=0;i<lineas.length;i++)
  {
    cambiaFondo(lineas[i],'gris');
  }
}

/**
 * funcion para busar la ascendencia de un item con cierto idcat, idcol
 */
function diggParents(meAndMyParents,myItem)
{
  var myClasses = Element.classNames(myItem) ;
  var idcol = myItem.id.substr (myItem.id.lastIndexOf('_'+1)) ;
  var idcat = myItem.id.substring (myItem.id.indexOf('idcat_') + 6 , myItem.id.indexOf('_idcol') )  ;
  var myParent = findParentIdCat (myItem) ;
  meAndMyParents.push ( idcat ) ;
  idcatcolParent = 'idcat_'+myParent+'_idcol_'+idcol ;
  if ( myParent != 0 )
    {
      meAndMyParents = diggParents ( meAndMyParents,$('item_'+idcatcolParent) );
    }
  else
    {
      meAndMyParents.push( '0' ) ;
    }
  return meAndMyParents ;

}

/**
 * function para obtener el idcat del padre
 * @param element el item
 * @return int idcat el idcat del padre
 * @author JoeZ
 */
function findParentIdCat ( itemObj )
{
  var classes = Element.classNames (itemObj) ;
  var parentClass = classes.find (function (clN){return clN.indexOf ('parent_') == 0 } ) ;
  var parentIdCat = parentClass.substr (parentClass.lastIndexOf ('_') + 1 ) ;
  return parentIdCat ;
}

