/****************************************************************
/* KOMPOST ANIMATION MANAGER
/* Version 2
/* 
/* Written by Cary Buchmann. (cjbuchmann)
/*
/* The Kompost animation engine for transition effects
/***************************************************************/

function KompostAnimationManager(slideshowID, slideSet)
{
	this.slideshowID = slideshowID;
	this.slideSet = slideSet;
	
	this.enterAnimation;
	this.exitAnimation;
	
	this.animationTypes = ['FADE', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'RANDOM'];
	
	this.enterAnimationDuration;
	this.exitAnimationDuration;
	this.betweenAnimationDelay;
	
	this.heightOffset = 0;
	
	this.currentImageClass = 'kompost_current_image';
	
	var _this = this;
	
	this.init = function()
	{
		_this.slideshowID = "#"+_this.slideshowID;
		$(_this.slideSet).css({ opacity : '0'});
		$($(_this.slideSet).get(0)).addClass(_this.currentImageClass).css({ opacity : '1'});
	}
	
	this.animate = function(currentIndex, nextIndex, enterAnimation, exitAnimation, 
		enterAnimationDuration, exitAnimationDuration, betweenAnimationDelay)
	{
		_this.enterAnimation = enterAnimation;
		_this.exitAnimation = exitAnimation;
		_this.enterAnimationDuration = enterAnimationDuration;
		_this.exitAnimationDuration = exitAnimationDuration;
		_this.betweenAnimationDelay = betweenAnimationDelay;
		
		_this.playExitAnimation(currentIndex);
		
		setTimeout(function(){
			_this.playEnterAnimation(nextIndex);
		}, _this.betweenAnimationDelay);
	}
	
	this.getAnimationTypes = function()
	{
		return _this.animationTypes;
	}
	
	this.playExitAnimation = function(index)
	{
		if(_this.exitAnimation == 'FADE')
		{
			_this.fadeOut(index);
		}
		else if(_this.exitAnimation == 'LEFT')
		{
			_this.exitLeft(index);
		}
		else if(_this.exitAnimation == 'RIGHT')
		{
			_this.exitRight(index);
		}
		else if(_this.exitAnimation == 'UP')
		{
			_this.exitUp(index);
		}
		else if(_this.exitAnimation == 'DOWN')
		{
			_this.exitDown(index);
		}
		else if(_this.exitAnimation == 'HALF_FADE_UP')
		{
			_this.exitHalfUpFade(index);
		}
		else if(_this.exitAnimation == 'RANDOM')
		{
			rand = Math.ceil(Math.random()*10)%_this.animationTypes.length;
			_this.exitAnimation = _this.animationTypes[rand];
			_this.playExitAnimation(index);
		}
	}
	
	this.playEnterAnimation = function(index)
	{
		if(_this.enterAnimation == 'FADE')
		{
			_this.fadeIn(index);
		}
		else if(_this.enterAnimation == 'LEFT')
		{
			_this.enterLeft(index);
		}
		else if(_this.enterAnimation == 'RIGHT')
		{
			_this.enterRight(index);
		}
		else if(_this.enterAnimation == 'UP')
		{
			_this.enterUp(index);
		}
		else if(_this.enterAnimation == 'DOWN')
		{
			_this.enterDown(index);
		}
		else if(_this.enterAnimation == 'FADE_DOWN')
		{
			_this.enterDownFade(index);
		}
		else if(_this.enterAnimation == 'RANDOM')
		{
			rand = Math.ceil(Math.random()*10)%_this.animationTypes.length;
			_this.enterAnimation = _this.animationTypes[rand];
			_this.playEnterAnimation(index);
		}
	}
	
	/********************************************************
	/* ENTER ANIMATIONS
	/*******************************************************/
	this.fadeIn = function(index)
	{
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.animate({ opacity : '1' }, _this.enterAnimationDuration);
	}
	
	this.enterLeft = function(index)
	{
		var width = $(_this.slideshowID).outerWidth();
		
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.css({ opacity : 1, marginLeft : width })
			.animate({ marginLeft : '-='+(width) }, _this.enterAnimationDuration);
	}
	
	this.enterRight = function(index)
	{
		var width = $(_this.slideshowID).outerWidth();
		width = width*-1;
		
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.css({ opacity : 1, marginLeft : width })
			.animate({ marginLeft : '-='+(width) }, _this.enterAnimationDuration);
	}
	
	this.enterUp = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.css({ opacity : 1, marginTop : height })
			.animate({ marginTop : '-='+(height) }, _this.enterAnimationDuration);
	}
	
	this.enterDown = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		height = height*-1;
		
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.css({ opacity : 1, marginTop : height })
			.animate({ marginTop : '-='+(height) }, _this.enterAnimationDuration);		
	}
	
	this.enterDownFade = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		height = height*-1;
		
		$($(_this.slideSet).get(index))
			.addClass(_this.currentImageClass)
			.css({ marginTop : height })
			.animate({ marginTop : '-='+(height), opacity : '1' }, _this.enterAnimationDuration);		
	}
	
	/********************************************************
	/* EXIT ANIMATIONS
	/*******************************************************/
	this.fadeOut = function(index)
	{
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ opacity : '0' }, _this.exitAnimationDuration);
	}
	
	this.exitLeft = function(index)
	{
		var width = $(_this.slideshowID).outerWidth();
		
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ marginLeft : '-='+(width) }, _this.exitAnimationDuration, function(){
				$(this).css({ opacity : 0, marginLeft : 0 });
			});
	}
	
	this.exitRight = function(index)
	{
		var width = $(_this.slideshowID).outerWidth();
		
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ marginLeft : '+='+(width) }, _this.exitAnimationDuration, function(){
				$(this).css({ opacity : 0, marginLeft : 0 });
			});
	}
	
	this.exitUp = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ marginTop : '-='+(height) }, _this.exitAnimationDuration, function(){
				$(this).css({ opacity : 0, marginTop : 0 });
			});
	}
	
	this.exitDown = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ marginTop : '+='+(height) }, _this.exitAnimationDuration, function(){
				$(this).css({ opacity : 0, marginTop : 0 });
			});		
	}
	
	this.exitHalfUpFade = function(index)
	{
		var height = $(_this.slideshowID).outerHeight()+_this.heightOffset;
		
		$($(_this.slideSet).get(index))
			.removeClass(_this.currentImageClass)
			.animate({ marginTop : '-='+(height)/2, opacity : '0' }, _this.exitAnimationDuration, function(){
				$(this).css({ marginTop : 0 });
			});
	}
}
