var timeToFade = 1000.0;
var totalWidth = 0;
var ribbonImages = 5;

function fade(elementId) {
	var element = document.getElementById(elementId);
	if (!element)
		return;

	if (element.FadeState == null) {
		if (element.style.opacity == null || element.style.opacity == '' || element.style.opacity == '1')
			element.FadeState = 2;
		else
			element.FadeState = -2;
	}

	if (element.FadeState == 1 || element.FadeState == -1) {
		element.FadeState = element.FadeState == 1 ? -1 : 1;
		element.FadeTimeLeft = timeToFade - element.FadeTimeLeft;
	}
	else {
		element.FadeState = (element.FadeState == 2) ? -1 : 1;
		element.FadeTimeLeft = timeToFade;
		setTimeout ("animateFade(" + new Date().getTime() + ",'" + elementId + "')", 33);
	}
}

function animateFade(lastTick, elementId) {
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;

	var element = document.getElementById(elementId);

	if (element.FadeTimeLeft <= elapsedTicks) {
		element.style.opacity = (element.FadeState == 1) ? '1' : '0';
		element.style.filter = 'alpha(opacity = ' + ((element.FadeState == 1) ? '100' : '0') + ')';
		element.FadeState = (element.FadeState == 1) ? 2 : -2;
		return;
	}

	element.FadeTimeLeft -= elapsedTicks;
	var newOpVal = element.FadeTimeLeft / timeToFade;
	if (element.FadeState == 1)
		newOpVal = 1 - newOpVal;

	element.style.opacity = newOpVal;
	element.style.filter = 'alpha(opacity = ' + (newOpVal*100) + ')';

	setTimeout ("animateFade(" + curTick + ",'" + elementId + "')", 33);
}

function scaleImage (myId) {
	var myImage = new Image();
	myImage.src = myId.src;
	var height = myImage.height;
	var width = myImage.width;

	var scale = height / 100;
	width = Math.round(width / scale);
	myId.setAttribute('width', width);
	height = Math.round(height / scale);
	if (height != 100)
		height = 100;
	myId.setAttribute('height', height);
	totalWidth+= width + 2;
}

function setScales () {
	for (i = 1; i <= 5; i++) {
		scaleImage (document.getElementById("ribbon_image_" + i));
		//totalWidth = totalWidth - 1;
		divID = document.getElementById('portfolio_ribbon');
		divID.style.width = totalWidth + 'px';
		fade ('portfolio_ribbon');
	}
}