/********************************************************************************************/
/* AJAX Engine creado por Oliver Meléndez (A.K.A Mr. X-ecutor)								*/
/* Puedes usar estas funciones como desees para aplicaciones comerciales y no comerciales,  */
/* ten en cuenta que no garantizo de la utilidad, versatilidad o exactitud y no acepta		*/
/* ninguna responsabilidad por cualquier pérdida causada por su uso.		                */
/********************************************************************************************/
//Creando la pseudo clase, así todo será mas sencillo
function classAjax() {
	this.http;
}
classAjax.prototype.getHttp = function() {
	try {
		req = new XMLHttpRequest(); /* Firefox,Netscape */
	} catch(e) {
		try {
			req = new ActiveXObject("Msxml2.XMLHTTP");  /* IE actuales */
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");  /* IE 6 */
			} catch (E) {
				req = false;
			}
		}
	}
	return req;
}
	
classAjax.prototype.do_ajax = function(metodo,pageUrl,val,str) {
    this.http.open(metodo,pageUrl,true);
	var bif = str;
	this.http.onreadystatechange = function() {
		//str nos indica a que aplicacion dirigir el objeto XMLHttpRequest, val es el numero que le hemos asignado a la funcion de respuesta en el switch de los arrayFunc que hemos creado
		switch(bif) {
			case "gal": arrayFunc(val); break;
			case "inic": arrIniFunc(val); break;
			case "mult": arrMultFunc(val); break;
		}
	};
    this.http.send(null);
}


//en esta funcion escribe todas las funciones para las respuestas del servidor.. CUIDADO!!: debes asegurarte que todas las funciones de respuestas el servidor encajen con los propósitos de los envios de cabeceras que hagas en los archivos framework!!

var docMultXML;
var num_video;
var vNombre_video = new Array();
var vFecha_video = new Array();
var video_XML = new Array();
var vUrl_video = new Array();
var vLargo = new Array();
var vAncho = new Array();
var vDesc_video = new Array();
var titulo_mult;
var cont;

//arrayFunc
function arrayFunc(valor) {
	switch(valor){
		case 1: updateList(); break;
		case 2: function2(); break;
		case 3: formatListGal(); break;
	}
}
function updateList() {
	if (ajax.http.readyState == 4) {
		if(ajax.http.status == 200) {
			var docXML = ajax.http.responseXML;
			var root = docXML.firstChild.nodeName;
			if (root == "gallery") {
				galleryList();
			}else{
				alert("error interno -- "+docXML.firstChild.childNodes[1].attributes[0].nodeValue+": "+docXML.firstChild.childNodes[1].childNodes[0].nodeValue);
				galleryList();
			}
			//alert(docXML);
		}
	}
}

function function2() {
	if (ajax.http.readyState == 4) {
		if(ajax.http.status == 200) {
			var docXML = ajax.http.responseXML;
			if (docXML.firstChild.nodeName) {
			/*raiz = docXML.firstChild;
			nodo = raiz.getElementsByTagName("sub")[0].childNodes[0].nodeValue;*/
			alert(docXML.firstChild.nodeName);
			}
		}
	}
}

function formatListGal() {
	if (ajax.http.readyState == 4) {
		if(ajax.http.status == 200) {
			docXML = ajax.http.responseXML;
			var list = n("gallery");
			var showCase = document.getElementById("showCase");
			var lista ="";
			for (a=0;a<list.length;a++){
				var numer = a + 1;
				//var title = list[a].getElementsByTagName("title")[0].childNodes[0].nodeValue;
				var nombre = list[a].getElementsByTagName("name")[0].childNodes[0].nodeValue;
				lista += "<li name=\""+nombre+"\" id=\"fila"+numer+"\" class=\"fila\" onclick=\"selectControl(this);\">"+nombre+"</li>";
			}
			showCase.childNodes[0].innerHTML = lista;
		}
	}
}

//arrIniFunc
function arrIniFunc(valor) {
	switch(valor){
		case 1: respNotice(); break;
		case 2: respListNotices(); break;
		case 3: respPrtNotice(); break;
	}
}

function respNotice() {
	if (ajax.http.readyState == 4) {
		if (ajax.http.status == 200) {
			docXML = ajax.http.responseXML;
			var apart = n("apartado")[0];
			//internet explorer no toma en consideracion a los nodos texto producido por espacios en blanco o nueva linea por lo que la cantidad de childs dentro del tag #apartado en mozilla sera mayor que los que registre en internet explorer.
			var nav = getNaveg();
			if (nav == 'Microsoft Internet Explorer') {
				var tam = apart.childNodes.length;
			}else{
				var tam = ((apart.childNodes.length)-1)/2;
			}
			var cont = "";
			var tabl = "";
			var liga = "";
			var fot = "";
			//recorriendo apartado
			for(x=0;x<tam;x++){
				if (nav == 'Microsoft Internet Explorer') {
					var goto = x;
				}else{
					var goto = (x*2)+1;
				}
				//variables variables
				var jump = apart.childNodes[goto];
				if(jump.nodeName == 'subtexto') {
					cont += "<p>"+jump.childNodes[0].nodeValue+"</p>";
				}else if(jump.nodeName == 'titulo'){
					var tit = '<h5><span class="notiL">'+jump.childNodes[0].nodeValue+'</span></h5>';
				}else if(jump.nodeName == 'foto') {
					if (jump.attributes[2].nodeValue == "center") {
						fot += '<center><img class="noticias" width="'+jump.attributes[0].nodeValue+'" height="'+jump.attributes[1].nodeValue+'" src="'+jump.childNodes[0].nodeValue+'" /></center>';
					}else{
						fot += '<img class="noticias" width="'+jump.attributes[0].nodeValue+'" height="'+jump.attributes[1].nodeValue+'" align="'+jump.attributes[2].nodeValue+'" src="'+jump.childNodes[0].nodeValue+'" />';
					}
				}else if(jump.nodeName == 'fecha') {
					var dat = 'Fecha: '+jump.childNodes[0].nodeValue;
				}else if(jump.nodeName == 'tabla') {
					tabl += '<br clear="right" /><center><table class="entire" style="border:1px solid #7C7C7C;">';
					var filen = ((jump.childNodes.length)-1)/2;
					for(y=0;y<filen;y++) {
						if (nav == 'Microsoft Internet Explorer') {
							var gofil = y;
						}else{
							var gofil = (y*2)+1;
						}
						rowData = jump.childNodes[gofil].childNodes[0].nodeValue;
						arrData = rowData.split(", ");
						arrDatL = arrData.length;
						tabl += "<tr>";
						for(z=0;z<arrDatL;z++) {
							tabl += '<td width="150" height="30" style="border:1px solid #7C7C7C; text-align:center">'+arrData[z]+'</td>';
						}
						tabl += "</tr>";
					}
					tabl += "</table></center>";
				}else if(jump.nodeName == 'enlace') {
					liga += '<a href="'+jump.attributes[0].nodeValue+'" style="margin:0 0 0 5px; color:#FF0000;">'+jump.childNodes[0].nodeValue+'</a>';
				}
			}
			//depositando el contenido
			document.getElementById("datecell").innerHTML = dat;
			document.getElementById("tabletit").innerHTML = tit;
			document.getElementById("tablebody").innerHTML = fot+cont+tabl+liga;
		}
	}
}

function respListNotices() {
	if (ajax.http.readyState == 4) {
		if (ajax.http.status == 200) {
			docXML = ajax.http.responseXML;
			var not = n("noticias")[0];
			var nav = getNaveg();
			if (nav == 'Microsoft Internet Explorer') {
				var tam = not.childNodes.length;
			}else{
				var tam = ((not.childNodes.length)-1)/2;
			}
			var cont = "";
			//recorriendo apartado
			for(x=0;x<tam;x++){
				if (nav == 'Microsoft Internet Explorer') {
					var goto = x;
				}else{
					var goto = (x*2)+1;
					
				}
				var sum = x + 1
				var jump = not.childNodes[goto];
				document.getElementById("titlelist").innerHTML += "<li class=\"common\"><a href=\"novedades.php?id=5&num="+sum+"\">"+jump.childNodes[0].nodeValue+"</a></li>\n";
			}
		}
	}
}

function respPrtNotice() {
	if (ajax.http.readyState == 4) {
		if (ajax.http.status == 200) {
			docXML = ajax.http.responseXML;
			var apart = n("apartado")[0];
			//internet explorer no toma en consideracion a los nodos texto producido por espacios en blanco o nueva linea por lo que la cantidad de childs dentro del tag #apartado en mozilla sera mayor que los que registre en internet explorer.
			var nav = getNaveg();
			if (nav == 'Microsoft Internet Explorer') {
				var tam = apart.childNodes.length;
			}else{
				var tam = ((apart.childNodes.length)-1)/2;
			}
			var cont = "";
			//recorriendo apartado
			for(x=0;x<tam;x++){
				if (nav == 'Microsoft Internet Explorer') {
					var goto = x;
				}else{
					var goto = (x*2)+1;
				}
				//variables variables
				var jump = apart.childNodes[goto];
				if(jump.nodeName == 'subtexto') {
					cont += "<p>"+jump.childNodes[0].nodeValue+"</p>";
				}else if(jump.nodeName == 'titulo'){
					var tit = '<h5>'+jump.childNodes[0].nodeValue+'</h5>';
				}else if(jump.nodeName == 'foto') {
					var fot = '<img class="noticias" width="'+jump.attributes[0].nodeValue+'" height="'+jump.attributes[1].nodeValue+'" align="right" src="'+jump.childNodes[0].nodeValue+'" />';
				}else if(jump.nodeName == 'fecha') {
					var dat = 'Fecha: '+jump.childNodes[0].nodeValue;
				}
			}
			//depositando el contenido
			document.getElementById("datecell").innerHTML = dat;
			document.getElementById("titlecell").innerHTML = tit;
			document.getElementById("contcell").innerHTML = "<div align=\"justify\">"+fot+cont+"</div>";
			window.print();
		}
	}
}

function arrMultFunc(valor) {
	switch(valor){
		case 1: respListVideo(); break;
	}
}

function respListVideo() {
	if (ajax.http.readyState == 4) {
		if (ajax.http.status == 200) {
			docMultXML = ajax.http.responseXML;
			crear_tMulti("Video", "videoFile");
		}
	}
}

function crear_tMulti(varia1, varia2) {
	mult_select(varia1, varia2);
	var tabla_sh = document.getElementById("multiTbShow");
	var rows_exist = document.getElementById("multiTbShow").rows.length;
	//tabla_sh.rows[0].cells[0].innerHTML = "<h3>"+titulo_mult+"</h3>"
	tabla_sh.rows[1].cells[0].innerHTML = "<a id='videoObj' href='#'><object width="+vLargo[0]+" height="+vAncho[0]+"><param name='movie' value="+vUrl_video[0]+"></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src="+vUrl_video[0]+" type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width="+vLargo[0]+" height="+vAncho[0]+"></embed></object></a>";
	if (rows_exist > 3) {
		alert("primera");
		for (var d=3; d<rows_exist; d++) {
			tabla_sh.deleteRow(3);
		}
		for (a=3; a<num_video+3; a++) {
			var c = a - 3;
			var filaX = tabla_sh.insertRow(a);
			tabla_sh.rows[a].id = a;
			tabla_sh.rows[a].setAttribute("class", "muldata");
			filaX.onmouseover = function() {
				this.style.cursor="pointer";
				this.style.backgroundColor="#C8E5FC";
			}
			filaX.onmouseout = function() {
				this.style.cursor="pointer";
				this.style.backgroundColor="#F0F8FE";
			}
			filaX.onclick = function() {
				var indexTb = this.id - 3;
				
				if (varia1 != "imagenes") {
					tabla_sh.rows[1].cells[0].innerHTML = "<a id='videoObj' href='#'><object width="+vLargo[indexTb]+" height="+vAncho[indexTb]+"><param name='movie' value="+vUrl_video[indexTb]+"></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src="+vUrl_video[indexTb]+" type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width="+vLargo[indexTb]+" height="+vAncho[indexTb]+"></embed></object></a>";
				}else{
					ventana_pop2(vUrl_video[indexTb], vLargo[indexTb], vAncho[indexTb]);
				}
			}
			var celda1 = filaX.insertCell(0);
			var celda2 = filaX.insertCell(1);
			var celda3 = filaX.insertCell(2);
			celda1.setAttribute('align', 'left');
			celda2.setAttribute('align', 'left');
			celda3.setAttribute('align', 'left');
			celda1.innerHTML = vNombre_video[c];
			celda2.innerHTML = vFecha_video[c];
			celda3.innerHTML = vDesc_video[c];
		}
	}else{
		for (a=3; a<num_video+3; a++) {
			var c = a - 3;
			var filaX = tabla_sh.insertRow(a);
			tabla_sh.rows[a].id = a;
			tabla_sh.rows[a].setAttribute("class", "multdata");
			filaX.onmouseover = function() {
				this.style.cursor="pointer";
				this.style.backgroundColor="#C8E5FC";
			}
			filaX.onmouseout = function() {
				this.style.cursor="pointer";
				this.style.backgroundColor="#F0F8FE";
			}
			filaX.onclick = function() {
				var indexTb = this.id - 3;
				document.getElementById("videoObj").focus();//focus al video
				if (varia1 != "imagenes") {
					tabla_sh.rows[1].cells[0].innerHTML = "<a id='videoObj' href='#'><object width="+vLargo[indexTb]+" height="+vAncho[indexTb]+"><param name='movie' value="+vUrl_video[indexTb]+"></param><param name='allowFullScreen' value='true'></param><param name='allowscriptaccess' value='always'></param><embed src="+vUrl_video[indexTb]+" type='application/x-shockwave-flash' allowscriptaccess='always' allowfullscreen='true' width="+vLargo[indexTb]+" height="+vAncho[indexTb]+"></embed></object></a>";
				}else{
					ventana_pop2(vUrl_video[indexTb], vLargo[indexTb], vAncho[indexTb]);
				}
			}
			var celda1 = filaX.insertCell(0);
			var celda2 = filaX.insertCell(1);
			var celda3 = filaX.insertCell(2);
			celda1.setAttribute('align', 'left');
			celda1.setAttribute('class', 'multdata');
			celda2.setAttribute('align', 'left');
			celda2.setAttribute('class', 'multdata');
			celda3.setAttribute('align', 'left');
			celda3.setAttribute('class', 'multdata');
			celda1.innerHTML = vFecha_video[c];
			celda2.innerHTML = vNombre_video[c];
			celda3.innerHTML = vDesc_video[c];
		}
	}
}

function mult_select(media, type) {
	var multiXML = docMultXML.getElementsByTagName(media)[0];
	num_video = multiXML.getElementsByTagName(type).length;
	//titulo_mult = media;
	for (var b=0; b<num_video; b++) {
		video_XML[b] = multiXML.getElementsByTagName(type)[b];
		vFecha_video[b] = video_XML[b].attributes.item(0).nodeValue;
		vNombre_video[b] = video_XML[b].attributes.item(1).nodeValue;
		vUrl_video[b] = video_XML[b].attributes.item(2).nodeValue;
		vLargo[b] = video_XML[b].attributes.item(3).nodeValue;
		vAncho[b] = video_XML[b].attributes.item(4).nodeValue;
		vDesc_video[b] = video_XML[b].childNodes[0].nodeValue;
	}
}
