var carousel = function(thumbnailElement,imgElement,transImgElement,useZoomOption)
{
	// html references
	this.thumbnailElement = (thumbnailElement) ? $(thumbnailElement) : $('carousel_thumbnails');
	this.thumbnailElementName = this.thumbnailElement.id;
	this.imgElement       = (imgElement) ? $(imgElement) : $('carousel_image');
	this.transImgElement  = (transImgElement) ? $(transImgElement) : $('carousel_transitional_image');
	this.zoomOption       = (useZoomOption) ? $(transImgElement) : $('carousel_transitional_image');
	this.thumbWidthAndQuality = new RegExp("-45-80", "g");
	this.imgWidthAndQuality = "-400-85";
	this.transitionInProgress = false;

	this.carousel_thumbs = new Array;
	this.carousel_pics = new Array;
	this.arrImages = new Array;
	this.thumbTransitions = new Array;
	this.anchorTransitions = new Array;
	this.synopsisTransitions = new Array;
	this.thumbTransitionsByForce = new Array;
	this.anchorTransitionsByForce = new Array;
	this.synopsisTransitionsByForce = new Array;
	this.currentHeadline = 0; // Set the first headline as the highlighted one initially.
	carouselRef = this;
}
carousel.prototype =
{
	initialise:function()
	{
		if (this.thumbnailElement != null)
		{
			// carousel setup functionality goes here.
			this.thumbPics = this.thumbnailElement.getElements('img');
			var x = this.thumbnailElement.getElements('li');
			for (var i=0;i<x.length;i++)
			{
				x[i].getElements("img")[0].headlineIndex = i;
				x[i].getElements("img")[0].onclick = function () { carouselRef.carousel_force(this,'now'); }
				x[i].onmouseover = function () { carouselRef.carousel_force(this.getElements("img")[0],'now'); }
			}
			// Create arrays of pics and thumbnails using the existing thumbnail sources.
			for (i = 0; i < this.thumbPics.length; i++)
			{
				this.carousel_thumbs[i] = this.thumbPics[i].src;
				this.carousel_pics[i] = this.carousel_thumbs[i].replace(this.thumbWidthAndQuality,this.imgWidthAndQuality);
			}
			this.transImgElement.src = this.carousel_pics[1];
			this.transitionImg = new Fx.Styles(this.transImgElement, {duration: 1000, transition: Fx.Transitions.Circ.easeOut});
			for (image=0; image < this.carousel_pics.length; image++)
			{
				this.thumbTransitions[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li img")[image], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
				this.anchorTransitions[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li a")[(image*2)+1], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
				this.synopsisTransitions[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li a p")[image], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
				this.thumbTransitionsByForce[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li img")[image], {duration: 5, transition: Fx.Transitions.Quad.easeOut});
				this.anchorTransitionsByForce[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li a")[(image*2)+1], {duration: 5, transition: Fx.Transitions.Quad.easeOut});
				this.synopsisTransitionsByForce[image] = new Fx.Styles(this.thumbnailElement.getElementsBySelector("li a p")[image], {duration: 5, transition: Fx.Transitions.Quad.easeOut});
				this.arrImages[image] = new Image();
				this.arrImages[image].src = this.carousel_pics[image];
				this.arrImages[image].iter = image;
				this.arrImages[image].onerror = function(){
					this.src = "/default/img/spacer.gif";
					carouselRef.thumbnailElement.getElements('img')[this.iter].src = this.src;
					carouselRef.carousel_pics[this.iter] = this.src;
				}
			}

			var gimmeThoseLinks = this.thumbnailElement.getElementsBySelector('li .carousel_link'); /* .carousel_link is the second link of the list item. */
			gimmeThoseLinks[0].setStyle('color','#ffffff');

			if (this.zoomOption == "zoom") {
				var theFirstThumbnail = new Fx.Styles(this.thumbnailElement.getElements("li")[0], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
				theFirstThumbnail.start({'font-size':['12px','16px']});
			}
			carousel_timer = setInterval(function(){carouselRef.rotate_carousel(carouselRef.thumbnailElement,carouselRef.imgElement,carouselRef.transImgElement,carouselRef.zoomOption)},5000);
			// Gives us time to load the images.
		}
	},
	carousel_force:function(clickedImage,immediateSwitch)
	{
		if (this.imgElement.src != this.transImgElement.src) {
			if (immediateSwitch == "now") {
				this.transImgElement.src = this.imgElement.src;
			} else { return false; }
		}
		if (this.transitionInProgress) { return false; }
		// Prevents image switches happening if there's a transition already in progress,
		// unless we've insisted on an immediate switch.
		for (i = 0; i < this.thumbPics.length; i++)
		{
			if (this.imgElement.src == clickedImage.src) {
				clearInterval(carousel_timer);
				carousel_timer = setInterval(function(){carouselRef.rotate_carousel(carouselRef.thumbnailElement,carouselRef.imgElement,carouselRef.transImgElement,carouselRef.zoomOption)},5000);
				return false;
			}
		}
		clearInterval(carousel_timer);
		this.transitionInProgress = true;
		// This whole section probably needs to go into a separate function. It's repeated elsewhere.
		for (image=0; image < this.carousel_pics.length; image++)
		{
			this.thumbnailElement.getElementsBySelector("li img")[image].className = "";
			this.thumbnailElement.getElementsBySelector("li a")[image*2+1].addClass("carousel_link");
			if (this.thumbnailElement.getElementsBySelector("li img")[image].headlineIndex == clickedImage.headlineIndex)
			{
				this.currentHeadline = image;
				this.thumbTransitionsByForce[image].start({'opacity': ['0.5','1']});
				this.thumbnailElement.getElementsBySelector("li")[image].setStyles({'border':'1px solid #06a','background-color':'#06a','background-image':'url(\'/default/img/carousel/carousel-li-bg.gif\')'});
				this.anchorTransitionsByForce[image].start({'color':['#ddd','#fff']});
//				this.synopsisTransitionsByForce[image].start({'color':['#333','#ddd']});
				if (this.zoomOption == "zoom") {
					this.thumbTransitionsByForce[image].start({'font-size':['12px','16px']});
				}

				this.transImgElement.src = this.imgElement.src;
				this.transImgElement.setStyles({'z-index':110, 'opacity':1,'filter':'alpha(opacity=100)'});
				this.tmpImageVar = this.arrImages[image].src;
				setTimeout(function(){carouselRef.imgElement.src = carouselRef.tmpImageVar;}, 50);
				this.thumbnailElement.getElementsBySelector("li a")[(image*2)+1].addClass("carousel_link");
			} else 	if (this.thumbnailElement.getElementsBySelector("li a")[(image*2)+1].getStyle('color') == "#ffffff"){
				this.thumbTransitionsByForce[image].start({'opacity': ['1','0.5']});
				this.thumbnailElement.getElements("li")[image].setStyles({'border':'none','background-color':'transparent','background-image':'none'});
				this.anchorTransitionsByForce[image].start({'color':['#fff','#ddd']});
//				this.synopsisTransitionsByForce[image].start({'color':['#ddd','#333']});
				if (this.zoomOption == "zoom") {
					this.thumbTransitionsByForce[image].start({'font-size':['16px','12px']});
				}
			}
		}
		this.fadeThat(this.imgElement,this.transImgElement,100);
		setTimeout(function(){carouselRef.transitionInProgress = false;},5);
		setTimeout(function(){carouselRef.tidyUp()},5);
		carousel_timer = setInterval(function(){carouselRef.rotate_carousel(carouselRef.thumbnailElement,carouselRef.imgElement,carouselRef.transImgElement,carouselRef.zoomOption)},5000);
	},
	rotate_carousel:function(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)
	{
		for (image=0; image < this.carousel_pics.length; image++)
		{
			if (this.thumbnailElement.getElementsBySelector("li img")[image].headlineIndex == this.currentHeadline) {
				this.thumbTransitions[image].start({'opacity': ['1','0.5']});
				this.thumbnailElement.getElements("li")[image].setStyles({'border':'none','background-color':'transparent','background-image':'none'});
				this.anchorTransitions[image].start({'color':['#fff','#ddd']});
//				this.synopsisTransitions[image].start({'color':['#ddd','#333']});
				if (this.zoomOption == "zoom") {
					this.thumbTransitions[image].start({'font-size':['16px','12px']});
				}
				this.transImgElement.setProperties({'src':this.arrImages[image].src});
				this.transImgElement.setStyles({'opacity':1, 'filter':'alpha(opacity=100)'});
			}
			clearInterval(carousel_timer);
			carousel_timer = setInterval(function(){carouselRef.rotate_carousel(carouselRef.thumbnailElement,carouselRef.imgElement,carouselRef.transImgElement,carouselRef.zoomOption)},5000);
		}
		this.transImgElement.setStyle("z-index", "110");
		this.currentHeadline++;
		if (this.currentHeadline > this.carousel_pics.length - 1) { this.currentHeadline = 0; }

		this.thumbTransitions[this.currentHeadline].set({'opacity': ['0.5','1']});
		this.thumbnailElement.getElements("li")[this.currentHeadline].setStyles({'border':'1px solid #06a','background-color':'#06a','background-image':'url(\'/default/img/carousel/carousel-li-bg.gif\')'});
		this.anchorTransitions[this.currentHeadline].start({'color':['#ddd','#fff']});
//		this.synopsisTransitions[this.currentHeadline].start({'color':['#333','#ddd']});
		if (this.zoomOption == "zoom") {
			this.thumbTransitions[this.currentHeadline].start({'font-size':['12px','16px']});
		}
		this.transImgElement.src = this.imgElement.src;
		this.imgElement.src = this.arrImages[this.currentHeadline].src;
		this.transImgElement.setStyles({'opacity':"1",'filter':'alpha(opacity=100)'});
		this.fadeThat(this.imgElement, this.transImgElement, 100);
		setTimeout(function(){carouselRef.imgElement.src = carouselRef.carousel_pics[carouselRef.currentHeadline]},30);
		setTimeout(function(){carouselRef.tidyUp()},500);
	},
	fadeThat:function(imgElementName,transImgElementName, opacity)
	{
		this.transitionImg.start({'opacity': ['1','0']});
		setTimeout(function(){
			carouselRef.transImgElement.src = carouselRef.imgElement.src;
		},1000);
	},
	tidyUp:function()
	{
		for (image=0; image < this.carousel_pics.length; image++)
		{
			if (this.thumbnailElement.getElementsBySelector("li img")[image].headlineIndex != this.currentHeadline) {
				this.thumbTransitions[image].set({'opacity':'0.5'});
				this.thumbnailElement.getElements("li")[image].setStyles({'border':'none','background-color':'transparent','background-image':'none'});
				this.anchorTransitions[image].set({'color':'#ddd'});
//				this.synopsisTransitions[image].set({'color':'#333'});
				if (this.zoomOption == "zoom") {
					this.thumbTransitions[image].set({'font-size':'12px'});
				}
			}
		}		
	}
}
