// JavaScript Document
var skin = null; 
var lan = null; 

function init(){
	idioma_usuario = get_cookie("idioma");
	/*cambioIdioma();*/
	
	estilo_usuario = get_cookie("estilo");
	/*cambioEstilo();*/
	
	
}

/* El maravillo insertaContenidos */
function unInner(lugar,loqueva) {
	var receptor = document.getElementById(lugar);
	var unhtml = loqueva;
	receptor.innerHTML = unhtml;
}

/* El maravillo rastreador de Clases */
function identClases(laClase,etiqueta,contenedor,accion){
	var contenedorVar=document.getElementById(contenedor);
    var tipoEtiqueta=contenedorVar.getElementsByTagName(etiqueta);
	if(tipoEtiqueta.length > 0)
	{
		for(i=0; i<tipoEtiqueta.length; i++)
		{
			if(tipoEtiqueta[i].className == laClase)
			{
				switch(accion)
				{
					case "quitarClase":
						tipoEtiqueta[i].className="";
						break;
					
					case "bgTop":
						tipoEtiqueta[i].style.backgroundPosition="top";
						break;
						
					case "hide":
						tipoEtiqueta[i].style.display="none";
						break;
					
					case "show":
						tipoEtiqueta[i].style.display="block";
						break;
				}
			}
		}
	}
}

function abrirVentana(unContenedor,unAncho,unAlto) { 
	var envioUnContenedor = document.getElementById(unContenedor).value;
	unInner('insertarCont',envioUnContenedor);
	var tamanyoContainer = document.getElementById('container').offsetHeight;
	document.getElementById('base').style.height = tamanyoContainer + "px";
	document.getElementById('base').style.opacity = ".01";
	document.getElementById('base').style.display = "block";
	document.getElementById('emergente').style.display = "block";
	document.getElementById('emergente').style.width = (unAncho+36) +"px";
	document.getElementById('insertarCont').style.width = unAncho +"px";
	document.getElementById('emergente').style.height = unAlto +"px";
	document.getElementById('emergente').style.visibility = "hidden";
	//centrarModal(unAncho,unAlto);
	setTimeout("centrarModal("+unAncho+","+unAlto+");", 500);
}

function cerrarVentana(cerrarVent) {	
	document.getElementById(cerrarVent).style.display = "none";
	document.getElementById('base').style.display = "none";
}

//setTimeout("centrarModal("+unAncho+","+unAlto+");", 500);

function centrarModal (centralAncho,centralAlto){
	document.getElementById('emergente').style.marginLeft="-" + Math.round(centralAncho/2) + "px";
	document.getElementById('emergente').style.marginTop="-" + Math.round(centralAlto/2) + "px";
	document.getElementById('emergente').style.visibility="visible";
}

function cerrarMenu() {
	identClases("subMenuOver","span","menu","quitarClase");
	identClases("submenu","div","menu","hide");
}

function mostrarID(cualID,coloreamos) {
	cerrarMenu();
	document.getElementById(cualID).style.display = "block";
	coloreamos.className="subMenuOver";
}

/* redimensiona imágenes */ 
function redImg(){
    var contenedor=document.getElementById("redimensiono");
    var lista=contenedor.getElementsByTagName("img");
   
    var total=lista.length;
    for (var i=0;i<total;i++){
        var a=lista[i].offsetWidth;
        var b=lista[i].offsetHeight;
        if(a<b){
           lista[i].style.width=120+"px";
        }else{
           lista[i].style.height=120+"px";
        }
    }
}

/* Idioma*/
function spanish() {
	identClases("english","span","content","hide");
	identClases("spanish","span","content","show");
	identClases("english","div","content","hide");
	identClases("spanish","div","content","show");
	/*enviar_cookie("idioma","spanish","60","vampipe.gelial.net");*/
	lan = "spa";
}

function english() {
	identClases("spanish","span","content","hide");
	identClases("english","span","content","show");
	identClases("spanish","div","content","hide");
	identClases("english","div","content","show");
	/*enviar_cookie("idioma","english","60","vampipe.gelial.net");*/
	lan = "eng";
}

function enviar_cookie(nombre_cookie,valor_cookie,vida_cookie_dias,dominio_cookie) {
	document.cookie =nombre_cookie + "=" + encodeURIComponent( valor_cookie ) + "; max-age=" + 60 * 60 * 24 * vida_cookie_dias + "; path=/; domain=" + dominio_cookie ;
}

/*function get_cookie(nombre_cookie) {
	alert(nombre_cookie);
if (document.cookie.length != 0) {
	var valor_cookie = document.cookie.match ('(^|;)[\s]*' + nombre_cookie + '=([^;]*)' );*/
	/*return decodeURIComponent ( valor_cookie[2] ) ;*/
/*}
return '' ;
}*/

function get_cookie ( cookie_name )
{
  var results = document.cookie.match ( '(^|;) ?' + cookie_name + '=([^;]*)(;|$)' );

  if ( results )
    return ( unescape ( results[2] ) );
  else
    return null;
}


function cambioIdioma(){
	if (idioma_usuario == "english") { english(); }
}

function createjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var fileref=document.createElement('script')
  fileref.setAttribute("type","text/javascript")
  fileref.setAttribute("src", filename)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var fileref=document.createElement("link")
  fileref.setAttribute("rel", "stylesheet")
  fileref.setAttribute("type", "text/css")
  fileref.setAttribute("href", filename)
 }
 return fileref
}

function replacejscssfile(oldfilename, newfilename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist using
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(oldfilename)!=-1){
   var newelement=createjscssfile(newfilename, filetype)
   allsuspects[i].parentNode.replaceChild(newelement, allsuspects[i])
  }
 }
}

function hacerAzul(){
	unInner("caja01","<img src='images/boxes/bHome01.png' alt='' />");
	unInner("luz","<img src='images/backgrounds/bLuz.png' alt='' />");
	replacejscssfile('dark.css','include/css/prueba.css','css');
}

var estiloAnterior = "dark";

function cambioEstilo(cual){
	var elEstilo = "include/css/"+cual+".css";
	skin = cual;
	replacejscssfile(estiloAnterior,elEstilo,'css');
	estiloAnterior=elEstilo;
	
	switch(cual){
		case "steampunk":
			claseIcono();
			var activoBoton=document.getElementById(cual);
			activoBoton.className="actual";
			unInner("caja01","<img src='images/boxes/bHome01.png' alt='' />");
			unInner("fondoLeft","<img src='images/boxes/bHomeLeft.png' alt='' />");
			unInner("fondoRight","<img src='images/boxes/bHomeRight.png' alt='' />");
			unInner("luz","<img src='images/backgrounds/bLuz.png' alt='' />");
			unInner("thumbPrev","<img src='images/botones/bLeft.png' alt='Anterior' title='Anterior'>");
			unInner("thumbNext","<img src='images/botones/bRight.png' alt='Siguiente' title='Siguiente'>");
			/*enviar_cookie("estilo","steampunk","60","vampipe.gelial.net");*/
			break;
		
		case "dark":
			claseIcono();
			var activoBoton=document.getElementById(cual);
			activoBoton.className="actual";
			unInner("caja01","<img src='images/boxes/home01.png' alt='' />");
			unInner("fondoLeft","<img src='images/boxes/homeLeft.png' alt='' />");
			unInner("fondoRight","<img src='images/boxes/homeRight.png' alt='' />");
			unInner("luz","<img src='images/backgrounds/luz.png' alt='' />");
			unInner("thumbPrev","<img src='images/botones/left.png' alt='Anterior' title='Anterior'>");
			unInner("thumbNext","<img src='images/botones/right.png' alt='Siguiente' title='Siguiente'>");
			/*enviar_cookie("estilo","dark","60","vampipe.gelial.net");*/
			break;
			
		case "contra":
			claseIcono();
			unInner("caja01","<img src='images/boxes/homeContra.gif' alt='' />");
			unInner("fondoLeft","<img src='images/boxes/contraHomeLeft.gif' alt='' />");
			unInner("fondoRight","<img src='images/boxes/contraHomeRight.gif' alt='' />");
			unInner("luz","<img src='images/backgrounds/contraLuz.gif' alt='' />");
			unInner("thumbPrev","<img src='images/botones/left.png' alt='Anterior' title='Anterior'>");
			unInner("thumbNext","<img src='images/botones/right.png' alt='Siguiente' title='Siguiente'>");
			/*enviar_cookie("estilo","dark","60","vampipe.gelial.net");*/
			break;
			
		case "underwater":
			claseIcono();
			var activoBoton=document.getElementById(cual);
			activoBoton.className="actual";
			unInner("caja01","<img src='images/boxes/uHome01.png' alt='' />");
			unInner("fondoLeft","<img src='images/boxes/uHomeLeft.png' alt='' />");
			unInner("fondoRight","<img src='images/boxes/uHomeRight.png' alt='' />");
			unInner("luz","<img src='images/backgrounds/bubbles2.gif' alt='' />");
			/*unInner("thumbPrev","<img src='images/botones/left.png' alt='Anterior' title='Anterior'>");
			unInner("thumbNext","<img src='images/botones/right.png' alt='Siguiente' title='Siguiente'>");*/
			/*enviar_cookie("estilo","underwater","60","vampipe.gelial.net");*/
			break;
	}
}

function autocambioEstilo(){
	cambioEstilo(estilo_usuario);
}

function claseIcono(){
	var contenedorVarIco=document.getElementById("iconos");
    var tipoEtiquetaIco=contenedorVarIco.getElementsByTagName("li");
	if(tipoEtiquetaIco.length > 0)
	{
		for(i=0; i<tipoEtiquetaIco.length; i++)
		{
			if(tipoEtiquetaIco[i].className == "actual")
			{
				tipoEtiquetaIco[i].className="icoBotones";
			}
		}
	}
}

function ilustraciones(numIlu){
	identClases("ilustracionSlide","img","ilustracionesSlide","hide")
	var nombreIlustracion = "ilu0" + numIlu;
	document.getElementById(nombreIlustracion).style.display= "block";
	siguienteIlu(numIlu);
}

function siguienteIlu(dameElNum){
	if (dameElNum==1){
		var adelante = dameElNum + 1;
		var atras = 7;
	}
	else if (dameElNum==7){
		var adelante = 1;
		var atras = dameElNum - 1;
	}
	else {
		var adelante = dameElNum + 1;
		var atras = dameElNum - 1;
	}
	var siguienteIlu = "<img src='images/botones/right.png' alt='Siguiente' title='Siguiente' onclick='ilustraciones(" + adelante + ");' />";
	var anteriorIlu = "<img src='images/botones/left.png' alt='Siguiente' title='Siguiente' onclick='ilustraciones(" + atras + ");' />";
	unInner("thumbNextIlu",siguienteIlu);
	unInner("thumbPrevIlu",anteriorIlu);
}


function func1() { 
	
	idioma_usuario = get_cookie("idioma");
	alert("uno");
	/*cambioIdioma();*/
} 

function func2() { 
alert("dos");
	/*estilo_usuario = get_cookie("estilo");
	cambioEstilo();*/
} 

function addLoadEvent(func) { 
	var oldonload = window.onload; 
	if (typeof window.onload != 'function') { 
		window.onload = func; 
		} else { 
			window.onload = function() { 
			if (oldonload) { 
			oldonload(); 
		} 
		func(); 
		} 
	} 
} 
 
//addLoadEvent(func1); 
//addLoadEvent(func2);

var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";  
$(document).keydown(function(e) {  
  kkeys.push( e.keyCode );  
  if ( kkeys.toString().indexOf( konami ) >= 0 ){  
    $(document).unbind('keydown',arguments.callee);  
	cambioEstilo("contra");
  }  
});  


function abrir(idAbrir,anchoPop,altoPop,queTipo) {
	var mandarContPop=document.getElementById(idAbrir).value;
	
	unInner('cajaPopup',mandarContPop);
	
	var cajaPopup = document.getElementById("interiorPopup");
	document.getElementById("pantallaNegra").style.display= "block";
	document.getElementById("popup").style.display= "block";
	
	var centrarH = (anchoPop/2) + 27;
	cajaPopup.style.width=anchoPop +"px";
	cajaPopup.style.marginLeft="-"+ centrarH +"px";
	
	var centrarV = (altoPop/2) + 27;
	cajaPopup.style.height=altoPop +"px";
	cajaPopup.style.marginTop="-"+ centrarV +"px"
	
	if(queTipo == "img") { document.getElementById("interiorPopup").style.padding= 0; document.getElementById("cajaPopup").style.marginTop= -1 + "px";  }
	else { document.getElementById("interiorPopup").style.padding= 20; }
}

function cerrarPopup() {
	document.getElementById("pantallaNegra").style.display= "none";
	document.getElementById("popup").style.display= "none";
	unInner("cajaPopup","");
}

function abrirTuit(direIframe) {
	var mandarContPop2="<iframe height=528 width=970 src="+direIframe+"></iframe>";
	
	unInner('cajaPopup',mandarContPop2);
	
	var cajaPopup = document.getElementById("interiorPopup");
	document.getElementById("pantallaNegra").style.display= "block";
	document.getElementById("popup").style.display= "block";
	
	var centrarH = 485 + 27;
	cajaPopup.style.width=970 +"px";
	cajaPopup.style.marginLeft="-"+ centrarH +"px";
	
	var centrarV = 269 + 27;
	cajaPopup.style.height=528 +"px";
	cajaPopup.style.marginTop="-"+ centrarV +"px"
	
	document.getElementById("interiorPopup").style.padding= 20;
}

function select_Formulario() {
	var diceComment=document.getElementById("message").value;
	if (diceComment == "Comentario") {
		/*var text_val=eval("document.formulario.message");
		text_val.focus();
		text_val.select();*/
		document.getElementById("message").value = "";
	}
}

function SetEmotes(emotes){
	var diceComment2=document.getElementById("message").value;
	if (diceComment2 == "Comentario" || diceComment2 == " La respuesta es la prueba de que eres humano y no un robot de spam " || diceComment2 == " The solution is the proof you are human and not an automatic spammer bot " ) { document.getElementById("message").value = "" }
	document.formulario.message.value+=''+emotes;document.formulario.message.focus();
	}

function insertarEmote(elInputEmote) {
	var diceComment3=document.getElementById("message").value;
	if (diceComment3 == "Comentario" || diceComment3 == " La respuesta es la prueba de que eres humano y no un robot de spam " || diceComment3 == " The solution is the proof you are human and not an automatic spammer bot " ) { document.getElementById("message").value = "" }
	var cualEmote=elInputEmote.value;
	cerrarPopup();
	document.formulario.message.value+=''+cualEmote;document.formulario.message.focus();
}

function dire(urlOrig){
	var url = urlOrig.href;
	if (lan != null) {
		var elLan = "lan=" + lan;
	}
	else { var elLan = ""; }
	if (skin != null) {
		if (lan != null){
			var laSkin = "&skin=" + skin;
		}
		else{ var laSkin = "skin=" + skin; }
	}
	else { var laSkin = ""; }
	
	if ( lan != null || skin != null) {
		var direCompleta = url + "?" + elLan + laSkin;
		urlOrig.href = direCompleta;
	}
	else { urlOrig.href = url; }
}

function pagina(numeroPag){
	identClases("pagina","div","contenido","hide");
	document.getElementById(numeroPag).style.display ="block";
}

function mostrar(mTitulo,mCaja) {
	if (document.getElementById(mCaja).style.display == "none") { 
		identClases("hidenBoxes","div","articulos","hide");
		document.getElementById(mCaja).style.display = "block"; 
		}
	else { document.getElementById(mCaja).style.display = "none";  }
}
