/* (c) Igor Topilsky for www.vecher.com */

// Settings
var minOpacity = 50;
var maxOpacity = 100;
var fadeUpSpeed = 450;
var fadeDownSpeed = 5000;

var faders = new Array();

function fadeUp(photoId) {
	if (faders[photoId]) { stopFade(photoId, minOpacity); }
	fade(photoId, getOpacity(photoId), maxOpacity, fadeUpSpeed);
}

function fadeDown(photoId) {
	if (faders[photoId]) { stopFade(photoId, maxOpacity); }
	fade(photoId, getOpacity(photoId), minOpacity, fadeDownSpeed);
}

function fade(photoId, opacityStart, opacityEnd, ms) {
	faders[photoId] = new Array();
	var speed = Math.round(ms / 100);
	var timer = 0;
	if (opacityStart > opacityEnd) {
		for (i = opacityStart; i >= opacityEnd; i--) {
			faders[photoId][i] = setTimeout("setOpacity(\"" + photoId + "\", " + i + ");", (timer * speed));
			timer++;
		}
	} else if (opacityStart < opacityEnd) {
		for (i = opacityStart; i <= opacityEnd; i++) {
			faders[photoId][i] = setTimeout("setOpacity(\"" + photoId + "\", " + i + ");", (timer * speed));
			timer++;
		}
	}
}

function stopFade(photoId, opacityEnd) {
	if (getOpacity(photoId) > opacityEnd) {
		for (i = getOpacity(photoId); i >= opacityEnd; i--) {
			clearTimeout(faders[photoId][i]);
		}
	} else if (getOpacity(photoId) < opacityEnd) {
		for (i = getOpacity(photoId); i <= opacityEnd; i++) {
			clearTimeout(faders[photoId][i]);
		}
	}
}

function getOpacity(photoId) {
	var photo = document.getElementById(photoId);
	if (!photo.style.opacity) {
		return minOpacity;
	} else {
		return photo.style.opacity * 100;
	}
}

function setOpacity(photoId, opacity) {
	var photo = document.getElementById(photoId);
	photo.style.opacity = (opacity / 100);
	photo.style.MozOpacity = (opacity / 100);
	photo.style.KhtmlOpacity = (opacity / 100);
	photo.style.filter = "alpha(opacity=" + opacity + ")";
}