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

var newsCarousel={
	idStart: 'newsCarousel',
	count: 0,
	current: 1,
	endX: 0,
	curX: 0,
	step: 10,
	distMove: 639,
	numOnDisplay: 3,
	width: 639,
	lastInteraction: 0,
	interactionLimit: 5, // !!number of seconds pause in slideshow!!
	autoPause: false,
	mess: '',

	init:function() {
		// set the width of the carouselStrip
		//we need to know how many items are here first
		var numItems = 0;
		$$('#carouselStrip div[class="carouselItem"]').each(function(){
			++numItems;
		});
		this.count = Math.ceil(numItems / this.numOnDisplay);
		$('carouselStrip').setStyle({
			width: ((Math.ceil(numItems / this.numOnDisplay)) * 675) + 'px'
		});
		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();
		newsCarousel.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(newsCarousel), newsCarousel.interactionLimit);
	},


	next:function() {
		var d = new Date();
		this.lastInteraction = d.getTime();
		if(this.current==this.count-((this.numOnDisplay/3)-1)) { // rewind to start (goes in opposite direction)
			this.current = 1;
			this.endX = 0;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.005);

		} else {
			this.current = this.current+1;
			this.endX = this.endX-this.distMove;
			this.step = 10;

			new PeriodicalExecuter(function(pe) {
				if(this.curX > this.endX) {
					this.curX = this.curX-(this.step*this.numOnDisplay);
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.005);
		}
		/*if (this.count > 1) {
			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.endX = -(this.current-1)*this.distMove;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX > this.endX) {
					this.curX = this.curX-this.step;
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.05);
		} else {
			this.current = this.current-1;
			this.endX = this.endX+this.distMove;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.05);
		}
		/*if (this.count > 1) {
			this.makeButtons();
		}*/
	},

	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="newsCarouselPrevButton"><<</a>';
		for (slideCounter=1;slideCounter<=this.count;slideCounter++) {
			slideCounterClass = 'carouselSlide';
			if (slideCounter == this.current) {
				slideCounterClass = 'carouselSlideOn';
			}
			buttonHtml += '<a href="#" title="View slide ' + slideCounter + ' feature" class="' + slideCounterClass + '" id="' + this.idStart + 'SlideNum' + slideCounter + '">' + slideCounter + '</a>';
		}
		$('carouselFooter').innerHTML = buttonHtml + '<a href="#" title="View next feature" class="nextButton" id="newsCarouselNextButton">>></a>';

		/* Next Button */
		$(this.idStart + 'NextButton').observe('click', function(e) {
			Event.stop(e); // stop default behaviour
			this.next();
		}.bind(newsCarousel));

		/* Prev Button */
		$(this.idStart + 'PrevButton').observe('click', function(e) {
			Event.stop(e); // stop default behaviour
			this.prev();
		}.bind(newsCarousel));

		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(newsCarousel));
			} else {
				$(this.idStart + 'SlideNum' + slideCounter).observe('click', this.slideTo.bindAsEventListener(this,slideCounter));
			}
		}
	},

	slideTo:function(e,newCounter) {
		Event.stop(e);// stop the default behaviour
		var d = new Date();
		this.lastInteraction = d.getTime();
		this.endX = -(this.distMove * (newCounter - 1));
		this.mess = '';

		if (newCounter < this.current) {
			this.step = (this.current - newCounter)*90;
			new PeriodicalExecuter(function(pe) {
				if (this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.05);
		} else {
			this.step = (newCounter - this.current)*90;
			new PeriodicalExecuter(function(pe) {
				if (this.curX > this.endX) {
					this.curX = this.curX-this.step;
					$('carouselStrip').style.left = this.curX+"px";
				} else {
					$('carouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.05);
		}

		this.current = newCounter;
		if (this.count > 1) {
			this.makeButtons();
		}
	}
}

var awardingBodiesCarousel={
	idStart: 'awardingBodiesCarousel',
	count: 0,
	current: 1,
	endX: 0,
	curX: 0,
	step: 10,
	distMove: 639,
	numOnDisplay: 3,
	width: 639,
	lastInteraction: 0,
	interactionLimit: 10, // !!number of seconds pause in slideshow!!
	autoPause: false,
	mess: '',

	init:function() {
		// set the width of the carouselStrip
		//we need to know how many items are here first
		var numItems = 0;
		$$('#awCarouselStrip div[class="carouselItem"]').each(function(){
			++numItems;
		});
		this.count = Math.ceil(numItems / this.numOnDisplay);
		$('awCarouselStrip').setStyle({
			width: ((Math.ceil(numItems / this.numOnDisplay)) * 675) + 'px'
		});
		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();
		awardingBodiesCarousel.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(awardingBodiesCarousel), awardingBodiesCarousel.interactionLimit);
	},


	next:function() {
		var d = new Date();
		this.lastInteraction = d.getTime();
		if(this.current==this.count-((this.numOnDisplay/3)-1)) { // rewind to start (goes in opposite direction)
			this.current = 1;
			this.endX = 0;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(awardingBodiesCarousel), 0.005);

		} else {
			this.current = this.current+1;
			this.endX = this.endX-this.distMove;
			this.step = 10;

			new PeriodicalExecuter(function(pe) {
				if(this.curX > this.endX) {
					this.curX = this.curX-(this.step*this.numOnDisplay);
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(awardingBodiesCarousel), 0.005);
		}
	},

	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.endX = -(this.current-1)*this.distMove;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX > this.endX) {
					this.curX = this.curX-this.step;
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(newsCarousel), 0.05);
		} else {
			this.current = this.current-1;
			this.endX = this.endX+this.distMove;
			this.step = 90;

			new PeriodicalExecuter(function(pe) {
				if(this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(awardingBodiesCarousel), 0.05);
		}
	},

	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="awardingBodiesCarouselPrevButton"><<</a>';
		for (slideCounter=1;slideCounter<=this.count;slideCounter++) {
			slideCounterClass = 'carouselSlide';
			if (slideCounter == this.current) {
				slideCounterClass = 'carouselSlideOn';
			}
			buttonHtml += '<a href="#" title="View slide ' + slideCounter + ' feature" class="' + slideCounterClass + '" id="' + this.idStart + 'SlideNum' + slideCounter + '">' + slideCounter + '</a>';
		}
		$('awCarouselFooter').innerHTML = buttonHtml + '<a href="#" title="View next feature" class="nextButton" id="awardingBodiesCarouselNextButton">>></a>';

		/* Next Button */
		$(this.idStart + 'NextButton').observe('click', function(e) {
			Event.stop(e); // stop default behaviour
			this.next();
		}.bind(awardingBodiesCarousel));

		/* Prev Button */
		$(this.idStart + 'PrevButton').observe('click', function(e) {
			Event.stop(e); // stop default behaviour
			this.prev();
		}.bind(awardingBodiesCarousel));

		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(newsCarousel));
			} else {
				$(this.idStart + 'SlideNum' + slideCounter).observe('click', this.slideTo.bindAsEventListener(this,slideCounter));
			}
		}
	},

	slideTo:function(e,newCounter) {
		Event.stop(e);// stop the default behaviour
		var d = new Date();
		this.lastInteraction = d.getTime();
		this.endX = -(this.distMove * (newCounter - 1));
		this.mess = '';

		if (newCounter < this.current) {
			this.step = (this.current - newCounter)*90;
			new PeriodicalExecuter(function(pe) {
				if (this.curX < this.endX) {
					this.curX = this.curX+this.step;
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(awardingBodiesCarousel), 0.05);
		} else {
			this.step = (newCounter - this.current)*90;
			new PeriodicalExecuter(function(pe) {
				if (this.curX > this.endX) {
					this.curX = this.curX-this.step;
					$('awCarouselStrip').style.left = this.curX+"px";
				} else {
					$('awCarouselStrip').style.left = this.endX+"px";
					pe.stop();
				}
			}.bind(awardingBodiesCarousel), 0.05);
		}

		this.current = newCounter;
		if (this.count > 1) {
			this.makeButtons();
		}
	}
}

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

