

(function($) {
	if(typeof $.fn.imageSlider === "undefined") {

	    var defaults = {
			'rotationSpeed' : 4000,
			'wrapperDivClass' : 'jquery_imageSlider',
			'containerClass' : 'jquery_imageSliderContainer',
			'cloneHolder' : true,
			'timedMove' : true,
			'prevHandle' : '',
			'nextHandle' : '',
			'slideDirection' : 'left',
			'slideElement' : 'img'
		};

	    $.fn.imageSlider = function(options) {

		    // build main options before element iteration

		    return this.each( function() {

			    var runOptions = $.extend(defaults, options);

			    var containerElement;

			    var timer;

			    var currentDirection = 'left';

            	var containerElement = $(this);

            	var currentElement = 0;

            	var maxElements = 2;

				var actualWidth = $(containerElement).width();

				var wrapper = $('<div class="' + runOptions.containerClass + '"></div>');
				$(wrapper).css('position', 'relative');
				$(wrapper).css('height', $(containerElement).height() + 'px');
				$(wrapper).css('width', $(containerElement).width() + 'px');

				$(containerElement).wrap($(wrapper));

				$(containerElement).css('overflow', 'hidden');
				$(containerElement).css('position', 'absolute');
				$(containerElement).css('top', '0px');
				$(containerElement).css('left', '0px');

				if ( runOptions.cloneHolder ) {
					var overlayElement = $(containerElement).clone();
					// remove elements from the clone.
					$(overlayElement).children().remove();
					$(overlayElement).css('position', 'absolute');
					$(overlayElement).css('z-index', '999');
					$(overlayElement).css('top', '0px');
					$(overlayElement).css('left', '0px');

					$(containerElement).after($(overlayElement));
				}

				// get the top position from the initial image, then assign that to all the other.
				// also move all but the first to the right of the box.
				var topPosition = 0;

				maxElements = $(containerElement).find(runOptions.slideElement).length;

				// need to work out the actual width.
				// this needs to include any left or right padding.

				// work out the various elements that can move the center.
				var leftPadding = $(containerElement).css('padding-left');
				var rightPadding = $(containerElement).css('padding-right');

				// add on any padding.
				if ( leftPadding != '' && leftPadding != 'undefined' ) {
					if ( leftPadding.substr(leftPadding.length-2, 2) == 'px' ) {
						actualWidth += parseFloat(leftPadding.substr(0, leftPadding.length-2));
					} else if ( leftPadding.substr(leftPadding.length-1, 1) == '%' ) {
						actualWidth += parseFloat(leftPadding.substr(0, leftPadding.length-1));
					}
				}

				if ( rightPadding != '' && rightPadding != 'undefined' ) {
					if ( rightPadding.substr(rightPadding.length-2, 2) == 'px' ) {
						actualWidth += parseFloat(rightPadding.substr(0, rightPadding.length-2));
					} else if ( rightPadding.substr(rightPadding.length-1, 1) == '%' ) {
						actualWidth += parseFloat(rightPadding.substr(0, rightPadding.length-1));
					}
				}

				// time to process the images.
				$(containerElement).find(runOptions.slideElement).each(function(index, value) {

					var newDivision = $('<div></div>');
					$(newDivision).addClass(runOptions.wrapperDivClass);
					$(newDivision).css('position', 'absolute');
					$(newDivision).css('z-index', '88');

					if ( index == 0 ) {
						// get the top position
						topPosition = $(value).css('top');

						// work out the left indent of the div.
						var leftPosition = 0;
						leftPosition = (actualWidth - $(value).width()) / 2;
						$(newDivision).css('left', leftPosition + 'px');
					} else {
						$(newDivision).css('top', topPosition);
						// position the initial image in the center.
						var leftPosition = actualWidth * 2;
						$(newDivision).css('left', leftPosition + 'px');
					}

					// wrap the image in the new holder.
					$(value).wrap($(newDivision));
					$(value).show();

				});

				// set the whole thing going.
				if ( runOptions.timedMove ) {
					timer = setTimeout(function(){nextImage();}, (runOptions.rotationSpeed * 2));
					currentDirection = 'left';
				}

				function nextImage() {
					if ( runOptions.slideDirection == 'left' ) {
						slideOutLeft();
						currentElement++;
						if ( currentElement >= maxElements ) {
							currentElement = 0;
						}
						slideInLeft();
					} else if ( runOptions.slideDirection == 'right' ) {
						slideOutRight();
						currentElement++;
						if ( currentElement >= maxElements ) {
							currentElement = 0;
						}
						slideInRight();
					} else if ( runOptions.slideDirection == 'switch' ) {
						slideOutRight();
						currentElement++;
						if ( currentElement >= maxElements ) {
							currentElement = 0;
						}
						slideInLeft();
					}
				}

				function previousImage() {
					if ( runOptions.slideDirection == 'left' ) {
						slideOutRight();
						currentElement--;
						if ( currentElement <= 0 ) {
							currentElement = maxElements;
						}
						slideInRight();
					} else if ( runOptions.slideDirection == 'right' ) {
						slideOutLeft();
						currentElement--;
						if ( currentElement <= 0 ) {
							currentElement = maxElements;
						}
						slideInLeft();
					} else if ( runOptions.slideDirection == 'switch' ) {
						slideOutLeft();
						currentElement--;
						if ( currentElement <= 0 ) {
							currentElement = maxElements;
						}
						slideInRight();
					}
				}

				function slideOutLeft() {
					$(containerElement).find('div.' + runOptions.wrapperDivClass).each(function(index, value) {
						if ( index == currentElement ) {
							$(value).animate({'left' : '-' + $(containerElement).width()}, 'linear', function() {
								$(value).css('left', (actualWidth * 2) + 'px');
							});
						}
					});
				}

				function slideOutRight() {
					$(containerElement).find('div.' + runOptions.wrapperDivClass).each(function(index, value) {
						if ( index == currentElement ) {
							$(value).animate({'left' : '+' + $(containerElement).width()}, 'linear', function() {
								$(value).css('left', (actualWidth * 2) + 'px');
							});
						}
					});
				}

				function slideInLeft() {

					$(containerElement).find('div.' + runOptions.wrapperDivClass).each(function(index, value) {
						if ( index == currentElement ) {
							var leftPosition = 0;
							leftPosition = ( actualWidth - $(value).width()) / 2;
							$(value).animate({'left' : leftPosition + 'px'});
						}
					});

					// lets go round again.
					if ( runOptions.timedMove && timer ) {
						clearTimeout(timer);
						if ( currentDirection == 'left' ) {
							timer = setTimeout(function(){nextImage();}, (runOptions.rotationSpeed * 2));
						} else if ( currentDirection == 'right' ) {
							timer = setTimeout(function(){previousImage();}, (runOptions.rotationSpeed * 2));
						}
					}

				}

				function slideInRight() {
					$(containerElement).find('div.' + runOptions.wrapperDivClass).each(function(index, value) {
						if ( index == currentElement ) {
							// need to move the initial position to be on the left rather than the right.
							$(value).css('left', '-' + ( $(value).width() + 5 ) + 'px');
							var leftPosition = 0;
							leftPosition = ( actualWidth - $(value).width()) / 2;
							$(value).animate({'left' : leftPosition + 'px'});
						}
					});

					// lets go round again.
					if ( runOptions.timedMove && timer ) {
						clearTimeout(timer);

						if ( currentDirection == 'left' ) {
							timer = setTimeout(function(){nextImage();}, (runOptions.rotationSpeed * 2));
						} else if ( currentDirection == 'right' ) {
							timer = setTimeout(function(){previousImage();}, (runOptions.rotationSpeed * 2));
						}
					}

				}

			});

		};

	}
})(jQuery);


