ImageRotator = Behavior.create({
	initialize: function(images, options) {
		this.images = images;
		this.imageLoadingCounter = 0;
		this.preloadImages();
		this.index = -1;
		this.imageTag = this.element.down('img');
		this.quote = this.element.down('blockquote');
		var oldOpacity = this.quote.getOpacity();
		this.quote.hide().appear({duration: 3, to: oldOpacity});
		this.options = Object.extend({duration: 6, frequency: 18, firstRotate: 10}, options);
	},
	
	preloadImages: function() {
		this.loadedImages = [];
		this.images.each(function(image) {
			var loader = new Image();
			loader.onload = this.startRotation.bindAsEventListener(this);
			loader.src = image.image;
			this.loadedImages.push(loader);
		}, this);
	},
	
	startRotation: function() {
		this.imageLoadingCounter++;
		if (this.imageLoadingCounter != this.images.size()) return;
		this.element.setStyle({backgroundRepeat: 'no-repeat', backgroundPosition: 'left top', backgroundColor: 'transparent'});
		this.setBackground(0);
		this.firstRotate = true;
		this.executor = new PeriodicalExecuter(this.rotate.bind(this), this.options.firstRotate);
	},
	
	setBackground: function(index) {
		this.element.setStyle({backgroundImage: 'url(' + this.loadedImages[index].src + ')'});
	},
	
	nextIndex: function() {
		return (this.index + 1)%this.images.length;
	},
	
	rotate: function() {
		if (this.firstRotate) {
			this.executor.stop();
			this.firstRotate = false;
			this.executor = new PeriodicalExecuter(this.rotate.bind(this), this.options.frequency);
		}
		this.index = this.nextIndex();
		var current = this.images[this.index];
		this.imageTag.visualEffect('fade', {duration: this.options.duration, afterFinish: function() {
			this.imageTag.writeAttribute('src', this.loadedImages[this.index].src);
			this.imageTag.up('a').writeAttribute('href', current.path);
			this.imageTag.writeAttribute('alt', current.alt);
			this.imageTag.writeAttribute('title', current.alt);
			this.imageTag.show();
			this.setBackground(this.nextIndex());
		}.bind(this)});
		var oldOpacity = this.quote.getStyle('opacity');
		this.quote.visualEffect('fade', {duration: this.options.duration/2, afterFinish: function() {
			var q = this.quote.down('p.quote');
			var author = this.quote.down('p.author');
			q.innerHTML = current.quote;
			q.cite = current.path;
			author.replaceChild(document.createTextNode(current.author), author.firstChild);
			this.quote.visualEffect('appear', {to: oldOpacity, duration: this.options.duration/2});
		}.bind(this)});
	}
});

Event.addBehavior({
	'#customer_story_box_img': ImageRotator([{"author":"Jack Doughery, deCODEme customer","path":"/customer-stories/genetic-test-is-sensible-self-investment","quote":"\"deCODEme is an investment in your health&#46;&#46;&#46; I am so excited about what can happen if you do the right things and have the right tools\"","alt":"deCODEme customer Jack Doughery. deCODEme is a test that calculates genetic risk for numerous diseases and traits, ranging from Heart Attack and Diabetes to Alcohol Flush Reaction and Male Pattern Baldness","image":"/images/cms/frontpage/Jack_Doughery_deCODEme_customer.jpg"},{"author":"Dr.Stefansson on the Martha Stewart Show","path":"/discussing-genetic-risk-testing-on-martha-stewart","quote":"Martha Stewart and her team find out what lies within their DNA - deCODEme featured on Martha Stewart","alt":"Martha Stewart discusses genetic risk with deCODE genetics CEO, Kari Stefansson. deCODEme is a test that calculates genetic risk for numerous diseases and traits, ranging from Heart Attack and Diabetes to Alcohol Flush Reaction and Male Pattern Baldness","image":"/images/cms/frontpage/deCODEme_dr_Kari_stefansson_Martha_stewart_show.jpg"},{"author":"Anna Peterson, deCODEme customer","path":"/customer-stories/the-gift-of-knowledge-genetics-and-prevention","quote":"\"Empowered by a greater understanding&#46;&#46;&#46; I have become even more proactive about prevention\"","alt":"deCODEme customer Anna Peterson. deCODEme is a test that calculates genetic risk for numerous diseases and traits, ranging from Heart Attack and Diabetes to Alcohol Flush Reaction and Male Pattern Baldness","image":"/images/cms/frontpage/customer_story_front_page.jpg"},{"author":"Chuck Wallace, deCODEme customer","path":"/customer-stories/chuck-wallace-how-decodeme-helped-a-heart-patient-fight-prostate-cancer?autostart=true","quote":"\"...Dr. Bale had the foresight to suggest that we do run the genetic test. I think it very possibly could have saved my life...\"","alt":"deCODEme customer Chuck Wallace. deCODEme is a test that calculates genetic risk for numerous diseases and traits, ranging from Heart Attack and Diabetes to Alcohol Flush Reaction and Male Pattern Baldness","image":"/images/cms/frontpage/Chuck.jpg"},{"author":"Amy Doneen, Nurse Practitioner","path":"/customer-stories/the-path-to-prevention","quote":"\"Now we have the ability to test someone&#39;s genetic risk for certain disease states and then make clinical decisions based on that genetic backdrop\"","alt":"Amy Doneen, Nurse Practitioner. deCODEme is a test that calculates genetic risk for numerous diseases and traits, ranging from Heart Attack and Diabetes to Alcohol Flush Reaction and Male Pattern Baldness","image":"/images/cms/frontpage/amy_doneen_deCODEme_customer.jpg"}])
});