/* =========================================================

// jquery.bannerRotator.js

// Date:    2011-07-08
// Company: Corvus Corone Solutions
// Author:  John David
// Mail:    admin@corvuscorone.net
// Web:     http://corvuscorone.net

// based on the work of Matt Oakes http://portfolio.gizone.co.uk/applications/slideshow/

 *
 *  <ul id="news"> 
 *      <li>content 1</li>
 *      <li>content 2</li>
 *      <li>content 3</li>
 *  </ul>
 *  
 *  $('#news').bannerRotator(
 *	{
 *		startAutomatically: true,
 *		delay: 5000,
 *		fadeSpeed: 250,
 *		controls:
 *		{
 *			autoHide: true,
 *			buttonHeight: 30
 *		}
 *  }); 
 *

// ========================================================= */

(function($)
{
	var methods = 
	{
		init : function(options) 
		{
			return this.each(function()
			{
				base = $(this);
			
				defaultOptions = 
				{
					startAutomatically: true,
					delay: 5000,
					fadeSpeed: 250
				};
				
				base.options = $.extend({}, defaultOptions, options);
				base.slides = base.find('li');
				base.timer = null;
				base.isTransitioning = false;
				base.currentSlide = 0;

				if(base.slides.length > 1)
				{
					for(var i = 0; i < base.slides.length; i++) 
					{
						$(base.slides[i]).css('z-index', String(base.slides.length - i)).css('position', 'absolute').hide();
					};

					$(base.slides[0]).show();
					
					if(base.options.startAutomatically)
					{
						base.isPlaying = true;
						
						base.timer = setTimeout(function() 
						{
							methods._next();
						}, base.options.delay);
					}
					
					if(base.options.controls)
					{
						controls = $('<div id="bannerRotatorControls" />').hide();
						previous = $('<div id="bannerRotatorControlsPrevious" />').css( {'background-position': ('0 -' + (base.options.controls.buttonHeight * 3) + 'px') } ).click(methods.previous);
						pause    = $('<div id="bannerRotatorControlsPause" />').css( {'background-position': ('0 -' + (base.options.controls.buttonHeight * 1) + 'px') } ).click(methods.pause);;
						next     = $('<div id="bannerRotatorControlsNext" />').css( {'background-position': ('0 -' + (base.options.controls.buttonHeight * 2) + 'px') } ).click(methods.next);;
						
						controls.append(previous, pause, next);
					
						base.parent().append(controls);
					
						if(base.options.controls.autoHide)
						{
							base.parent().bind('mouseover', function()
							{
								controls.show();
							});
				
							base.parent().bind('mouseout', function()
							{
								controls.hide();
							});
						}

					}
				}
			});
		},
		
		pause : function( ) 
		{
			if(base.isPlaying)
			{
				methods._pause();
			}
			else
			{
				base.isPlaying = true;
				methods._next();
				$('#bannerRotatorControlsPause').css( {'background-position': ('0 -' + (base.options.controls.buttonHeight * 1) + 'px') } );
			}
		},
		
		next : function( ) 
		{
			methods._pause();
			methods._next();
		},
		
		previous : function() 
		{ 
			methods._pause();
			methods._previous();
		},

		_pause : function()
		{
			clearTimeout(base.timer);
			base.isPlaying = false;
			$('#bannerRotatorControlsPause').css( {'background-position': ('0 0') } );
		},
		
		_next : function()
		{
			if(!base.isTransitioning)
			{
				$(base.slides[base.currentSlide]).fadeOut(base.options.fadeSpeed);
				++base.currentSlide;
				base.currentSlide %= base.slides.length;
				$(base.slides[base.currentSlide]).fadeIn(base.options.fadeSpeed, function() { base.isTransitioning = false } );
				base.isTransitioning = true;
				
				if(base.isPlaying)
				{
					base.timer = setTimeout(function() 
					{
						methods._next();
					}, base.options.delay);
				}
			}
		}		,
		
		_previous : function()
		{
			if(!base.isTransitioning)
			{
				$(base.slides[base.currentSlide]).fadeOut(base.options.fadeSpeed);
				base.currentSlide += base.slides.length - 1;
				base.currentSlide %= base.slides.length;
				$(base.slides[base.currentSlide]).fadeIn(base.options.fadeSpeed, function() { base.isTransitioning = false } );
				base.isTransitioning = true;
			}
		}		
	};
	
	$.fn.bannerRotator = function(method) 
	{
		if(methods[method] ) 
		{
			return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
		} 
		else if(typeof method === 'object' || !method) 
		{
			return methods.init.apply(this, arguments);
		} 
		else 
		{
			$.error( 'Method ' +  method + ' does not exist on jQuery.bannerRotator');
		}    
	};
	
	$.BannerRotator = function()
	{
	}
	
})(jQuery);


