/**
 * jQuery iNav v 1.1
 * http://codecanyon.net/item/jquery-inav-products-showcasing-navigation/224484
 *
 * Copyright 2011, Nicolas Gaston Gutierrez
 * You need to buy a license if you want use this script.
 * http://codecanyon.net/wiki/buying/howto-buying/licensing/
 *
 */
(function( $ ) 
{
	
	$.fn.inav = function( options )
	{
		
		var self = this;
		
		/**
		 * extending default options
		 */
		var opt = $.extend( 
		{
			'slider'				: '#slides-container',
			'pindicator'			: '#slides-control',
			'slideWidth'			: 0,
			'pageMargin'			: 0,
			'enableScroller'		: true,
			'hiddenClassName'		: 'hidden',
			'caretClassName'		: 'caret',
			'activeClassName'		: 'active',
			'nextClassName'			: 'next',
			'onChange'				: '',
			'onSelect'				: ''
		}, options );

		/**
		 * saving jquery objects
		 */
		self.$pslider 		= $( opt.slider );
		self.$pindicator	= $( opt.pindicator );
		self.$current		= self.$pslider.children( '.' + opt.activeClassName );
		self.$caret			= self.$pindicator.find( '.' + opt.caretClassName );
		self.$target		= null;
		
		if( $().delegate ){
			self.$prev	= $( '<a />', { 'id' : 'inav-prev-item', 'class' : 'inav-arrows', 'href' : '#' } ).hide();
			self.$next	= $( '<a />', { 'id' : 'inav-next-item', 'class' : 'inav-arrows', 'href' : '#' } ).hide();
		}
		else {
			self.$prev	= $( '<a id="inav-prev-item" href="#" class="inav-arrows"></a>' ).hide();
			self.$next	= $( '<a id="inav-next-item" href="#" class="inav-arrows"></a>' ).hide();		
		}


		/**
		 * local settings
		 */
		opt.sliderWidth 		= self.$pslider.width();
		opt.activity			= false;
		opt.direction			= null;
		opt.maxVisibleItems		= opt.sliderWidth / opt.slideWidth;
		opt.currentSliderItems	= self.$current.children().size();
		opt.disableClassName	= 'disabled';
		
		opt.scrollerStep			= 1;
		opt.scrollerCurrentItem		= 0;
		opt.scrollerOutsideItems 	= opt.currentSliderItems - opt.maxVisibleItems;
		
		/**
		 * configuration
		 *
		 * @access private
		 */
		self.easings = function()
		{

			$.easing.easeOutBounce = function( x, t, b, c, d )
			{
				if ((t/=d) < (1/2.75)) {
					return c*(7.5625*t*t) + b;
				} 
				else if (t < (2/2.75)) {
					return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
				} 
				else if (t < (2.5/2.75)) {
					return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
				} 
				else {
					return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
				}
			};

			$.easing.easeInBack = function( x, t, b, c, d, s )
			{
				if (s == undefined) s = 1.70158;
				return c*(t/=d)*t*((s+1)*t - s) + b;
			};

			$.easing.easeInQuint = function( x, t, b, c, d )
			{
				return c*(t/=d)*t*t*t*t + b;
			};

		};
		
		/**
		 * bounce a set of images
		 *
		 * @access private
		 */
		self.bounceImages = function( $level )
		{
			var $ul = self.$current;
			$level 	= $level || 'first';
			
			if( $level != 'first' )
				$ul = $level;

			$ul.find( 'li' ).each(function( i )
			{
				var lft = ( i * opt.slideWidth ) + Math.ceil( opt.slideWidth / 2 );
				
				$( this ).animate(
					{ top: 0, left: lft },
					{ queue: false, duration: 350 }
				);
			});
			
			setTimeout(function()
			{
				
				self.$current.removeAttr( 'style' );

			}, 700 );
		};

		/**
		 * caret place
		 *
		 * @access private
		 */
		self.caretPlace = function( animated )
		{
			animated = animated || false;
			
			var $active = self.$pindicator.find( '.' + opt.activeClassName );
			var mleft 	= $active.position().left + Math.ceil( $active.outerWidth() / 2 );
			
			if( !animated )
				self.$caret.css( 'left', mleft );
			else {
				self.$caret.stop().animate(
					{ left: mleft },
					{ queue: false, duration: 800 }
				);
			}
		};

		/**
		 * fade in icons
		 *
		 * @access private
		 */
		self.setupfadeInIcons = function()		
		{
		
			self.$target.find( 'li' ).each(function( i )
			{
				
				var mleft = opt.direction == 'right'
						  ? opt.sliderWidth + ( opt.slideWidth * i )
						  : 0 - ( opt.slideWidth * ( i + 1 ) );
				
				$( this ).css(
				{
					'position'		: 'absolute',
					'z-index'		: '0',
					'top' 			: 0,
					'opacity'		: 0,
					'left' 			: mleft
				});
			});
		};

		/**
		 * fade in icons
		 *
		 * @access private
		 */
		self.fadeInIcons = function()
		{
			var pause = 50;
			var timer = null;
			
			var $liCollection 	= opt.direction == 'right'
								? self.$target.find( 'li' )
								: $( self.$target.find( 'li' ).get().reverse() );
			
			var liCount = $liCollection.size() - 1;
			
			$liCollection.each(function( i )
			{
				var $this = $( this );
				var mleft = opt.direction == 'right'
						  ? opt.slideWidth * i
						  : opt.slideWidth * ( liCount - i );
				
				timer = setTimeout(function()
				{
					$this.animate(
						{ left: mleft, opacity: 1 },
						{ queue: false, duration: 200  }
					);				
				}, pause * i);

			});
			
			setTimeout(function()
			{
				self.$current.removeClass( opt.activeClassName ).css( 'margin-left', '0' );
				self.$target.removeClass( opt.nextClassName ).addClass( 'active' );
				
				self.$current 				= self.$target;
				opt.activity 				= false;
				opt.currentSliderItems 		= self.$current.children().size();
				opt.scrollerOutsideItems 	= opt.currentSliderItems - opt.maxVisibleItems;
				opt.scrollerCurrentItem		= 0;
				
				self.$next.removeClass( opt.disableClassName );
				
				if( opt.currentSliderItems > opt.maxVisibleItems ){
					self.$next.fadeIn( 400 );
					self.$prev.fadeIn( 250 ).addClass( opt.disableClassName );
				}
				else {
					self.$next.hide().removeClass( opt.disableClassName );
					self.$prev.hide().removeClass( opt.disableClassName );
				}
				
			}, pause * self.$target.find( 'li' ).size() );
		};
		
		/**
		 * fade out icons
		 *
		 * @access private
		 */
		self.fadeOutIcons = function()
		{
			var pause = 50;
			var timer = null;
			
			var $liCollection 	= opt.direction == 'right'
								? self.$current.find( 'li' )
								: $( self.$current.find( 'li' ).get().reverse() );
			
			$liCollection.each(function( i )
			{
				var $this = $( this );
				
				timer = setTimeout(function()
				{
					$this.animate(
						{ left: opt.direction == 'left' ? opt.sliderWidth + opt.slideWidth : -500, opacity: 0 },
						{ queue: false, duration: 200  }
					);				
				}, pause * i);

			});
			
			setTimeout(function()
			{
				$liCollection.removeAttr( 'style' );
				self.$current.removeClass( opt.activeClassName ).addClass( opt.hiddenClassName );
				self.$target.addClass( opt.activeClassName ).removeClass( opt.hiddenClassName );
				self.fadeInIcons();
				
			}, pause * self.$current.find( 'li' ).size() );
		};
		
		/**
		 * changing the current menu
		 *
		 * @access private
		 */		
		self.changeMenu = function()
		{
			if( opt.activity || $( this ).hasClass( opt.activeClassName ) )
				return false;
					
			opt.activity 	= true;
			var $this 		= $( this );
			self.$target 	= $( $this.attr( 'href' ) );
			
			opt.direction	= self.$current.index() > self.$target.index() 
							? 'left' 
							: 'right';
			
			$this.addClass( opt.activeClassName ).parent().siblings().children().removeClass( opt.activeClassName );
			
			self.$target.addClass( opt.nextClassName );

			if( opt.onChange instanceof Function ){
				opt.onChange({
					direction	: opt.direction,
					choosed		: this
				});
			}

			self.caretPlace( 'animated' );
			self.fadeOutIcons();
			self.setupfadeInIcons();

			
			return false;
		};
		
		self.showPrevItem = function()
		{
			if( !opt.scrollerCurrentItem || $( this ).hasClass( opt.disableClassName ) )
				return false;

			if( self.$next.hasClass( opt.disableClassName ) )
				self.$next.removeClass( opt.disableClassName );

			var newMargin = ( opt.scrollerCurrentItem - 1 ) * opt.slideWidth;			

			self.$current.animate(
				{ marginLeft: -newMargin },
				{ queue: false, duration: 150 }
			);
			
			opt.scrollerCurrentItem--;
			
			if( !opt.scrollerCurrentItem ){
				$( this ).addClass( opt.disableClassName );
				self.$next.removeClass( opt.disableClassName );
			}
			
			return false;
		};
		
		self.showNextItem = function()
		{
			if( opt.scrollerCurrentItem >= opt.scrollerOutsideItems || $( this ).hasClass( opt.disableClassName ) )
				return false;
			
			if( self.$prev.hasClass( opt.disableClassName ) )
				self.$prev.removeClass( opt.disableClassName );
			
			if( self.$prev.is( ':hidden' ) )
				self.$prev.fadeIn( 150 );
			
			var newMargin = ( opt.scrollerCurrentItem + 1 ) * opt.slideWidth;			

			self.$current.animate(
				{ marginLeft: -newMargin },
				{ queue: false, duration: 150 }
			);
			
			opt.scrollerCurrentItem++;
			
			if( opt.scrollerCurrentItem >= opt.scrollerOutsideItems ){
				$( this ).addClass( opt.disableClassName );
				self.$prev.removeClass( opt.disableClassName );
			}
			
			return false;
		};
		
		/**
		 * automatic init the plugin
		 */
		(self.set2start = function()
		{
			
			/**
			 * enable easings
			 */
			self.easings();
			
			/**
			 * arrows item direction
			 */
			if( opt.enableScroller ){
				
				self.$prev.appendTo( opt.slider ).bind( 'click', self.showPrevItem );
				self.$next.appendTo( opt.slider ).bind( 'click', self.showNextItem );
				
				if( opt.currentSliderItems > opt.maxVisibleItems )
					self.$next.fadeIn( 1150 );
			}
			
			/**
			 * caret place
			 */
			self.caretPlace();
			
			/**
			 * setting temporary style
			 */
			self.$current.css(
			{
				'width'		: opt.sliderWidth,
				'overflow' 	: 'hidden'
			});

			/**
			 * hiding first level images
			 */			
			self.$current.find( 'li' ).css(
			{
				'position'		: 'absolute',
				'z-index'		: '0',
				'top' 			: -1800,
				'left' 			: Math.ceil( opt.sliderWidth / 2 ),
				'margin-left'	: -Math.ceil( opt.slideWidth / 2 )
			});

			/**
			 * showing first menu
			 */
			self.$current.removeClass( opt.hiddenClassName );

			/**
			 * bouncing the first time the icons
			 */
			self.bounceImages();
			
			/**
			 * adding listeners
			 */
			self.$pindicator.find( '.page' ).bind( 'click', self.changeMenu );
			
			if( opt.onSelect instanceof Function ){
				self.$pslider.find( 'a' ).click( opt.onSelect );
			}
			
		})();
	}
	
})( jQuery );
