var topics = new Array(), tInterval = 7000, fInterval = 25, current=0, pause=false, timeId=null, toShow=0;

function topic_init() {
    topics[0] = $('topic-1');
    topics[1] = $('topic-2');
    topics[2] = $('topic-3');

	for(i=1;i<topics.length;i++) {
        topics[i].xOpacity = 0;
        topics[i].style.display = "none";
    }
	topics[0].style.display = "block";
	topics[0].xOpacity = 1;

	timeId = window.setTimeout(topic_fade, tInterval);
}

function topic_fade() {
	cOpacity = topics[current].xOpacity;
	nIndex = topics[current+1]?current+1:0;

	nOpacity = topics[nIndex].xOpacity;

	cOpacity-=0.05;
	nOpacity+=0.05;

	topics[nIndex].style.display = "block";
	topics[current].xOpacity = cOpacity;
	topics[nIndex].xOpacity = nOpacity;

    topics[current].setOpacity(cOpacity);
    topics[nIndex].setOpacity(nOpacity);

	if(cOpacity<=0) {
        if (pause) return;
		topics[current].style.display = "none";
		current = nIndex;
		timeId = window.setTimeout(topic_fade, tInterval);
	} else {
		timeId = window.setTimeout(topic_fade, fInterval);
	}
}

function topic_out() {
    pause=false;
    timeId = setTimeout(topic_fade, tInterval);
}

function topic_over() {
    pause=true;
    window.clearTimeout(timeId);
    cOpacity = topics[current].xOpacity;
	nIndex = topics[current+1]?current+1:0;
    if (cOpacity >= 0.50) {    
        cOpacity = 1;
        nOpacity = 0;
        topics[nIndex].style.display = "none";
        topics[current].xOpacity     = cOpacity;
        topics[nIndex].xOpacity      = nOpacity;
        topics[current].setOpacity(cOpacity);
        topics[nIndex].setOpacity(nOpacity);
    } else {
        cOpacity = 0;
        nOpacity = 1;
        topics[current].style.display = "none";
        topics[current].xOpacity     = cOpacity;
        topics[nIndex].xOpacity      = nOpacity;
        topics[current].setOpacity(cOpacity);
        topics[nIndex].setOpacity(nOpacity); 
        current = nIndex;        
    }

}

//function topic_show (i) {
function show_topic (i) {
    pause=true;
    window.clearTimeout(timeId);
    toShow = i-1;
    timeId = window.setTimeout(show_fade,fInterval);
}

function show_fade () {
	cOpacity = topics[current].xOpacity;
	nOpacity = topics[toShow].xOpacity;

	cOpacity-=0.05;
	nOpacity+=0.05;

	topics[toShow].style.display = "block";
	topics[current].xOpacity = cOpacity;
	topics[toShow].xOpacity = nOpacity;

    topics[current].setOpacity(cOpacity);
    topics[toShow].setOpacity(nOpacity);

	if(cOpacity<=0) {
		topics[current].style.display = "none";
		current = toShow;
        toShow = 0;
        pause=true;
        window.clearTimeout(timeId);
	} else {
		timeId = window.setTimeout(show_fade,fInterval);
	}
}

