var pic = Math.floor(Math.random()*4)+1;
var prev = '';
var total = 4;

function timedNext() {
	fadeNext();
	seconds=10;
	setTimeout("timedNext()",seconds*2000);
}

function fadePrev() {
	prev = 'text' + pic;
	pic -=1;
	if (pic < 1) { pic = 4; };
	display = 'text' + pic;
	fade(prev, display);
	
}

function fadeNext() {
	prev = 'text' + pic;
	pic += 1;
	if(pic > 4) { pic = 1; };
	display = 'text' + pic;
	fade(prev, display);
}

function fade(a,b) {
	opacity(a, 100, 0, 500);
	document.getElementById(a).style.display = 'none';
	document.getElementById(b).style.display = 'block';
	opacity(b, 0, 100, 500);
}


function opacity(id, opacStart, opacEnd, millisec) {
    //speed for each frame
    var speed = Math.round(millisec / 100);
    var timer = 0;

    //determine the direction for the blending, if start and end are the same nothing happens
    if(opacStart > opacEnd) {
        for(i = opacStart; i >= opacEnd; i--) {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    } else if(opacStart < opacEnd) {
        for(i = opacStart; i <= opacEnd; i++)
            {
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
            timer++;
        }
    }
} 

function changeOpac(opacity, id) {
	var object = document.getElementById(id);
	object.style.opacity = (opacity / 100);
	object.style.MozOpacity = (opacity / 100);
	object.style.KhtmlOpacity = (opacity / 100);  
}