﻿var animContainer; //reference to container element for animation
var animations = new Array();
var animationIndex; //index of current animation

//general functions
function addAnimationEventListiner(obj, evt, func) {
	if (obj) {
		if (obj.addEventListener)
			obj.addEventListener(evt, func, false);
		else if (obj.attachEvent)
			obj.attachEvent('on' + evt, func);
	}
}

function removeAnimationEventListiner(obj, evt, func) {
	if (obj) {
		if (obj.removeEventListener)
			obj.removeEventListener(evt, func, false);
		else if (obj.detachEvent)
			obj.detachEvent('on' + evt, func);
	}
}

function removeChildNodes(obj) {
	if (obj && obj.childNodes) {
		while (obj.childNodes[0])
			obj.removeChild(obj.childNodes[0]);
	}
}

var setImgsToDefaultState;
var setAnimationOpacity;

function getNextAnimationIndex() {
	return (animationIndex + 1 < animations.length) ? animationIndex + 1 : 0;
}


//init and start animation functions
function startAnimation(randomise) {
	if (animations && animations.length > 0) {
		if (randomise)
			mixAnimationArray(animations, 3);

		animationIndex = 0;
		var useFilters = (animContainer.filters) ? true : false;
		if (useFilters)
			initAnimationFilterFunctions();
		else
			initAnimationOpacityFunctions();
		
		prepareAnimation(animationIndex);
	}
}

function initAnimationFilterFunctions() {
	setImgsToDefaultState = function(imgs) {
		for (var i = 0; i < imgs.length; ++i)
			imgs[i].style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=0)';
	};

	setAnimationOpacity = function(obj, opacity) {
	obj.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(opacity=' + opacity + ')';
	};
}

function initAnimationOpacityFunctions() {
	setImgsToDefaultState = function(imgs) {
		for (var i = 0; i < imgs.length; ++i)
			imgs[i].style.opacity = 0;
	};
	setAnimationOpacity = function(obj, opacity) {
	obj.style.opacity = opacity / 100;
	};
}

function mixAnimationArray(arr, iterationsCount) {
	var count = arr.length;
	if (1 == count) //nothing to mix
		return;
		
	var j, temp;
	while (iterationsCount > 0) {
		for (var i = 0; i < count; ++i) {
			j = Math.floor(Math.random() * count);
			temp = arr[i];
			arr[i] = arr[j];
			arr[j] = temp;
		}
		iterationsCount--;
	}
}


//prepare animation functions
function prepareAnimation(index) {
	var preparingAnimation = animations[index];
	if (preparingAnimation.ready) {
		if (index == animationIndex)
			showAnimation();
	}
	else {
		var curAnimation = animations[index];
		curAnimation.readyCount = 0;
		curAnimation.images = new Array();
		var count = curAnimation.columnsCount * curAnimation.rowsCount;
		for (var i = 0; i < count; ++i) {
			var img = new Image();
			curAnimation.images[i] = img;
			addAnimationEventListiner(img, 'load', function(){animationImageLoaded(index);})
			img.alt = '';
			img.src = curAnimation.path + i.toString() + curAnimation.extension;
		}
	}
}


function animationImageLoaded(index) {
	var curAnimation = animations[index];
	curAnimation.readyCount++;

	if (curAnimation.readyCount == curAnimation.columnsCount * curAnimation.rowsCount) {
		curAnimation.ready = true;
		if (animationIndex == index)
			showAnimation();
	}
}



//animation functions
function showAnimation() {
	//prepare next animation
	var nextIndex = getNextAnimationIndex();
	if (nextIndex != animationIndex)
		prepareAnimation(nextIndex);
	
	//clear current content
	removeChildNodes(animContainer);
	//create new content
	createAnimationContent(animContainer);
	//show Animation
	AnimateShowing(animationIndex);
}

function createAnimationContent(container) {
	var curAnimation = animations[animationIndex];
	if (curAnimation) {
		var tbl;
		if (curAnimation.container) {
			tbl = curAnimation.container;
			container.appendChild(tbl);
			setImgsToDefaultState(curAnimation.images);
			tbl.style.cssText = '';
		}
		else {
			tbl = document.createElement('table');
			tbl.cellPadding = 0;
			tbl.cellSpacing = 0;

			var imgIndex = 0;
			for (var i = 0; i < curAnimation.rowsCount; ++i) {
				var row = createAnimationRow(tbl);
				for (var j = 0; j < curAnimation.columnsCount; ++j) {
					var cell = createAnimationCell(row);
					var img = curAnimation.images[imgIndex];

					

					cell.appendChild(img);
					imgIndex++;
				}
			}
			curAnimation.container = tbl;
			setImgsToDefaultState(curAnimation.images);
			container.appendChild(tbl);
		}
	}
}

function createAnimationRow(tbl) {
	if (tbl.insertRow)
		return tbl.insertRow(-1);
	else {
		var row = document.createElement('tr');
		tbl.appendChild(row);
		return row;
	}
}

function createAnimationCell(row) {
	if (row.insertCell)
		return row.insertCell(-1);
	else {
		var cell = document.createElement('td');
		row.appendChild(cell);
		return cell;
	}
}

function AnimateShowing(index) {
	var curAnimation = animations[index];
	
	//create new Array with images
	var imgs = curAnimation.images.slice(0);

	//mix images
	mixAnimationArray(imgs, 3);

	var delay = curAnimation.delay;
	var curOpacity = 0;
	var step = curAnimation.step;
	var imgPos = imgs.length;
	var realOpacity = 0;
	var lastIndex = imgPos - 1;

	//show image
	setTimeout(function() {

		//hide currenly showedImage
		if ((imgPos <= lastIndex) && (realOpacity < 100))
			setAnimationOpacity(imgs[imgPos], 0);

		//calculate new imgPos and realOpacity
		if (imgPos < lastIndex) {
			imgPos++;
		}
		else {
			imgPos = 0;
			realOpacity += step;
			if (realOpacity > 100)
				realOpacity = 100;
		}
		//show Image
		setAnimationOpacity(imgs[imgPos], realOpacity);

		//check if need to exit
		if ((imgPos == lastIndex) && (realOpacity == 100))
			waitHidingAnimation();
		else
			setTimeout(arguments.callee, delay);
	}, delay);
}


function showCurrentJob() {
	var curAnimation = animations[animationIndex];
	if (curAnimation.url)
		location.href = curAnimation.url;
}

function waitHidingAnimation() {
	var curAnimation = animations[animationIndex];
	if (curAnimation.container) {
		addAnimationEventListiner(curAnimation.container, 'click', showCurrentJob);
		curAnimation.container.style.cursor = 'pointer';
	}
	
	var nextAnimation = animations[getNextAnimationIndex()];
	var showTimeSpan = animations[animationIndex].showTimeSpan;
	setTimeout(function() {
		if (nextAnimation.ready) {
			if (curAnimation.container) {
				removeAnimationEventListiner(curAnimation.container, 'click', showCurrentJob);
				curAnimation.container.style.cursor = 'default';
			}
			AnimateHiding();
		}
		else
			setTimeout(arguments.callee, showTimeSpan);
	}, showTimeSpan);
}

function AnimateHiding() {
	var curAnimation = animations[animationIndex];

	var delay = curAnimation.hideDelay;
	var step = curAnimation.hideStep;
	var realOpacity = 100 - step;
	var containerObj = curAnimation.container;

	setTimeout(function() {
		setAnimationOpacity(containerObj, realOpacity);
		//calculate next opacityValue
		if (realOpacity == 0)
			showNextAnimation();
		else {
			realOpacity -= step;
			if (realOpacity < 0)
				realOpacity = 0;
			setTimeout(arguments.callee, delay);
		}
	}, delay);
}

function showNextAnimation() {
	setTimeout(function() {
		animationIndex = getNextAnimationIndex();
		showAnimation();
	}, animations[animationIndex].hideTimeSpan);
}