if (document.getElementById) // || FUTURE_GLOBAL_PAGEJS == 'home')
{
	function carousel_setup(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)
	{
		var carousel_thumbs = new Array;
		var carousel_pics = new Array;
		// Create arrays of pics and thumbnails using the existing thumbnail sources.
		var thumbPics = $$("#"+thumbnailElementName+" img");
		for (i = 0; i < thumbPics.length; i++)
		{
			carousel_thumbs[i] = thumbPics[i].src;
			var thumbSize = new RegExp("-45-80", "g");
			carousel_pics[i] = carousel_thumbs[i].replace(thumbSize,"-400-85");
		}
		$(transImgElementName).src = carousel_pics[1];
		imageObj = new Array;
		for (image=0; image < carousel_pics.length; image++)
		{
			imageObj[image] = new Image();
			imageObj[image].src = carousel_pics[image];
			imageObj[image].iter = image;
			imageObj[image].onerror = function(){
				this.src = "/default/img/spacer.gif";
				$$("#"+thumbnailElementName+" img")[this.iter].src = this.src;
				carousel_pics[this.iter] = this.src;
			}
		}

		var gimmeThoseLinks = $$("#"+thumbnailElementName+" li .carousel_link"); /* .carousel_link is the second link of the list item. */
		var gimmeThoseSynopses = $$("#"+thumbnailElementName+" li .carousel_link p"); /* .carousel_link is the second link of the list item. */
		/* I don't know why this is done. */
		gimmeThoseLinks[0].setStyle('color','#ffffff');
		gimmeThoseSynopses[0].setStyle('color','#dddddd');
		gimmeThoseLinks[0].addClass('carousel_link');

		if (useZoomOption == "zoom") { styleShift(document.getElementById(thumbnailElementName).getElementsByTagName("li")[0],"fontSize","x","12","16",5,0,25); }
		carousel_timer = setInterval(function(){rotate_carousel(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)},5000);
		// Time to load the images.
		// Lets do some tidying up here, just in case.
		carousel_pics = null;
		carousel_thumbs = null;
		thumbPics = null;
		gimmeThoseLinks = null;
	}

	function carousel_force(clickedImage,thumbnailElementName,imgElementName,transImgElementName,useZoomOption,immediateSwitch)
	{
		if ($(imgElementName).src != $(transImgElementName).src) {
			if (immediateSwitch == "now") {
				$(transImgElementName).src = $(imgElementName).src;
			} else { return false; }
		}
		// Prevents image switches happening if there's a transition already in progress,
		// unless we've insisted on an immediate switch.
		var carousel_thumbs = new Array;
		var carousel_pics = new Array;
		var thumbPics = $$("#"+thumbnailElementName+" img");
		for (i = 0; i < thumbPics.length; i++)
		{
			carousel_thumbs[i] = thumbPics[i].src;
			var thumbSize = new RegExp("-45-80", "g");
			carousel_pics[i] = carousel_thumbs[i].replace(thumbSize,"-400-85");
			if ($(imgElementName).src == clickedImage.src) {
				clearInterval(carousel_timer);
				carousel_timer = setInterval(function(){rotate_carousel(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)},5000);
				return false;
			}
		}
		clearInterval(carousel_timer);
		for (image=0; image < carousel_pics.length; image++)
		{
			//var colorString = new RegExp(" ", "g");
			$$("#"+thumbnailElementName+" li img")[image].className = "";
			$$("#"+thumbnailElementName+" li a")[image*2+1].addClass("carousel_link");
			var theThumbnail = new Fx.Styles($$("#"+thumbnailElementName+" li img")[image], {duration: (immediateSwitch == "now" ? 1 : 500), transition: Fx.Transitions.Quad.easeOut});
			var theAnchor = new Fx.Styles($$("#"+thumbnailElementName+" li a")[(image*2)+1], {duration: (immediateSwitch == "now" ? 1 : 500), transition: Fx.Transitions.Quad.easeOut});
			var theSynopsis = new Fx.Styles($$("#"+thumbnailElementName+" li a p")[image], {duration: (immediateSwitch == "now" ? 1 : 500), transition: Fx.Transitions.Quad.easeOut});
			if (carousel_thumbs[image] == clickedImage.src)
			{
				theThumbnail.start({'opacity': ['0.5','1']});
				$$("#"+thumbnailElementName+" li")[image].setStyles({'border-color':'#06a','background-color':'#06a','background-image':'url(\'/default/img/carousel/carousel-li-bg.gif\')'});
				theAnchor.start({'color':['#000','#fff']});
				theSynopsis.start({'color':['#333','#ddd']});
				if (useZoomOption == "zoom") {
					theThumbnail.start({'font-size':['12px','16px']});
				}

				$(transImgElementName).src = $(imgElementName).src;
				$(transImgElementName).setStyles({'z-index':110, 'opacity':1,'filter':'alpha(opacity=100)'});
				document.tmpImageVar = imageObj[image].src;
				setTimeout(function(){$(imgElementName).src = document.tmpImageVar;}, 50);
				$$("#"+thumbnailElementName+" li a")[(image*2)+1].addClass("carousel_link");
			} else 	if ($$("#"+thumbnailElementName+" li a")[(image*2)+1].getStyle('color') == "#ffffff"){
				theThumbnail.start({'opacity': ['1','0.5']});
				$$("#"+thumbnailElementName+" li")[image].setStyles({'border-color':'#fff','background-color':'#fff','background-image':'none'});
				theAnchor.start({'color':['#fff','#000']});
				theSynopsis.start({'color':['#ddd','#333']});
				if (useZoomOption == "zoom") {
					theThumbnail.start({'font-size':['16px','12px']});
				}
			}
			theThumbnail = null;
			theListItem = null;
			theAnchor = null;
			theSynopsis = null;
		}
		fadeThat(imgElementName,transImgElementName,100);
		carousel_timer = setInterval(function(){rotate_carousel(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)},5000);
		// Time to load the images.
	}

	function rotate_carousel(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)
	{
		var carousel_pics = new Array;
		var thumbSize = new RegExp("-45-80", "g");
		for (i = 0; i < document.getElementById(thumbnailElementName).getElementsByTagName("li").length; i++)
		{
			carousel_pics[i] = document.getElementById(thumbnailElementName).getElementsByTagName("li")[i].getElementsByTagName("img")[0].src.replace(thumbSize,"-400-85");
		}
		carousel_counter = -1;
		for (image=0; image < carousel_pics.length; image++)
		{
			var theThumbnail = new Fx.Styles($$("#"+thumbnailElementName+" li img")[image], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
			var theAnchor = new Fx.Styles($$("#"+thumbnailElementName+" li a")[(image*2)+1], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
			var theSynopsis = new Fx.Styles($$("#"+thumbnailElementName+" li a p")[image], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
			//var colorString = new RegExp(" ", "g");
			if ($$("#"+thumbnailElementName+" li a")[image*2+1].getStyle('color') == "#ffffff") {
				theThumbnail.start({'opacity': ['1','0.5']});
				$$("#"+thumbnailElementName+" li")[image].setStyles({'border-color':'white','background-color':'#fff','background-image':'none'});
				theAnchor.start({'color':['#fff','#000']});
				theSynopsis.start({'color':['#ddd','#333']});
				if (useZoomOption == "zoom") {
					theThumbnail.start({'font-size':['16px','12px']});
				}
			}
			if (carousel_pics[image] == $(imgElementName).src)
			{
				carousel_counter = image + 1;
				$(transImgElementName).setProperties({'src':imageObj[image].src, 'imgCounter':image});
				$(transImgElementName).setStyles({'opacity':1, 'filter':'alpha(opacity=100)'});
			}
			theThumbnail = null;
			theListItem = null;
			theAnchor = null;
			theSynopsis = null;
		}
		if (carousel_counter < 0)
		{
			if (document.getElementById(transImgElementName).imgCounter == null) { document.getElementById(transImgElementName).imgCounter = 0; }
			$(imgElementName).src = imageObj[1].src;
			$(transImgElementName).src = imageObj[0].src;
			$(transImgElementName).setStyles({'opacity':"1",'filter':'alpha(opacity=100)'});
			clearInterval(carousel_timer);
			carousel_timer = setInterval(function(){rotate_carousel(thumbnailElementName,imgElementName,transImgElementName,useZoomOption)},5000);
			return true;
			// If the main background doesn't match any of the thumbs, we'll force the damn thing.
		}
		$(transImgElementName).style.zIndex = "110";
		if (carousel_counter > carousel_pics.length - 1) { carousel_counter = 0; }
		fadeThat(imgElementName, transImgElementName, 100);
		setTimeout(function(){$(imgElementName).src = carousel_pics[carousel_counter]},30);

		var theThumbnail = new Fx.Styles($$("#"+thumbnailElementName+" li img")[carousel_counter], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
		var theAnchor = new Fx.Styles($$("#"+thumbnailElementName+" li a")[(carousel_counter*2)+1], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
		var theSynopsis = new Fx.Styles($$("#"+thumbnailElementName+" li a p")[carousel_counter], {duration: 500, transition: Fx.Transitions.Quad.easeOut});
		theThumbnail.set({'opacity': ['0.5','1']});
		$$("#"+thumbnailElementName+" li")[carousel_counter].setStyles({'border-color':'#06a','background-color':'#06a','background-image':'url(\'/default/img/carousel/carousel-li-bg.gif\')'});
		theAnchor.start({'color':['#000','#fff']});
		theSynopsis.start({'color':['#333','#ddd']});
		if (useZoomOption == "zoom") {
			theThumbnail.start({'font-size':['12px','16px']});
		}
	}

	function fadeThat(imgElementName,transImgElementName, opacity)
	{
//		$("carousel_image").setStyle('z-index',50);
		var transitionImg = new Fx.Styles($(transImgElementName), {duration: 500, transition: Fx.Transitions.Cubic.easeIn});
		transitionImg.start({'opacity': ['1','0']});
		setTimeout(function(){
			$(transImgElementName).src = $(imgElementName).src;
//			$("carousel_image").setStyle('z-index',90);
		},500);
	}
}
addWindowOnLoadEvent(attachNewWindowEvent);
