window.addEvent('domready', function() {

	// Locate the banner to show the case studies in.
	var caseStudyBanner = $('top_box');
	if (!caseStudyBanner) return;
	
	// Locate the box to show case study captions in.
	var caseStudyBannerCaption = $('case_study_banner_caption');
	if (!caseStudyBannerCaption) return;
	
	// Locate the link for case studies.
	var caseStudyBannerLink = $('case_study_banner_link');
	if (!caseStudyBannerLink) return;
	
	// Locate the title for case studies.
	var caseStudyBannerTitle = $('case_study_banner_title');
	if (!caseStudyBannerTitle) return;

	// Fix the sliding caption in IE6.
	if (Browser.Engine.trident && parseFloat(/MSIE ([0-9\.]+)/.exec(navigator.appVersion)[1]) < 7) {
		var oldBackgroundImage = /^url\(['"]?(.*?)['"]?\)$/.exec(caseStudyBannerCaption.getStyle('background-image'))
		caseStudyBannerCaption.setStyle('background-image', caseStudyBannerCaption.getStyle('background-image').replace(/\.png/, '.gif'));
		caseStudyBannerCaption.setStyle('margin-right', '-1px'); // IE6 draws the box one pixel too far to the right.
	}
	
	// Request the banner images.
	new Request.JSON({
		url : Uri.create('/services/pexhurst/case_study_banner.php', { start_image : /^url\(['"]?(.*?)['"]?\)$/.exec(caseStudyBanner.getStyle('background-image'))[1] }),
		onSuccess : function(o, t) {
			
			// Convert the returned object into an array.
			var caseStudies = [];
			for (var id in o.case_studies) {
				var caseStudy = o.case_studies[id];
				if (typeof caseStudy == 'object' && /^[\d]+$/.test(id)) {
					caseStudy.id = id;
					caseStudies.push(caseStudy);
				}
			}
			
			var tweenDuration = 1000;
			
			// Load the images.
			var caseStudyImagePaths = [];
			caseStudies.each(function(caseStudy) {
				caseStudyImagePaths.push(caseStudy.image);
			});
			var caseStudyImages = new Asset.images(caseStudyImagePaths, {
				onComplete : function() {
					// Inject the images to the page after setting their styles appropriately.
					for (var i = 0; i < caseStudyImages.length; ++i) {
						var caseStudyImage = caseStudyImages[i];
						caseStudyImage.setStyles({
							position : 'absolute',
							opacity : 0,
							'z-index' : 0,
							width : caseStudyBanner.getStyle('width'),
							height : caseStudyBanner.getStyle('height')
						});
						caseStudyImage.inject(caseStudyBanner, 'bottom');
						caseStudies[i].imageTween = new Fx.Tween(caseStudyImage, { duration : tweenDuration, link : 'cancel' });
					}
					// Generate objects to perform animation tweening.
					var caseStudyBannerCaptionTween = new Fx.Tween(caseStudyBannerCaption, { duration : tweenDuration, link : 'cancel' });
					// Start the banner cycling.
					o.start_index = 10;
					var caseStudyIndex = (o.start_index + caseStudies.length - 1) % caseStudies.length;
					var advanceNextCaseStudy = function() {
						// Slide out the caption box.
						caseStudyBannerCaptionTween.start('right', '-' + caseStudyBannerCaption.getCoordinates().width + 'px');
						// Fade out the current image.
						caseStudies[caseStudyIndex].imageTween.start('opacity', 0);
						caseStudyImages[caseStudyIndex].setStyle('z-index', 0);
						// Fade in the next image.
						caseStudyIndex = (caseStudyIndex + 1) % caseStudies.length;
						caseStudyImages[caseStudyIndex].setStyle('z-index', 1);
						caseStudies[caseStudyIndex].imageTween.start('opacity', 1).chain(function() {
							// We've faded in the next image, so slide in the caption box.
							caseStudyBannerCaption.setStyle('right', '-633px');
							if (caseStudies[caseStudyIndex].name || caseStudies[caseStudyIndex].title) {
								caseStudyBannerCaption.setStyle('visibility', 'visible');
							} else {
								caseStudyBannerCaption.setStyle('visibility', 'hidden');
							}
							caseStudyBannerTitle.set('text', caseStudies[caseStudyIndex].title);
							caseStudyBannerLink.set('text', caseStudies[caseStudyIndex].name);
							var caseStudyBannerWidth = caseStudyBannerCaption.getCoordinates().width;
							caseStudyBannerCaption.setStyle('right', '-' + caseStudyBannerWidth + 'px');
							caseStudyBannerLink.set('href', caseStudies[caseStudyIndex].href);
							caseStudyBannerCaptionTween.start('right', 0);
						});
					}
					advanceNextCaseStudy();
					if (caseStudies.length > 1) advanceNextCaseStudy.periodical(5000);
					// Make the entire banner clickable.
					caseStudyBanner.addEvent('click', function(e) {
						e.stop();
						window.location.href = caseStudies[caseStudyIndex].href;
					});
					caseStudyBanner.setStyle('cursor', 'pointer');
				}
			});			
		}
	}).get();
});
