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

	    var defaults = {
			'rotationSpeed' : 1000,
			'containerClass' : 'jquery_imageSlideContainer',
			'previousHandle' : '',
			'nextHandle' : '',
			'slideElement' : 'div',
			'slideOutSpeed' : 200,
			'slideInSpeed' : 300
		};

	    $.fn.imageSlide = function(options) {

		    // build main options before element iteration

		    return this.each( function() {

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

				var currentCustomerComment = 0;

            	var containerElement = $(this);

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

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

				// tag some extra bits to the container
				$(containerElement).addClass(runOptions.container);

				//$(containerElement).css('position', 'relative');

				// hide the extra divisions off screen to the right, ready to slide in.
				$(containerElement).find(runOptions.slideElement).each(function(index, value) {
					$(value).css('position', 'absolute');
					$(value).css('top', '0px');
					if ( index > 0 ) {
						$(value).css('left', (actualWidth + 10) + 'px');
					}
				});

				// attach to the handles.
				if ( runOptions.nextHandle ) {
					$(runOptions.nextHandle).click(function(event) {
						event.preventDefault;

						nextImage();

						return false;
					});
				}

				if ( runOptions.previousHandle ) {
					$(runOptions.previousHandle).click(function(event) {
						event.preventDefault;

						previousImage();

						return false;
					});
				}

				function nextImage() {
					if ( currentCustomerComment < maxElements-1 ) {
						$(containerElement).find(runOptions.slideElement).each(function(index, value) {
							if ( index == currentCustomerComment ) {
								$(value).animate(
									{'left' : '-' + actualWidth},
									runOptions.slideOutSpeed,
									'linear',
									function() {
								});
							}
							if ( index == currentCustomerComment+1 ) {
								$(value).animate(
									{'left' : '0px'},
									runOptions.slideInSpeed,
									'linear',
									function() {
								});
							}
						});
						currentCustomerComment++;
						if ( currentCustomerComment >= maxElements ) {
							currentCustomerComment = maxElements;
						}
					}
				}

				function previousImage() {
					if ( currentCustomerComment > 0 ) {
						$(containerElement).find(runOptions.slideElement).each(function(index, value) {
							if ( index == currentCustomerComment ) {
								$(value).animate(
									{'left' : actualWidth + 'px'},
									runOptions.slideOutSpeed,
									'linear',
									function() {
								});
							}
							if ( index == currentCustomerComment-1 ) {
								$(value).animate(
									{'left' : '0px'},
									runOptions.slideInSpeed,
									'linear',
									function() {
								});
							}
						});
						currentCustomerComment--;
						if ( currentCustomerComment <= 0 ) {
							currentCustomerComment = 0;
						}
					}
				}

			});
		};

	}
})(jQuery);


