jQuery.fn.extend({

	cs_rotator :
		
		function( settings ){
		
			settings = {
				navigation : settings.navigation || false,
				interval : settings.interval || 3000
			} 


			var thisID = $(this).attr("id");
			var thisTag = this.get(0).tagName;
			var i = 0 // intializeRotator

			if ( thisTag != "DIV" ) {
			    alert("From 'jquery.rotator.js' - Banner must be contained within a <div>.");
				$("#"+thisID+" > a").css({ "display" : "none"  })
				return;			
			} else if ( thisID == "" ) {
			    alert("From 'jquery.rotator.js' - Rotating banner's id undefined. Please use a unique to run rotating banner.");
				return;			
			} else {				
				runBannerCode();				
			}

			// begin - Run banner code
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
			
			function runBannerCode(){
	
				//hide other banners
				$("#"+thisID+" > a img:gt(0)").css( {"display" : "none"} );
				$("#"+thisID+"").addClass("cs-rotator");
				
				
				var numberOfBanners = $("#"+thisID+" > a").size();
				//alert(numberOfBanners);
				
				var bannerWidth = $("#"+thisID+" > a:first-child img").attr("width");
				var bannerHeight = $("#"+thisID+" > a:first-child img").attr("height");

				//alert(bannerWidth)
				
				$("#"+thisID+" > a img").each(
					function (i) {
				
						// Make sure sizes match

					    if ( bannerWidth > this.width || bannerWidth < this.width ) { $("#"+thisID+"").css({ "display" : "none" }); alert( thisID+": Banner widths don't match");  return }; // stop

						if ( bannerHeight > this.height || bannerHeight < this.height ) { $("#"+thisID+"").css({ "display" : "none" }); alert( thisID+": Banner heights don't match"); return }; // stop

						( (numberOfBanners-1) == i ) ? displayFirstBanner() : null; // display first banner
						
					}

				);

				//Generate Navigation

				( settings.navigation == true  ) ? generateNav() : null; // do nothing
				
				function generateNav(){
					$("#"+thisID+" > a:last").after('\n<ol>\n<li><a href="javascript:void(0)" class="on">&bull;</a></li>\n</ol>');
				
					$("#"+thisID+" > a img").each(
						function (i) {
							/* if */ 
							( i < ( numberOfBanners - 1 ) ) ? $("#"+thisID+" ol li:last").after('\n<li><a href="javascript:void(0)">&bull;</a></li>') : null; // else do nothing
						}
					);
					
				}
				
				function startRotation(){
				
					if ( i < ( numberOfBanners - 1 ) ) {

						$("#"+thisID+" > a:eq("+i+") img").fadeOut( 200,
							function(){
								$("#"+thisID+" > a:eq("+( i + 1 )+") img").fadeIn( 450 );
								$("#"+thisID+" ol li:eq("+i+") a").removeClass("on");
								$("#"+thisID+" ol li:eq("+( i + 1 )+") a").addClass("on");
								i++
							}
						);
					
					} else if (  i >= ( numberOfBanners - 1 ) ) {	
	
						$("#"+thisID+" > a:last img").fadeOut( 200,
							function(){
								$("#"+thisID+" > a:eq(0) img").fadeIn( 450 );
								$("#"+thisID+" ol li:last a").removeClass("on");
								$("#"+thisID+" ol li:eq(0) a").addClass("on");
							}
						);	
						i = 0
					}
	
				}

				var start = setInterval ( startRotation, settings.interval );

				$("#"+thisID+" , #"+thisID+" > a").css({ "width" : bannerWidth+"px" , "height" : bannerHeight+"px" });

				//displayFirstBanner();
				
				function displayFirstBanner(){
					$("#"+thisID+" > a:first-child").css({ "display" : "block" , "visibility" : "visible" })
					$("#"+thisID+" > a:gt(0)").css({ "visibility" : "visible" })
				}
				
				$("#"+thisID+" ol li a").click(
					
					function(){

						var thisPosition = $(this).parent().prevAll().length + 1;

						if (  thisPosition == ( i + 1 ) ){
		
							clearInterval(start);
						
						} else {

						  	clearInterval(start);
							$("#"+thisID+" > a:eq("+( i )+") img").fadeOut( 200 ,
								function(){
									$("#"+thisID+" > a:eq("+( thisPosition - 1 )+") img").fadeIn( 450 );
									i = thisPosition - 1;
									$("#"+thisID+" ol li a").removeClass("on");
									$("#"+thisID+" ol li:eq("+ i +") a").addClass("on");
								}
							);

						}

					}
				
				);
			
			}


			// end - Run banner code
			//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

		}

});

