/*
 * Tiny Carousel 1.76
 *
 * Copyright (c) 2010 Maarten Baijs
 *
 * Date: 01 / 07 / 2010
 * Library: jQuery
 *
 */
(function($){
	$.fn.tinycarousel = function(options){
		var defaults = { 
			start: 1, // where should the carousel start?
			display: 1, // how many blocks do you want to move at 1 time?
			axis: 'x', // vertical or horizontal scroller? ( x || y ).
			controls: true, // show left and right navigation buttons.
			pager: false, // is there a page number navigation present?
			interval: false, // move to another block on intervals.
			intervaltime: 3000, // interval time in milliseconds.
			animation: true, // false is instant, true is animate.
			duration: 1000, // how fast must the animation move in ms?
			callback: null, // function that executes after every move
			displayLast: 1
		};
		var options = $.extend(defaults, options);  

		var oSlider = $(this);
		var oViewport = $('.viewport', oSlider);
		var oContent = $('.overview', oSlider);
		var oPages = oContent.children();

		var oSliderLast = $(this);
		var oViewportLast = $('.viewport', oSlider);
		var oContentLast = $('.overview', oSlider);
		var oPagesLast = oContent.children();
		
		var oBtnNext = $('.next', oSlider);
		var oBtnPrev = $('.prev', oSlider);
		var oPager = $('.pager', oSlider);
		var iPageSize, iSteps, iCurrent, oTimer, bForward = true, bAxis = options.axis == 'x';
		
		var iPageSizeLast, iStepsLast, iCurrentLast, oTimerLast, bForwardLast = true;	
		
		return this.each(function(){
			initialize();
		});
		function initialize(){
			iPageSize = bAxis ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			var iLeftover = Math.ceil(((bAxis ? oViewport.outerWidth() : oViewport.outerHeight()) / (iPageSize * options.display)) -1);
			iSteps = Math.max(1, Math.ceil(oPages.length / options.display) - iLeftover);
			iCurrent = Math.min(iSteps, Math.max(1, options.start)) -2;
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			move(1);
			setEvents();
			initializeLast();
		}
		function setButtons(){
			if(options.controls){
				oBtnPrev.toggleClass('display', !(iCurrent > 0));
				oBtnNext.toggleClass('display', !(iCurrent +1 < iSteps));
			}
		}
		function setEvents(){
			if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
				oBtnPrev.click(function(){move(-1); return false;});
				oBtnNext.click(function(){move( 1); return false;});
			}
			if(options.pager && oPager.length > 0){
				oPager.click(setPager);
			}
		}
		function setEventsLast(){
			if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
				oBtnPrev.click(function(){moveLast(-1); return false;});
				oBtnNext.click(function(){moveLast( 1); return false;});
			}
			if(options.pager && oPager.length > 0){
				oPager.click(setPager);
			}
		}

		function setPager(oEvent){
			var oTarget = oEvent.target;
			if($(oTarget).hasClass('pagenum')){
				iCurrent = parseInt(oTarget.rel) -1;
				move(1);
			}
			return false;
		}
		function setPagerActive(){
			if(options.pager){
				var oNumbers = $('.pagenum', oPager);
				oNumbers.removeClass('active');
				$(oNumbers[iCurrent]).addClass('active');
			}
		}
		function setTimer(bReset){
			if(options.interval && !bReset){
				clearInterval(oTimer);
				oTimer = window.setInterval(function(){
					bForward = iCurrent +1 == iSteps ? false : iCurrent == 0 ? true : bForward;
					move(bForward ? 1 : -1, true);
				}, options.intervaltime);
			}
		}

		function initializeLast(){
			//options.displayLast = options.display - (oPagesLast.length%options.display)-1;
			options.displayLast = oPagesLast.length%options.display;
			iPageSizeLast = bAxis ? $(oPagesLast[0]).outerWidth(true) : $(oPagesLast[0]).outerHeight(true);
			var iLeftoverLast = Math.ceil(((bAxis ? oViewportLast.outerWidth() : oViewportLast.outerHeight()) / (iPageSizeLast * options.displayLast)) -1);
			iStepsLast = Math.max(1, Math.ceil(oPagesLast.length / options.displayLast) - iLeftoverLast);
			iCurrentLast = iStepsLast-1;
			oContentLast.css(bAxis ? 'width' : 'height', (iPageSizeLast * oPagesLast.length));
			//moveLast(1);
			setEventsLast();
		}
		
		function moveLast(iDirection, bTimerReset){
			if((iCurrent==iSteps-1)){
				//if(iCurrentLast + iDirection > -1 && iCurrentLast + iDirection < iStepsLast){
					//iCurrentLast += iDirection;
	
						var oPositionLast = {};
						oPositionLast[bAxis ? 'left' : 'top'] = -(iCurrentLast * (iPageSizeLast * options.displayLast));	
						oContentLast.animate(oPositionLast,{
							queue: false,
							duration: options.animation ? options.duration : 0,
							complete: function(){
								if(typeof options.callback == 'function')
								options.callback.call(this, oPagesLast[iCurrentLast], iCurrentLast);
							}
						});
					setButtons();
					//setPagerActive();
					//setTimer(bTimerReset);
				//}
			}
		}
		function move(iDirection, bTimerReset){
			if(iCurrent + iDirection > -1 && iCurrent + iDirection < iSteps){
				iCurrent += iDirection;
					if(iCurrent==(iSteps-1)){
						moveLast(1);
					}
					else{
						//if(iDirection>0)
							//iCurrentLast += options.display;
						//else
							//iCurrentLast -= options.display;
						var oPosition = {};
						oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));	
						oContent.animate(oPosition,{
							queue: false,
							duration: options.animation ? options.duration : 0,
							complete: function(){
								if(typeof options.callback == 'function')
								options.callback.call(this, oPages[iCurrent], iCurrent);
							}
						});
						setButtons();
						setPagerActive();
						setTimer(bTimerReset);
					}
			}
		}
	};
})(jQuery);
