/*
*  2fluid custom code
*  requires prototype - (AB)
*  Amended from code on new 2Fluid site (ASB)
*/

var homeBanner={

	idStart: 'homeBanner',
	count: 0,
	current: 1,
	endY: 0,
	curY: 0,
	step: 35,
	distMove: 225,
	numOnDisplay: 1,
	lastInteraction: 0,
	interactionLimit: 10, // !!number of seconds pause in slideshow!!
	autoPause: false,

	init:function() {
		if($(this.idStart + 'FeatureCarousel')) {
			$(this.idStart + 'FeatureCarousel').style.display = "inline-block";
			if($(this.idStart + 'SlidesCount') != null) {
				var allDIVS = document.getElementsByTagName('DIV');
				var el, i = 0, howmany = 0;
				while (el = allDIVS.item(i++)) {
					if (el.id.substr(0,22) == this.idStart + 'CarouselItem') {
						howmany++;
						this.count = howmany;
					}
				}
				this.count = Math.ceil(this.count/this.numOnDisplay)
				if (this.count > 1) {
					this.makeButtons();
				}
			}

			/* Everytime the user interacts with the Carousel we reset this time
			*  if it has been X+ secs then it goes into slideshow mode */
			var d = new Date();
			homeBanner.lastInteraction = d.getTime();

			/* Poll every X secs to see if we should auto-slide */
			new PeriodicalExecuter(function(pe) {
				var newD = new Date();
				var cTime = newD.getTime();
				var lastTime = this.lastInteraction + (this.interactionLimit*1000);

				if(lastTime <= cTime && this.autoPause == false) {
					this.next();
				}

			}.bind(homeBanner), homeBanner.interactionLimit);
			$(this.idStart + 'FeatureCarousel').observe('mouseover', function() {
				this.autoPause = true;
			}.bind(homeBanner));

			$(this.idStart + 'FeatureCarousel').observe('mouseout', function() {
				var d = new Date();
				this.lastInteraction = d.getTime();
				this.autoPause = false;
			}.bind(homeBanner));
			
		}
	},

	makeButtons:function() {
		// slideCounter = current button in loop
		// this.current = first slide currently on display
		// this.numOnDisplay = number of slides on display at any one time
		// this.count = total number of slides in slideshow
		buttonHtml = '<a href="#" title="View previous feature" class="prevButton" id="homeBannerPrevButton"><<</a>';		
		for (slideCounter=1;slideCounter<=this.count;slideCounter=slideCounter+1) {
			slideCounterClass = 'carouselSlide';
			if (slideCounter == this.current) {
				slideCounterClass = 'carouselSlideOn';
			}					
			buttonHtml += '<a href="#" title="View slide ' + slideCounter + ' feature" class="' + slideCounterClass + ' ' + this.idStart + slideCounterClass + '" id="' + this.idStart + 'SlideNum' + slideCounter + '">' + slideCounter + '</a>';							
		}
		$(this.idStart + 'SlidesCount').innerHTML = buttonHtml + '<a href="#" title="View next feature" class="nextButton" id="homeBannerNextButton">>></a>';
		
		/* Next Button */
    $(this.idStart + 'NextButton').observe('click', function(e) {
    	Event.stop(e); // stop default behaviour
    	this.next();
    }.bind(homeBanner));
    
    /* Prev Button */
    $(this.idStart + 'PrevButton').observe('click', function(e) {
    	Event.stop(e); // stop default behaviour
    	this.prev();
    }.bind(homeBanner));
    
		for (slideCounter=1;slideCounter<=this.count;slideCounter++) {
			if (slideCounter == this.current) {
				$(this.idStart + 'SlideNum' + slideCounter).observe('click', function(e) {
  	    		Event.stop(e); // stop default behaviour
      		}.bind(homeBanner));
        } else {
            $(this.idStart + 'SlideNum' + slideCounter).observe('click', this.slideTo.bindAsEventListener(this,slideCounter));
        }
		}
	},
	
	next:function() {
		var d = new Date();
		this.lastInteraction = d.getTime();
		if(this.current==this.count-((this.numOnDisplay)-1)) { // rewind to start (goes in opposite direction)
			this.current = 1;
			this.endY = 0;
			stepSize = this.step;

			new PeriodicalExecuter(function(pe) {
				if(this.curY < this.endY) {
					this.curY = this.curY+stepSize;
					$(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
				} else {
					$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
					pe.stop();
				}
			}.bind(homeBanner), 0.005);

		} else {
			this.current = this.current+1;
			this.endY = this.endY-this.distMove;
			stepSize = this.step;

			new PeriodicalExecuter(function(pe) {
				if(this.curY > this.endY) {
					this.curY = this.curY-(stepSize*this.numOnDisplay);
					$(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
				} else {
					$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
					pe.stop();
				}
			}.bind(homeBanner), 0.005);
		}
		this.makeButtons();
	},

	prev:function() {
		var d = new Date();
		this.lastInteraction = d.getTime();
		if(this.current==1) { // rewind to start (goes in opposite direction)
			this.current = this.count; 
			this.endY = -(this.current-1)*this.distMove;
			stepSize = this.step;

			new PeriodicalExecuter(function(pe) {
				if(this.curY > this.endY) {					
					this.curY = this.curY-stepSize;
					$(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
				} else {
					$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
					pe.stop();
				}
			}.bind(homeBanner), 0.05);
		} else {
			this.current = this.current-1;
			this.endY = this.endY+this.distMove;
			stepSize = this.step;

			new PeriodicalExecuter(function(pe) {
				if(this.curY < this.endY) {
					this.curY = this.curY+stepSize;
					$(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
				} else {
					$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
					pe.stop();
				}
			}.bind(homeBanner), 0.05);
		}
		this.makeButtons();
	},
  
  slideTo:function(e, newCounter) {
  	Event.stop(e);// stop the default behaviour
    var d = new Date();
    this.lastInteraction = d.getTime();
    this.endY = -(this.distMove * (newCounter - 1));
    this.mess = '';
		stepSize = this.step;
    
    if (newCounter < this.current) {
    	stepSize = stepSize*(this.current - newCounter);
        new PeriodicalExecuter(function(pe) {
        	if(this.curY < this.endY) {
            	this.curY = this.curY+stepSize;
                $(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
            } else {
            	$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
                pe.stop();
            }
        }.bind(homeBanner), 0.05);
    } else {
    	stepSize = stepSize*(newCounter - this.current);
        new PeriodicalExecuter(function(pe) {
        	if (this.curY > this.endY) {
                this.curY = this.curY-stepSize;
                $(this.idStart + 'CarouselStrip').style.top = this.curY+"px";
            } else {
            	$(this.idStart + 'CarouselStrip').style.top = this.endY+"px";
                pe.stop();
            }
        }.bind(homeBanner), 0.05);
    }
    
    this.current = newCounter;
    this.makeButtons();
  }
}

Event.observe(window, 'load', function() {
	homeBanner.init();
});
