/*!
 * Tiny Carousel 1.8
 * http://www.baijs.nl/tinycarousel
 *
 * Copyright 2010, Maarten Baijs
 * Dual licensed under the MIT or GPL Version 2 licenses.
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.opensource.org/licenses/gpl-2.0.php
 *
 * Date: 10 / 11 / 2010
 * Depends on 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.
			othersintervaltime: 10000, // first interval time in milliseconds.
			intervaltime: 5000, // interval time in milliseconds.
			rewind: false, // If interval is true and rewind is true it will play in reverse if the last slide is reached.
			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 oTimer = true;
		var oSlider = $(this);
		var oViewport = $('.viewport:first', oSlider);
		var oContent = $('.overview:first', oSlider);
		var oBtnNext = $('.next:first', oSlider);
		var oBtnPrev = $('.prev:first', oSlider);
		var oPager = $('.pager:first', oSlider);
		// createClonedElement();
		var oPages = oContent.children();
		var iPageSize, iSteps, iCurrent, oTimer, bPause, bForward = true, bAxis = options.axis == 'x';
		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; /*3*/
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			move(1);
			setEvents();
			setHoverVignette();
		}
		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.interval){
				oSlider.hover(function(){clearTimeout(oTimer); bPause = true},function(){bPause = false; setTimer();});
			}
			if(options.pager && oPager.length > 0){
				//$('a',oPager).click(setPager);
			}
		}
		function setButtons(){
			if(options.controls){
				/*oBtnPrev.toggleClass('disable', !(iCurrent > 0));
				oBtnNext.toggleClass('disable', !(iCurrent +1 < iSteps));*/
			}
			if(options.pager){
				var oNumbers = $('.pagenum', oPager);
				oNumbers.fadeOut('slow');
				oNumbers.eq(iCurrent).stop(true, true).fadeIn('slow');

				var oVignette = $('.overview li');
				oVignette.removeClass('activeVignette');
				$(oVignette[iCurrent]).addClass('activeVignette');
			}			
		}		
		function setPager(oEvent){
			if($(this).hasClass('pagenum')){
				iCurrent = parseInt(this.rel) -1;
				move(1);
			}
			return false;
		}
		function setTimer(){
			if(options.interval && !bPause){
				clearTimeout(oTimer);
				oTimer = setTimeout(function(){
					iCurrent = !options.rewind && (iCurrent +1 == iSteps) ? -1 : iCurrent;
					bForward = iCurrent +1 == iSteps ? false : iCurrent == 0 ? true : bForward;
					move((options.rewind ? (bForward ? 1 : -1) : 1));
				}, options.intervaltime);
				options.intervaltime = options.othersintervaltime;
			}
			
		}
		function move(iDirection){

			if( iCurrent + iDirection == -1 ){
				iCurrent = iSteps;
			}else if( iCurrent + iDirection == iSteps ){
				iCurrent = -1;
			}
			iCurrent += iDirection;
			var oPosition = {};
			var countElement = oPages.length;
			if( iCurrent < (countElement - 3) )
				oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));	
			else
				oPosition[bAxis ? 'left' : 'top'] = -((countElement - 4) * (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);

					/*if( iCurrent + iDirection == -1 ){
						iCurrent = iSteps -8;
						oPosition[bAxis ? 'left' : 'top'] = -((iCurrent) * (iPageSize * options.display));
						$(this).animate(oPosition,{
							queue: false,
							duration: 0,
							complete: function(){
								if(typeof options.callback == 'function')
								options.callback.call(this, oPages[iCurrent], iCurrent);
							}
						});
					}else if( iCurrent + iDirection == iSteps -3){
						iCurrent = 4;
						oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (4 * options.display));
						$(this).animate(oPosition,{
							queue: false,
							duration: 0,
							complete: function(){
								if(typeof options.callback == 'function')
								options.callback.call(this, oPages[iCurrent], iCurrent);
							}
						});
					}*/
				}
			});
			setButtons();
			setTimer();
			
			

		}
		function setHoverVignette(){

			$('.overview li').hover(function(){
				
				clearTimeout(oTimer);

				var oVignette = $('.overview li');
				var indexHover = oVignette.index(this);
				if(indexHover != iCurrent ){
					oVignette.removeClass('activeVignette');
					$(oVignette[indexHover]).addClass('activeVignette');
					var oNumbers = $('.pagenum', oPager);
					oNumbers.fadeOut('slow');
					oNumbers.eq(indexHover).stop(true, true).fadeIn('slow');
					iCurrent = indexHover;
				}

			}, function(){
				setTimer();
			});
			
		}
		function createClonedElement(){
			/*oPages.append(oPages.find(':lt(4)'));*/
			var lengthPager = oPager.children().length-1 -4;
			var lengthContent = oContent.children().length-1 -4;

			var FirstFoorPager = oPager.find('li:lt(4)');
			var LastFoorPager = oPager.find('li:gt('+ lengthPager +')');

			FirstFoorPager.addClass('cloned').clone().appendTo(oPager);
			LastFoorPager.addClass('cloned').clone().prependTo(oPager);

			var FirstFoorContent = oContent.find('li:lt(4)');
			var LastFoorContent = oContent.find('li:gt('+ lengthContent +')');
			
			FirstFoorContent.addClass('cloned').clone().appendTo(oContent);
			LastFoorContent.addClass('cloned').clone().prependTo(oContent);
			/* oPager */
		}
	};
})(jQuery);
