
function reset_submit(formName) {
	Form.disable(formName);
	$(formName).submit();
}

function add_uploader(arr_clas) {
  id_div = "uploaders";
  class_uploader = "uploader";
  id_uploader = "uploader_";
  id_input = "fichero_";
  suf_clas = "_clas";
  uldr = $(id_div).getElementsByClassName(class_uploader);
  num_uploader = uldr.length + 1;
  select =
    "<select id='" + id_input + num_uploader + suf_clas + "' name ='" + 
    id_input + num_uploader + suf_clas + "' >\n";
  for(var i=0 ; i<arr_clas.length; i++)
    select += " <option value='"+arr_clas[i][0] + "'>"+arr_clas[i][1] + "</option>\n";
  select +="</select>\n";
  new Insertion.Bottom(
    id_div,
    '\n'+
    '<div id="' + id_uploader + num_uploader+'">'+
      ' <a href="#" onclick="del_uploader(\'' + id_uploader + num_uploader + '\'); '+
      'return false;">eliminar</a>' +
      " " + select +
      '<input type="file" class="' + class_uploader + '" name="' + id_input 
      + num_uploader+'" '+
      'id="' + id_input + num_uploader+'" title="Engadir" /> ' +
    '</div>'
  );
}

function del_uploader(a) {
  $(a).remove();
}


function traducir_noticia(locale) {
	form = $('form_noticia');
	
	if (locale == undefined) {
		locale = $F('culture');
	}
	$('ir_traduccion').setValue(locale);
	
	form.submit();
}

function traducir_galeria(locale) {
	form = $('form_galeria');
	
	if (locale == undefined) {
		locale = $F('culture');
	}
	$('ir_traduccion').setValue(locale);
	
	form.submit();
}

function traducir_elemento(locale) {
	form = $('form_elemento');
	
	if (locale == undefined) {
		locale = $F('culture');
	}
	$('ir_traduccion').setValue(locale);
	
	form.submit();
}

function guardar_continuar(id_form) {
	form = $(id_form);
	$('continuar').setValue(true);	
	form.submit();
}



Event.observe(window, 'load',
    function() {
	
		visores = $$('.visor_fotos');
		
		for (i=0; i< visores.length; i++) {

			// Poner el offset a 0 al refrescar
			lst = visores[i].readAttribute("id").split("_");
			ga_id = lst[1];
			$("offsetActual_" + ga_id).value = 0;
			
			elemPrev = visores[i].getElementsByClassName('goPrev');			
			elemNext = visores[i].getElementsByClassName('goNext');

			
			if (visores[i].hasClassName('coleccion') == true) {
				if ((elemPrev != null) && (elemPrev[0] != undefined)) {
					Event.observe(elemPrev[0], "click", goPrevColeccion);
				}
				if ((elemNext != null) && (elemNext[0] != undefined)) {
					Event.observe(elemNext[0], "click", goNextColeccion);
				}
			}
			else {
				if ((elemPrev != null) && (elemPrev[0] != undefined)) {
					Event.observe(elemPrev[0], "click", goPrev);
				}
				if ((elemNext != null) && (elemNext[0] != undefined)) {
					Event.observe(elemNext[0], "click", goNext);
				}
			}

		}
          
    });
/*
 * FUNCIONES MOVER GALERIAS
 */
function goPrev(evento) {
	
	el_controlador = Event.element(evento);
	lst = el_controlador.readAttribute("id").split("_");
	
	tipo_id = lst[2];
	offset = $("offsetActual_" + tipo_id).value;
	offset = parseInt(offset) - 1;
	
	
	if(offset < 0) {
		return false;
	}
	
	if (offset == 0) {
		$('img_goPrev_' + tipo_id).src = '/images/general/flecha_atras_nor.gif'; 
		$('img_goPrev_' + tipo_id).addClassName('noClick');
	}
	
	if ($('img_goNext_' + tipo_id).hasClassName('noClick'))
	{
		$('img_goNext_' + tipo_id).src = '/images/general/flecha_adelante_act.gif'; 
		$('img_goNext_' + tipo_id).removeClassName('noClick');
	}
  
	if (offset >= 0) {		
		$("offsetActual_" + tipo_id).value = offset;
		moverTira(tipo_id, offset, false);
	}
	return false;
}

function goNext(evento) {
	
	el_controlador = Event.element(evento);
	lst = el_controlador.readAttribute("id").split("_");
	
	tipo_id = lst[2];
	
	offset = $("offsetActual_" + tipo_id).value;
	offset = parseInt(offset) + 1;
	offsetMaximo = $("offsetMax_" + tipo_id).value;	
	
	
	if (offsetMaximo <= 0)
		return false;
    
	if ($('img_goPrev_' + tipo_id).hasClassName('noClick'))
	{
		$('img_goPrev_' + tipo_id).src = '/images/general/flecha_atras_act.gif'; 
		$('img_goPrev_' + tipo_id).removeClassName('noClick');
	}
  
	if (offset == offsetMaximo) {
		$('img_goNext_' + tipo_id).src = '/images/general/flecha_adelante_nor.gif';  
		$('img_goNext_' + tipo_id).addClassName('noClick');
	}
  
	if (offset <= offsetMaximo) {
		$("offsetActual_" + tipo_id).value = offset;
		moverTira(tipo_id, offset, true);
	}
  
	return false;
	
}


function moverTira(tipo_id, offset) {
    ajaxurl = "/index.php/portada/ajaxImagenes";

    new Ajax.Request(ajaxurl, {
        method:'get', 
        encoding:'UTF-8',
        asyncronous: false,
        evalJSON: true,
        parameters: {tipo_id: tipo_id,
                     offset: offset},
        onCreate: function(){
          for (pos=1; pos<4; pos++) {
	          elem = $('foto_' + tipo_id + "_" + pos);
	          elem.src = '/images/general/loader_grande.gif';
          }
        },
        onFailure:function(){
          //alert('Fallo carga fotos');
        },
        onComplete: function(transport, json){
          lst_fotos = json;

          for (i=0; i<3; i++)
          {        	
            if(lst_fotos[i] == undefined) {
	            elem = $('foto_' + tipo_id + "_" + (i+1));
	            elem.src = '/images/general/pixel.gif';
	            elem.setAttribute("alt", '');
	            elem.setAttribute("title", '');
	            
	            $('texto_' + tipo_id + "_" + (i+1)).update('');
        		
	            elemOcul = $('full_' + tipo_id + "_" + (i+1));            
	            elemOcul.value = '';
            }
            else { 
	        	ruta_foto = lst_fotos[i][0];  
	            ruta_thum = lst_fotos[i][1];
	            texto_alt = lst_fotos[i][2];
	            
	            elem = $('foto_' + tipo_id + "_" + (i+1));
	            elem.src = "/ficheros/" + ruta_thum;
	            elem.setAttribute("alt", texto_alt);
	            elem.setAttribute("title", texto_alt);
	            
	            $('texto_' + tipo_id + "_" + (i+1)).update(texto_alt);
	            		
	            elemOcul = $('full_' + tipo_id + "_" + (i+1));            
	            elemOcul.value = ruta_foto;
            } 
          }
        }
    });
}
