// JavaScript Document


function declaraVariablesMenu( sSeccion )
{	
	if( sSeccion != null ) sSeccionActual = sSeccion;
	
	intervaloComun = null;
	
	sIdActual = "";
	
	nAlturaActual = 0;
	
	imagenesDePrecarga();
}


function imagenesDePrecarga()
{
	var asImagenesSeccion = new Array( "inicio/portada.jpg", "inicio/fondo.jpg" );
	
	var sComun = "comun/garra.png,comun/sombra.png,menu/adoptar.png,menu/adoptar-hover.png,menu/colaboran.png,menu/colaboran-hover.png,menu/colaborar.png,menu/colaborar-hover.png,menu/contactar.png,menu/contactar-hover.png,menu/informacion-util.png,menu/informacion-util-hover.png,menu/inicio.png,menu/inicio-hover.png,menu/inicio-sup.png,menu/inicio-sup-hover.png,menu/la-asociacion.png,menu/la-asociacion-hover.png,menu/no-al-abandono-y-al-maltrato.png,menu/no-al-abandono-y-al-maltrato-hover.png,menu/nos-han-dejado.png,menu/nos-han-dejado-hover.png,menu/noticias.png,menu/noticias-hover.png,menu/socios-junior.png,menu/socios-junior-hover.png,menu/tienda-solidaria.png,menu/tienda-solidaria-hover.png,menu/webs-amigas.png,menu/webs-amigas-hover.png";

	var asImagenesComun= sComun.split( "," );
	
	asImagenes = new Array();
	
	asImagenes = asImagenesSeccion.concat( asImagenesComun );
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// EVENTOS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function resize()
{		
	var anTam = tamVentana();
	
	var nAnchura = 800;

	var nX = anTam[ 0 ]/2 - nAnchura/2;	
	
	if( nX < 0 ) nX = 0;

	if( document.getElementById( "textoPortada" ) != null )
	{
		var nYtexto = 649;	
		
		document.getElementById( "textoPortada" ).style.top = 520+"px";
	}	
	
	if( document.getElementById( "sombraIzq" ) != null )
	{
		document.getElementById( "sombraIzq" ).style.left = (nX-13)+"px";
		document.getElementById( "sombraDer" ).style.left = (nX+800-33)+"px";
		
		document.getElementById( "sombraIzq" ).style.height = document.getElementById( "sombraDer" ).style.height =  anTam[ 1 ]+"px";
	}	

	var nH = 520+document.getElementById( 'textoPortada' ).scrollHeight;
	
	if( nH < anTam[ 1 ] ) nH = anTam[ 1 ];
	
	document.getElementById( 'contenedor' ).style.left = nX+"px";	
	document.getElementById( 'contenedor' ).style.height = nH+"px";

	document.getElementById( 'fondoContenedor' ).style.left = nX+"px";
	document.getElementById( 'fondoContenedor' ).style.height = document.getElementById( 'contenedor' ).style.height;
	
	document.getElementById( 'leerMas' ).style.left = anTam[ 0 ]/2 - 250 +"px";
	document.getElementById( 'leerMas' ).style.top = anTam[ 1 ]/2 - 150 +"px";
}


function precarga( nScrollY )
{			
	sNavegador = getNavegador();	
	if ( sNavegador.indexOf( "explorer" ) == -1 ) bExplorer = false;
	
	leerCSS();	
		
	resize();		
	
	imgs = new Array();		
	
	for( n=0; n < asImagenes.length; n++ )
	{	
		imgs[ n ] = new Image();
		imgs[ n ].src = "media/imagenes/"+asImagenes[ n ];
	}			

	if( sSeccionActual == "editor-inicio" ) 
	{
		scrollTo( 0, nScrollY );
	}
	
	intervaloComun = window.setInterval( "onProgress()", 100 );
}



function onProgress()
{		
	for( u=0; u < imgs.length; u++ ) if( imgs[ u ].complete == false ) return;		
		
	document.getElementById( "contenedor" ).style.backgroundImage="url(media/imagenes/inicio/portada.jpg)";
	
	document.getElementById( "contenedor" ).style.display = "block";
	
	window.clearInterval( intervaloComun );	
	
	nAux1 = 1;
	
	intervaloComun = window.setInterval( "filtroDesaparecer()", 1 );
}


function filtroDesaparecer()
{
	nAux1-=0.02;		
			
	document.getElementById( "cargando" ).style.opacity = nAux1;
	document.getElementById( "cargando" ).style.filter = "alpha(opacity="+( nAux1*100 )+")";	
	
	if( nAux1 <= 0 )
	{
		document.getElementById( "cargando" ).style.display = "none";	
		
		reiniciarGarras();
	
		window.setInterval( "crearGarra()", 500 );
	
		inicio();	
	}
}


//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// NOSOTROS
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


function inicio()
{
	window.clearInterval( intervaloComun );	
	
	if( sSeccionActual == "inicio" ) leerMas();	
}


function reiniciarGarras()
{
	window.clearInterval( intervaloComun );	
	
	document.getElementById( "garras" ).style.opacity = 1;
	document.getElementById( "garras" ).style.filter = "alpha(opacity=100)";
	
	intervaloComun = null;
	
	nGarra = 0;
	
	document.getElementById( "garras" ).innerHTML = "";		
}


function crearGarra()
{	
	if( intervaloComun == null )
	{
		var nAncho = 300 - Math.ceil( Math.random()*250 );
		var nAlto = nAncho;	
		var nX = Math.ceil( Math.random()*screen.width )-nAncho/2;
		var nY = Math.ceil( Math.random()*screen.height )-nAlto/2;	
		
		document.getElementById( "garras" ).innerHTML += "<div style='position:absolute; top:"+nY+"px; left:"+nX+"px;'><img src='media/imagenes/comun/garra.png' width='"+nAncho+"' height='"+nAlto+"' /></div>";
		
		if( ++nGarra >= 20 ) 
		{
			nGarra = 1;			
			intervaloComun = window.setInterval( "desaparecerGarras()", 1 );
		}
	}
}


function desaparecerGarras()
{
	nGarra -= 0.01;
	
	document.getElementById( "garras" ).style.opacity = nGarra;
	document.getElementById( "garras" ).style.filter = "alpha(opacity="+nGarra*100+")";
	
	if( nGarra <= 0 ) reiniciarGarras();
}


function sobreBoton( nBoton )
{
	var aBotonesMenu = getElementsByClassName( document.getElementById( "menu" ), "botonMenu" );
	
	aBotonesMenu[ nBoton ].getElementsByTagName( "img" )[ 0 ].src = "media/imagenes/menu/"+aBotonesMenu[ nBoton ].id+"-hover.png";	
}


function fueraBoton( nBoton )
{
	var aBotonesMenu = getElementsByClassName( document.getElementById( "menu" ), "botonMenu" );
	
	aBotonesMenu[ nBoton ].getElementsByTagName( "img" )[ 0 ].src = "media/imagenes/menu/"+aBotonesMenu[ nBoton ].id+".png";
}


function subirImagen( sId, sW, sH )
{
	ventana = window.open( "uploadimagennoticia.php?w="+sW+"&h="+sH+"&id="+sId, "ventana" );	

	ventana.focus();
}


function getImagen( sId, sAccion )
{	
	if( sAccion == "borrar" ) borrarImagen( sId );
	else document.location.href="editor-inicio.php";
}



function nuevaNoticia( sId )
{
	document.getElementById( "hAccion"+sId  ).value = "crear";
	
	document.getElementById( "hScroll"+sId ).value = window.scrollY;
	
	document.getElementById( "formPadre"+sId ).submit();
	
}


function mostrarImagen( sId, sMostrar )
{
	getElementsByClassName( document.getElementById( "noticia"+sId ), "imagen" )[ 0 ].style.display = sMostrar;
}


function mostrarNoticia( sId, sNombre, sTexto, sAnchura, sAltura, sColorFondoNombre, 
						 sColorFondoNoticia, sColorBordeNoticia, sColorLetrasNombre, sColorLetrasTexto )
{		
	var nAnchura = parseInt( sAnchura );
	
	document.getElementById( "noticia"+sId ).style.width = nAnchura+"px";
	
	if( sId != "0" )
	{			
		var nAltura = parseInt( sAltura );
		var nMargen1 = 10;
		var nMargen2 = 60;

		if( sSeccionActual == "editor-inicio" )
		{
			if( sNombre.length == 0 ) document.getElementById( "nombre"+sId ).value = "Titulo";
			if( sTexto.length == 0 ) document.getElementById( "texto"+sId ).value = "Contenido";
			
			nMargen1 = 25;
			nMargen2 = 56;
		}
		else
		{			
			getElementsByClassName( document.getElementById( "nombre"+sId ), "contenido" )[ 0 ].innerHTML = sNombre;
			getElementsByClassName( document.getElementById( "texto"+sId ), "contenido" )[ 0 ].innerHTML = sTexto;
			
			if( sNombre.length == 0 || sNombre == "Titulo" )
			{
				document.getElementById( "nombre"+sId ).style.display = "none";
				nMargen2 = 30;
				nAltura -= 25;
				document.getElementById( "texto"+sId ).style.textAlign = "justify";
			}
			if( sTexto.length == 0 || sTexto == "Contenido" ) document.getElementById( "texto"+sId ).style.display = "none";
			
			if( document.getElementById( "texto"+sId ).scrollHeight > nAltura )
			{				
				document.getElementById( "leerMas"+sId ).style.position = "absolute";
				document.getElementById( "leerMas"+sId ).style.left = nAnchura-15+"px";
				document.getElementById( "leerMas"+sId ).style.height = 0+"px";
				document.getElementById( "leerMas"+sId ).style.display = "block";
				
				//alert(document.getElementById( "texto"+sId ).scrollHeight+","+nAltura+","+nMargen2);				
			}			
		}		
		
		document.getElementById( "noticia"+sId ).style.height = nAltura+"px";
		document.getElementById( "noticia"+sId ).style.backgroundColor = sColorFondoNoticia;	
		document.getElementById( "noticia"+sId ).style.borderColor = sColorBordeNoticia;
		
		document.getElementById( "nombre"+sId ).style.width = nAnchura-nMargen1+"px";		
		document.getElementById( "nombre"+sId ).style.color = sColorLetrasNombre;
		
		document.getElementById( "texto"+sId ).style.width = nAnchura-nMargen1+"px";
		document.getElementById( "texto"+sId ).style.height = nAltura-nMargen2+"px";
		document.getElementById( "texto"+sId ).style.color = sColorLetrasTexto;
		
		getElementsByClassName( document.getElementById( "noticia"+sId ), "nombre" )[ 0 ].style.backgroundColor = sColorFondoNombre;
		getElementsByClassName( document.getElementById( "noticia"+sId ), "texto" )[ 0 ].style.backgroundColor = sColorFondoNoticia;
		
		getElementsByClassName( document.getElementById( "noticia"+sId ), "marco" )[ 0 ].style.width = nAnchura+6+"px";
		getElementsByClassName( document.getElementById( "noticia"+sId ), "marco" )[ 0 ].style.height = nAltura+6+"px";
	}	
}



function aplicarCambios( sId )
{
	var nAnchura = parseInt( document.getElementById( "anchura"+sId ).value );
	var nAltura = parseInt( document.getElementById( "altura"+sId ).value );
	
	if( nAnchura < 200 || nAnchura > 760 ) alert( "La anchura debe ser superior a 200px e inferior a 760px" );
	else if( sId != "0" && nAltura < 100 ) alert( "La altura debe ser superior a 100px" );
	else
	{
		document.getElementById( "hId"+sId  ).value = sId;
		
		document.getElementById( "hColorFondoNombre"+sId  ).value = rgbConvert( getElementsByClassName( document.getElementById( "noticia"+sId ), "nombre" )[ 0 ].style.backgroundColor );
		document.getElementById( "hColorFondoNoticia"+sId  ).value = rgbConvert( getElementsByClassName( document.getElementById( "noticia"+sId ), "texto" )[ 0 ].style.backgroundColor );
		document.getElementById( "hColorBordeNoticia"+sId  ).value = rgbConvert( document.getElementById( "noticia"+sId ).style.borderColor );
		document.getElementById( "hColorLetrasNombre"+sId  ).value = rgbConvert( document.getElementById( "nombre"+sId ).style.color );
		document.getElementById( "hColorLetrasTexto"+sId  ).value = rgbConvert( document.getElementById( "texto"+sId ).style.color );
				
		document.getElementById( "hScroll"+sId ).value = window.scrollY;
		
		if( document.getElementById( "nombre"+sId ).value == "Titulo" ) document.getElementById( "nombre"+sId ).value = "";
		if( document.getElementById( "texto"+sId ).value == "Contenido" ) document.getElementById( "texto"+sId ).value = "";
	
		document.getElementById( "formPadre"+sId ).submit();
	}
}


function cambiarColorFondo( sId, sFondo )
{
	if( sIdActual != null )
	{
		sIdActual = sId;
		sAux = sFondo;
		
		intervaloComun = window.setInterval( "colorFondo()", 500 );	
	}
	else sIdActual = "";
}


function colorFondo()
{
	var nAnchura = parseInt( document.getElementById( "anchura"+sIdActual ).value )
	
	if( sAux == "marco" )
	{
		startColorPicker( document.getElementById( "noticia"+sIdActual ), "borderColor" );
	}
	else if( sAux == "letras-nombre" )
	{
		startColorPicker( document.getElementById( "nombre"+sIdActual ), "color" );
	}
	else if( sAux == "letras-texto" )
	{
		startColorPicker( document.getElementById( "texto"+sIdActual ), "color" );
	}
	else
	{
		startColorPicker( getElementsByClassName( document.getElementById( "noticia"+sIdActual ), sAux )[ 0 ], "backgroundColor" );
	}		
	
	window.clearInterval( intervaloComun );
}


function anular()
{
	sIdActual = null;
	
	window.clearInterval( intervaloComun );
}

			   
function eliminarNoticia( sId )
{
	var bAceptar = confirm ( "¿Desea eliminar esta Noticia?" );
	
	if( bAceptar )
	{ 	
		document.getElementById( "hId"+sId  ).value = sId;
		
		document.getElementById( "hAccion"+sId ).value = "eliminar";
	
		document.getElementById( "hScroll"+sId ).value = window.scrollY;
		
		document.getElementById( "formPadre"+sId ).submit();
	}
}


function cambiarPosicion( sId, sAccion )
{
	document.getElementById( "hId"+sId  ).value = sId;
	
	document.getElementById( "hAccion"+sId ).value = sAccion;

	document.getElementById( "hScroll"+sId ).value = window.scrollY;
	
	document.getElementById( "formPadre"+sId ).submit();		
}



function leerMas()
{
	var aUltimos = getElementsByClassName( document.getElementById( "noticia0" ), "ultimo" );
	var aLeerMasUltimos = getElementsByClassName( document.getElementById( "noticia0" ), "leerMas" );
	
	document.getElementById( "noticia0" ).style.border = "none";
	document.getElementById( "noticia0" ).style.marginLeft = "7px";
	
	for( i=0; i < aUltimos.length; i++ )
	{
		if( aUltimos[ i ].scrollHeight > 110 ) 
		{
			aLeerMasUltimos[ i ].style.visibility = "visible";
			
			//ponerPuntos( aUltimos[ i ] );			
		}
	}
}


function ponerPuntos( ultimoDiv )
{
	var contenidoDiv = getElementsByClassName( ultimoDiv, "contenido" )[ 0 ];
	
	var textoDiv = document.createElement( "DIV" );
	textoDiv.style.position = "relative";
	textoDiv.style.width = contenidoDiv.scrollWidth-105+"px";
	textoDiv.style.fontSize = "12px";
	textoDiv.style.fontFamily = "Arial, Helvetica, sans-serif";
	textoDiv.style.textAlign = "justify";
	
	textoDiv.innerHTML = contenidoDiv.innerHTML;
	document.getElementById( "aux" ).appendChild( textoDiv );
	
	var sTexto = textoDiv.innerHTML;
	
	for( l = textoDiv.innerHTML.length-1; l >= 0; l-- )
	{
		if( sTexto.charAt( l ) == " " )
		{
			textoDiv.innerHTML = sTexto.substring( 0, l )+"... <span class='negrita verde'>[+]</span>";
			
			if( textoDiv.scrollHeight < 100 )
			{
				contenidoDiv.innerHTML = textoDiv.innerHTML;
				l = -1;
			}
		}
	}
}


function mostrarLeerMas( sId, sNombre, sTexto, sAnchura, sAltura, sColorFondoNombre, 
						 sColorFondoNoticia, sColorBordeNoticia, sColorLetrasNombre, sColorLetrasTexto )
{		
	sNombre = getElementsByClassName( document.getElementById( "nombre"+sId ), "contenido" )[ 0 ].innerHTML;
	sTexto = getElementsByClassName( document.getElementById( "texto"+sId ), "contenido" )[ 0 ].innerHTML;
			
	document.getElementById( "leerMas" ).style.backgroundColor = sColorFondoNoticia;	
	document.getElementById( "leerMas" ).style.borderColor = sColorBordeNoticia;

	document.getElementById( "nombreLeerMas" ).style.color = sColorLetrasNombre;
	document.getElementById( "nombreLeerMas" ).style.backgroundColor = sColorFondoNombre;
	document.getElementById( "nombreLeerMas" ).innerHTML = "<span>"+sNombre+"</span>";
	
	document.getElementById( "textoLeerMas" ).style.color = sColorLetrasTexto;
	document.getElementById( "textoLeerMas" ).innerHTML = "<span><img class='imagenLeerMas' src='media/imagenes/noticias/"+sId+"/imagen.jpg' align='left' style='margin-right:5px; margin-bottom:5px;'/>"+sTexto+"</span>";
	
	document.getElementById( "filtro" ).style.display = "block";
	document.getElementById( "leerMas" ).style.display = "block";
}


function cerrarLeerMas()
{
	document.getElementById( "filtro" ).style.display = "none";
	document.getElementById( "leerMas" ).style.display = "none";	
}


function mostrarLeerMasUltimos( nNum )
{
	var aUltimos = getElementsByClassName( document.getElementById( "noticia0" ), "ultimo" );
	var aLeerMas = getElementsByClassName( document.getElementById( "noticia0" ), "leerMas" );
	
	var sLeer = aLeerMas[ nNum ].innerHTML;
	
	for( i=0; i < aUltimos.length; i++ )
	{
		aUltimos[ i ].style.height = "110px";
		aUltimos[ i ].style.overflow = "hidden";		
		aLeerMas[ i ].innerHTML = "[+]";
	}
	
	if( sLeer == "[+]" )
	{		
		document.getElementById( "contenedor" ).style.overflow = "visible";
		
		aUltimos[ nNum ].style.height = aUltimos[ nNum ].scrollHeight+"px";
		aUltimos[ nNum ].style.overflow = "visible";		
		aLeerMas[ nNum ].innerHTML = "[-]";		
	}		
	else document.getElementById( "contenedor" ).style.overflow = "hidden";
	
}


function borrarImagen( sId )
{
	alert( "Imagen borrada con éxito" );
	
	document.getElementById( "hId"+sId  ).value = sId;
		
	document.getElementById( "hAccion"+sId ).value = "borrar-imagen";

	document.getElementById( "hScroll"+sId ).value = window.scrollY;
	
	document.getElementById( "formPadre"+sId ).submit();
}


function mostrarInstrucciones()
{
	if( document.getElementById( "instrucciones" ).style.display == "none" )
	{
		document.getElementById( "instrucciones" ).style.display = "block";
	}
	else document.getElementById( "instrucciones" ).style.display = "none";
	
}
