/**
 *	jQuery Oscillate plugin by  Adam Passey
 *	adamjpassey@gmail.com
 */

(function($) {
	
	//	declare static variables, if needed
	
	$.fn.oscillate = function(options) {
	
		//	dfaults
		var defaults = {
			navigationHolderID:		'',				//	the id of the object holding the navigation controls
			automaticDelay:			5000,			//	the delay (in mili's) between automatic swaps
			afterClickDelay:		8000,			//	after the user clicks, how long until we begin automatic transitions
			animationStyle:			'slide'			//	how to animate between frames
														//	accepts: slide, 
		};
		
		//	extend defaults with options provided
		if(defaults)
			var settings = $.extend(defaults, options);
			
		//	iterate through matched elements
		return this.each(function() {
			
			//	the object
			object = $(this);
			
			//	begin initializing
			
			//	count how many li's are in this ul
			settings.listCount = $('li', object).length;
			
			//	get the width of an li to calculate
			settings.listElementWidth = $('li:nth-child(1)', object).width();
			
			//	set the ul's width to be the size of the entire li's together
			$(object).width( settings.listElementWidth * settings.listCount );
			
			//	create the navigation for the banner if set
			if( settings.navigationHolderID != null && settings.listCount > 1 ) {
				
				//	create the variable that we can interact with for navigation
				settings.navigationHolder = $('#'+settings.navigationHolderID);
				
				//	set the navigationUl
				if( $(settings.navigationHolder).is('ul') ) {
				
					//	if the navigation holder is a ul
					settings.navigationUl = settings.navigationHolder;
				} else {
				
					//	otherwise, create it in the navigationHolder
					$(settings.navigationHolder).append('<ul></ul>');
					settings.navigationUl = $('ul', settings.navigationHolder);
				}
					
				//	prepare the appending li's holder
				//	you don't want to modify html too much
				//	so add all the li's to this var, and
				//	append once
				var navigationLis = '';
				
				//	create the navigation for the banners
				//	currently uses a number system (for cool looking dots)
				for( i = 1; i <= settings.listCount; i ++ )
					navigationLis += '<li><a href="#'+i+'">'+i+'</a></li>';
					
				//	append the li's to the ul
				$(settings.navigationUl).append( navigationLis );
				
				//	if the navigation ul's opacity is 0, fade it in
				if( $(settings.navigationUl).css('opacity') == 0 )
					$(settings.navigationUl).animate({ 'opacity' : 1.0 }, 1000);
				
				//	make the first one active
				$('li:nth-child(1) a',settings.navigationUl).addClass('active');
				
				//	make the current position the first element
				settings.currentPos = 1;
				
				//	bind click to the navigation
				$('a', settings.navigationUl).click(function( ) {
					
					//	what banner are we moving to?
					href = $(this).attr('href');
					pos = href.substr( href.length - 1, 1 );
					
					moveToPos( pos, settings );
					settings.currentPos = pos;
					
					//	clear and reset the interval
					clearInterval( settings.interval );
					clearTimeout( settings.afterClickTimeout );
					settings.afterClickTimeout = setTimeout( function() {
						settings.interval = setInterval( function(){ automaticTransition( settings ); }, settings.automaticDelay );
					}, settings.afterClickDelay );
					
					return false;
				});
			}
			
			//	set the interval for automatic transition
			if( settings.listCount > 1 ) 
				settings.interval = setInterval( function(){ automaticTransition( settings ); }, settings.automaticDelay );

			//	return this for chaining
			return this;
			
		});
		
	};
	
	//	move to banner #
	function moveToPos( pos, settings ) {
	
		if(settings.animationStyle == 'slide') {
		
			var newMargin = ((pos * settings.listElementWidth) - settings.listElementWidth);
			
			newMargin = newMargin * -1;
			
			$(object).animate({
				'marginLeft': newMargin
			}, 400);
		}

		activateLink( pos, settings );
	}
	
	//	activate link #
	function activateLink( pos, settings ) {
		
		$('a', settings.navigationUl).each(function() {
			$(this).removeClass('active');
		});
		$('li:nth-child('+pos+') a', settings.navigationUl).addClass('active');
	}
	
	//	automatic transition
	function automaticTransition( settings ) {
		
		settings.currentPos ++;
		
		if( settings.currentPos > settings.listCount )
			settings.currentPos = 1;
		
		moveToPos( settings.currentPos, settings );
		activateLink( settings.currentPos, settings );
	}

})(jQuery);
