(function($) {

	if(typeof $.fn.bannerRotator === "undefined") {

	    var defaults = {
			'rotationSpeed' : 2000,
			'elementClass' : 'bannerImage',
			'imageDivClass' : 'jquery_bannerRotator',
			'overlayClass' : 'jquery_bannerRotatorOverlay',
			'textOverlayClass' : 'jquery_bannerRotatorText',
			'containerClass' : 'jquery_bannerRotatorContainer',
			'dotContainerClass' : 'jquery_bannerDots',
			'addDots' : false,
			'textOverlay' : false,
			'sidebar' : false,
			'generateSidebar' : false,
			'imageSidebar' : false,
			'sidebarDivision' : 'sidebar',
			'sidebarBlockClass' : 'jQuery_sidebar',
			'sidebarBlockHightlightClass' : 'jQuery_sidebar_hightlight',
			'activeDotImage' : '',
			'dotImage' : '',
			'fadeSpeed' : 2000
		};

	    $.fn.bannerRotator = function(options) {

		    // build main options before element iteration
		    var runOptions = $.extend(defaults, options);


			return this.each( function() {

				var containerElement = $(this);

				var sidebarContainer = $('#' + runOptions.sidebarDivision);

	           	var currentElement = 0;

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

				// tweak the container to allow the javascript to run.
				$(this).css('position', 'relative');

				maxElements = $(containerElement).children('.' + runOptions.elementClass).length;

				var starterText = '';

				// if we are using a text overlay, start it here.
				// this is also added if the user requests dots adding.
				if ( runOptions.textOverlay || runOptions.addDots ) {

					var overlay = $('<div></div>');

					$(overlay).addClass(runOptions.overlayClass);
					$(overlay).css('z-index', '89');

					$(containerElement).append($(overlay));

				}

				// if we are adding dots, add the container
				if ( runOptions.addDots ) {
					var dotOverlay = $('<div></div>');

					$(dotOverlay).addClass(runOptions.dotContainerClass);
					$(dotOverlay).css('z-index', '91');

					$(containerElement).append($(dotOverlay));
				}

				$(containerElement).children('.' + runOptions.elementClass).each(function(index, value) {

					var newDivision = $('<div></div>');

					$(newDivision).addClass(runOptions.imageDivClass);
					$(newDivision).css('position', 'absolute');
					$(newDivision).css('top', '0');
					$(newDivision).css('left', '0px');
					$(newDivision).css('z-index', '88');

					if ( runOptions.textOverlay ) {

						var overlayText = $('<div></div>');

						$(overlayText).addClass(runOptions.textOverlayClass);
						$(overlayText).css('z-index', '90');

						$(overlayText).html($(value).attr('alt'));

						// add the new text block to the overlay.
						$(containerElement).append($(overlayText));
					}

					if ( runOptions.sidebar ) {

						if ( runOptions.generateSidebar ) {
							var sidebarText = $('<div></div>');

							$(sidebarText).addClass(runOptions.sidebarBlockClass);
							if ( index == 0 ) {
								$(sidebarText).addClass(runOptions.sidebarBlockHightlightClass);
							}
							$(sidebarText).css('z-index', '90');

							$(sidebarText).html($(value).find('img').attr('alt'));

							// add the new text block to the overlay.
							$(sidebarContainer).append($(sidebarText));
						} else {
                        	$(sidebarContainer).children('div').each(function(index, value) {
								$(value).addClass(runOptions.sidebarBlockClass);
								if ( index == 0 ) {
									$(value).addClass(runOptions.sidebarBlockHightlightClass);
								}
								$(value).css('z-index', '90');
							});
						}

					}

					if ( runOptions.imageSidebar ) {

						var sidebarBlock = $('<div></div>');

						$(sidebarBlock).addClass(runOptions.sidebarBlockClass);
						$(sidebarBlock).css('z-index', '90');

						var sidebarImage = $('<img></img>');

						var bits = $(value).find('img').attr('src').split('/');
						bits[bits.length] = bits[bits.length-1];
						bits[bits.length-2] = 'small_banners';

						$(sidebarImage).attr('src', bits.join('/'));

						newLink = $('<a></a>');
						$(newLink).attr('href', $(value).attr('href'));

						$(newLink).append($(sidebarImage));

						if ( index > 0 ) {
							$(sidebarBlock).css('opacity', '0.25');
						}

						// add the image to the new block.
						$(sidebarBlock).append($(newLink));

						// add the new block to the overlay.
						$(sidebarContainer).append($(sidebarBlock));

					}

					// see if this should be visible or not.
					if ( index == 0 ) {
						$(newDivision).show();
						starterText = $(value).attr('alt');
					} else {
						$(newDivision).hide();
						// if we are using overlay text hide all but the first.
						if ( runOptions.textOverlay ) {
							$(overlayText).hide();
						}
					}

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

				});

				// set the whole thing off.
				if ( maxElements > 1 ) {
					addDots();
					// no point in rotating one image.
					timer = setTimeout(function(){nextImage();}, (runOptions.rotationSpeed * 2));
				}


				function nextImage() {
					fadeOut();
					fadeIn();
				}

				function fadeOut() {

					// fade out the image
					$(containerElement).find('div.' + runOptions.imageDivClass).each(function(index, value) {
						if ( index == currentElement ) {
							$(value).fadeOut(runOptions.fadeSpeed / 2);
						}
					});

					if ( runOptions.textOverlay ) {
						// fade out the associated text.
						$(containerElement).find('div.' + runOptions.textOverlayClass).each(function(index, value) {
							if ( index == currentElement ) {
								$(value).fadeOut(runOptions.fadeSpeed / 2);
							}
						});
					}

					if ( runOptions.sidebar ) {
						// fade out the associated text.
						$(sidebarContainer).find('div.' + runOptions.sidebarBlockClass).each(function(index, value) {
							if ( index == currentElement ) {
								$(value).removeClass(runOptions.sidebarBlockHightlightClass);
							}
						});
					}

					if ( runOptions.imageSidebar ) {
						// fade out the associated text.
						$(sidebarContainer).find('div.' + runOptions.sidebarBlockClass).each(function(index, value) {
							if ( index == currentElement ) {
								$(value).animate({'opacity': '0.25'});
							}
						});
					}

					// step up the index pointer
					currentElement++;
					if ( currentElement >= maxElements ) {
						currentElement = 0;
					}
				}

				function fadeIn(jumpTo) {

                	var imageNumber = currentElement;
                	if ( jumpTo ) {
                    	imageNumber = jumpTo;
                    	currentElement = jumpTo;
					}

                	// fade in the image
					$(containerElement).find('div.' + runOptions.imageDivClass).each(function(index, value) {
						if ( index == imageNumber ) {
							$(value).fadeIn(runOptions.fadeSpeed);
						}
					});

					if ( runOptions.textOverlay ) {
	                	// fade in the text
						$(containerElement).find('div.' + runOptions.textOverlayClass).each(function(index, value) {
							if ( index == imageNumber ) {
								$(value).fadeIn(runOptions.fadeSpeed);
							}
						});
					}

					if ( runOptions.sidebar ) {
	                	// fade in the text
						$(sidebarContainer).find('div.' + runOptions.sidebarBlockClass).each(function(index, value) {
							if ( index == imageNumber ) {
								$(value).addClass(runOptions.sidebarBlockHightlightClass);
							}
						});
					}

					if ( runOptions.imageSidebar ) {
						// fade out the associated text.
						$(sidebarContainer).find('div.' + runOptions.sidebarBlockClass).each(function(index, value) {
							if ( index == imageNumber ) {
								$(value).animate({'opacity': '1'});
							}
						});
					}

					// if needs be add dots to the overlay.
					addDots()

					// lets go round again.
					if ( timer ) {
						clearTimeout(timer);
					}
					timer = setTimeout(function(){nextImage();}, (runOptions.rotationSpeed * 2));
				}

				function addDots() {
                   
                	if ( runOptions.addDots ) {

						$(containerElement).find('div.' + runOptions.dotContainerClass).each(function(index, value) {

							// clear the dots.
							$(value).html('');

							// add them back in.
							$(containerElement).find('div.' + runOptions.imageDivClass).each(function(imageIndex, imageValue) {

								var newImage = $('<img></img>');

								//$(newImage).addClass(runOptions.imageDivClass);
								$(newImage).attr('index', imageIndex);
								$(newImage).click(function(event) {
									if ( $(this).attr('index') != currentElement ) {
										fadeOut();
										fadeIn($(this).attr('index'));
									}
								});

								if ( imageIndex == currentElement ) {
									$(newImage).attr('src', runOptions.activeDotImage);
	                            } else {
									$(newImage).attr('src', runOptions.dotImage);
								}

								$(value).append($(newImage));

							});

						});
					}

				}

			});

		};

	}

})(jQuery);

