(function($)
{					
	$.fn.imgRotator = function(options) {
		$.fn.imgRotator.defaults = 
		{
			navigation: 'auto', /* Left and right navigation mode:  none - no buttons, always - always on top, auto - shown only if over the control */
			navigation_speed: 100, /* Speed of animation when displaying and hiding navigation bar */
			navigation_pos: 'middle', /* Relative vertical position of the navigation bar */
			previous: 'Prev', /* Img/text for previous */
			next: 'Next', /* Img/text for next */
			display_time: 5000, /* Number of millisecs an image is displayed */
			transition_speed: 200, /* Speed of transition in millisecs */
			auto_run: true, /* Automated rotation if true (display time used for automation) */
			style: 'fade', /* Can be: slide, move, fade */
			stopOnHover: true, /* Suspend rotation on mouseOver event */
			width: -1, /* control width -1 means read from first image */
			height: -1 /* control height -1 means read from first image */
		};
		
		var opts = $.extend({}, $.fn.imgRotator.defaults, options), /* Puts default values to variable */
				clicked = false; /* indicating left or right has been clicked */
		
		return this.each(function()
		{
			/* Variables */
			var $this = $(this), /* Sets a "Global" variable for the current matched object */
					$parent = $this.parent(), /* Sets a "Global" variable for the matched object's parent */
					timer = false, /* The setinterval function identifier */
					$images = $this.children(), /* The images to be rotated */
					count = $images.size(), /* The total number of images that will be rotated */
					$rotatingImages = $('<DIV></DIV>'), /* Div for rotating images */
					$moveContainer = null, /* Div for 3 images to move to the left or to the right */
					$currentImgIndex = $images.length; /* current image index in the rotatingImages array */
			
			if (count == 0) {
        return;
			}
			
			/* Setup the images */
			setup();			
			
			/* Sets up the image displays */
			function setup()
			{
				/* Override the css styles */
				$this.css('position','relative');
				$this.css('overflow','hidden');
				if (opts.width < 0) {
					$this.css('width', $($images[0]).width() + 'px');
				}
				else {
					$this.css('width', opts.width + 'px');
				}
				if (opts.height < 0) {
					$this.css('height', $($images[0]).height() + 'px');
				}
				else {
					$this.css('height', opts.height + 'px');
				}
				
				$rotatingImages.appendTo($this);
				$rotatingImages.append($images);
				if (opts.style == 'fade') {
					$images.css({"position":"absolute","top":"0px","left":"0px"}).removeClass('hide').hide();
					$rotatingImages.children(':first').show();
				}
				else if (opts.style == 'slide') {
          $images.css({"position":"absolute","top":"0px","left":$rotatingImages.children(':first').width() + "px"}).removeClass('hide').hide();
          $rotatingImages.children(':first').show();
				}
				else if (opts.style == 'move') {
          $images.removeClass('hide').hide();
          $moveContainer = new Object();
          $moveContainer.div = $("<div style='position: absolute'></div>");
          $moveContainer.currentImage = $("<div></div>");
          $moveContainer.currentImage.append($($images[0]).clone().css('display', 'block'));
          $moveContainer.currentImage.css('display', 'block');
          $moveContainer.prevImage = $("<div></div>");
          $moveContainer.prevImage.append($($images[0]).clone().css('display', 'block'));
          $moveContainer.prevImage.css('display', 'block');
          $moveContainer.nextImage = $("<div></div>");
          $moveContainer.nextImage.append($($images[0]).clone().css('display', 'block'));
          $moveContainer.nextImage.css('display', 'block');
          $moveContainer.prevImage.appendTo($moveContainer.div);
          $moveContainer.currentImage.appendTo($moveContainer.div);
          $moveContainer.nextImage.appendTo($moveContainer.div);
          $moveContainer.div.appendTo($this);
          $($moveContainer.currentImage).css({'position': 'absolute', 'left': $($moveContainer.currentImage).width() + 'px'});
          $($moveContainer.prevImage).css({'position': 'absolute', 'left': '0px'});
          $($moveContainer.nextImage).css({'position': 'absolute', 'left': $($moveContainer.currentImage).width() * 2 + 'px'});
          $($moveContainer.div).css({'width': $($moveContainer.currentImage).width() * 3 + 'px', 'left': '-' + $($moveContainer.currentImage).width() + 'px'});
          $($moveContainer.div).css("left", -$($moveContainer.currentImage).width() + "px");
	if (opts.width < 0) {
		  $this.css('width', $($moveContainer.currentImage).width() + 'px');
	}
	else {
		  $this.css('width', opts.width + 'px');
	}
	if (opts.height < 0) {
		  $this.css('height', $($moveContainer.currentImage).height() + 'px');
	}
	else {
		  $this.css('height', opts.height + 'px');
	}
				}
				else {
          $images.css({"position":"absolute","top":"0px","left":"0px"}).removeClass('hide').hide();
				}
				
							
				/* Call the function to create the navigation */
				if (count > 1)
				{
					createNavigation();
				}
				
				/* Set the rotator to run automatically if auto is true, and more than 1 object exists to rotate */
				if(opts.auto_run && count > 1)
				{
					initiateInterval();
					
					if (opts.stopOnHover) {
            /* Pause the rotator when the user has their mouse over an image */
            $this.hover(function()
            {
              clearInterval(timer);
            },function()
            {
              initiateInterval();
            });
					}
				}
			}
			
			/* Constructor */
			function rotate(direction)
			{
				var $current_image = null,
						$previous_image = null,
						$next_image = null;
				
				if(count > 1)
				{
          $current_image = $rotatingImages.children(':visible:first') == 'undefined' ? $rotatingImages.children(':first') : $rotatingImages.children(':visible:first'),
					$previous_image = $current_image.prev() == 'undefined' || $current_image.prev().html() == null ? $rotatingImages.children(':last') : $current_image.prev(),
					$next_image = $current_image.next() == 'undefined' || $current_image.next().html() == null ? $rotatingImages.children(':first') : $current_image.next();
          if (opts.style == 'fade') {
            $current_image.fadeOut(opts.transition_speed);
            
            switch(direction)
            {
              case -1:
                $previous_image.fadeIn(opts.transition_speed,function()
                {
                  clicked = false;
                });
                break;
              default:
                $next_image.fadeIn(opts.transition_speed,function()
                {
                  clicked = false;
                });
                break;
            }
					}
          else if (opts.style == 'move') {
            $($moveContainer.currentImage).empty().append($($images[$currentImgIndex % $images.length]).clone().css('display', 'block'));
            $($moveContainer.prevImage).empty().append($($images[($currentImgIndex - 1) % $images.length]).clone().css('display', 'block'));
            $($moveContainer.nextImage).empty().append($($images[($currentImgIndex + 1) % $images.length]).clone().css('display', 'block'));
            $($moveContainer.currentImage).css({'position': 'absolute', 'left': $($moveContainer.currentImage).width() + 'px'});
            $($moveContainer.prevImage).css({'position': 'absolute', 'left': '0px'});
            $($moveContainer.nextImage).css({'position': 'absolute', 'left': $($moveContainer.currentImage).width() * 2 + 'px'});
      
            switch(direction)
            {
              case -1:
                $($moveContainer.div).animate({
                  left: '+=' + $($moveContainer.currentImage).width()
                  }, opts.transition_speed, function () {
                    $currentImgIndex--;
                    if ($currentImgIndex == 0) {
                      $currentImgIndex = $images.length - 1;
                    }
                    $($moveContainer.currentImage).empty().append($($images[$currentImgIndex % $images.length]).clone().css('display', 'block'));
                    $($moveContainer.div).css("left", -$($moveContainer.currentImage).width() + "px");
                    clicked = false;
                  }
                );
                break;
              default:
                $($moveContainer.div).animate({
                  left: '-=' + $($moveContainer.currentImage).width()
                  }, opts.transition_speed, function () {
                    $currentImgIndex++;
                    $($moveContainer.currentImage).empty().append($($images[$currentImgIndex % $images.length]).clone().css('display', 'block'));
                    $($moveContainer.div).css("left", -$($moveContainer.currentImage).width() + "px");
                    clicked = false;
                  }
                );
                break;
            }
            
            
					}
				}
			}
			
			/* Creates the navigation to scroll */
			function createNavigation()
			{
				var $div = $('<DIV></DIV>'), /* Create the div object */
						$next = $div.clone().addClass('sideswap_nav sideswap_next').html(opts.next).appendTo($this).hide(), /* Create the next object */
						$previous = $div.clone().addClass('sideswap_nav sideswap_previous').html(opts.previous).appendTo($this).hide(); /* Create the previous object */
						
				if (opts.navigation_pos == 'bottom') {
          $next.css('bottom', '0px');
          $previous.css('bottom', '0px');
				}
				else if (opts.navigation_pos == 'top') {
          $next.css('top', '0px');
          $previous.css('top', '0px');
				}
				else {
          $next.css('top', Math.round(($this.height() - $next.height()) / 2) + 'px');
          $previous.css('top', Math.round(($this.height() - $next.height()) / 2) + 'px');
				}
				
				$next.click(function()
				{
					if(!clicked)
					{
						clicked = true;
						rotate(1);
					}
				});
				
				$previous.click(function()
				{
					if(!clicked)
					{
						clicked = true;
						rotate(-1);
					}
				});
				
				if (opts.navigation == 'auto') {
          $this.hover(function()
          {
            $next.fadeIn(opts.navigation_speed);
            $previous.fadeIn(opts.navigation_speed);
          },function()
          {
            $next.fadeOut(opts.navigation_speed);
            $previous.fadeOut(opts.navigation_speed);
          });
				}
				else if (opts.navigation == 'always') {
          $next.fadeIn(opts.navigation_speed);
          $previous.fadeIn(opts.navigation_speed);
				}

			}
			
			/* Initiates the interval timer */
			function initiateInterval()
			{
				timer = setInterval(function()
				{
					rotate();
				},opts.display_time);
			}
			
		});	
	};
})(jQuery);

