// Array for pictures
var pics;
// Array for the job description and links
var comments;
// Specify the index of the old pic
var oldPic = 0;
// Specify the index of the current pic
var curPic = 0;
// Specify the current opacity of the new picture
var opacity = 1;
// Timer for separate the fading times
var timer;
// Mouse over
var over = 0;
//img in foreground
var img1;
//img in background
var img2;
//Left property of the foreground picture
var leftImg1 = 0;


function init() {
	over = 0;
	img1 = obj('img1');
	img2 = obj('img2');
	preload();
	timer = setTimeout("startFading(-1)",4000);
}

// Load the next Picture with its informations
function startFading(num) {
	cancel();
	if (over == 0) {
		oldPic = curPic;
		curPic = (num > -1?num:(curPic+1)%pics.length);
		// set the opacity of the new picture to 0
		opacity = 0.00;
		img1.style.opacity = 0;
		img1.style.filter = 'alpha(opacity=0)';	
		loadInfos();
		changeOpacity();
	}
}

// Load the specified job
function gotoJob(num) {
	i = 0;
	while (i < pics.length-1 && pics[i][0]!=num)
		i++;
	cancel();
	startFading(i);
}

// Stop the animation
function cancel(force) {
	if (force==1)
		over = 1;
	opacity = 0.00;
	img1.style.opacity = 1;
	img1.style.filter = 'alpha(opacity=100)';
	leftImg1 = 0;
	img1.style.left = '0px';
	img2.style.left = '0px';
	clearTimeout(timer);
}

// Change the opacity of the picture and load the next picture when the opacity is full
function changeOpacity() {
	// opacity full
	if (opacity > 0.99) {
		if (over == 0) {
			preload();
			timer = setTimeout("startFading(-1)",4000);
		}
	}// increase the opacity
	else {
		opacity = (over==1?1:opacity + 0.15);
		img1.style.opacity = opacity;
		img1.style.filter = 'alpha(opacity=' + (opacity * 100) + ')';
		// increase again the opacity in 100ms
		timer = setTimeout("changeOpacity()",100);
	}
}

function startMove(left) {
	if (leftImg1 != 0)
		move(left);
	else cancel();
	if (over == 0) {
		oldPic = curPic;
		curPic = (curPic+(left?-1:1))%pics.length;
		curPic = (curPic<0?pics.length + curPic:curPic);
		loadInfos();
		leftImg1 = (left?-750:750);
		img1.style.left = leftImg1+'px';
		move(left);
	}
}

function move(left) {
	if (leftImg1 == 0) {
		if (over == 0) {
			cancel();
			preload();
			timer = setTimeout("startFading(-1)",4000);
		}
	} else {
		leftImg1 += (left?75:-75);
		img1.style.left = leftImg1+'px';
		img2.style.left = (leftImg1 + (left?750:-750))+'px';
		timer = setTimeout("move("+left+")",25);
	}
}
function preload() {
	img2.src = 'img/portfolio/'+pics[(curPic+1)%pics.length][1];
}
function loadInfos() {
	// load the current picture in foreground
	if (oldPic>=0) {
		if (pics[oldPic][0] != pics[curPic][0])
			obj('client'+pics[oldPic][0]).className = 'item';
			obj('client'+pics[oldPic][0]).style.background='';
		img2.src = 'img/portfolio/'+pics[oldPic][1];
	}else oldPic = 0;
	// load the new picture in background
	img1.src = 'img/portfolio/'+pics[curPic][1];
	// load the job informations
	if (pics[oldPic][0] != pics[curPic][0]) {
		obj('client'+pics[curPic][0]).className = 'item onlink';
		obj('client'+pics[curPic][0]).style.background='url(img/overmenu.png)';
		obj('comments').innerHTML = comments[pics[curPic][0]][0];
		obj('link').href=comments[pics[curPic][0]][1];
	}
}

