/*******************************************************
*
*	Carousel js object by Jere
*	@version 1.00 rc1
*	@copyright Naviatech Solution Oy
*
*******************************************************/
/*******************************************************
*	Fix IE setTimeout / setInterval
*******************************************************/
/*@cc_on
(function(f){
window.setTimeout = f(window.setTimeout);
window.setInterval = f(window.setInterval);
})(function(f){
return function(c,t){
var a = Array.prototype.slice.call(arguments,2);
if(typeof c != "function")
c = new Function(c);
return f(function(){
c.apply(this, a)
}, t)
}
});
@*/

function carousel( element ) 
{
	/*******************************************************
	*	Class variables
	*******************************************************/
	this.containerDiv = element;
	this.zoomMultiply = 1.1;
	this.direction = 'up';
	this.scrollSpeed = 100;
	this.containerElement = 'a';
	this.usePauseZoom = false;
	this.stepCount = 1;
	this.stepEffect = 'scroll'; // fade, scroll
	this.stepEffectSpeed = 900;
	this.imageCarousel = false;
	
	this.scrollStatus = 'stop';
	this.myTimer = 0;
	this.fastCount =  0;
	this.fastMoving = false;
	this.mouseOn = false;


	/*******************************************************
	*	Init carousel
	*******************************************************/
	this.init = function()
	{
		this.caruselContainer = 'div#'+ element +' div.carouselCont';
		this.caruselElementsContainer = this.caruselContainer +' div.carouselContainer';
		this.caruselElementsContainerFirst = this.caruselElementsContainer +' '+ this.containerElement +':first';
		this.caruselElementsContainerLast = this.caruselElementsContainer +' '+ this.containerElement +':last';
		
		$(this.caruselContainer).bind('mouseenter', {thisObj: this}, this.mouseOver);
		$(this.caruselContainer).bind('mouseleave', {thisObj: this}, this.mouseOut);
		if ( this.direction == 'up' || this.direction == 'down' )
		{
			$(this.caruselContainer +' a.carouselArrow1').bind('click', {thisObj: this, direction: 'up'}, this.step);
			$(this.caruselContainer +' a.carouselArrow2').bind('click', {thisObj: this, direction: 'down'}, this.step);
		}
		else
		{
			$(this.caruselContainer +' a.carouselArrow1').bind('click', {thisObj: this, direction: 'left'}, this.step);
			$(this.caruselContainer +' a.carouselArrow2').bind('click', {thisObj: this, direction: 'right'}, this.step);
		}
		if ( this.usePauseZoom == true )
		{
			$(this.caruselContainer +' a').bind('mouseenter', {thisObj: this}, this.zoomIn);
		}
		if ( this.imageCarousel == true )
		{
			$(this.caruselElementsContainer +' a').each(function(arr) 
			{
				$(this).addClass('link' + arr);
				if ( this.usePauseZoom == true )
				{
					var cloneImg = $(this).clone(true);
					$(cloneImg).addClass('cloneImg');
					$(cloneImg).unbind('mouseenter', zoomIn);
					$('div.cloneImgCont').append(cloneImg);
				}
			});
		}
		
		if ( this.stepEffect == 'step' )
		{
			this.myTimer = setTimeout( function(thisObj) { thisObj.step(thisObj); }, this.scrollSpeed, this);
			//this.step(this);
		}

		//$('a.cloneImg').bind('mouseleave', {thisObj: this}, this.clearImg);
		//moveImages();

	}
	/*******************************************************
	*	Move carousel to some direction
	*******************************************************/
	this.scroll = function() 
	{
		this.scrollStatus = 'scroll';
		switch ( this.direction )
		{
			/*******************************************************
			*	UP
			*******************************************************/
			case 'up':
				if($(this.caruselElementsContainer).position().top <= - ($(this.caruselElementsContainerFirst).outerHeight()) - 1) 
				{
					$(this.caruselElementsContainerFirst).appendTo(this.caruselElementsContainer);
					$(this.caruselElementsContainer).css({ top:0 });
				}
				$(this.caruselElementsContainer).css({ top: $(this.caruselElementsContainer).position().top - 2 + 'px' });
			break;
			/*******************************************************
			*	DOWN
			*******************************************************/
			case 'down':
				if($(this.caruselElementsContainer).position().top > 0)
				{
					$(this.caruselElementsContainerLast).prependTo(this.caruselElementsContainer);
					$(this.caruselElementsContainer).css({ top: - $(this.caruselElementsContainerLast).outerHeight() + 1 });
				}
				$(this.caruselElementsContainer).css({ top: $(this.caruselElementsContainer).position().top + 2 + 'px' });
			break;
			/*******************************************************
			*	LEFT
			*******************************************************/
			case 'left':
				//clearImg();
				if($(this.caruselElementsContainer).position().left <= - ($(this.caruselElementsContainerFirst).outerWidth()) - 1) 
				{
					$(this.caruselElementsContainerFirst).appendTo(this.caruselElementsContainer);
					$(this.caruselElementsContainer).css({ left:0 });
				}
				$(this.caruselElementsContainer).css({ left: $(this.caruselElementsContainer).position().left - 2 + 'px' });
			break;
			/*******************************************************
			*	RIGHT
			*******************************************************/
			case 'right':
				if($(this.caruselElementsContainer).position().left >0) 
				{
					$(this.caruselElementsContainerLast).prependTo(this.caruselElementsContainer);
					$(this.caruselElementsContainer).css({ left: - $(this.caruselElementsContainerLast).outerWidth() });
				}
				$(this.caruselElementsContainer).css({ left: $(this.caruselElementsContainer).position().left + 2 + 'px' });
			break;
		}
		this.myTimer = setTimeout( function(thisObj) { thisObj.scroll(); }, this.scrollSpeed, this);
	}
	/*******************************************************
	*	Stop carousel scroll on mouse over
	*******************************************************/
	this.mouseOver = function(event) 
	{
		event.data.thisObj.mouseOn = true;
		clearTimeout(event.data.thisObj.myTimer);
	}
	/*******************************************************
	*	start carousel scroll on mouse out
	*******************************************************/
	this.mouseOut = function(event) 
	{
		object = event.data.thisObj;
		object.mouseOn = false;
		if( object.fastMoving == false && object.scrollStatus == 'scroll' )
		{
			object.scroll();
		}
		else if ( object.fastMoving == false && object.stepEffect == 'step' )
		{
			object.myTimer = setTimeout( function(thisObj) { thisObj.step(thisObj); }, object.scrollSpeed, object);
		}
	}
	/*******************************************************
	*	Step carousel 
	*******************************************************/
	this.step = function(event, test)
	{
		if ( event.data )
		{
			event.currentTarget.blur();
			object = event.data.thisObj;
			direction = event.data.direction;
		}
		else
		{
			object = event;
			direction = object.direction;
		}
		if ( object.fastMoving == false )
		{
			switch ( direction )
			{
				/*******************************************************
				*	UP
				*******************************************************/
				case 'up':
					if ( direction == 'up' || direction == 'down' )
					{
						object.toggleArrow(object.caruselContainer +' a.carouselArrow1');
						object.moveUp(object);
					}
				break;
				/*******************************************************
				*	DOWN
				*******************************************************/
				case 'down':
					if ( direction == 'up' || direction == 'down' )
					{
						object.moveDown(object);
						object.toggleArrow(object.caruselContainer +' a.carouselArrow2');
					}
				break;
				/*******************************************************
				*	LEFT
				*******************************************************/
				case 'left':
					if ( direction == 'left' || direction == 'right' )
					{
						object.toggleArrow(object.caruselContainer +' a.carouselArrow1');
						object.moveLeft(object);
					}
				break;
				/*******************************************************
				*	RIGHT
				*******************************************************/
				case 'right':
					if ( direction == 'left' || direction == 'right' )
					{
						object.toggleArrow(object.caruselContainer +' a.carouselArrow2');
						object.moveRight(object);
					}
				break;
			}
			if ( object.mouseOn == false && object.stepEffect == 'step' )
			{
				object.myTimer = setTimeout( function(thisObj) { thisObj.step(thisObj); }, object.scrollSpeed, object);
			}
		}
	}
	/*******************************************************
	*	Move right
	*******************************************************/
	this.moveRight = function(object) 
	{
		object.fastMoving = true;
		if ( $(object.caruselElementsContainer).position().left <= 0 && Math.abs($(object.caruselElementsContainer).position().left) < $(object.caruselElementsContainerFirst).outerWidth() )
		{
			ii = 2;
			if ( $(object.caruselElementsContainer).position().left == 0 )
				ii = 1;
			tmp = Math.abs($(object.caruselElementsContainer).position().left);
			for ( i = ii; i <= object.stepCount; i++ )
			{
				tmp += $(object.caruselElementsContainerLast).outerWidth();
				$(object.caruselElementsContainerLast).prependTo($(object.caruselElementsContainer));
			}
			$(object.caruselElementsContainer).css({ left: 0 - (tmp)});
		}
		switch ( object.stepEffect )
		{
			case 'fade':
				$(object.caruselElementsContainer).fadeOut(object.stepEffectSpeed, function() { object.moveRightCallback(event, false); })
			break;
			default: // *** scroll
				$(object.caruselElementsContainer).animate({ left: 0 }, object.stepEffectSpeed, '', function() { object.moveRightCallback(object, false); });
		}
	}
	/*******************************************************
	*	Move right callback
	*******************************************************/
	this.moveRightCallback = function(object, compeleted) 
	{
		switch ( object.stepEffect )
		{
			case 'fade':
				if ( compeleted == false )
				{
					$(object.caruselElementsContainer).css({ left: 0});
					$(object.caruselElementsContainer).fadeIn(object.stepEffectSpeed, function() { object.moveRightCallback(object, true); });
				}
				else
				{
					object.fastMoving = false;
					object.toggleArrow(object.caruselContainer +' a.carouselArrow2');
				}
				break;
			default: // *** scroll
				if( $(object.caruselElementsContainer).position().left == 0 ) 
				{
					tmp = 0;
					for ( i = 1; i <= object.stepCount; i++ )
					{
						tmp += $(object.caruselElementsContainerLast).outerWidth();
						$(object.caruselElementsContainerLast).prependTo(object.caruselElementsContainer);
					}
					$(object.caruselElementsContainer).css({ left: (0 - tmp)});
				}
				object.fastMoving = false;
				object.toggleArrow(object.caruselContainer +' a.carouselArrow2');
		}
		if( object.mouseOn == false && object.scrollStatus == 'scroll' )
			object.scroll();
	}
	/*******************************************************
	*	Move left
	*******************************************************/
	this.moveLeft = function(object) 
	{
		//object = event.data.thisObj;
		object.fastMoving = true;
		if ( $(object.caruselElementsContainer).position().left < 0 )
		{
			for ( i = 1; i <= object.stepCount; i++ )
			{
				$(object.caruselElementsContainerFirst).appendTo(object.caruselElementsContainer);
			}
			$(object.caruselElementsContainer).css({ left: 0});
			if ( Math.abs($(object.caruselElementsContainer).position().left) > $(object.caruselElementsContainerFirst).outerWidth() )
			{
				ElementsContainerCount = (Math.floor(Math.abs($(object.caruselElementsContainer).position().left) / $(object.caruselElementsContainerFirst).outerWidth()));
				for ( i = 1; i <= ElementsContainerCount; i++ )
				{
					$(object.caruselElementsContainerFirst).appendTo(object.caruselElementsContainer);
				}
				$(object.caruselElementsContainer).css({ left: ($(object.caruselElementsContainer).position().left + ($(object.caruselElementsContainerFirst).outerWidth() * ElementsContainerCount))});
			}
			//if ( object.stepCount > 1 )
			//{
				containerPosition = 0 - $(object.caruselElementsContainerFirst +' + '+ object.containerElement).position().left * object.stepCount;
			//}
			//else
			//{
			//	containerPosition = 0 - $(object.caruselElementsContainerFirst +' + '+ object.containerElement).position().left * 2;
			//}
		}
		else
		{
			containerPosition = 0 - $(object.caruselElementsContainerFirst).outerWidth() * object.stepCount;
		}
		switch ( object.stepEffect )
		{
			case 'fade':
				$(object.caruselElementsContainer).fadeOut(object.stepEffectSpeed, function() { object.moveLeftCallback(object, containerPosition, false); })
				break;
			default: // *** scroll
				$(object.caruselElementsContainer).animate({ left: containerPosition }, object.stepEffectSpeed, '', function() { object.moveLeftCallback(object, null, false); });
		}
	}
	/*******************************************************
	*	Move left callback
	*******************************************************/
	this.moveLeftCallback = function(object, containerPosition, compeleted) 
	{
		switch ( object.stepEffect )
		{
			case 'fade':
				if ( compeleted == false )
				{
					$(object.caruselElementsContainer).css({ left: containerPosition});
					$(object.caruselElementsContainer).fadeIn(object.stepEffectSpeed, function() { object.moveLeftCallback(object, null, true); });
				}
				else
				{
					object.fastMoving = false;
					object.toggleArrow(object.caruselContainer +' a.carouselArrow1');
				}
				break;
			default: // *** scroll

				object.fastMoving = false;
				object.toggleArrow(object.caruselContainer +' a.carouselArrow1');
		}
		
		if( object.mouseOn == false && object.scrollStatus == 'scroll' )
			object.scroll();
	}
	/*******************************************************
	*	Move up
	*******************************************************/
	this.moveUp = function(object) 
	{
		object.fastMoving = true;
		if ( $(object.caruselElementsContainer).position().top < 0 )
		{
			containerTopPosition = 0 - $(object.caruselElementsContainerFirst +' + a').position().top * object.stepCount;
		}
		else
		{
			containerTopPosition = 0 - ($(object.caruselElementsContainerFirst).outerHeight()) * object.stepCount;
		}
		$(object.caruselElementsContainer).animate({ top: containerTopPosition }, object.stepEffectSpeed, '', function() { object.moveUpCallback(object); });
	}
	/*******************************************************
	*	Move up callback
	*******************************************************/
	this.moveUpCallback = function(object) 
	{
		if($(object.caruselElementsContainer).position().top < 0) 
		{
			for ( i = 1; i <= object.stepCount; i++ )
			{
				$(object.caruselElementsContainerFirst).appendTo(object.caruselElementsContainer);
			}
			$(object.caruselElementsContainer).css({ top: 0});
		}
		object.fastMoving = false;
		if( object.mouseOn == false && object.scrollStatus == 'scroll' )
			object.scroll();
	}
	/*******************************************************
	*	Move down
	*******************************************************/
	this.moveDown = function(object) 
	{
		object.fastMoving = true;
		tmp = Math.abs($(object.caruselElementsContainer).position().top);
		for ( i = 1; i <= object.stepCount; i++ )
		{
			tmp += $(object.caruselElementsContainerLast).outerHeight()
		}
		$(object.caruselElementsContainer).css({ top: 0 - tmp });
		for ( i = 1; i <= object.stepCount; i++ )
		{
			$(object.caruselElementsContainerLast).prependTo(object.caruselElementsContainer);
		}
		$(object.caruselElementsContainer).animate({ top:0 }, object.stepEffectSpeed, '', function() { object.moveDownCallback(object); });
	}
	/*******************************************************
	*	Move down callback
	*******************************************************/
	this.moveDownCallback = function(object) 
	{
		object.fastMoving = false;
		if(object.mouseOn == false && object.scrollStatus == 'scroll')
			object.scroll();
	}
	/*******************************************************
	*	Toggle arrow image
	*******************************************************/
	this.toggleArrow = function(element) 
	{
		//alert(element);
		//object = event.data.thisObj;
		//if ( object.fastMoving == false )
		//{
			$(element).toggleClass('arrowPressed');
			//$(this).toggleClass('arrowPressed');
		//}
	}
	/*******************************************************
	*	Clear zoom image
	*******************************************************/
	this.clearImg = function() 
	{
		$('div#cloneImgCont a.cloneImg').css({ 'display':'none' });
	}
}