Event.observe(window, 'load', function() {
    $$('a[rel="external"]').each(function(link){
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#'){
            link.writeAttribute('target','_blank');
        }
    });
});



$(document.body).observe('click', function(e) {
	var a = e.findElement('a');
	  if (a) { var metodo = e.findElement('a[method]');
		 var metodo_id = e.findElement('a[id]');
		 var metodo_temp = '';
		 var existeMetodo= '';
		 if (metodo != null ){
			 existeMetodo = a.getAttribute('method');
		 }else if (metodo_id != null){
			 metodo_temp = a.getAttribute('id').split('-')[0];
		 }
		 if ( existeMetodo != ''){	
			  	e.stop();
				new Ajax.Request(a.href, { method: existeMetodo });
		 }
	     
	  }
	  // Segun Framework si existe un elemento de tipo imagen que tenga
	  // el nombre de atributo imagen, la segunda imagen si es diferente de null
	  // es de tipo popup
	  if (e.findElement('img')) {
		  a = e.findElement('img').getAttribute('id');
		  	
		  	if (a != null && a.include("imagen")){
				 var idImagen2 = a.split('-')[1];
				 if(idImagen2 != ''){
					 var llamadaUrl = $('contextoImagen').readAttribute('href') + idImagen2;
					 Modalbox.show(llamadaUrl, {title: e.findElement('img').getAttribute('title'), width: 800, height:650});
				 }
		  	}
		  
		  	if (a != null && a.include("mapa")){
				 var tipoDocumento = a.split('-')[1];
				 var idDocumento = a.split('-')[2];
				 if(idImagen2 != ''){
					 var llamadaUrl = $('contextoOtros').readAttribute('href') + tipoDocumento+"-"+idDocumento;
					 Modalbox.show(llamadaUrl, {title: e.findElement('img').getAttribute('title'), width: 800, height:650});
				 }
		  	}
		   	if (a != null && a.include("categoria")){
		 		  var tipoDocumento = a.split('-')[1];
				  $('formularioEnvio').writeAttribute('action',  $('href-categoria-'+tipoDocumento).href);
				  $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
				
		  	}  	
	  } 
});

$(document.body).observe('click', function(e) {
	 var a = e.findElement('a');
	 if (a) {
		 var metodo_id = a.getAttribute('id');
		 if (metodo_id != null){
			 
			 if (metodo_id ==  'eliminar' ){
		  		e.stop();
				var url = e.findElement('a[href]');
				$('formulario').writeAttribute('action',url);
				$('formulario').writeAttribute('method','POST');
				$('formulario').submit();
			 }
			 
			 if ( metodo_id == 'popup' ){
				 e.stop();
				 var llamadaUrl = e.findElement('a').getAttribute('href');
				 Modalbox.show(llamadaUrl, {title: e.findElement('a').getAttribute('title'), width: 800, height:600});
			 }
			
			 if ( metodo_id == 'noticia' ){
				 e.stop();
				 var llamadaUrl = e.findElement('a').getAttribute('href');
				 Modalbox.show(llamadaUrl, {title: 'Detalle', width: 800, height:600}); 
			 }
						 
		 }
	  }
});

/*··HOOYOS···*/ 
if ( $('id_hoyoaaa1') != null){
	$('id_hoyoaaa1').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo1').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa2') != null){
	$('id_hoyoaaa2').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo2').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa3') != null){
	$('id_hoyoaaa3').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo3').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa4') != null){
	$('id_hoyoaaa4').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo4').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa5') != null){
	$('id_hoyoaaa5').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo5').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa6') != null){
	$('id_hoyoaaa6').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo6').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa7') != null){
	$('id_hoyoaaa7').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo7').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa8') != null){
	$('id_hoyoaaa8').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo8').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}
if ( $('id_hoyoaaa9') != null){
	$('id_hoyoaaa9').observe('click', function(e) { 
		  var url = e.findElement('a[href]');
		 $('formularioEnvio').writeAttribute('action',  $('id_url_hoyo9').href);
		 $('formularioEnvio').writeAttribute('method','GET'); $('formularioEnvio').submit();
	});
}


function cambioTiempo(indice) { 
	var vContexto =  $('contextoTiempo').href +indice;
	new Ajax.Request(vContexto,{method:'get',onSuccess: function(transport){$('tiempo').innerHTML = transport.responseText;
			    },onFailure: function(){ alert('Tiempo No disponible...') }});
}

function actualizacionTiempo() {
        var target = $('tiempo');
        if (!target) return false;
        if ($('contextoTiempo') != null){
    	  new Ajax.PeriodicalUpdater(target, $('contextoTiempo').href,{frequency:'300000'});
       }
}
Event.observe(window, 'load', actualizacionTiempo, false);



