/*******************************************************
*
*	Carousel js object by Jere
*	@version 0.85
*	@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.animationTime = 900;
	
	this.scrollStatus = 'stop';
	this.myTimer =  0;
	this.fastCount =  0;
	this.fastMoving = false;
	this.mouseOn = false;

	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';
	/*******************************************************
	*	Init carousel
	*******************************************************/
	this.init = function()
	{
		$(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.carouselTopArrow').bind('click', {thisObj: this, direction: 'up'}, this.step);
			$(this.caruselContainer +' a.carouselDownArrow').bind('click', {thisObj: this, direction: 'down'}, this.step);
		}
		else
		{
			$(this.caruselContainer +' a.carouselTopArrow').bind('click', {thisObj: this, direction: 'left'}, this.step);
			$(this.caruselContainer +' a.carouselDownArrow').bind('click', {thisObj: this, direction: 'right'}, this.step);
		}
		//$('div#'+ this.containerDiv +' a.carouselArrow').bind('mouseenter', {thisObj: this}, this.clearImg);
		$(this.caruselContainer +' a.carouselArrow').bind('mousedown mouseup', {thisObj: this}, this.toggleArrow);
		if ( this.usePauseZoom == true )
		{
			$(this.caruselContainer +' a').bind('mouseenter', {thisObj: this}, this.zoomIn);
		}
		$(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);
			}
		});

		//$('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();
	}
	/*******************************************************
	*	Step carousel 
	*******************************************************/
	this.step = function(event)
	{
		if ( event.currentTarget )
			event.currentTarget.blur();
		object = event.data.thisObj;
		if ( object.fastMoving == false )
		{
			switch ( (direction = event.data.direction) )
			{
				/*******************************************************
				*	UP
				*******************************************************/
				case 'up':
					if ( direction == 'up' || direction == 'down' )
						object.moveUp(event);
				break;
				/*******************************************************
				*	DOWN
				*******************************************************/
				case 'down':
					if ( direction == 'up' || direction == 'down' )
						object.moveDown(event);
				break;
				/*******************************************************
				*	LEFT
				*******************************************************/
				case 'left':
					if ( direction == 'left' || direction == 'right' )
						object.moveLeft(event);
				break;
				/*******************************************************
				*	RIGHT
				*******************************************************/
				case 'right':
					if ( direction == 'left' || direction == 'right' )
						object.moveRight(event);
				break;
			}
		}
	}
	/*******************************************************
	*	Move right
	*******************************************************/
	this.moveRight = function(event) 
	{
		object = event.data.thisObj;
		object.fastMoving = true;
		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.caruselElementsContainer).animate({ left: 0 }, this.animationTime, '', function() { object.moveRightCallback(event); });
	}
	/*******************************************************
	*	Move right callback
	*******************************************************/
	this.moveRightCallback = function(event) 
	{
		object = event.data.thisObj;
		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;
		if( object.mouseOn == false && object.scrollStatus == 'scroll' )
			object.scroll();
	}
	/*******************************************************
	*	Move left
	*******************************************************/
	this.moveLeft = function(event) 
	{
		object = event.data.thisObj;
		object.fastMoving = true;
		if ( $(object.caruselElementsContainer).position().left < 0 )
		{
			if ( Math.abs($(object.caruselElementsContainer).position().left) == ($(object.caruselElementsContainerFirst).outerWidth() * object.stepCount) )
			{
				for ( i = 1; i <= object.stepCount; i++ )
				{
					$(object.caruselElementsContainerFirst).appendTo(object.caruselElementsContainer);
				}
				$(object.caruselElementsContainer).css({ left: 0});
			}
			containerTopPosition = 0 - $(object.caruselElementsContainerFirst +' + a').position().left * object.stepCount;
		}
		else
		{
			containerTopPosition = 0 - $(object.caruselElementsContainerFirst).outerWidth() * object.stepCount;
		}
		$(object.caruselElementsContainer).animate({ left: containerTopPosition }, this.animationTime, '', function() { object.moveLeftCallback(event); });
	}
	/*******************************************************
	*	Move left callback
	*******************************************************/
	this.moveLeftCallback = function(event) 
	{
		object = event.data.thisObj;
		if($(object.caruselElementsContainer).position().left < 0) 
		{
			for ( i = 1; i <= object.stepCount; i++ )
			{
				$(object.caruselElementsContainerFirst).appendTo(object.caruselElementsContainer);
			}
			$(object.caruselElementsContainer).css({ left: 0});
		}
		object.fastMoving = false;
		if( object.mouseOn == false && object.scrollStatus == 'scroll' )
			object.scroll();
	}
	/*******************************************************
	*	Move up
	*******************************************************/
	this.moveUp = function(event) 
	{
		object = event.data.thisObj;
		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 }, this.animationTime, '', function() { object.moveUpCallback(event); });
	}
	/*******************************************************
	*	Move up callback
	*******************************************************/
	this.moveUpCallback = function(event) 
	{
		object = event.data.thisObj;
		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(event) 
	{
		object = event.data.thisObj;
		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 }, this.animationTime, '', function() { object.moveDownCallback(event); });
	}
	/*******************************************************
	*	Move down callback
	*******************************************************/
	this.moveDownCallback = function(event) 
	{
		object = event.data.thisObj;
		object.fastMoving = false;
		if(object.mouseOn == false && object.scrollStatus == 'scroll')
			object.scroll();
	}
	/*******************************************************
	*	Toggle arrow image
	*******************************************************/
	this.toggleArrow = function() 
	{
		$(this).toggleClass('arrowPressed');
	}
	/*******************************************************
	*	Clear zoom image
	*******************************************************/
	this.clearImg = function() 
	{
		$('div#cloneImgCont a.cloneImg').css({ 'display':'none' });
	}
}