/*
 * Tiny Carousel
 *
 * Copyright (c) 2010 Maarten Baijs
 *
 * Date: 02 / 03 / 2010
 * Library: Jquery
 * Version: 1.6
 *
 *
 */
(function($){
	$.fn.tinycarousel = function(options){
		var defaults = { 
			display: 1, // how many blocks do you want to move at 1 time?
			axis: 'x', // vertical or horizontal scroller? ( x || y ).
			controls: false, // show left and right navigation buttons.
			pager: false, // is there a page number navigation present?
			interval: false, // move to another block on intervals.
			intervaltime: 6000, // 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
		};
		var options = $.extend(defaults, options);  
		
		var oSlider = $(this);
		var oViewport = $('.viewport', oSlider);
		var oContent = $('.overview', oSlider);
		var oPages = oContent.children();
		var oBtnNext = $('.next', oSlider);
		var oBtnPrev = $('.prev', oSlider);
		var oPager = $('.pager', oSlider);
		
		var iCurrent = 0, iPageSize = 0, oTimer = 0, iLeftover = 0, bForward = true, iSteps = 0;
		
		return this.each(function(){  
			initialize();
		});
		function initialize(){
			iPageSize = options.axis == 'x' ? $(oPages[0]).outerWidth(true) : $(oPages[0]).outerHeight(true);
			iLeftover = Math.ceil(((options.axis == 'x' ? oViewport.outerWidth(true) : oViewport.outerHeight(true)) / (iPageSize * options.display)) -1);
			iSteps = Math.ceil(oPages.length / options.display) -(iLeftover <= 1 ? 0 : iLeftover);
			oContent.css(options.axis == 'x' ? 'width' : 'height', (iPageSize * oPages.length));
			
			setButtons();
			setEvents();
			setTimer();
		}
		function setButtons(){
			if(options.controls){
				oBtnPrev.toggleClass('disable', !(iCurrent > 0));
				oBtnNext.toggleClass('disable', !(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 setPager(oEvent){
			var oTarget = oEvent.target;
			if(oTarget.className.indexOf('pagenum') != -1){
				iCurrent = parseInt(oTarget.rel) -1;
				move(1);
			}
			return false;
		}
		function setPagerActive(){
			if(options.pager){
				var oNumbers = $('.pagenum', oSlider);
				oNumbers.removeClass('active');
				$(oNumbers[iCurrent]).addClass('active');
			}
		}
		function setTimer(){
			if(options.interval){
				oTimer = window.setInterval(function(){
					var iDirection = 1, exp = iCurrent -1 == -1;
					bForward ? iCurrent + 1 == iSteps ? bForward = false : iDirection = 1 : exp ? bForward = true : iDirection = -1;
					move(iDirection, true); 
				}, options.intervaltime);
			}
		}
		function resetTimer(bReset){
			if(options.interval && !bReset){
				clearInterval(oTimer);
				setTimer();
			}
		}	
		function move(iDirection, bTimerReset){
			var oPosition = {}
			var exp1 = iDirection == -1 && iCurrent > 0;
			var exp2 = iDirection ==  1 && iCurrent +1 < iSteps;

			if (exp1 || exp2){
				iCurrent += iDirection;
				oPosition[options.axis == 'x' ? '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();
				resetTimer(bTimerReset);		
			}
		}
	};
})(jQuery);

