/****************************************************************
/* KOMPOST PLAYER
/* Version 2
/* 
/* Written by Cary Buchmann. (cjbuchmann)
/*
/* 
/* Current Animations : 'FADE', 'UP', 'DOWN', 'LEFT', 'RIGHT', 'RAND'
/***************************************************************/

function KompostPlayer(slideshowID)
{

	this.playStatus = false;
	
	this.useOverlays = true;
	
	this.slidePauseDuration = 3000;
	
	this.enterAnimationDuration = 1200;
	this.exitAnimationDuration = 1200;
	this.betweenAnimationDelay = 0;
	
	this.overlayEnterAnimationDuration = 1200;
	this.overlayExitAnimationDuration = 1200;
	this.overlayBetweenAnimationDelay = 0;
	
	this.totalDuration;
	
	this.playAfterThumbnailClick = true;
	this.afterThumbnailClickDelay = 10000;
	
	this.enterAnimation = 'RANDOM';
	this.exitAnimation = 'RANDOM';
	
	this.overlayEnterAnimation = 'RANDOM';
	this.overlayExitAnimation = 'RANDOM';
	
	this.currentSlide = 0;
	
	this.slideshowID = slideshowID;
	
	this.kompostContainer = '.kompost_outer_frame'
	this.slideSet = '.kompost_inner_frame ul li';
	this.thumbSet = '.kompost_outer_thumb_frame ul li';
	this.overlaySet = '.kompost_inner_overlay_frame ul li';
	this.currentImageClass = '.kompost_current_image';
	
	this.timeout;

	var _windowFocus;
	var _animation;
	var _overlayAnimation;
	var _this = this;
	
	this.init = function()
	{
		_windowFocus = new JSWindowFocus();
		_windowFocus.init();
		
		_animation = new KompostAnimationManager(
			_this.slideshowID,
			_this.slideSet
		);
		_animation.init();
		
		if(_this.useOverlays)
		{
			_overlayAnimation = new KompostAnimationManager(
				_this.slideshowID,
				_this.overlaySet
			);
			_overlayAnimation.init();
		}
		
		_this.totalDuration = _this.slidePauseDuration;
		_this.totalDuration+= _this.betweenAnimationDelay;
		
		if(_this.enterAnimationDuration > _this.exitAnimationDuration)
			_this.totalDuration+= _this.enterAnimationDuration;
		else
			_this.totalDuration+= _this.exitAnimationDuration;
			
		_this.setListeners();
		
		_this.play();
		_this.makeLoop();
	}
	
	this.setListeners = function()
	{
		$(_this.thumbSet).click(function(){
			_this.pause();
			
			if($(_this.slideSet+':animated').length<1 && _this.currentSlide != $(this).index())
			{
				_animation.animate(
						_this.getCurrentSlide(), 
						$(this).index(), 
						_this.enterAnimation, 
						_this.exitAnimation,
						_this.enterAnimationDuration,
						_this.exitAnimationDuration,
						_this.betweenAnimationDelay
					);
					
				if(_this.useOverlays)
				{
					_overlayAnimation.animate(
						_this.getCurrentSlide(), 
						$(this).index(), 
						_this.overlayEnterAnimation, 
						_this.overlayExitAnimation,
						_this.overlayEnterAnimationDuration,
						_this.overlayExitAnimationDuration,
						_this.overlayBetweenAnimationDelay
					);
				}
					
				_this.currentSlide = $(this).index();
				
				clearTimeout(_this.timeout);
				
				if(_this.playAfterThumbnailClick)
				{
					_this.timeout = 
					setTimeout(function(){
						_this.play();
					}, _this.afterThumbnailClickDelay);
				}
			}
		});
	}
	
	this.play = function()
	{
		_this.playStatus = true;
	}
	
	this.pause = function()
	{
		_this.playStatus = false;
	}
	
	this.getPlayStatus = function()
	{
		return _this.playStatus;
	}
	
	this.updateCurrentSlide = function()
	{
		_this.currentSlide = (_this.currentSlide+1)%$(_this.slideSet).length;;
	}
	
	this.getCurrentSlide = function()
	{
		return _this.currentSlide;
	}
	
	this.getNextSlide = function()
	{
		return (_this.currentSlide+1)%$(_this.slideSet).length;
	}
	
	this.makeLoop = function()
	{
		setInterval(function()
		{
			if(_this.playStatus && _windowFocus.isFocused())
			{	
				_animation.animate(
					_this.getCurrentSlide(), 
					_this.getNextSlide(), 
					_this.enterAnimation, 
					_this.exitAnimation,
					_this.enterAnimationDuration,
					_this.exitAnimationDuration,
					_this.betweenAnimationDelay
				);
				
				if(_this.useOverlays)
				{
					_overlayAnimation.animate(
						_this.getCurrentSlide(), 
						_this.getNextSlide(), 
						_this.overlayEnterAnimation, 
						_this.overlayExitAnimation,
						_this.overlayEnterAnimationDuration,
						_this.overlayExitAnimationDuration,
						_this.overlayBetweenAnimationDelay
					);
				}
				
				_this.updateCurrentSlide();
			}
			
		}, _this.totalDuration);
	}
	
}
