function changeHoverText(id, text) {
	element = eval( document.getElementById(id) );
	if( element ) {
		element.innerHTML = text;
	}
}

function toggleDetails(src, id, more, less) {
	srcElement = eval( document.getElementById(src) );
	element = eval( document.getElementById(id) );
	if( element ) {
		shown = element.style.display == "block";
		element.style.display = shown ? "none" : "block";
		srcElement.innerHTML = shown ? more : less;
	}
}

var photo_timer;

function fadeIn( front, back, count, alpha, showTime, alphaStep, stepTime ) {
	alpha += alphaStep;
	
	if( alpha >= 100 ) {
		frontImage = document.getElementById("photo-" + front);
		frontImage.style.filter = "alpha( opacity = 100 )";
		frontImage.style.opacity = 1;

		backImage = document.getElementById("photo-"+back);
		backImage.style.filter = "alpha( opacity = 0 )";
		backImage.style.opacity = 0;
		backImage.style.zIndex = 0;

		next = front + 1;
		if( next >= count ) {
			next = 0;
		}
		photo_timer = setTimeout( "changePhoto("+next+", "+front+", "+count+", "+showTime+", "+alphaStep+", "+stepTime+")", showTime );
	}
	else {
		frontImage = document.getElementById("photo-" + front);
		frontImage.style.filter = "alpha( opacity = " + alpha + " )";
		frontImage.style.opacity = 1.0 * alpha / 100.0;

		photo_timer = setTimeout( "fadeIn("+front+", "+back+", "+count+", "+alpha+", "+showTime+", "+alphaStep+", "+stepTime+")", stepTime );
	}
}

function changePhoto( front, back, count, showTime, alphaStep, stepTime ) {
	frontImage = document.getElementById("photo-" + front);
	frontImage.style.filter = "alpha( opacity = 0 )";
	frontImage.style.opacity = 0;
	frontImage.style.zIndex = 9999;

	backImage = document.getElementById("photo-" + back);
	backImage.style.zIndex = 1000;

	fadeIn(front, back, count, 0, showTime, alphaStep, stepTime);
}

function moveHorJob( id, toX, stepCount, deltaX, stepTime ) {
	object = document.getElementById("movable-"+id);

	if( 0 == --stepCount ) {
		object.style.left = toX + "px";
	}
	else {
		x = object.offsetLeft;
		object.style.left = (x + deltaX) + "px";
		setTimeout( 'moveHorJob(' + id + ", "+toX+", "+stepCount+", "+deltaX+", "+stepTime+")", stepTime );
	}
}

function moveHor( id, toX, stepCount, animTime ) {
	object = document.getElementById("movable-"+id);

	x = object.offsetLeft;
	delta = (toX - x);
	deltaX = 1.0 * delta / stepCount;
	stepTime = 1.0 * animTime / stepCount;
	
	moveHorJob(id, toX, stepCount, deltaX, stepTime);
}


function getSelectValueIndex(theSel, value) {
	var foundIndex = 0;
	for( ; foundIndex < theSel.length; ++foundIndex ) {
		if( theSel.options[foundIndex].value == value )
			break;
	}
	return foundIndex < theSel.length ? foundIndex : -1;
}

function updateSelect(theSel, values, titles) {
	var i = 0;
	
	// add default
	var newOpt = new Option('---', 0);
	theSel.options[i++] = newOpt;
	
	// replace beginning with new options
	for( var value in values ) {
		newOpt = new Option(titles[value], value);
		theSel.options[i++] = newOpt;
	}
	
	// clear rest
	for( j = theSel.length-i; j--; ) {
	    theSel.options[i] = null;
	}
	
	// reset selection
	theSel.selectedIndex = 0;
}

function calc_reset() {
	// show default result
	resultElement.innerHTML = priceDefault;

	// show all width & heights
	updateSelect(widthElement, widths, widths);
	updateSelect(heightElement, heights, heights);
}

function calc_widthChanged() {
	var width = parseInt(widthElement.value);
	var height = parseInt(heightElement.value);
	
	// reset if width is not selected
	if( !width ) {
		calc_reset();
		return;
	}
	
	// update heights to available ones
	updateSelect(heightElement, prices[width], heights);
	
	if( !height )
		return;
	
	var index = getSelectValueIndex(heightElement, height);
	if( -1 == index ) {
		// show default result
		resultElement.innerHTML = priceDefault;

		// reset height and stop
		heightElement.selectedIndex = 0;
		return;
	}
	else {
		// restore height
		heightElement.selectedIndex = index;
	}

	// show result
	resultElement.innerHTML = (extraRate * prices[width][height] + extraCharge) + priceEpilog;
}

function calc_heightChanged() {
	var width = parseInt(widthElement.value);
	var height = parseInt(heightElement.value);
	
	// reset if height is not selected
	if( !height ) {
		calc_reset();
		return;
	}
	
	// update widths to available ones
	var availableWidths = new Array();
	for( var _width in prices ) {
		if( prices[_width][height] ) {
			availableWidths[_width] = 1;
		}
	}
	updateSelect(widthElement, availableWidths, widths);

	if( !width )
		return;
	
	var index = getSelectValueIndex(widthElement, width);
	if( -1 == index ) {
		// show default result
		resultElement.innerHTML = priceDefault;

		// reset width and stop
		widthElement.selectedIndex = 0;
		return;
	}
	else {
		// restore width
		widthElement.selectedIndex = index;
	}

	// show result
	resultElement.innerHTML = (extraRate * prices[width][height] + extraCharge) + priceEpilog;
}

