/**
 * @author rafaespada
 */
 
var xhr = crearNuevaConexion();

var el = document.getElementById("submenua");

if (el.addEventListener) {
    el.addEventListener('click', cogerletra, false);
}
else 
    if (el.attachEvent) {
        el.attachEvent('onclick', cogerletra);
}

function cogerletra(e){
	// IdentificaciÛn cross-browser del objeto event 
	e = e || window.event;
	// Averiguamos en quÈ nodo (elemento) del DOM se ha originado el evento
	var elmObjetivo = e.target || e.srcElement;
	
	// SÛlo si el evento se ha producido en un nodo <a>...</a> haremos algo
	if (elmObjetivo.tagName.toLowerCase() == "a") {
		// Navegamos hasta el nodo <div class="comentario"> que envuelve nuestro comentario
		if (elmObjetivo.id == "enlaces" || elmObjetivo.id == "biblioteca" || elmObjetivo.id == "videos" || elmObjetivo.id == "salir" || elmObjetivo.id == "admincov") {
			 enlaces(elmObjetivo.id);
		} else {
			accederHTML(elmObjetivo.id);
			}
	}
	
	var anodes = el.getElementsByTagName("a");

for (var a=0; a<anodes.length; a++) {
	anodes[a].style.color="#999999";
}
		elmObjetivo.style.color="#99cc00";
    

}

function cogercontenido(e){
	// IdentificaciÛn cross-browser del objeto event 
	e = e || window.event;
	// Averiguamos en quÈ nodo (elemento) del DOM se ha originado el evento
	var elmObjetivo = e.target || e.srcElement;
	
	// SÛlo si el evento se ha producido en un nodo <a>...</a> haremos algo
	if (elmObjetivo.tagName.toLowerCase() == "a") {
		// Navegamos hasta el nodo <div class="comentario"> que envuelve nuestro comentario
		if (elmObjetivo.id=="subirfile") {
			 subirportada(elmObjetivo.id);
		} 
	}
}
		
	


function accederHTML(url){
	var artxibo = 'includes/txt/' + url + '.html';
    xhr.open("GET", artxibo, true);
    //xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4 && xhr.status == 200) {
           var texto = xhr.responseText;
            document.getElementById("content").innerHTML = texto;
            
        }
    };
    xhr.send(null);
}

function enlaces(url){

	var artxibo = 'includes/lecturasSQL.php';
    xhr.open("POST", artxibo, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4 && xhr.status == 200) {
           var texto = xhr.responseText;
            document.getElementById("content").innerHTML = texto;
            
        }
    };
    xhr.send("txt=" + url);
}

function subirportada(){
	var artxibo = 'includes/upload.processor.php';
	var file = document.getElementById("file").value;
    xhr.open("POST", artxibo, true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.onreadystatechange = function(){
        if (xhr.readyState == 4 && xhr.status == 200) {
           var texto = xhr.responseText;
            document.getElementById("content").innerHTML = texto;         
        }
    };
    xhr.send("file=" + file);
}

// Funcion cross-browser que crea el objeto XMLHttpRequest 

function crearNuevaConexion(){
    var xhr;
    var ieAXOs = ['MSXML2.XMLHTTP.6.0', 'MSXML2.XMLHTTP.3.0', 'MSXML2.XMLHTTP', 'Microsoft.XMLHTTP'];
    try {
        xhr = new XMLHttpRequest();
    } 
    catch (e) {
        for (var i = 0; i < ieAXOs.length; i++) {
            try {
                xhr = new ActiveXObject(ieAXOs[i]);
                break;
            } 
            catch (e) {
            }
        }
    }
    finally {
        return xhr;
    }
}

