﻿nie = (navigator.appName.indexOf("Microsoft") == -1);

function getel(doc, id) {
    return doc.getElementById(id);
}

function el(id) {
    return getel(document, id);
}

function arr(n) {
    this.length = n
    for (var i = 0; i < n; i = i + 1) {
        this[i] = ''
    }
}

function element_top(el) {
    var et = 0
    while (el) {
        et += el.offsetTop
        el = el.offsetParent
    }
    return et
}

function element_left(el) {
    var et = 0
    while (el) {
        et += el.offsetLeft
        el = el.offsetParent
    }
    return et
}

function getElementsById(sId) {
    var outArray = new Array();

    if (typeof (sId) != 'string' || !sId) {
        return outArray;
    };

    if (document.evaluate) {
        var xpathString = "//*[@id='" + sId.toString() + "']"
        var xpathResult = document.evaluate(xpathString, document, null, 0, null);
        while ((outArray[outArray.length] = xpathResult.iterateNext())) { }
        outArray.pop();
    }
    else if (document.all) {

        if (document.all[sId])
            for (var i = 0, j = document.all[sId].length; i < j; i += 1) {
            outArray[i] = document.all[sId][i];
        }

    } else if (document.getElementsByTagName) {

        var aEl = document.getElementsByTagName('*');
        for (var i = 0, j = aEl.length; i < j; i += 1) {

            if (aEl[i].id == sId) {
                outArray.push(aEl[i]);
            };
        };

    };

    if ((outArray.length == 0) && (el(sId)))
        outArray[0] = el(sId);

    return outArray;
}


function showProgressImage(id) {
    pimg = el(id);
    pimg.style.visibility = 'visible';
    pimg.src = pimg.src;
}

function escreve(s) {
    document.write(s);
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function getAjax() {
    http_request = false;
    if (window.XMLHttpRequest) { // Mozilla, Safari,...
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            // set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            // http_request.overrideMimeType("text/html; charset=windows-1252");
        }
    } else if (window.ActiveXObject) { // IE
        try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e) {
            try {
                http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) { }
        }
    }
    if (!http_request) {
        alert('Desculpe, seu navegador é incompatível com este recurso.');
        return false;
    }
    return http_request;
}

function mv(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1)
        return document.all[movieName]
    else if (document[movieName])
        return document[movieName]
    else
        return el(movieName)
}
function loadMovie(id, movie, params) {
    mv(id).LoadMovie(0, movie);
    if (typeof (params) != 'undefined')
        for (p = 0; p < params.length; p += 2)
        mv(id).SetVariable(params[p], params[p + 1]);
}

function movieIsLoaded(theMovie) {
    if (typeof (theMovie) != "undefined") {
        return ((typeof (theMovie.PercentLoaded) != "undefined") && (theMovie.PercentLoaded() == 100));
    } else {
        return false;
    }
}

function movieIsReady(theMovie) {
    return (typeof (theMovie.PercentLoaded) != "undefined");
}

function sdWriteFlash(id, src, width, height, align, salign, wmode, mozila, versao, onmouseover, onmouseout, flashvars) {
    s = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=' + (versao) + ',0,0,0" width="' +
        (width) + '" height="' + (height) + '" id="' +
        id + '" ' + (align != '' ? 'align="' + align + '"' : '') +
        (onmouseover != '' ? ' onmouseover="' + onmouseover + '"' : '') +
        (onmouseout != '' ? ' onmouseout="' + onmouseout + '"' : '') +
        '><param name="quality" value="high" />' +
        '<param name="allowFullScreen" value="true" />' +
        (src != '' ? '<param name="movie" value="' + src + '" />' : '') +
        (salign != '' ? '<param name="salign" value="' + salign + '" />' : '') +
        ((wmode && (wmode != '')) ? '<param name="wmode" value="' + wmode + '" />' : '') +
        '<param name="menu" value="false" /><param name="scale" value="noscale" />' +
        ((flashvars && (flashvars != '')) ? '<param name="flashvars" value="' + flashvars + '" />' : '');
    if (mozila)
        s += '<embed ' +
            (src != '' ? 'src="' + src + '" ' : '') +
            'loop="false" menu="false" quality="high" scale="noscale" width="' +
            (width) + '" height="' + (height) + '" name="' + id + '" ' +
            (align != '' ? 'align="' + align + '" ' : '') +
            (salign != '' ? 'salign="' + salign + '" ' : '') +
            (onmouseover != '' ? 'onmouseover="' + onmouseover + '" ' : '') +
            (onmouseout != '' ? 'onmouseout="' + onmouseout + '" ' : '') +
            ((wmode && (wmode != '')) ? 'wmode="' + wmode + '" ' : '') +
            ((flashvars && (flashvars != '')) ? 'flashvars="' + flashvars + '" ' : '') +
            'allowScriptAccess="sameDomain" allowFullScreen="true" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true" />';
    s += '</object>';
    document.write(s);
}

function formataTempo(segundos) {
    h = Math.floor(segundos / 60 / 60);
    m = Math.floor(segundos / 60) - (h * 60);
    s = Math.floor(segundos % 60);
    return numero(h) + "h" + numero(m, 2, 0) + "m" + numero(s, 2, 0) + "s";
}

function numero(valor, digitosAntes, digitosDepois) {
    result = valor.toFixed(digitosDepois);
    numint = (valor + '').substr(0, (valor + '.').indexOf('.'));
    if (numint.length < digitosAntes)
        for (var z = 0; z < digitosAntes - numint.length; z++)
        result = '0' + result;

    return result;
}

function formatFixed(num, length) {
    num = num.toFixed(length);
    numint = num.substr(0, (num + '.').indexOf('.'));
    numdec = num.substr((num + '.').indexOf('.') + 1);
    for (var i = 0; i < Math.floor((numint.length - (1 + i)) / 3); i++)
        numint = numint.substring(0, numint.length - (4 * i + 3)) + '.' +
            numint.substring(numint.length - (4 * i + 3));
    return numint + (length > 0 ? ',' + numdec : '');
}

function mostraDialogo(html, width, height, showClose) {
    ov = el('overlay');
    amp = el('ampliacao');
    ampi = el('ampliInner');
    ct = el('ampliContent');

    ct.innerHTML = '';

    ct.style.height = (height) + 'px';
    ct.style.width = (width) + 'px';
    ampi.style.width = (width) + 'px';

    oh = document.body.scrollHeight > document.body.clientHeight ? document.body.scrollHeight : document.body.clientHeight;
    if (oh < document.documentElement.clientHeight)
        oh = window.document.documentElement.clientHeight;
    ov.style.height = (oh) + 'px';
    ov.style.top = (document.body.scrollTop) + 'px';
    ov.style.left = (document.body.scrollLeft) + 'px';

    // th = document.body.clientHeight;
    th = window.document.documentElement.clientHeight;
    th = ((th - height - 12 - 18 - 3) / 2) - 20;
    if (th < 0)
        th = 0;
    th = th + document.body.scrollTop;
    ampi.style.marginTop = (th) + 'px';

    if ((typeof (showClose) == 'undefined') || showClose) {
        el('divBtnFechar').style.display = 'block';
    }
    else
        el('divBtnFechar').style.display = 'none';

    ov.style.display = 'block';
    amp.style.display = 'block';

    h = document.body.scrollTop;
    document.body.style.overflow = 'hidden';
    document.body.scrollTop = h;

    ct.innerHTML = html;
}

function mostraDialogoFrame(src, width, height) {
    mostraDialogo('<iframe width=' + (width) + ' height=' + (height) + ' frameborder=0 src="' + src + '" scrolling=auto></iframe>', width, height);
}

function fechaDialogo() {
    ov = el('overlay');
    amp = el('ampliacao');

    ov.style.display = 'none';
    amp.style.display = 'none';

    document.body.style.overflow = '';
}

function contaCaracter(campoVC, caracterN, limiteN) {
    if (campoVC.value.length > limiteN) {
        campoVC.value = campoVC.value.substring(0, limiteN);
    } else {
        el(caracterN).innerHTML = limiteN - campoVC.value.length;
    }
}

/* ----------------------------------------------- */

function mainCursosLoad() {
    carregaStatusAtendimento();
    if (el('banner') && el('nav-banner') && el('bannerCarregando'))
        iniciaOutdoor();

    setInterval('keepSession()', 60000);
}

function carregaStatusAtendimento() {
    img = el('hcIcon');
    if ((img) && img.complete) {
        c = img.width;
        if (c == 7) {
            el('box-atendimento').style.display = 'block';
        }
        else if (c == 15) {
            // offline
        }
        else if (c == 12) {
            // ocupado
        }
        else {
            // desconhecido... 
            // alert(c);
        }
    }
    else
        setTimeout('carregaStatusAtendimento()', 100)
}

function keepSession() {
    url = htmlbase + 'keep.aspx?rnd=' + (Math.random());
    req = getAjax();
    req.open('GET', url, true);
    req.send(null);    
}

/* ----------------------------------------------- */

var bannerIndex = -1;
var allBannersCreated = 0;
var bannerAutoChangeDelay = 7000;
var loadAllBannersIndex = 1;
var loadBannersCalled = 0;

function iniciaOutdoor() {
    if (banners.length == 0)
        return;

    if (!loadBannersCalled) {
        loadBanners();
        loadBannersCalled = 1;
    }
    else {
        vaiParaBanner(0);
    }
}

function loadBanners() {

    if (banners.length == 0)
        return;

    // el('button-left').style.display = banners.length > 1 ? 'block' : 'none';
    // el('button-right').style.display = banners.length > 1 ? 'block' : 'none';

    criaBanner(banners[0], 'banner0');

    setTimeout('loadAllBanners()', 500);

    trocaBanner(1, 1, -1);
}

function criaBanner(html, id) {
    el('bannerBanner').innerHTML += html;
}

function loadAllBanners() {

    if (bannerIsLoaded('banner0') || nie) {
        if (banners.length > 1) {
            criaBanner(banners[loadAllBannersIndex], 'banner' + (loadAllBannersIndex));
        }
        if (loadAllBannersIndex < banners.length - 1) {
            loadAllBannersIndex++;
            setTimeout('loadAllBanners()', 100);
        }
        else {
            el('bannerCarregando').style.display = 'none';
            allBannersCreated = 1;
        }
    }
    else
        setTimeout('loadAllBanners()', 500);
}


function bannerIsLoaded(id) {
    if ((mv(id).tagName == 'EMBED') || (mv(id).tagName == 'OBJECT'))
        return movieIsLoaded(mv(id));
    else if (mv(id).tagName == 'IMG')
        return el(id).complete;
}

var oldBannerIndex;
function trocaBanner(avanca, fast, novoIndice) {
    if ((!allBannersCreated) && (bannerIndex != -1))
        return;

    oldBannerIndex = bannerIndex;

    if (avanca > -1) {
        if (avanca)
            bannerIndex++;
        else
            bannerIndex--;

        if (bannerIndex == -1)
            bannerIndex = banners.length - 1;
        else if (bannerIndex >= banners.length)
            bannerIndex = 0;
    }
    else if (novoIndice > -1) {
        bannerIndex = novoIndice;
    }

    if (bannerIndex > -1) {
        if (!bannerIsLoaded('banner' + (bannerIndex)) && (oldBannerIndex > -1))
            el('divbanner' + (oldBannerIndex)).style.display = 'none';
    }

    mostraBanner(fast);
}

function avancaBanner(avanca) {
    trocaBanner(avanca, 0, -1);
}

function vaiParaBanner(indice) {
    trocaBanner(-1, 0, indice);
    el('bannerBanner').focus();
}

var timeoutid;
var timeoutid2;
function mostraBanner(fast) {
    if (bannerIsLoaded('banner' + (bannerIndex))) {
        // el('bannerBanner').style.backgroundColor = 'white';
        // clearTimeout(timeoutid2);
        if ((!nie) && (!fast)) {
            oDiv = el('bannerBanner');
            oDiv.style.filter = "blendTrans(duration=1.0)";
            oDiv.filters.blendTrans.apply();
            oDiv.filters.blendTrans.play();
        }
        if (oldBannerIndex > -1)
            el('divbanner' + (oldBannerIndex)).style.display = 'none';
        el('divbanner' + (bannerIndex)).style.display = 'block';
        // timeoutid2 = setTimeout('el(\'bannerBanner\').style.backgroundColor = \'\'', 3000);
        acertaBullets(oldBannerIndex, bannerIndex);
        if (timeoutid)
            clearTimeout(timeoutid);
        timeoutid = setTimeout('trocaBanner(1, 0, -1)', bannerAutoChangeDelay);
    }
    else {
        if (nie) {
            if (oldBannerIndex > -1)
                el('divbanner' + (oldBannerIndex)).style.display = 'none';
            el('divbanner' + (bannerIndex)).style.display = 'block';
            acertaBullets(oldBannerIndex, bannerIndex);
            if (timeoutid)
                clearTimeout(timeoutid);
            timeoutid = setTimeout('trocaBanner(1, 0, -1)', bannerAutoChangeDelay);
        }
        else
            setTimeout('mostraBanner(' + (fast) + ')', 100);
    }
}

function acertaBullets(oldindex, newindex) {
    if (el('but-banner-' + (oldindex + 1)))
        el('but-banner-' + (oldindex + 1)).className = 'but-banner-normal';
    if (el('but-banner-' + (newindex + 1)))
        el('but-banner-' + (newindex + 1)).className = 'but-banner-active';
}

/* ----------------------------------------------- */
