var xmlhttp         = null;
var alphaVal        = 0;
var appearDivInv    = null;
//var playerWidth     = 384;
//var playerHeight    = 288;
var playerWidth     = 504; // resolucion normal: 448*336
var playerHeight    = 336;
var objTimeout      = null;
var intervalTimer   = null;
var actualTime      = 0;
var addthis_url     = "";
var alphaVal        = 0;
var appearDivInv    = null;

function getFirstVideo()
{
    loadJSON("nextVideo","/services/getNextVideo.php?promo=1&id=" + obj('idNoticia').value + "&idSecc=" + obj('idSecc').value + "&idSubSecc=" + obj('idSubSecc').value);
}

function getNextVideo()
{
    loadJSON("nextVideo","/services/getNextVideo.php?id=" + obj('idNoticia').value + "&idSecc=" + obj('idSecc').value + "&idSubSecc=" + obj('idSubSecc').value);
}

function getLastVideo()
{
    loadJSON("lastVideo","/services/getLastVideo.php?id=" + obj('idNoticiaAnt').value + "&idSecc=" + obj('idSecc').value + "&idSubSecc=" + obj('idSubSecc').value);
}

function getChoiceVideo(idNoticia)
{
    loadJSON("nextVideo","/services/getLastVideo.php?id=" + idNoticia + "&idSecc=" + obj('idSecc').value + "&idSubSecc=" + obj('idSubSecc').value);
}

function getNewestVideo(idSecc)
{
    loadJSON("newestVideo","/services/getNewestVideo.php?idSecc=" + idSecc);
}

function loadJSON(accion, url)
{
	if(window.XMLHttpRequest){		// code for Mozilla, etc.
		if(xmlhttp){
			xmlhttp.abort;
		}
		xmlhttp = new XMLHttpRequest();
	}
	else{
		if(window.ActiveXObject){	// code for IE
            try{
                xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
            }
			catch(e){
                try{
                    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
                }
				catch(e){
					alert("Name:\t" + e.name + "\nMessage:\t" + e.message);
				}
            }
		}
	}
    if(xmlhttp){
		if(accion == "nextVideo"){
			xmlhttp.onreadystatechange = nextVideo;
		}
		if(accion == "lastVideo"){
			xmlhttp.onreadystatechange = lastVideo;
		}
		if(accion == "newestVideo"){
			xmlhttp.onreadystatechange = newestVideo;
		}
        xmlhttp.open("GET",url,true);		//asynchronious false para que se detenga; false no funciona en Firefox en modo Asincrono
        xmlhttp.send(null);
    }
	else{
    	alert('Giving up :( Cannot create an XMLHTTP instance');
    	return false;
	}
	return 0;
}

function nextVideo()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			if(xmlhttp.responseText){
                //alert(xmlhttp.responseText);
                var jsonObj = eval("(" + xmlhttp.responseText + ")");
                if(jsonObj.msgError == "YES"){
                    obj('encabezado').style.visibility = "hidden";
                    obj('sintesis').style.visibility   = "hidden";
                    startAppearDiv('encabezado');   //dentro de esta funcion se llama a startAppearDiv('sintesis')
                    obj('idNoticiaAnt').value       = obj('idNoticia').value;
                    //obj('noticiaAntDiv').innerHTML  = "<span class='texto1'>Noticia anterior</span><br><span class='liga3' onclick='getLastVideo()'>" + obj('encabezado').innerHTML + "</span";
                    obj('idNoticia').value          = jsonObj.nombre;
                    obj('fechaHora').innerHTML      = jsonObj.fechaHora;
                    obj('encabezado').innerHTML     = jsonObj.encabezado;
                    obj('sintesis').innerHTML       = jsonObj.sintesis;
                    // 2009-05-05 comentado por hackeo // obj('commentsNum').innerHTML    = (parseInt(jsonObj.commentsNum) > 0 ? "(" + jsonObj.commentsNum + ")" : "");
                    obj('commentsNum').innerHTML    = (parseInt(jsonObj.commentsNum) > 0 ? "(" + jsonObj.commentsNum + ")" : "");
                    if(obj('contenido')){
                        obj('contenido').innerHTML  = jsonObj.texto;
                    }
					//obj('hd').innerHTML				= jsonObj.hd;
                    var siguiente    = "/videos/" + jsonObj.archivo;
                    var siguienteImg = "/videos/" + (jsonObj.archivo).substring(0, (jsonObj.archivo).length - 4) + "_imgVideo.jpg";
                    playerOne = null;
                    playerOne = new FAVideo("divOne", null, playerWidth, playerHeight);
                    playerOne.setSkinAutoHide(1);
                    playerOne.load(siguiente);
                    playerOne.setPreviewImagePath(siguienteImg);
                    playerOne.setBufferTime(7);
                    playerOne.setVolume(60);
                    playerOne.addEventListener("complete",this,getNextVideo);
                    playerOne.addEventListener("stateChange",this,checkState);
                    if(objTimeout != null){
                        objTimeout = null;
                    }
                    objTimeout = window.setTimeout("playerOne.play('" + siguiente + "')", 7000);
                    setTimer(5);
                    videoMsgs(jsonObj.nombre);
                    //if(addthis_url){
                    /*    addthis_url = "http://www.info7.com.mx/noticia.php?id=" + jsonObj.nombre + "&secc=" + jsonObj.idSecc;
                        obj('addthis_script').src = "/";
                        obj('addthis_script').src = "http://s7.addthis.com/js/addthis_widget.php?v=12";*/
                    //}
                }
                else{
                    alert(jsonObj.msgError);
                }
			}
        }
    }
	return 0;
}

function lastVideo()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			if(xmlhttp.responseText){
                //alert(xmlhttp.responseText);
                var jsonObj = eval("(" + xmlhttp.responseText + ")");
                if(jsonObj.msgError == "YES"){
                    obj('encabezado').style.visibility = "hidden";
                    obj('sintesis').style.visibility   = "hidden";
                    if(obj('contenido')){
                        obj('contenido').style.visibility  = "hidden";
                    }
                    startAppearDiv('encabezado');   //dentro de esta funcion se llama a startAppearDiv('sintesis')
                    obj('idNoticiaAnt').value       = "";
                    obj('noticiaAntDiv').innerHTML  = "";
                    obj('idNoticia').value          = jsonObj.nombre;
                    obj('fechaHora').innerHTML      = jsonObj.fechaHora;
                    obj('encabezado').innerHTML     = jsonObj.encabezado;
                    obj('sintesis').innerHTML       = jsonObj.sintesis;
                    obj('commentsNum').innerHTML    = (parseInt(jsonObj.commentsNum) > 0 ? "(" + jsonObj.commentsNum + ")" : "");
                    if(obj('contenido')){
                        obj('contenido').innerHTML  = jsonObj.texto;
                    }
					//obj('hd').innerHTML				= jsonObj.hd;
                    var siguiente    = "/videos/" + jsonObj.archivo;
                    var siguienteImg = "/videos/" + (jsonObj.archivo).substring(0, (jsonObj.archivo).length - 4) + "_imgVideo.jpg";
                    playerOne = null;
                    playerOne = new FAVideo("divOne", null, playerWidth, playerHeight);
                    playerOne.setSkinAutoHide(1);
                    playerOne.load(siguiente);
                    playerOne.setPreviewImagePath(siguienteImg);
                    playerOne.setBufferTime(7);
                    playerOne.setVolume(60);
                    playerOne.addEventListener("complete",this,getNextVideo);
                    playerOne.addEventListener("stateChange",this,checkState);
                    if(objTimeout != null){
                        objTimeout = null;
                    }
                    objTimeout = window.setTimeout("playerOne.play('" + siguiente + "')", 7000);
                    setTimer(5);
                    videoMsgs(jsonObj.nombre);
                }
                else{
                    alert(jsonObj.msgError);
                }
			}
        }
    }
	return 0;
}

function newestVideo()
{
	if(xmlhttp.readyState == 4){
  		if(xmlhttp.status == 200){
			if(xmlhttp.responseText){
                //alert(xmlhttp.responseText);
                var jsonObj = eval("(" + xmlhttp.responseText + ")");
                if(jsonObj.msgError == "YES"){
                    obj('encabezado').style.visibility = "hidden";
                    obj('sintesis').style.visibility   = "hidden";
                    if(obj('contenido')){
                        obj('contenido').style.visibility  = "hidden";
                    }
                    startAppearDiv('encabezado');   //dentro de esta funcion se llama a startAppearDiv('sintesis')
                    obj('idNoticiaAnt').value       = "";
                    obj('noticiaAntDiv').innerHTML  = "";
                    obj('idSecc').value             = jsonObj.idSecc;
                    obj('idSubSecc').value          = jsonObj.idSubSecc;
                    obj('idNoticia').value          = jsonObj.nombre;
                    obj('fechaHora').innerHTML      = jsonObj.fechaHora;
                    obj('encabezado').innerHTML     = jsonObj.encabezado;
                    obj('sintesis').innerHTML       = jsonObj.sintesis;
                    obj('commentsNum').innerHTML    = (parseInt(jsonObj.commentsNum) > 0 ? "(" + jsonObj.commentsNum + ")" : "");
                    if(obj('contenido')){
                        obj('contenido').innerHTML  = jsonObj.texto;
                    }
					//obj('hd').innerHTML				= jsonObj.hd;
                    var siguiente    = "/videos/" + jsonObj.archivo;
                    var siguienteImg = "/videos/" + (jsonObj.archivo).substring(0, (jsonObj.archivo).length - 4) + "_imgVideo.jpg";
                    playerOne = null;
                    playerOne = new FAVideo("divOne", null, playerWidth, playerHeight);
                    playerOne.setSkinAutoHide(1);
                    playerOne.load(siguiente);
                    playerOne.setPreviewImagePath(siguienteImg);
                    playerOne.setBufferTime(7);
                    playerOne.setVolume(60);
                    playerOne.addEventListener("complete",this,getNextVideo);
                    playerOne.addEventListener("stateChange",this,checkState);
                    if(objTimeout != null){
                        objTimeout = null;
                    }
                    objTimeout = window.setTimeout("playerOne.play('" + siguiente + "')", 7000);
                    setTimer(5);
                    obj('videoListFrm').src = "/includes/noticias/videoList.php?id=" + jsonObj.nombre + "&secc=" + jsonObj.idSecc;
                }
                else{
                    alert(jsonObj.msgError);
                }
			}
        }
    }
	return 0;
}

function checkState()
{
    if(objTimeout != null){
        if(playerOne.state == "playing"){
            window.clearTimeout(objTimeout);
            objTimeout = null;
            if(intervalTimer != null){
                window.clearInterval(intervalTimer);
                intervalTimer = null;
                actualTime = 0;
                obj('timerTxt').innerHTML = "&nbsp;";
            }
        }
    }
}

function setTimer(x)
{
    if(intervalTimer != null){
        window.clearInterval(intervalTimer);
        intervalTimer = null;
        actualTime = 0;
        obj('timerTxt').innerHTML = "";
    }
    actualTime = x;
    intervalTimer = window.setInterval("changeTimer()", 1000);
}

function changeTimer()
{
    if(actualTime >= 0){
        obj('timerTxt').innerHTML = ":" + actualTime.toString();
        actualTime--;
    }
    else{
        window.clearInterval(intervalTimer);
        intervalTimer = null;
        actualTime = 0;
        obj('timerTxt').innerHTML = "";
    }
}

function videoMsgs(idNoticia)
{
    for(var i = 0; i < videoList.length; i++){
        window.frames['videoListFrm'].document.getElementById('thumb' + videoList[i]).style.border = "2px solid #EEEEEE";
        //window.frames['videoListFrm'].document.getElementById('thumbMsg' + videoList[i]).innerHTML = "";
    }
    for(var i = 0; i < videoList.length; i++){
        //if(videoList[i] == idNoticia)
        //window.frames['videoListFrm'].document.getElementById('thumbMsg' + videoList[i+1]).innerHTML = "Siguiente";
    }
    window.frames['videoListFrm'].document.getElementById('thumb' + idNoticia).style.border = "2px solid #b34141";
    //window.frames['videoListFrm'].document.getElementById('thumbMsg' + idNoticia).innerHTML = "Actual";
}

function startAppearDiv(divId)
{
	if(obj(divId)){
		if(appearDivInv != null){
			window.clearInterval(appearDivInv);
			appearDivInv = null;
			alphaVal = 0;
		}
		if(browser == "Firefox"){
			obj(divId).style.MozOpacity = 0.0;
		}
		else{
			obj(divId).style.filter = "alpha(opacity=0)";
		}
        obj(divId).style.visibility = "visible";
		aplhaVal = 0;
		appearDivInv = window.setInterval('appearDiv("' + divId + '")', 100);
	}
}

function appearDiv(divId)
{
	if(alphaVal < 100){
		alphaVal += 10;
		if(browser == "Firefox"){
			obj(divId).style.MozOpacity	= (alphaVal) / 100;
		}
		else{
			obj(divId).style.filter	= "alpha(opacity=" + (alphaVal + 10).toString() + ")";
		}
	}
	else{
		if(appearDivInv != null){
			window.clearInterval(appearDivInv);
			appearDivInv = null;
			alphaVal = 0;
            if(divId == 'encabezado'){
                startAppearDiv('sintesis');
            }
            if(divId == 'sintesis'){
                startAppearDiv('contenido');
            }
		}
	}
}

function arrows(n)
{
    switch(n){
        /*case 1:
            playerWidth  = (obj('hd').value == 1 ? 320 * 1.125 : 320);
            playerHeight = 240;
            //obj('arrow1').style.visibility = 'visible';
            obj('arrow2').style.visibility = 'hidden';
            obj('arrow3').style.visibility = 'hidden';
            //obj('arrow4').style.visibility = 'hidden';
            break;*/
        /*case 2:
            playerWidth  = (obj('hd').value == 1 ? 384 * 1.125 : 384);
            playerHeight = 288;
            //obj('arrow1').style.visibility = 'hidden';
            obj('arrow2').style.visibility = 'visible';
            obj('arrow3').style.visibility = 'hidden';
            //obj('arrow4').style.visibility = 'hidden';
            break;
        case 3:
            playerWidth  = (obj('hd').value == 1 ? 512 * 1.125 : 512);
            playerHeight = 384;
            //obj('arrow1').style.visibility = 'hidden';
            obj('arrow2').style.visibility = 'hidden';
            obj('arrow3').style.visibility = 'visible';
            //obj('arrow4').style.visibility = 'hidden';
            break;*/
        /*case 4:
            playerWidth  = (obj('hd').value == 1 ? 640 * 1.125 : 640);
            playerHeight = 480;
            obj('arrow1').style.visibility = 'hidden';
            obj('arrow2').style.visibility = 'hidden';
            obj('arrow3').style.visibility = 'hidden';
            obj('arrow4').style.visibility = 'visible';
            break;*/
    }
}

function irVideoComentarios()
{
    window.location.href = "/noticiaOp.php?id=" + obj('idNoticia').value + "&secc=" + obj('idSecc').value + "&subsecc=" + obj('idSubSecc').value;
    return true;
}
