﻿nie = (navigator.appName.indexOf("Microsoft") == -1);

function getel(doc, id) {
    return doc.getElementById(id);
}

function el(id) {
    return getel(document, id);
}

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 escreve(s) {
    document.write(s);
}

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 lstVai(lst, frame) {
    lnk = lst.options[lst.selectedIndex].value;
    if (lnk.length < 1)
        return;
    if (typeof(frame) == "undefined")
        window.location.href = lnk;
    else
        window.frames[frame].location.href = lnk;
    lst.selectedIndex = 0;
}

/* 12/10/2009 */
function escreveFlash(id, src, width, height, align, salign, transparent, 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" />' +
        (src != '' ? '<param name="movie" value="' + src + '" />' : '') +
        (salign != '' ? '<param name="salign" value="' + salign + '" />' : '') +
        (transparent ? '<param name="wmode" value="transparent" />' : '') +
        (((typeof(flashVars) != "undefined") && (flashVars != '')) ? '<param name="flashVars" value="' + flashVars + '" />' : '') +
        '<param name="menu" value="false" /><param name="scale" value="noscale" />';
    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 + '" ' : '') +
            (transparent ? 'wmode="transparent" ' : '') +
            (((typeof(flashVars) != "undefined") && (flashVars != '')) ? 'flashVars="' + flashVars + '" ' : '') +
            'allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" swliveconnect="true" />';
    s += '</object>';
    escreve(s);
}

function loadFlashWithPreload(id, src, preloadId, paramsStr) {
    preloaded = movieIsLoaded(mv(preloadId));
    loaded = movieIsLoaded(mv(id));

    if ((!loaded) && (preloaded)) {
        cmd = 'loadMovie(\'' + id + '\', \'' + src + '\', [' + paramsStr + '])';
        setTimeout(cmd, 1);
    }

    paramsStr = paramsStr.replace(/\'/g, '\\\'');
    cmd = 'loadFlashWithPreload(\'' + id + '\', \'' + src + '\', \'' + preloadId + '\', \'' + paramsStr + '\')';

    if ((loaded) && (nie))
        setTimeout(cmd, 1000);
    else if (!loaded)
        setTimeout(cmd, 100);

}

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);
    for (p = 0; p < params.length; p += 2)
        mv(id).SetVariable(params[p], params[p + 1]);
}

function loadMovieWithDelay(id, movie, paramsStr) {
    cmd = 'loadMovie(\'' + id + '\', \'' + movie + '\', ' + paramsStr + ')';
    setTimeout(cmd, 500);
}

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 newImage(arg) {
    if (document.images) {
        rslt = new Image();
        rslt.src = arg;
        return rslt;
    }
}

function imprime() {
    window.focus();
    setTimeout("window.print()", 500);
}

var ie5 = document.all && !window.opera
var lastHeight = 0;
var autoHeight = 1;
var autoHSet = 0;
function adjustIFrameSize() {
    if (!el('iframe-wrapper'))
        return;
    iframeElement = getel(window.parent.document, 'ifcontent');
    iframeWindow = window.parent.frames[name];
    if (nie)
        iframeElement.style.height = '';
    h = 0;
    hMin = 0;
    h = el('iframe-wrapper').offsetHeight;
    hMin = 300;
    h = h > hMin ? h : hMin;
    lastHeight = h;

    iframeElement.style.height = (h) + 'px';

    if ((autoHeight) && (!autoHSet)) {
        autoHSet = 1;
        setTimeout('autoH()', 500);
    }
}

function autoH() {
    h = el('iframe-wrapper').offsetHeight;
    hMin = 300;
    h = h > hMin ? h : hMin;
    if (h != lastHeight) {
        scrollPos = window.parent.document.documentElement.scrollTop;
        adjustIFrameSize();
        window.parent.document.documentElement.scrollTop = scrollPos;
    }
    setTimeout('autoH()', 500);
}

var elementosFonte = '';
function fonte(aumenta) {
    // default
    dif = Number(readCookie('fontSize'));

    els = elementosFonte.split(',');

    for (i = 0; i < els.length; i++) {

        if (els[i].substr(0, 1) == '_') {
            dfs = getElementsById(els[i].substr(1, els[i].length - 1));
        }
        else
            dfs = [el(els[i])];

        for (xx = 0; xx < dfs.length; xx++) {
            df = dfs[xx];
            if (!df)
                continue;
            def = Number(df.getAttribute('default-font', 0));
            if (aumenta > -1) {
                size = Number(df.style.fontSize.substr(0, df.style.fontSize.length - 2));
                dif = size - def;
            }
            else {
                dif = Number(readCookie('fontSize'));
                size = def + dif;
            }
            //alert(els[i] + ' default: ' + (def) + ' atual: ' + (size));

            if (aumenta > -1) {
                if ((dif <= 0) && !aumenta) {
                    alert(fonteMinTxt);
                    return;
                }
                if ((dif >= 4) && aumenta) {
                    alert(fonteMaxTxt);
                    return;
                }
                if (aumenta)
                    size++;
                else
                    size--;
            }

            dif = size - def;

            df.style.fontSize = (size) + "px";
        }
    }

    var expire = new Date();
    expire.setTime(new Date().getTime() + 3600000 * 24 * 5000);
    document.cookie = 'fontSize' + "=" + escape((dif) + '') + ";expires=" + expire.toGMTString();

    adjustIFrameSize();
}

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 indica() {
    ifc = window.parent.frames['ifcontent'];
    iurl = 'indique.aspx?1=1';
    if (ifc) {
        if (ifc.idcontent)
            iurl += '&idContent=' + (ifc.idcontent);
        if (ifc.idcontentsection)
            iurl += '&idContentSection=' + (ifc.idcontentsection);
        ifc.location = htmlbase + iurl;
    }
    else
        window.location = iurl;
}

function amplia2(id, pw, ph, text) {
    wp = window.parent;
    wpd = wp.document;
    ov = getel(wpd, 'overlay');
    amp = getel(wpd, 'ampliacao');
    ampi = getel(wpd, 'ampliInner');
    img = getel(wpd, 'imgAmpliar2');
    txt = getel(wpd, 'ampliText');

    ampi.style.width = (pw + 12) + 'px';
    ov.style.height = (wpd.documentElement.scrollHeight) + 'px';
    img.src = wp.htmlbase + 'images/blank.gif';
    txt.style.display = (text != '' ? 'block' : 'none');
    txt.innerHTML = text;

    th = wpd.documentElement.clientHeight;
    th = ((th - ph - 12 - 18 - 3) / 2) - 20;
    if (th < 0)
        th = 0;
    th = th + window.parent.document.documentElement.scrollTop;
    ampi.style.marginTop = (th) + 'px';

    ov.style.display = 'block';
    amp.style.display = 'block';

    img.src = wp.htmlbase + 'ModuleHandlers/Content/picture.aspx?idContentPicture=' +
        (id) + '&w=' + (pw) + '&h=' + (ph);
}

function fechaAmpliar2() {
    wp = window.parent;
    wpd = wp.document;
    img = getel(wpd, 'imgAmpliar2');
    txt = getel(wpd, 'ampliText');
    ov = getel(wpd, 'overlay');
    amp = getel(wpd, 'ampliacao');

    ov.style.display = 'none';
    amp.style.display = 'none';
    img.src = '';
    txt.innerHTML = '';
}



/* ----------------------------------------------- */

var menuItens = new arr(0);
var menuComplete = 0;
var menuLoaded = 0;
var menuShowed = 0;

function addMenuItem(id, src, preloadId, paramsStr) {
    menuItens.length++;
    i = menuItens.length - 1;
    menuItens[i] = new arr(4);
    menuItens[i][0] = id;
    menuItens[i][1] = src;
    menuItens[i][2] = preloadId;
    menuItens[i][3] = paramsStr;
}

function menuDone() {
    menuComplete = 1;
}

function mostraMenu() {
    preloaded = movieIsLoaded(mv('menuPreload'));
    loaded = movieIsLoaded(mv('menuPreload'));

    if ((!menuComplete) || (!preloaded) || (!loaded))
        setTimeout('mostraMenu()', 100);
    else if (!menuLoaded) {
        menuLoaded = 1;
        for (i = 0; i < menuItens.length; i++) {
            cmd = 'loadMovie(\'' + menuItens[i][0] + '\', \'' + menuItens[i][1] + '\', [' + menuItens[i][3] + '])';
            setTimeout(cmd, i * 100);
            if (i == 0) {
                el('titu_destinos').style.visibility = 'visible';
            }
            if (i == indiceSaoPedro) {
                cmd = 'el(\'titu_saopedro\').style.visibility = \'visible\'';
                setTimeout(cmd, i * 100);
            }
        }
    }
}

function umclique(base, sel) {
    val = sel.options[sel.selectedIndex].value;
    if (val != '')
        window.location = base + val;
}

// *********************************************************************************************************************

function slideShow(varId, elementId, outerElementId, linkId, picturesCount, auto, slide_time, slide_change_speed, startDelay, legendElementId) {

    this.varid = varId;
    this.elementId = elementId;
    this.outerElementId = outerElementId;
    this.linkId = linkId;
    this.automatic = auto;
    this.slideTime = slide_time;
    this.speed = slide_change_speed;
    this.startDelay = startDelay;
    this.legendElementId = legendElementId;

    this.outItens = new matriz(0);

    this.changingImage = 0;
    this.outItemIndex = -1;
    this.newIndex = -1;
    this.changeStep = 0;
    this.changeDirection = 1;
    this.isFirstChange = 1;
    this.timeId = 0;

    this.addOutItem = function(foto, texto, link, title, legend) {
        this.outItens.length++;
        this.outItens[this.outItens.length - 1] = new matriz(5);
        this.outItens[this.outItens.length - 1].src = foto;
        this.outItens[this.outItens.length - 1].texto = texto;
        this.outItens[this.outItens.length - 1].link = link;
        this.outItens[this.outItens.length - 1].title = title;
        this.outItens[this.outItens.length - 1].legend = legend;
    }

    this.init = function() {
        setTimeout(varId + '.loadPictures()', 100);
        setTimeout(varId + '.changePicture(1, false)', 100);
    }

    this.loadPictures = function() {
        for (i = 0; i < this.outItens.length; i++) {
            if (!(this.outItens[i].ok)) {
                // verifica se a imagem anterior já foi carregada
                if ((i > 0) && !(this.outItens[i - 1].img.complete)) {
                    setTimeout(varId + '.loadPictures()', 100);
                    break;
                }
                else {
                    this.outItens[i].img = newImage(this.outItens[i].src);
                    this.outItens[i].ok = 1;
                }
            }
        }
    }

    this.changePicture = function(forward, byUser, destindex) {

        if ((this.changingImage) && (byUser))
            return;

        // if (byUser)
        //    this.automatic = 0;

        if (byUser)
            clearTimeout(this.timeId);

        img = el(elementId);

        if (this.newIndex == this.outItemIndex) {
            this.newIndex = this.outItemIndex;
            if (forward) {
                this.newIndex++;
                if (this.newIndex >= this.outItens.length)
                    this.newIndex = 0;
            }
            else if (typeof (destindex) != "undefined") {
                this.newIndex = destindex;
            }
            else {
                this.newIndex--;
                if (this.newIndex < 0)
                    this.newIndex = this.outItens.length - 1;
            }
        }

        if ((this.outItens[this.newIndex].ok) && (this.outItens[this.newIndex].img.complete)) {
            this.changingImage = 0;

            slow = 0;
            if ((!byUser) && (this.outItemIndex > -1)) {
                slow = 1;
                this.slowChange(1);
            }
            else
                this.show(this.newIndex);

            this.outItemIndex = this.newIndex;

            if (this.automatic)
                this.scheduleNext(slow);
        }
        else {
            img.src = "images/blank.gif";
            this.changingImage = 1;
            setTimeout(varId + '.changePicture(' + (forward) + ', 0)', 100);
        }
    }

    this.scheduleNext = function(slow) {
        clearTimeout(this.timeId);
        x = this.slideTime + (slow ? (this.speed * 1000) : 0) + (this.isFirstChange ? this.startDelay : 0);
        this.timeId = setTimeout(varId + '.changeSlide()', x);
        this.isFirstChange = 0;
    }

    this.slowChange = function(start) {
        try {
            oDiv = el(outerElementId);
            oDiv.style.filter = "blendTrans(duration=" + (this.speed) + ")";
            oDiv.filters.blendTrans.apply();
            oDiv.filters.blendTrans.play();
            this.show(this.newIndex);
        }
        catch (e) {
            this.show(this.newIndex);
        }
    }

    this.changeSlide = function() {
        if (this.automatic)
            this.changePicture(1, 0);
    }

    this.show = function(index) {
        el(elementId).src = this.outItens[index].src;
        mv('flashHome').SetVariable('titulo', this.outItens[index].title);
        mv('flashHome').SetVariable('subtitulo', this.outItens[index].legend);
        mv('flashHome').SetVariable('but_url', this.outItens[index].link);
        // mv('flashHome').SetVariable('isOverSub', 'yes');
        
        
        if (el(this.linkId)) {
            el(this.linkId).href = this.outItens[index].link;
            el(this.linkId).title = this.outItens[index].title;
        }
        // custom sj
        setTimeout(this.varid + '.showText()', 10)
    }

    this.showText = function() {
        if (!el(elementId).complete)
            setTimeout(this.varid + '.showText()', 50)
        else {

            if (el('foto')) {
                w = el(this.elementId).offsetWidth;
                el('foto').style.width = (w) + 'px';
            }

            index = this.newIndex;
            if (el(this.legendElementId)) {
                el(this.legendElementId).innerHTML = this.outItens[index].legend;
                el(this.legendElementId).style.display = this.outItens[index].legend != '' ? 'block' : 'none';

                // custom sj
                el(this.legendElementId).style.width = (w - 44) + 'px';
                if (el('bg-p')) {
                    el('bg-p').style.display = el(this.legendElementId).style.display;
                    el('bg-p').style.width = el(this.legendElementId).style.width
                }
            }
        }
    }

}

function matriz(n) {
    this.length = n
    for (var i = 0; i < n; i = i + 1) {
        this[i] = ''
    }
}

/* ----------------------------------------------- */

function contentBeforePrint() {
    getel(window.parent.document, 'ifcontent').style.height = (getel(window.parent.document, 'ifcontent').offsetHeight + 150) + 'px';
}

function contentAfterPrint() {
    getel(window.parent.document, 'ifcontent').style.height = (getel(window.parent.document, 'ifcontent').offsetHeight - 150) + 'px';
}
