// Gallery Javascript 0.1
//   by Michael Rienstra
//   created 2008-02-20
//   last updated 2008-02-24

// scaleSteps : the number of 'frames' of animation
scaleSteps = 3;

// scaleMaxDelay : the longest delay time in milliseconds (uses a sine-wave ease-in, so it starts faster than this).
scaleMaxDelay = 75;

// Initial dimensions (normally 0,0)
scaleWidth = 0;
scaleHeight = 0;

// Default preview height, in pixels (115 x 83)
defaultWidth = 115;
defaultHeight = 83;

//
//
//
//
//
//
//
//
//
//
//
//
//
//
//

i = 0;
stopAnim = '';
currentWidth = backpicWidth = nextpicWidth = defaultWidth;
currentHeight = backpicHeight = nextpicHeight = defaultHeight;

function scaleImage() {
changeOpac(100,targetImage);
if (i==0) {
stopAnim = '';
if(targetImage=='backpic'){
currentWidth = backpicWidth;
currentHeight = backpicHeight;
} else if(targetImage=='nextpic'){
currentWidth = nextpicWidth;
currentHeight = nextpicHeight;
}
}
if (stopAnim != '') {
i = 0;
scaleWidth = 0;
scaleHeight = 0;
document[stopAnim].width = 0;
document[stopAnim].height = 0;
stopAnim = '';
} else {
if (i<scaleSteps) {
scaleWidth = scaleWidth + (currentWidth - scaleWidth)/(scaleSteps-i);
scaleHeight = scaleHeight + (currentHeight - scaleHeight)/(scaleSteps-i);
document[targetImage].width = scaleWidth;
document[targetImage].height = scaleHeight;
i++;
if (i<scaleSteps) {
delayTime = parseInt(scaleMaxDelay*Math.sin(Math.PI*i/(2*scaleSteps)));
setTimeout('scaleImage()',delayTime);
} else {
document[targetImage].width = currentWidth;
document[targetImage].height = currentHeight;
i = 0;
scaleWidth = 0;
scaleHeight = 0;
}
}
}
}

function scaleImageWidth() {
changeOpac(100,targetImage);
if (i==0) {
stopAnim = '';
if(targetImage=='backpic'){
currentWidth = backpicWidth;
currentHeight = backpicHeight;
} else if(targetImage=='nextpic'){
currentWidth = nextpicWidth;
currentHeight = nextpicHeight;
}
}
if (stopAnim != '') {
i = 0;
scaleWidth = 0;
document[stopAnim].width = 0;
document[stopAnim].height = 0;
stopAnim = '';
} else {
if (i==0) {
document[targetImage].height = currentHeight;
}
if (i<scaleSteps) {
scaleWidth = scaleWidth + (currentWidth - scaleWidth)/(scaleSteps-i);
document[targetImage].width = scaleWidth;
i++;
if (i<scaleSteps) {
delayTime = parseInt(scaleMaxDelay*Math.sin(Math.PI*i/(2*scaleSteps)));
setTimeout('scaleImageWidth()',delayTime);
} else {
document[targetImage].width = currentWidth;
i = 0;
scaleWidth = 0;
}
}
}
}

function scaleImageHeight() {
changeOpac(100,targetImage);
if (i==0) {
stopAnim = '';
if(targetImage=='backpic'){
currentWidth = backpicWidth;
currentHeight = backpicHeight;
} else if(targetImage=='nextpic'){
currentWidth = nextpicWidth;
currentHeight = nextpicHeight;
}
}
if (stopAnim != '') {
i = 0;
scaleHeight = 0;
document[stopAnim].width = 0;
document[stopAnim].height = 0;
stopAnim = '';
} else {
if (i==0) {
document[targetImage].width = currentWidth;
}
if (i<scaleSteps) {
scaleHeight = scaleHeight + (currentHeight - scaleHeight)/(scaleSteps-i);
document[targetImage].height = scaleHeight;
i++;
if (i<scaleSteps) {
delayTime = parseInt(scaleMaxDelay*Math.sin(Math.PI*i/(2*scaleSteps)));
setTimeout('scaleImageHeight()',delayTime);
} else {
document[targetImage].height = currentHeight;
i = 0;
scaleHeight = 0;
}
}
}
}

function fadeInImage() {
changeOpac(0,targetImage);
if(targetImage=='backpic'){
currentWidth = backpicWidth;
currentHeight = backpicHeight;
} else if(targetImage=='nextpic'){
currentWidth = nextpicWidth;
currentHeight = nextpicHeight;
}
document[targetImage].width = currentWidth;
document[targetImage].height = currentHeight;
opacity(targetImage, 0, 99, 500);
}

function showImage() {
changeOpac(100,targetImage);
if(targetImage=='backpic'){
currentWidth = backpicWidth;
currentHeight = backpicHeight;
} else if(targetImage=='nextpic'){
currentWidth = nextpicWidth;
currentHeight = nextpicHeight;
}
document[targetImage].width = currentWidth;
document[targetImage].height = currentHeight;
}

function fadeOutImage() {
ua = navigator.userAgent;
if (ua.indexOf('MSIE') != -1 && ua.indexOf('Mac') != -1) {
stopAnim = targetImage;
document[targetImage].width = -1;
document[targetImage].height = -1;
} else {
opacity(targetImage, 99, 0, 500);
}
}

function hideImage() {
stopAnim = targetImage;
document[targetImage].width = -1;
document[targetImage].height = -1;
}

function opacity(id, opacStart, opacEnd, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;

	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = 'alpha(opacity=' + opacity + ')';
}

function initGallery(){
targetImage='backpic';
hideImage();
targetImage='nextpic';
hideImage();
}

function checkById(){
if (getRefToDiv('backpic')&&getRefToDiv('nextpic')){
initGallery();
}else{
setTimeout('checkById();',100);
}
}

if (!document.addEventListener) {
setTimeout("checkById();",100);
}
