/*!
 * 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
 *
 *
 * Customized by Dmitrijs Kurilciks (WildWest) in May 2011
 *
 */
 
(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: true, // is there a page number navigation present?
			tpager: true, // is there a thumbs navigation present?
			interval: false, // move to another block on intervals.
			intervaltime: 3000, // 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: false, // false is instant, true is animate.
			duration: 1000, // how fast must the animation move in ms?
			callback: null, // function that executes after every move
			easing: 'easeOutSine' // easing method for slide transition
		};
		var options = $.extend(defaults, options);  

		var oSlider = $(this);
		var oViewport = $('.viewport:first', oSlider);
		var oContent = $('.overview:first', oSlider);
		var oPages = oContent.children();
		var oBtnNext = $('.next:first', oSlider);
		var oBtnPrev = $('.prev:first', oSlider);
		var oPager = $('.pager:first');
		var oTpager = $('.thumbs_box:first');
		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.innerWidth() : oViewport.innerHeight()) / (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;
			iPrev = -1;
			oContent.css(bAxis ? 'width' : 'height', (iPageSize * oPages.length));
			move(1);
			options.animation = true;
			setEvents();
			
			
			//$('#steps').html(digitToText(iSteps));			
		}
		function setEvents(){
			if(options.controls && oBtnPrev.length > 0 && oBtnNext.length > 0){
				oBtnPrev.click(function(){iPrev = iCurrent; move(-1); return false;});
				oBtnNext.click(function(){iPrev = iCurrent; move( 1); return false;});
			}
			if(options.interval){
				oSlider.hover(function(){clearTimeout(oTimer); bPause = true},function(){bPause = false; setTimer();});
			}
			if(options.pager && oPager.length > 0){
				$('li',oPager).click(setPager);
				$('li a',oPager).click(function(e){
					e.stopPropagation();								
				});
			}
			if(options.tpager && oTpager.length > 0){
				$('img',oTpager).click(setTpager);
			}
		}
		function setButtons(){
			if(options.controls){
				oBtnPrev.toggleClass('disable', !(iCurrent > 0));
				oBtnNext.toggleClass('disable', !(iCurrent +1 < iSteps));
			}
			if(options.pager){
				var oNumbers = $('.pagenum', oPager);
				
				//fadeout animation for previous list item
				$('.pagenum div').fadeOut(800, function(){
					$(this).removeClass('highlighted');
				});				
				
				// fadein animation				
				$('.pagenum div').eq(iCurrent).addClass('highlighted').fadeIn(800);				
				
			}	
			if(options.tpager){
							
				// stop highlighting previously active class 
				$('img.thumbnum', oTpager).removeClass('active');								
				
				// outlne active thumb				
				$('img.thumbnum', oTpager).eq(iCurrent).removeClass('hovered').addClass('active');	
			}
			
			
			// show location link near island map
			if (iCurrent == 3 && $('ul.topnav li.island').hasClass('active')){
				$('.comment_box').css({display: 'block'});
			}
			else {
				$('.comment_box').css({display: 'none'});
			}
			
			//$('#step').html(digitToText(iCurrent+1));	
		}		
		function setPager(oEvent){
			if($(this).hasClass('pagenum')){
				iPrev = iCurrent;
				iCurrent = parseInt($(this).attr('data-page')) -1;				
				move(1);				
			}
			return false;
		}
		function setTpager(oEvent){
			if($(this).hasClass('thumbnum')){
				iPrev = iCurrent;
				iCurrent = parseInt($(this).attr('data-ipage')) -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);
			}
		}
		function move(iDirection){
			if(iCurrent + iDirection > -1 && iCurrent + iDirection < iSteps){
				iCurrent += iDirection;
				//alert('Prev: '+iPrev+' ; Current: '+iCurrent);
				if(iPrev != iCurrent)
				{
					var oPosition = {};
					oPosition[bAxis ? 'left' : 'top'] = -(iCurrent * (iPageSize * options.display));
					
					
					oContent.animate(oPosition,{
						queue: false,
						duration: options.animation ? options.duration : 0,
						easing: options.easing,
						complete: function(){
							if(typeof options.callback == 'function')
							options.callback.call(this, oPages[iCurrent], iCurrent);
						}
					});				
				   	setButtons();
					setTimer();
				}
			}
		}
	};
})(jQuery);
