// precaricamento immagini banda corse tris
var images = Array();
images[0] = '/matchpoint/Immagini/Tris/tris_numeri_off.jpg';
images[1] = '/matchpoint/Immagini/Tris/tris_numeri_on.jpg';
var imgout = new Image();
imgout.src=images[0];
var imgover = new Image();
imgover.src=images[1];
function rollImg(ref,over)
{
    var p = document.getElementById(ref);
    if(p != null)
    {
        p.style.background = 'url(' + images[((over) ? 1 : 0)] + ')';
    }
}



var logger = null;

// funzione per il recupero del logger
function getLogger(category, url)
{
    logger = log4javascript.getLogger(category);
    //var popUpLayout = new log4javascript.PatternLayout("%d [%c] [%5p] %m%n");
    //popUpAppender.setLayout(popUpLayout);
    //logger.addAppender(popUpAppender);
    var ajaxAppender = new log4javascript.AjaxAppender(url);
    ajaxAppender.setThreshold(log4javascript.Level.DEBUG);
    ajaxAppender.setWaitForResponse(true);
    logger.addAppender(ajaxAppender);
    
    logger.debug("getLogger - logger recuperato");
}

// funzione per il recupero dell'oggetto XMLHttpRequest o equivalente
function getXmlHttpObject(logger)
{
    var xmlHttp = null;
    
    try
    {
        // Firefox, Opera 8.0+, Safari (anche Internet Explorer 7)
        xmlHttp = new XMLHttpRequest();
    }
    catch (e)
    {
        // Internet Explorer
        try
        {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e)
        {
            try
            {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e)
            {
                logger.error("getXmlHttpObject - " + e, e);
            
                alert("Il tuo browser non supporta AJAX!");
            }
        }
    }
    
    return xmlHttp;
}

// funzione per l'interazione asincrona
// parametri:
// action l'url da richiamare
// id l'id dell'elemento che include il contenuto da aggiornare
// nextCall la funzione da eseguire al completamento della chiamata
function ajaxCall(action, id, nextCall)
{  
    logger.debug("ajaxCall - inizio");
    logger.debug("ajaxCall - action: " + action);
    logger.debug("ajaxCall - id: " + id);
    logger.debug("ajaxCall - nextCall: " + nextCall);
    
    // semaforo assente: posso eseguire la chiamata
    if (document.getElementById('semaforo_' + id) == null)
    {
        try
        {
            // creo il semaforo
            var semaforo = document.createElement('div');
            semaforo.setAttribute('id', 'semaforo_' + id);
            var ajaxDiv = document.getElementById(id);
            if (ajaxDiv != null)
            {
                ajaxDiv.appendChild(semaforo);
                
                logger.debug("ajaxCall - messo semaforo_" + id);
            }
            
            // recupero l'oggetto XMLHttpRequest o equivalente
            var xmlHttp = getXmlHttpObject();
            if (xmlHttp == null)
            {
                return;
            }
            
            // gestisco l'esito della chiamata ajax
            xmlHttp.onreadystatechange = function()
            {
                if(xmlHttp.readyState == 4)
                {
                    if (xmlHttp.status == 200)
                    {
                        logger.trace("ajaxCall - responseText: " + xmlHttp.responseText);
                        
                        // caso cambio pagina
                        if (xmlHttp.responseText.indexOf("location.href") == 0)
                        {
                            eval(xmlHttp.responseText);
                        }
                        // caso messaggio
                        else if (xmlHttp.responseText.indexOf("alert") == 0)
                        {
                            eval(xmlHttp.responseText);
                        }
                        // caso pop up
                        else if (xmlHttp.responseText.indexOf("window.open") >= 0)
                        {
                            eval(xmlHttp.responseText);
                        }
                        // caso cambio stato
                        else if (xmlHttp.responseText.indexOf("noop") == 0)
                        {
                        }
                        // caso sostituzione
                        else
                        {
                            if (ajaxDiv != null)
                            {
                                logger.trace("ajaxCall - innerHTML: " + ajaxDiv.innerHTML);
                                
                                ajaxDiv.innerHTML = xmlHttp.responseText;
                            }
                        }
                        
                        // funzione in cascata
                        if (nextCall != null)
                        {
                            eval(nextCall);
                        }
                    }
                    else
                    {
                        logger.error("ajaxCall - status: " + xmlHttp.status);
                    }
                }
            }
            
            // eseguo la chiamata ajax
            xmlHttp.open("POST", action, true);
            xmlHttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
            xmlHttp.send('');
            delete xmlHttp;
        }
        catch(e)
        {
            logger.error("ajaxCall - " + e, e);
            
            try
            {
                // rimuovo il semaforo
                ajaxDiv.removeChild(semaforo);
            }
            catch(e)
            {
                logger.error("ajaxCall - " + e, e);
            }
            
            logger.debug("ajaxCall - tolto semaforo_" + id);
        }
    }
    // semaforo presente: non eseguo la chiamata
    else
    {
        logger.debug("ajaxCall - semaforo_" + id + " presente");
    }
    
    logger.debug("ajaxCall - fine");
}

function ajaxCallSport(queryString, id, context, masterId, masterIdOld, redirect, anchor)
{
    try
    {
        var sportDiv = document.getElementById(masterId);
        
        // cambio pagina
        if (sportDiv == null && redirect != null)
        {
            if (anchor != null)
            {
                anchor.href = redirect + "&" + queryString;
            }
            else
            {
                location.href = redirect + "&" + queryString;
            }
        }
        else
        {
            var action = context + "/pubblico/servlet/palinsesto?" + queryString;
            var ajaxDiv = document.getElementById(id);
            if (ajaxDiv == null)
            {   
                var sportDivLen = sportDiv.childNodes.length;
                sportDiv.style.display = "block";
                var newDiv = document.createElement('div');
                newDiv.setAttribute('id', id);
                if (sportDivLen > 0)
                {
                    sportDiv.insertBefore(newDiv,sportDiv.childNodes[0]);
                }
                else
                {
                    sportDiv.appendChild(newDiv);
                }
            }
            ajaxCall(action, id);
            resetSportIppica(masterIdOld);
            if (anchor != null)
            {
                window.scrollTo(0,0);
                anchor.href = 'javascript:;';
            }
        }
    }
    catch (e)
    {
        logger.error("ajaxCallSport - " + e, e);
    }
}

function ajaxCallIppica(queryString, id, context, masterId, masterIdOld, redirect, anchor)
{
    try
    {
        var ippicaDiv = document.getElementById(masterId);
        
        // cambio pagina
        if (ippicaDiv == null && redirect != null)
        {
            if (anchor != null)
            {
                anchor.href = redirect + "&" + queryString;
            }
            else
            {
                location.href = redirect + "&" + queryString;
            }
        }
        else
        {
            var action = context + "/pubblico/servlet/palinsesto?" + queryString;
            var ajaxDiv = document.getElementById(id);
            if (ajaxDiv == null)
            {
                var ippicaDivLen = ippicaDiv.childNodes.length;
                ippicaDiv.style.display = "block";
                var newDiv = document.createElement('div');
                newDiv.setAttribute('id', id);
                if (ippicaDivLen > 0)
                {
                    ippicaDiv.insertBefore(newDiv,ippicaDiv.childNodes[0]);
                }
                else
                {
                    ippicaDiv.appendChild(newDiv);
                }
            }
            ajaxCall(action, id);
            resetSportIppica(masterIdOld);
            if (anchor != null)
            {
                window.scrollTo(0,0);
                anchor.href = 'javascript:;';
            }
        }
    }
    catch (e)
    {
        logger.error("ajaxCallIppica - " + e, e);
    }
}

function ajaxCallTris(queryString, context, masterId, nextMasterId)
{
    try
    {
        var action = context + "/pubblico/servlet/tris?" + queryString;
        var nextAction = context + "/pubblico/servlet/quoteTris";
        ajaxCall(action, masterId, 'ajaxCall("' + nextAction + '", "' + nextMasterId + '");');
    }
    catch (e)
    {
        logger.error("ajaxCallTris - " + e, e);
    }
}

function ajaxCallCps(queryString, context, masterId, nextMasterIdOne, nextMasterIdTwo)
{
    try
    {
        var action = context + "/pubblico/servlet/schedina?" + queryString;
        var nextActionOne = context + "/pubblico/servlet/ultimotc";
        var nextActionTwo = context + "/pubblico/servlet/ultimotg";
        
        ajaxCall(action, masterId, 'ajaxCall("' + nextActionOne + '", "' + nextMasterIdOne + '"); ajaxCall("' + nextActionTwo + '", "' + nextMasterIdTwo + '");');
    }
    catch (e)
    {
        logger.error("ajaxCallCps - " + e, e);
    }
}

function ajaxCallPrestazioniIppica(queryString, context)
{
    try
    {
        var action = context + "/pubblico/servlet/prestazioni?" + queryString;    
        ajaxCall(action);
    }
    catch(e)
    {
        logger.error("ajaxCallPrestazioniIppica - " + e, e);
    }    
}

function removeSportIppica(element, redirect)
{
    try
    {
        var el = document.getElementById(element);
        var par = el.parentNode;
        if (el != null && par != null)
        {
            par.removeChild(el);
            
            // cambio pagina
            var change = true;
            for (i = 0; i < par.childNodes.length; i++)
            {
                if (par.childNodes[i].id != null)
                {
                    change = false;
                }
            }
            if (change || (par.childNodes.length == 0))
            {
                if (redirect != null)
                {
                    location.href = redirect;
                }
            }
        }
    }
    catch (e)
    {
        logger.error("removeSportIppica - " + e, e);
    }
}

function resetSportIppica(container)
{
    try
    {
        var con = document.getElementById(container);
        if (con != null)
        {
            while (con.hasChildNodes())
            {
                con.removeChild(con.firstChild);
            }
        }
    }
    catch (e)
    {
        logger.error("resetSportIppica - " + e, e);
    }
}
    
function resetEvidenceMenuSportIppica(section)
{
    try
    {
        var list = document.getElementsByTagName("a");
        if (list != null)
        {
            for(i = 0; i < list.length; i++)
            {
                var id = list.item(i).getAttribute("id");
                if (id != null)
                {
                    if (id.indexOf(section) >= 0)
                    {
                        list.item(i).className = "";
                    }
                }
            }
        }
    }
    catch (e)
    {
        logger.error("resetEvidenceMenuSportIppica - " + e, e);
    }
}

function evidenceMenuSportIppica(section, element)
{
    try
    {
        var el = document.getElementById(section + element);
        if (el != null)
        {
            el.className = "dettaglio";
        }
        if (section == "sportdioggi2_")
        {
            el = document.getElementById('sport2_' + element);
            if (el != null)
            {
                el.className = "dettaglio";
            }
            resetEvidenceMenuSportIppica("ippica2_");
        }
        else if (section == "sport2_")
        {
            el = document.getElementById('sportdioggi2_' + element);
            if (el != null)
            {
                el.className = "dettaglio";
            }
            resetEvidenceMenuSportIppica("ippica2_");
        }
        else if (section == "ippica2_")
        {
            resetEvidenceMenuSportIppica("sport2_");
            resetEvidenceMenuSportIppica("sportdioggi2_");
        }
    }
    catch (e)
    {
        logger.error("evidenceMenuSportIppica - " + e, e);
    }
}

function clearEvidenceMenuSportIppica(section, element)
{
    try
    {
        var el = document.getElementById(section + element);
        if (el != null)
        {
            el.className = "";
        }
        if (section == "sportdioggi2_")
        {
            el = document.getElementById('sport2_' + element);
            if (el != null)
            {
                el.className = "";
            }
        }
        else if (section == "sport2_")
        {
            el = document.getElementById('sportdioggi2_' + element);
            if (el != null)
            {
                el.className = "";
            }
        }
    }
    catch (e)
    {
        logger.error("clearEvidenceMenuSportIppica - " + e, e);
    }
}

// funzione per gestire la visibilità di un elemento
function setDisplay(element, display)
{
    try
    {
        var el = document.getElementById(element);
        if (el != null)
        {
            if (display == true)
            {
                el.style.display = "block";
            }
            else if (display == false)
            {
                el.style.display = "none";
            }
            // modalità non valida
            else
            {
            }
        }
    }
    catch (e)
    {
        logger.error("setDisplay - " + e, e);
    }
}

// funzione per cambiare la visibilità di un elemento
function toggleDisplay(element)
{
    try
    {
        var el = document.getElementById(element);
        if (el != null)
        {
            if(el.style.display == "block")
            {
                el.style.display = "none";
            }
            else
            {
                el.style.display = "block";
            }
        }
    }
    catch (e)
    {
        logger.error("toggleDisplay - " + e, e);
    }
}

function registerStatusMenu(element, channel, context)
{
    toggleDisplay(element);
    
    var el = document.getElementById(element);
    if (el != null)
    {
        try
        {
            var queryString = "voce=" + element + "&stato=" + el.style.display + "&con=" + channel;
            var action = context + "/pubblico/servlet/menu?" + queryString;    
            ajaxCall(action);
        }
        catch(e)
        {
            logger.error("registerStatusMenu - " + e, e);
        }
    }
}

function registerStatusSportIppica(element, channel, context)
{
    toggleDisplay(element);
    
    var el = document.getElementById(element);
    if (el != null)
    {
        try
        {
            var queryString = "voce=" + element + "&stato=" + el.style.display + "&con=" + channel;
            var action = context + "/pubblico/servlet/palinsesto?" + queryString;    
            ajaxCall(action);
        }
        catch(e)
        {
            logger.error("registerStatusSportIppica - " + e, e);
        }
    }
}