//-----------------------------------------------------------
//		Banner Rotator for Yahoo! Store - jquery plugin
//		Author: FastPivot (http://www.fastpivot.com)
// 
//		Version: 1.0 (12.21.2009)
//-----------------------------------------------------------

(function($){
	
	$.fn.bannerRotator = function(options){
		
		var defaults = {
			bannerWidth: 600,
			showNumbers: true,
			rotateAuto: true,
			rotateTimer: 3,
			fadeTimer: 2
		};
		
		var settings = $.extend({},defaults,options);
		
		return this.each(function(){

			var mainObj = $(this);
			var subMainObj = mainObj.children("div");
			var rotateObjs = mainObj.children("div").children("div");
			var rotateNumber = subMainObj.children("div").length;
			var currNum = 0,pauseBtn;
			var btnhover = "btnOn";
			
			subMainObj.css('width',(settings.bannerWidth)+"px");
			
			function startInterval(){
				var rotateTime = settings.rotateTimer*1000;
				
				fpSBInterval = setInterval(processAuto,rotateTime);
			}
			
			function processBtn(_btnnum){
				var numberBtns = numberContainer.children("a");
				
				numberBtns.removeClass(btnhover);
				numberBtns.eq(_btnnum).addClass(btnhover);
				currNum = _btnnum;
			}
			
			function processAuto(){
				(currNum==(rotateNumber-1))?currNum=0:currNum+=1;
				(settings.showNumbers)?processBtn(currNum):'';
				processTransition(currNum);
			}
			
			function processTransition(_btnnum){
				var fadeTime = settings.fadeTimer*1000;

				subMainObj.children().eq(_btnnum).fadeIn(settings.fadeTime);
				subMainObj.children().not(rotateObjs.eq(_btnnum)).hide();
				$("div.rotateBtns .desc").hide();
				$("div#description_" + _btnnum).show();
			}
			
			function showNumbers(){
				numberContainer = $(document.createElement("div")).addClass("rotateBtns").css("bottom",10).css("left",10);
				rotateObjs.each(function(i){
					numberContainer.append($(document.createElement("a")).click(function(e){
						processBtn(i);
						processTransition(i);
						(settings.rotateAuto)?clearInterval(fpSBInterval):'';
						e.preventDefault();
						$("div.pause").addClass("pauseOn");
					}).addClass("numBtn").text(i+1));
				});
				mainObj.append(numberContainer);
				numberContainer.children("a").eq(0).addClass(btnhover);
			}
			
			$.fn.initialize = function(){
				rotateObjs.each(function(){
					slide=$(this);
					slide.width(settings.bannerWidth).height(mainObj.height());
					slide.not(rotateObjs.eq(0)).hide();
					rotateObjs.eq(0).show();
				});
				(settings.showNumbers)?showNumbers():'';
				if(settings.rotateAuto){
					startInterval();
					pauseBtn = $(document.createElement("div")).addClass("pause");
					numberContainer.append(pauseBtn);
					pauseBtn.click(function(){
						if($(this).hasClass("pauseOn")){
							startInterval();
							$(this).removeClass("pauseOn");
						}else{
							$(this).addClass("pauseOn");
							clearInterval(fpSBInterval);
						}
					});
					numberContainer.append($(document.createElement("div")).addClass("desc").attr("id", "description_0").text("Pregnancy & Ovulation Tests").show());
					numberContainer.append($(document.createElement("div")).addClass("desc").attr("id", "description_1").text("Top Products").hide());
					numberContainer.append($(document.createElement("div")).addClass("desc").attr("id", "description_2").text("Fertility Monitors").hide());
				}
			}
			
			mainObj.initialize();
		});
	}
})(jQuery);
