var ss_img1 = '';
var ss_img2 = '';
var ss_imgList = new Array();
var ss_speed = 6000;			// milliseconds
var ss_changeSpeed = 4000;		// milliseconds


/* call these functions */

function startSlideShow() {
	// preload next image
	preloadImage(ss_curImg+1);
	// start timer
	setInterval("nextImage()", ss_speed); 
}

function setPic(pic) {
	ss_inPic = true;
	switchPic(pic, 500);
}
function unsetPic() {
	ss_inPic = false;
	window.setTimeout("unsetPic2()", 333);
}


/* internal variables and functions */

var ss_preloadedImages = new Array();
var ss_inPic = false;
var ss_curImg = 0;
var ss_smoothOpacity = new Array(100);
var ss_fadeTo = 2;

function nextImage() {
	ss_curImg++;
	if(ss_curImg>(ss_imgList.length-1)) ss_curImg = 0;
	switchPic(ss_imgList[ss_curImg], ss_changeSpeed);
	preloadImage(ss_curImg+1);
}

function preloadImage(i) {
	if(i>(ss_imgList.length-1)) i = 0;
	if(ss_preloadedImages[i]===undefined) {
		ss_preloadedImages[i] = new Image;
		ss_preloadedImages[i].src = ss_imgList[i];
	}
}

function switchPic(pic, speed) {
	
	var a = document.getElementById(ss_img1);
	var b = document.getElementById(ss_img2);
	
	if(ss_fadeTo==2) {
		//a.src = pic;
		a.style.backgroundImage = "url("+pic+")";
		doFade(ss_img2, 100, 0, speed);
		ss_fadeTo = 1;
	} else {
		//b.src = pic;
		b.style.backgroundImage = "url("+pic+")";
		doFade(ss_img2, 0, 100, speed);
		ss_fadeTo = 2;
	}

}

function doFade(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0;
    var step = 1;

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i = i - step) { 
            setTimeout("changeOpacity('" + id + "', " + i + ")",(timer * speed * step)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i = i + step) 
            { 
            setTimeout("changeOpacity('" + id + "', " + i + ")",(timer * speed * step)); 
            timer++; 
        } 
    } 
}

function calcSmoothFade() {
	for(i=0; i<=100; i++) {
		ss_smoothOpacity[i] = i;
	}
	//return;
	for(a=1; a<=3; a++) {
		for(i=0; i<=100; i++) {
			x = ss_smoothOpacity[i];
			ss_smoothOpacity[i] = (1-(Math.cos(x*Math.PI/100)+1)/2)*100;
		}
	}
	for(i=0; i<=ss_smoothOpacity.length-1; i++) {
		ss_smoothOpacity[i] = Math.round(ss_smoothOpacity[i]*1000)/1000;
	}
}
calcSmoothFade();

function changeOpacity(element_id, opacity) {
	opacity = ss_smoothOpacity[opacity];
	a = document.getElementById(element_id);
	a.style.opacity = (opacity / 100);
    a.style.MozOpacity = (opacity / 100); 
    a.style.KhtmlOpacity = (opacity / 100); 
    a.style.filter = "alpha(opacity=" + opacity + ")"; 
}

function unsetPic2() {
	if(!ss_inPic) {
		switchPic('img/default.jpg', 1000);
	}
}