var gallery_animation_timer;
var gallery_current_object_id = 1;
var gallery_is_paused = false;

if (typeof(GALLERY_ANIMATION_INTERVAL) == 'undefined') {
	GALLERY_ANIMATION_INTERVAL = 3; // seconds
}
if (typeof(GALLERY_ANIMATION_LENGTH) == 'undefined') {
	GALLERY_ANIMATION_LENGTH = 1; // seconds
}
if (typeof(GALLERY_ANIMATION) == 'undefined') {
	GALLERY_ANIMATION = true;
}

function gallery_pause() {
	gallery_is_paused = true;
}
function gallery_pause_if_ie() {
	if (navigator.appName == "Microsoft Internet Explorer") {
		gallery_is_paused = true;
	}
}
function gallery_resume() {
	gallery_is_paused = false;
}

function gallery_changeToImage(obj, container) {
	if (container == null) {
		container = document.getElementById('gallery-main');
		
		// The request is from a mouse click
		var secondary = document.getElementById('gallery-secondary');
		if (secondary.filters != null) {
			secondary.filters[0].Stop();
		}
		secondary.style.display = 'none';
	}
	
	var i;
	var newNode;

	for (i = container.childNodes.length - 1; i >= 0; i--) {
		container.removeChild(container.childNodes[i]);
	}

	for (i = 0; i < obj.childNodes.length; i++) {
		newNode = obj.childNodes[i].cloneNode(true);
		newNode.removeAttribute("width");
		newNode.removeAttribute("height");
		container.appendChild(newNode);
	}
	
	if (gallery_animation_timer) {
		clearTimeout(gallery_animation_timer);
	}
	if (animationTimer) {
		clearInterval(animationTimer);
	}
}

function gallery_transitionStart() {
	if (gallery_is_paused) {
		gallery_animation_timer = setTimeout(gallery_transitionStart, GALLERY_ANIMATION_INTERVAL * 1000);
		return;
	}
	
	var nextObjectId = gallery_current_object_id + 1;
	if (document.getElementById('gallery-thumb-'+nextObjectId) == null) {
		nextObjectId = 1;
	}
	gallery_current_object_id = nextObjectId;
	
	// Change to front-side image.
	document.getElementById('gallery-secondary').style.display = 'none';
	gallery_changeToImage(document.getElementById('gallery-thumb-'+nextObjectId), document.getElementById('gallery-secondary'));
	
	if (navigator.appName == "Microsoft Internet Explorer") {
		gallery_transition_ie(document.getElementById('gallery-main'), document.getElementById('gallery-secondary'));
	}
	else {
		gallery_transition_moz(document.getElementById('gallery-main'), document.getElementById('gallery-secondary'));
	}
}


/**
* Animation function for Internet Explorer.
*/
function gallery_transition_ie(sourceObj, targetObj) {
	targetObj.filters[0].Apply();
	targetObj.style.display = 'block';
	targetObj.filters[0].Play();
	setTimeout(gallery_transitionEnd, GALLERY_ANIMATION_LENGTH * 1000);
}


/**
* Animation function for Mozilla etc.
*/
var animationCallback = null;
var animationSource = null;
var animationTarget = null;
var animationStep = 0;
var animationTotalSteps = 0;
var animationTimer = 0;

function gallery_transition_moz(fromObj, toObj) {
	var animationTick = 30; // ms
	var animationTime = GALLERY_ANIMATION_LENGTH * 1000; // ms
	animationTotalSteps = Math.round(animationTime / animationTick);
	//alert('animation takes '+animationTotalSteps+' steps.');

	animationStep = 0;
	animationSource = fromObj;
	animationTarget = toObj;
	//alert('starting animation...');
	animationTimer = setInterval(mozAnimationDisplay, animationTick);
}

function mozAnimationDisplay() {
	if (animationStep == 0) {
		animationSource.style.zIndex = 1;
		animationSource.style.opacity = 1;
		animationSource.style.display = 'block';
		animationTarget.style.zIndex = 2;
		animationTarget.style.opacity = 0;
		animationTarget.style.display = 'block';
	}
	else if (animationStep >= animationTotalSteps) {
		animationTarget.style.opacity = 1;
		animationSource.style.opacity = 1;
		mozAnimationEnd();
	}
	else {
		// Tweening step
		var tween = (animationStep / animationTotalSteps);
		animationTarget.style.opacity = tween;
		//animationSource.style.opacity = 1 - tween;
		window.status = tween;
	}
	animationStep++;
}

function mozAnimationEnd() {
	//alert('animationEnd()');
	gallery_transitionEnd();
	clearInterval(animationTimer);
}


function gallery_transitionEnd() {
	if (document.getElementById('gallery-secondary').style.display != 'none') {
		gallery_changeToImage(document.getElementById('gallery-secondary'), document.getElementById('gallery-main'));
		document.getElementById('gallery-secondary').style.display = 'none';
		
		gallery_animation_timer = setTimeout(gallery_transitionStart, GALLERY_ANIMATION_INTERVAL * 1000);
	}
}

if (GALLERY_ANIMATION == true) {
	gallery_animation_timer = setTimeout(gallery_transitionStart, GALLERY_ANIMATION_INTERVAL * 1000);
}
