﻿//var prefix = "clt00_ContentPlaceHolder1_BannerRotativo1_";
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, first=0, last=1, key=false;

function so_init() 
{
    if(!d.getElementById || !d.createElement)return;

    css = d.createElement("link");
    css.setAttribute("href","../estilo.css");
    css.setAttribute("rel","stylesheet");
    css.setAttribute("type","text/css");
    d.getElementsByTagName("head")[0].appendChild(css);

    imgs = d.getElementById("imageContainer").getElementsByTagName("img");
    for(i=1;i<imgs.length;i++) imgs[i].xOpacity = 0;
    imgs[0].style.display = "block";
    imgs[0].xOpacity = .99;

    //Setting last image index
    last = imgs.length - 1;
    
    zInterval = setTimeout(forward, imgs[0].getAttribute("time"));
}

function so_xfade(next, isForward) 
{
    if(key == false)
    {
        imgs[next].src = "";
        if(next == 0)
        {
            imgs[next].src = "../midia/formacao.gif";
        }
        else if(next == 1)
        {
            imgs[next].src = "../midia/livro.gif";
        }
        else if(next == 2)
        {
            imgs[next].src = "../midia/principal.gif";
        }
    }
    
    cOpacity = imgs[current].xOpacity;
    nIndex = next;

    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;
        zInterval = setTimeout(forward, imgs[current].getAttribute("time"));
        key = false;
    } 
    else 
    {
        key = true;
        if(isForward)
        {
            zInterval = setTimeout(forward,20);
        }
        else
        {
            zInterval = setTimeout(back,20);
        }
    }
}

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) + ")";
}

function forward()
{
    var next;
    if(current == last)
    {
        next = first;
    }
    else 
    {
        next = current + 1;
    }
    
    clearTimeout(zInterval);
    so_xfade(next, true);
}

function back()
{
    var next;
    if(current == first)
    {
        next = last;
    }
    else 
    {
        next = current - 1;
    }
    clearTimeout(zInterval);
    so_xfade(next, false);
}

//function nextPicture() {
//    // Hide current picture
//    object = imgs[current];
//    object.style.display = 'none';
//        
//    // Show next picture, if last, loop back to front
//    if (current == last) { current = 1; }
//    else { current++ }
//    object = imgs[current];
//    object.style.display = 'block';
//}

//function previousPicture() {
//    // Hide current picture
//    object = imgs[current];
//    object.style.display = 'none';
//        
//    if (current == first) { current = last; }
//    else { current--; }
//    object = imgs[current];
//    object.style.display = 'block';
//}

//function start()
//{
//    zInterval = setInterval("forward()", 3000);
//}

//function forward() 
//{
//	fadeOut(imgs[current], 1000);
//	if (current == last)
//	{
//		current = first;
//	}
//	else
//	{
//		current = current + 1;
//	}
//	fadeIn(imgs[current], 1000);
//}

//function back() 
//{
//	fadeOut(imgs[current], 1000);
//	if (current == first)
//		cur = last;
//	else
//		current = current - 1;
//	fadeIn(imgs[current], 1000);
//}


//function fadeOut(id, time)
//{
//    target = id;
//    alpha = 100;
//    timer = (time*1000)/50;
//    var i = setInterval(20, timer);
//}

//function fadeIn(id, time) {
//    target = id;
//    alpha = 0;
//    timer = (time*1000)/50;
//    var i = setInterval(20, timer);
//}
