function getObjById(id){ 
	var o;
	if(document.getElementById) o = document.getElementById(id);
	else if(document.all) o = document.all.id;	
	if( o == null ) alert('Warning: null object for id:'+id+'!');
	return o;	
}
function validarEmail(mail) { return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(mail)); }

//var host = "http://olanet.dyndns.org/olaFutbol/";
//var host = "http://127.0.0.1/olafutbol/";
var host = "http://www.olafutbol.com.ar/";
//var host = "http://www.olatest.com.ar/olafutbol/";

var READY_STATE_COMPLETE=4;
var peticion_http = null;
 
function httpRequesInit() {
  if(window.XMLHttpRequest) {
    peticion_http = new XMLHttpRequest(); 
  }
  else if(window.ActiveXObject) {
    peticion_http =  new ActiveXObject("Microsoft.XMLHTTP");
  } 
}
 
function httpRequesSend(param,manejador) {
	//alert(param);
	httpRequesInit();
   	if(peticion_http) {
		peticion_http.onreadystatechange = manejador;
		peticion_http.open("POST", host+"index.php", true);
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		peticion_http.send(param);
		return true;
	}
	return false;
}

function enviarMensaje(nom,ma,emp,tel,men){ 
	 httpRequesInit();
 	 if(peticion_http) {
		peticion_http.onreadystatechange = procesaRespuestaEnviarMensaje;
		peticion_http.open("POST", host+"index.php", true);
		peticion_http.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		peticion_http.send("seccion=enviarMensaje&nombre="+nom.value+"&mail="+ma.value+"&empresa="+emp.value+"&telefono="+tel.value+"&mensaje="+men.value);
	}
}

function procesaRespuestaEnviarMensaje() {
  if(peticion_http.readyState == READY_STATE_COMPLETE) {
    if(peticion_http.status == 200) {
      getObjById('respuestaEnviarMensaje').innerHTML = peticion_http.responseText;
    }
  }
}

function mostrarPublicidadGrande(img){ 
	jQuery(function($) { $.slimbox(img);} );
}

function buscar(form,np,btn){
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	else return true;
	if (keycode == 13 || btn){
   		if(getObjById('criterio').value.length<3){
			alert('El criterio de busqueda debe tener al menos 3 carwerracteres');
			return false;
		}
		form.evento.value = 'buscar';
		form.seccion.value = 'noticias';
		form.nroPagina.value = np;
		form.submit();
	}
}
function menuPrincipal(form,ev,sec){
	
	//alert(ev);
	switch(ev){
		case 'buscar':
			if(getObjById('criterio').value.length<3){
				alert('El criterio de busqueda debe tener al menos 3 caracteres');
				return;
			}
			break;
		case 'verCalendario':
			
			obj = getObjById('calendario');
			if(obj.style.display == 'block')
				obj.style.display = 'none';
			else{
				//alert('verCalendario mostrar');
				getObjById('divWaiting').style.display = 'block';
				getObjById('contacto').style.display = 'none';
				navegar('','');
			}
			return;
		case 'verSeccion':
			form.seccionNoticias.value = sec;
			break;
		case 'nuestroStaff':
			getObjById('divWaiting').style.display = 'block';
			getObjById('calendario').style.display = 'none';
			if(! httpRequesSend("seccion=contacto&evento=nuestroStaff",procesaRespuestaContacto)){
				alert('Internal error');
			}
			return;
		case 'initContacto':
			getObjById('divWaiting').style.display = 'block';
			getObjById('calendario').style.display = 'none';
			if(! httpRequesSend("seccion=contacto&evento=initContacto",procesaRespuestaContacto)){
				alert('Internal error');
			}
			return;
		case 'enviarMensaje':
			enviarMensaje();
			return;
	}
	form.evento.value = ev;
	form.seccion.value = 'noticias';
	form.submit();
}

function menuIzquierdo(form,ev,sec){
	if(ev=='verSecciones'){
		mostrar('menuSecciones');
		return;
	}
	form.seccionNoticias.value = sec;
	form.evento.value = ev;
	if(ev=='estadisticas') form.seccion.value = 'estadisticas';
	else form.seccion.value = 'noticias';
	form.submit();
}

function menuNoticia(form,id,ev,coment){
	form.seccion.value = 'noticias';
	if(ev=='recomendarNoticia'){ recomendarNoticia(id); return; }
	form.evento.value = ev;
	form.comentarios.value = coment;
	form.idNot.value = id;
	if(ev=='agregarComent'){
		if(getObjById('nombre').value=='' || getObjById('comentario').value==''){
			alert('Los campos nombre y comentario son obligatorios');
			return;
		}
	}
	form.submit();
}

function mostrar(campo){ 
	c = getObjById(campo); 
    if(c.style.display =='block'){ c.style.display = 'none'; }
    else{ c.style.display = 'block'; }
}

function initRecomendarNoticia(id){
	if(! httpRequesSend("seccion=noticias&evento=initRecomendarNoticia&idNot="+id,procesaRespuestaRecomendar)){
		alert('Internal error');
		return;	
	}
}

function recomendarNoticia(id){
	nombre = getObjById('nombre').value;
	nombreDest = getObjById('nombreDest').value;
	mailDest = getObjById('mailDest').value;
	comentario = getObjById('comentario').value;
	titulo = getObjById('tituloNoticiaGrande').innerHTML;
	if(nombre==''||nombreDest==''||mailDest==''){
		getObjById('msgErrorRec').innerHTML = 'Error: Debe completar los campos obligatorios';
		return;
	}
	if(!validarEmail(mailDest)){ 
		getObjById('msgErrorRec').innerHTML = 'Error: La dirección de mail no es válida';
		return;
	}
	req = "seccion=noticias&evento=recomendarNoticia&idNot="+id+
			"&nombre="+nombre+"&nombreDest="+nombreDest+"&mailDest="+mailDest+"&comentario="+comentario+"&titulo="+titulo;
	if(! httpRequesSend(req,procesaRespuestaRecomendar)){
		alert('Internal error');
		return;	
	}
}

function procesaRespuestaRecomendar() {
	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if(peticion_http.status == 200) {
			divRec = getObjById('espacioParaRecomendar');
			divRec.innerHTML = peticion_http.responseText;
			divRec.style.display = 'block';
		}
	}
}

function procesaRespuestaContacto() {
	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if(peticion_http.status == 200) {
			getObjById('divWaiting').style.display = 'none';
			divCont = getObjById('contacto');
			divCont.innerHTML = peticion_http.responseText;
			divCont.style.display = 'block';
		}
	}
}

function verSecciones(){
	getObjById('divWaiting').style.display = 'block';
	if(! httpRequesSend("seccion=noticias&evento=verSecciones",procesaRespuestaVerSecciones)){
		alert('Internal error');
		return;	
	}
}
function procesaRespuestaVerSecciones(){
	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if(peticion_http.status == 200) {
			getObjById('divWaiting').style.display = 'none';
			divSec = getObjById('secciones');
			divSec.innerHTML = peticion_http.responseText;
			divSec.style.display = 'block';
		}
	}
}

function enviarMensaje(){
	nombre = getObjById('nombre').value;
	telefono = getObjById('telefono').value;
	mail = getObjById('mail').value;
	mensaje = getObjById('mensaje').value;
	if(nombre==''||mensaje==''||mail==''){
		getObjById('msgErrorCont').innerHTML = 'Error: Debe completar los campos obligatorios';
		return;
	}
	if(!validarEmail(mail)){ 
		getObjById('msgErrorCont').innerHTML = 'Error: La dirección de mail no es válida';
		return;
	}
	req = "seccion=contacto&evento=enviarMensaje&nombre="+nombre+"&mail="+mail+"&mensaje="+mensaje;
	if(telefono!='') req += "&telefono="+telefono;
	//alert(req);
	if(! httpRequesSend(req,procesaRespuestaContacto)){
		alert('Internal error');
		return;	
	}
}


function fotoAnt(form){
	act = parseInt(form.fotoActual.value); cant = parseInt(form.cantFotos.value);
	if(act<=1){	return;	}
	idDivAct = 'foto'+act; ant = parseInt(act)-1;
	idDivAnt = 'foto' + ant; form.fotoActual.value=ant;
	getObjById(idDivAct).style.display='none';
	getObjById(idDivAnt).style.display='block';
	setStyleButtons(form,act);
}

function fotoSig(form){
	act = parseInt(form.fotoActual.value); cant = parseInt(form.cantFotos.value);
	if(act>=cant){	alert('act>=cant');return;	}
	idDivAct = 'foto'+act;	sig = parseInt(act)+1;
	idDivSig = 'foto' + sig; form.fotoActual.value=sig;
	getObjById(idDivAct).style.display='none';
	getObjById(idDivSig).style.display='block';
	setStyleButtons(form,act);
}

function setStyleButtons(form,antNum){
	act = parseInt(form.fotoActual.value); cant = parseInt(form.cantFotos.value);
	getObjById('leyendaFoto').innerHTML=getObjById('leyendaFoto'+act).value;
	if( act == cant ){	getObjById('idFlechaSig').style.visibility='hidden'; }
	else{	getObjById('idFlechaSig').style.visibility='visible'; }
	if( act == 1 ){ getObjById('idFlechaAnt').style.visibility='hidden';  }
	else{ getObjById('idFlechaAnt').style.visibility='visible'; }
	idTdAct = 'spanFoto'+act;	idTdAnt = 'spanFoto'+antNum;
	getObjById(idTdAct).className = 'spanNumFotoSel';
	getObjById(idTdAnt).className = 'spanNumFoto';
}

function verFoto(form,fotoNum,td){
	act = form.fotoActual.value; idDivAct = 'foto'+act;
	getObjById(idDivAct).style.display='none';
	idDivNewAct	= 'foto'+fotoNum;
	getObjById(idDivNewAct).style.display='block';
	form.fotoActual.value=fotoNum;
	setStyleButtons(form,act);
}

var tamTextoObj1=12;
var tamTextoObj2=13;
function aumentarTamanioTexto(obj1,obj2){
	if(obj1){
		if(tamTextoObj1<16){
			tamTextoObj1+=1;
			obj1.style.fontSize = tamTextoObj1;
		}
	}
	if(obj2){
		if(tamTextoObj2<17){
			tamTextoObj2+=1;
			obj2.style.fontSize = tamTextoObj2;
		}
	}
}
function disminuirTamanioTexto(obj1,obj2){
	if(obj1){
		if(tamTextoObj1>12){
			tamTextoObj1-=1;
			obj1.style.fontSize = tamTextoObj1;
		}
	}
	if(obj2){
		if(tamTextoObj2>13){
			tamTextoObj2-=1;
			obj2.style.fontSize = tamTextoObj2;
		}
	}
}
/*
Ejemplo de uso de ajax
function llamador(){
	var url = host+'index.php?param1Key=param1Value&param2Key=param2Value';
	if(! httpRequesSend(url,recibidor)){
		alert('Internal error');
		return;	
	}
}
fuction recibidor(){
	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if(peticion_http.status == 200) {
			obj = getObjById('idDelDivDondeVaElContenido');
			//alert(req.responseText);
			obj.innerHTML = req.responseText;
		}
	}
}
*/
/*calendario*/
var req;

function navegar(month,year) {
	//getObjById('divWaiting').style.display = 'block';
	//setFade(0,'calendario');
	if( month!='' && year!='' )
		var url = host+"index.php?&seccion=noticias&evento=verCalendario&month="+month+"&year="+year;
	else
		var url = host+'index.php?seccion=noticias&evento=verCalendario';
	if( ! httpRequesSend(url,procesaRespuestaNavegar) ){
		alert('Internal error');
		return;	
	}
}

function procesaRespuestaNavegar() {	
	//alert('papap');
	if(peticion_http.readyState == READY_STATE_COMPLETE) {
		if(peticion_http.status == 200) {
			obj = getObjById('calendario');
			getObjById('divWaiting').style.display = 'none';
			obj.innerHTML = peticion_http.responseText;
			if(obj.style.display != 'block') obj.style.display = 'block';
			fade(70,'calendario');
		}
	}
}
var idIntervalDest; 
var actDest=1;
var cantDest=0;
function pasadorDestacadas(){ idIntervalDest = setInterval("navegarDestacadas('x')",6000); }
function pararPasador(){ setTimeout("clearInterval("+idIntervalDest+")",1); }

function navegarDestacadas(opcion){
	if(!cantDest) cantDest = getObjById('cantNotDest').value;
	
	if( opcion=='p' ) {  pararPasador(); return; }
	desvanecer('contenidoDestacadas');
	
	getObjById('noticiaDest'+actDest).style.display = 'none';
	if(opcion=='i'){
		pararPasador();
		if(actDest>1) actDest--; else actDest = cantDest;
	}else{
		if(opcion == 'd'){
			pararPasador();
			if(actDest<cantDest) actDest++; else actDest = 1;
		}else {//x
			 if(actDest<cantDest){
				 actDest++;
			 }else{
				 actDest = 1;
			 }
		}
	}
	
	getObjById('noticiaDest'+actDest).style.display = 'block';
	
	aparecer('contenidoDestacadas');
}

var itemEspActual=1;
var idIntervalEsp;
function pasadorEspeciales(){ idIntervalEsp = setInterval("navegarEspeciales('x')",4000); }
function pararPasador(){ setTimeout("clearInterval("+idIntervalEsp+")",1); }

function navegarEspeciales(opcion){
	
	
	if( opcion=='p' ) {  pararPasador(); return; }
	desvanecerEsp('contenidoEspeciales');
	cont=1;
	while(cont<5){
		getObjById('itemEsp'+cont).className='itemEspeciales';
		cont++;
	}
	
	getObjById('noticiaEsp'+itemEspActual).style.display = 'none';
	itemEspActual++;
	if(itemEspActual==5)itemEspActual=1;
	getObjById('itemEsp'+itemEspActual).className='itemEspecialesSel';
	getObjById('noticiaEsp'+itemEspActual).style.display = 'block';
	/*
	getObjById('noticiaDest'+actDest).style.display = 'none';
	if(opcion=='i'){
		pararPasador();
		if(actDest>1) actDest--; else actDest = cantDest;
	}else{
		if(opcion == 'd'){
			pararPasador();
			if(actDest<cantDest) actDest++; else actDest = 1;
		}else {//x
			 if(actDest<cantDest){
				 actDest++;
			 }else{
				 actDest = 1;
			 }
		}
	}
	
	getObjById('noticiaDest'+actDest).style.display = 'block';
	*/
	aparecerEsp('contenidoEspeciales');
}

var color = 0;
var suma = 10;
function aparecer(objId){
	var objDest = getObjById(objId);
	color += suma;
	if (!(color>=110)){
		objDest.style.filter = 'alpha(opacity='+color+')';
		objDest.style.opacity = color/100;
		objDest.style.MozOpacity = color/100;
		objDest.style.KHTMLOpacity = color/100;
		window.setTimeout ("aparecer('"+objId+"');", 50);
	}else color = 0;
}
function desvanecer(objId) {
	var objDest = getObjById(objId);
	color -= suma;
	if (!(color <=(-10))){	
		objDest.style.filter = 'alpha(opacity='+color+')';
		objDest.style.opacity = color/100;
		objDest.style.MozOpacity = color/100;
		objDest.style.KHTMLOpacity = color/100;
		window.setTimeout ("desvanecer('"+objId+"');", 50);
	}
}



var colorEsp = 0;
var sumaEsp = 10;
function aparecerEsp(objId){
	var objEsp = getObjById(objId);
	colorEsp += sumaEsp;
	if (!(colorEsp>=110)){
		objEsp.style.filter = 'alpha(opacity='+colorEsp+')';
		objEsp.style.opacity = colorEsp/100;
		objEsp.style.MozOpacity = colorEsp/100;
		objEsp.style.KHTMLOpacity = colorEsp/100;
		window.setTimeout ("aparecerEsp('"+objId+"');", 30);
	}else colorEsp = 0;
}
function desvanecerEsp(objId) {
	var objEsp = getObjById(objId);
	colorEsp -= sumaEsp;
	if (!(colorEsp <=(-10))){	
		objEsp.style.filter = 'alpha(opacity='+colorEsp+')';
		objEsp.style.opacity = colorEsp/100;
		objEsp.style.MozOpacity = colorEsp/100;
		objEsp.style.KHTMLOpacity = colorEsp/100;
		window.setTimeout ("desvanecerEsp('"+objId+"');", 30);
	}
}
	
function verEdicAnterior(form,dia){
	form.evento.value = 'verEdicAnter';
	form.seccion.value = 'noticias';
	form.diaVerEdicAnt.value = dia;
	form.submit();
}


function fade(amt,obj) {
	if(amt <= 100) {
		setFade(amt,obj);
		amt += 10;
		setTimeout("fade("+amt+")", 5);
    }
}

function setFade(amt,obj) {
	var obj = getObjById('calendario');
	amt = (amt == 100)?99.999:amt;
	obj.style.filter = "alpha(opacity:"+amt+")";
	obj.style.KHTMLOpacity = amt/100;
	obj.style.MozOpacity = amt/100;
	obj.style.opacity = amt/100;
}

//------------------  Images Rotator -------------------------------------------------------------------------------//
//window.addEventListener?window.addEventListener('load',so_init,false):window.attachEvent('onload',so_init);

var d=document, imgs = new Array(), zInterval = null, current=0, pause=false;

function so_init()
{
	if(!d.getElementById || !d.createElement)return;

	imgs = d.getElementById('contenidoDestacadas').getElementsByTagName('img');
	for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
	imgs[0].style.display = 'block';
	imgs[0].xOpacity = .99;

	setTimeout(so_xfade,3000);
}

function so_xfade()
{
	cOpacity = imgs[current].xOpacity;
	nIndex = imgs[current+1]?current+1:0;
	nOpacity = imgs[nIndex].xOpacity;

	cOpacity-=.05;
	nOpacity+=.05;

	imgs[nIndex].style.display = 'block';
	imgs[current].xOpacity = cOpacity;
	imgs[nIndex].xOpacity = nOpacity;

	setOpacity(imgs[current]);
	setOpacity(imgs[nIndex]);

	if(cOpacity<=0)
	{
		imgs[current].style.display = 'none';
		current = nIndex;
		setTimeout(so_xfade,3000);
	}
	else
	{
		setTimeout(so_xfade,50);
	}

	function setOpacity(obj)
	{
		if(obj.xOpacity>.99)
		{
			obj.xOpacity = .99;
			return;
		}

		obj.style.opacity = obj.xOpacity;
		obj.style.MozOpacity = obj.xOpacity;
		obj.style.filter = 'alpha(opacity=' + (obj.xOpacity*100) + ')';
	}
}
