window.addEvent('domready', function() {

						 
	if ($('date'))
	{
		myCal = new Calendar({ date: 'd/m/Y' });

		$('date').addEvent('keypress',function(){
			$('date').set('value','');		
		});
		
	}
	
	if ($('desde'))
		myCal = new Calendar({ desde: 'd/m/Y' });
	
	if ($('hasta'))
		myCal = new Calendar({ hasta: 'd/m/Y' });
		
	if ($('archivos'))
			new MultiUpload( $( 'archivos' ) );

	if($('searchAv'))
	{
		$('buscador').addEvent('mouseover',function(headersearch){
			if ( $('moreSearchDiv').get('class') == "hide" )
			{
				$('searchAv').removeClass('hide');
			}
			else
			{
				$('searchBas').removeClass('hide');
			}
			
		});
		$('buscador').addEvent('mouseout',function(headersearch){
				$('searchAv').set('class','hide');
				$('searchBas').set('class','hide');
		});
		$('searchAv').addEvent('click',function(){
			$('moreSearchDiv').removeClass('hide');
			$('buscador').setStyle('width','600px');
			$('searchAv').addClass('hide');
		});
		$('searchBas').addEvent('click',function(){
			$('moreSearchDiv').set('class','hide');
			$('searchBas').addClass('hide');
		});		
	}
	
	if($('loginLink'))
	{
		$('loginLink').addEvent('click',function(){
			$('loginDiv').removeClass('hide');
			$('foroLinks').destroy();
		});
	}
	
	if($('cuerpo')){
		$('cuerpo').mooEditable({
			actions: 'bold italic underline strikethrough | formatBlock justifyleft justifyright justifycenter justifyfull | insertunorderedlist insertorderedlist indent outdent | undo redo | createlink unlink | urlimage | toggleview'
		});
	}
	
	if($('notaSubseccion')){
		$('notaSubseccion').addEvent('focus', function(subseccion) {
			new Request({
				method:	'post',
				url:		'JSmapSubsecciones',
				data:		'seccion='+$('seccion').value,
				onComplete:function(txt)
				{ 
					try{
						eval('var resp = '+txt+';');
					}catch(e){
						var resp = false;
					}  
										
					$('notaSubseccion').set('html',txt);
					
					return true;
					
				}
			}).send();
		});
	}
	
	if($('revistas')){
		$('revistas').addEvent('change', function(subseccion) {
			window.location= 'http://sistemasjudiciales.org/admin/subsecciones/1/'+$('revistas').value;
		});
	}
	
	if($('enviar')){
		$('enviar').addEvent('click', function() {
			$('enviarNota').removeClass('hide');
		});
	}

	if($('imprimir')){
		$('imprimir').addEvent('click', function() {
			window.open("http://sistemasjudiciales.org/imprimir/"+$('imprimir').get('title')+" ","_blank","location=0,status=0,scrollbars=1,width=300,height=600");
		});
	}
	
	if($('linkNumerosAnteriores'))
	{
		$('linkNumerosAnteriores').addEvent('click', function() {
			$('numerosAnteriores').removeClass('hide');
		});

	}
	
	if($$('.valorar')){ 
		$$('.valorar').each(function(valorar){ 
			valorar.addEvent('click', function() { 
				new Request({
					method:	'post',
					url:	valorar.get('dir'),
					data:	valorar.get('title'),
					onComplete:function(txt)
					{
						try{
							eval('var resp = '+txt+';');
						}catch(e){
							var resp = false;
						}
						if ((isset(resp)==false)||(resp === false)) 
						{
							mfwAlert('Error del servidor, intente luego de unos minutos.');
							return false;
						}
						else if (resp.status === false)
						{
							var alertar = 'Error: ';
							if(resp.errors)
							{
								resp.errors.each(
									function(error){
										alertar += error.reason;
									}
								);
							}
							else if(resp.message)
							{
								alertar += resp.message;
							}
			
							mfwAlert(alertar);
							
							return false;
						}
						else {
							$$('.nvaloracion').set('html',parseInt($$('.nvaloracion').get('html'))+1);
							mfwAlert('Su valoracion fue emitida, muchas gracias.');
							return true;
						}
					}
				}).send();
			});
		});
	}
	
	if($('amNotaArchivos')){

		var up = new FancyUpload2($('demo-status'), $('demo-list'), { 
			verbose: false,
			
			fileSizeMax: 20971520,
			
			url: $('amNotaArchivos').get('action'),
			
			path: jud.fancy,
			
			data:  'id_nota='+$('id_nota').get('value'),
			
			target: 'demo-browse',
			
			onLoad: function() {
								
				this.target.addEvents({
					click: function() {
						return false;
					},
					mouseenter: function() {
						this.addClass('hover');
					},
					mouseleave: function() {
						this.removeClass('hover');
						this.blur();
					},
					mousedown: function() {
						this.focus();
					}
				});

				
				$('demo-clear').addEvent('click', function() {
					up.remove(); 
					return false;
				});

				$('demo-upload').addEvent('click', function() {
					up.start();
					return false;
				});
			},
			
			onSelectFail: function(files) {
				files.each(function(file) {
					new Element('li', {
						'class': 'validation-error',
						html: file.validationErrorMessage || file.validationError,
						title: MooTools.lang.get('FancyUpload', 'removeTitle'),
						events: {
							click: function() {
								this.destroy();
							}
						}
					}).inject(this.list, 'top');
				}, this);
			},
			

			onFileSuccess: function(file, response) {
				var json = new Hash(JSON.decode(response, true) || {});
				
				if (json.get('status') == true) {
					file.element.addClass('file-success');
					file.info.set('html', 'El archivo ha sido cargado exitosamente.');
				} else {
					file.element.addClass('file-failed');
					file.info.set('text', 'An error occured: ' + (json.get('error') ? (json.get('error') + ' #' + json.get('code')) : response));
				}
			},
			

			onFail: function(error) {
				switch (error) {
					case 'hidden': // works after enabling the movie and clicking refresh
						//~ alert('To enable the embedded uploader, unblock it in your browser and refresh (see Adblock).');
						break;
					case 'blocked': // This no *full* fail, it works after the user clicks the button
						//~ alert('To enable the embedded uploader, enable the blocked Flash movie (see Flashblock).');
						break;
					case 'empty': // Oh oh, wrong path
						//~ alert('A required file was not found, please be patient and we fix this.');
						break;
					case 'flash': // no flash 9+ :(
						//~ alert('To enable the embedded uploader, install the latest Adobe Flash plugin.')
				}
			}
			
		});
	
	}
	
});