function popUp(direccion,ancho,alto,iframe){
	if(iframe){
		try{
			w.$('popUpIframe').remove();
			w.$('cerrar_popUpIframe').remove();
		}catch(ex){}
    if(window.parent){
    //if((window.parent) && (window.name!='web')){
        w=window.parent;
    }else{
        w=window;  
    }
    var alto_ventana=window.getHeight()+window.getScrollTop();
    var el=w.document.createElement('iframe');
    el.id='popUpIframe';
    el.src=direccion;
    el.scrolling='auto';
    el.frameBorder=0;
    el.style.width=ancho+'px';
    el.style.height=alto+'px';
    el.style.top=Math.round((alto_ventana-alto)/2).toString()+'px';
    el.style.marginLeft='-'+Math.round(ancho/2).toString()+'px';
    w.document.getElementsByTagName('body')[0].appendChild(el);
    var cerrar=w.document.createElement('img');
    cerrar.src='/images/comunes/cerrar.png';
    $(cerrar).addClass('png');
    cerrar.onclick=function(){
    	try{	
    		w.$('popUpIframe').remove();
    		w.$('cerrar_popUpIframe').remove();
    	}catch(ex){}
    }
    cerrar.id='cerrar_popUpIframe';
    cerrar.style.marginLeft='-'+Math.round(ancho/2+14).toString()+'px';
    cerrar.style.top=Math.round((alto_ventana-alto)/2-12).toString()+'px'; 
    w.document.getElementsByTagName('body')[0].appendChild(cerrar);
	}else{
		var cadena='toolbar=0';
		if(ancho) cadena+=', width='+ancho;
		if(alto) cadena+=', height='+alto;
		ventana=window.open(direccion,'',cadena);
    ventana.focus();
	}
}

/*
Muestra una ventana de dialogo
ventana_dialgo(mensaje,nombre_boton1,string_funcion1,nombre_boton2,string_funcion2,...);
*/

function ventana_dialogo(msg){
		var w;
    try{
        $('alerta').remove();
        $('fondo_alerta').remove();    
    }catch(ex){};

    if(window.parent){
        w=window.parent;
    }else{
        w=window;  
    }
    
    var num_args=arguments.length;
  
    var fondo=w.document.createElement('div');
    fondo.id='fondo_alerta';
    w.document.getElementsByTagName('body')[0].appendChild(fondo); 
    fondo.style.width=w.Window.getScrollWidth()+'px';   
    fondo.style.height=w.Window.getScrollHeight()+'px';   
    var div_alerta=$(w.document.createElement('div')); //new Element('div');
    div_alerta.id='alerta';
    w.document.getElementsByTagName('body')[0].appendChild(div_alerta); 
    div_alerta.innerHTML='<div>'+msg+'</div>';    
    
    for(var i=1;i<num_args;i+=2){	
        boton=$(w.document.createElement('button'));  //new Element('button');
        boton.setHTML(arguments[i]);
        boton.addEvent('click',arguments[i+1]);
        boton.addEvent('click',function(){
            w.$('alerta').remove();
            w.$('fondo_alerta').remove();
            w.document.onkeypress=null;
        });
        div_alerta.adopt(boton);
    }
        
    div_alerta.style.marginTop="-"+Math.round($(div_alerta).offsetHeight/2)+"px";
    w.document.onkeypress=function(){
    	try{
	      w.$('alerta').remove();
	      w.$('fondo_alerta').remove();
    	}catch(ex){}
      w.document.onkeypress=null;
      return false;
    }  
}

function alerta(msg){
    //alert(msg);      
    ventana_dialogo(msg,"Aceptar",function(){});
}

function confirmar(msg,funcion_si,funcion_no){
    ventana_dialogo(msg,"Sí",funcion_si,"No",funcion_no);
}

function cancelarRespuesta(evt){
    try{
        evt.preventDefault();
    }catch(ex){
        window.event.returnValue=false;
    }  
}

function redimensionarIframe(id){
    window.addEvent("load",function(){
    		var altura=window.getScrollHeight();
    		if(altura<300) altura=300;
        window.parent.$(id).style.height=altura+2+"px";
    });
}


function esAncestro(padre,hijo){
    try{
        if(hijo==padre)
            return true;
        if(hijo.tagName=="BODY")
            return false;
        return esAncestro(padre,hijo.parentNode);
    }catch(ex){
        return false;  
    }
}


function colorearTablas(){
    tablas=$$('table');
    for(var k=0;k<tablas.length;k++){
        trs=tablas[k].getElementsByTagName('tr');
        if(!tablas[k].hasClass('no_resaltar')){
            for(var q=0;q<trs.length;q++){
            
                if( (q%2)==0)
                    $(trs[q]).addClass('fila_color');  
                else
                    $(trs[q]).addClass('fila_sin_color');
                    
                $(trs[q]).addEvent('mouseover',function(){
                    this.addClass('fila_resaltada');
                });                 

                $(trs[q]).addEvent('mouseout',function(){
                    this.removeClass('fila_resaltada');  
                });
            }
        }
    }
}

function resaltarCampo(campo){
    campo.addClass('campo_resaltado');
    campo.addEvent('keypress',function(){
        this.removeClass('campo_resaltado');  
    });
    campo.addEvent('blur',function(){
        this.removeClass('campo_resaltado');  
    });
    campo.focus();
}

function textoPorDefecto(){
    $$('.texto_por_defecto').each(function(el){
        if(el.value=='' && el.title!=''){
            el.value=el.title;
            el.addEvent('focus',function(){
                if(this.value==this.title) this.value='';
            });
            el.addEvent('blur',function(){
                if(this.value=='') this.value=this.title;
            });
        }
    });
    
     $$('form').each(function(el){
        el.addEvent('submit',function(em){
            $$('.texto_por_defecto').each(function(em){
                if(em.value==em.title) em.value='';
            });  
        }); 
     });   
}

function fechaMayor ( fechaIni, fechaFin )
{
    partes1 = fechaIni.split('/');
    fechaIniFormateada = Date.parse(''+partes1[1]+'/'+partes1[0]+'/'+partes1[2]+'');
    partes2 = fechaFin.split('/');
    fechaFinFormateada = Date.parse(''+partes2[1]+'/'+partes2[0]+'/'+partes2[2]+'');
    if (fechaIniFormateada < fechaFinFormateada ) return 2;
    if (fechaIniFormateada == fechaFinFormateada ) return 0;
    if (fechaIniFormateada > fechaFinFormateada ) return 1;
}

function contenidoIframe(ref){
	try{
	    return ref.contentDocument;
	}catch(ex){
	    try{
	        return ref.contentWindow.document;
	    }catch(ex2){}
	}
}

function menuHover(idMenu){
	$$('#'+idMenu+' a img').each(function(el){
		el.img_off=new Image();
		el.img_off.src=el.src;
		el.img_on=new Image();
		el.img_on.src=el.src.replace(/(.*)(\.gif|\.jpg|\.png)/,"$1_on$2");
		el.addEvent('mouseover',function(){
			this.src=this.img_on.src;
		});
		el.addEvent('mouseout',function(){
			this.src=this.img_off.src;
		});		
	});
}

function mostrarFotoVar(url) {
	$('contenedor').setStyle('background-image','url('+url+')');
}

function sustituirImagen(selector,fuente,tamanyo,color,fondo,hover,sombra){
	$$(selector).each(function(el){	
		var dimensiones=el.getSize().size;
		if(dimensiones.y<tamanyo) dimensiones.y=Math.round(tamanyo*1.25);
		var padding_horizontal=el.getStyle('padding-left').toInt()+el.getStyle('padding-right').toInt();
		var padding_vertical=el.getStyle('padding-top').toInt()+el.getStyle('padding-bottom').toInt();
		if(padding_horizontal>0) dimensiones.x-=padding_horizontal;
		if(padding_vertical>0) dimensiones.y-=padding_vertical;
		var texto=el.getText();
		if(texto=='') return;
		if (el.getStyle('text-transform')=='uppercase'){
			texto=texto.toUpperCase();
		}
/*****Calcular saltos de linea****/
		var div=new Element('div');
		div.setStyle('visibility','hidden');
		div.setStyle('font-size',el.getStyle('font-size'));
		div.setStyle('width',dimensiones.x);
		div.setStyle('font-family',el.getStyle('font-family'));
		div.setText(texto);
		$E('body').adopt(div);
		var altura_inicial=div.getSize().size.y;
		var altura_actual;
		var texto_final=texto;
		var longitud_texto=texto.length;	
		for (var k=longitud_texto;k>0;k--){		
			div.setText(texto.substr(0,k));
			altura_actual=div.getSize().size.y;		
			if(altura_actual<altura_inicial && texto.substr(k-1,1)==' '){	
				altura_inicial=altura_actual;		
				texto_final=texto.substring(0,k-1)+String.fromCharCode(10)+texto.substring(k,longitud_texto);
				dimensiones.y+=Math.round(tamanyo/5);
			}
		}
		div.remove();
		texto=texto_final;
		
/*********************************/
		
		var src='/bd/titulares/'+(texto+fuente+color+tamanyo+fondo).replace(/[^a-zA-Z0-9]/g,"")+'.png';
		
		var img=new Element('img', {
			alt:texto
		});
		
		
		el.innerHTML='';
				
		img.onerror=function(){  
			src='/imagenes/titular/'+fuente+'/'+tamanyo+'/'+dimensiones.x+'/'+dimensiones.y+'/'+fondo+'/'+color;
			if(sombra){
				src+='/'+sombra;
			}
			src+='?texto='+escape(texto);
			img.src=src;
			this.onerror=null;
			this.onmouseover=null;
			this.onmouseout=null;
		}	
		img.src=src;
		el.adopt(img);

		if(hover){
			img.imageHover=new Image();
			img.onmouseover=function(){
				this.src=this.imageHover.src;
			}
			img.imageHover.onerror=function(){  
					var src_on='/imagenes/titular/'+fuente+'/'+tamanyo+'/'+dimensiones.x+'/'+dimensiones.y+'/'+fondo+'/'+hover;
					if(sombra){
						src_on+='/'+sombra;
					}
					src_on+='?texto='+escape(texto);
					this.src=src_on;
					this.onerror=null;
					img.onmouseover=null;
					img.onmouseout=null;
				}	
			img.imageHover.src='/bd/titulares/'+(texto+fuente+hover+tamanyo+fondo).replace(/[^a-zA-Z0-9]/g,"")+'.png';
			
			img.onmouseout=function(){
				this.src=src;
			}
		}
	
	});
	
}

function calendario_input(){
	$$('.fecha').each(function(el){
		if(el.getTag()=='input'){
			var img=new Element('img', {
				'alt':'',
				'src':'/images/intranet/ico_fecha.gif',
				'class':'img_aux',
				'styles': {
					cursor:'pointer'
				},
				'events': {
					'click' : function(){var obj=this;dp=new DatePicker();var elm = dp.create();dp.onchange=function(){if (this.getDate())  {mes=this.getDate().getMonth()+1;if (mes<10) mes='0'+mes;dia=this.getDate().getDate();if (dia<10) dia='0'+dia;el.value=dia+'/'+mes+'/'+this.getDate().getFullYear()} else {el.value=''}};document.getElementsByTagName('body')[0].appendChild(elm);elm.style.left=el.getLeft()+100+'px';elm.style.top=el.getTop()-145+'px';}
				}
			});
			
			img.injectAfter(el);
			
		}
	});
}

function menuSeleccionado(){
	$$('#menu_arbol a').each(function(el){
		if(el.href==window.location.href){
			el.addClass('seleccionado');
			el.innerHTML+=' &gt';
		}
	});
}


window.addEvent('domready',colorearTablas);
window.addEvent('domready',textoPorDefecto);
window.addEvent('domready',calendario_input);

