/**
 * Meet Ruth script
 * @author Derek Beumer <derek@thoughtstack.net>
 * @copyright Copyright (c) 2008, Derek Beumer
 * @version $Id$
 * @package lahmayer
 */

window.initMe = function (fnc) {
	if (typeof fnc == 'function') {
		// Optimally use addEventListener, which does our job for us but isn't universally supported
		if (window.addEventListener) {
			window.addEventListener('load', fnc, false);
		// Attempt to use the IE-proprietary attachEvent method alternately
		} else if (window.attachEvent) {
			window.attachEvent('onload', fnc);
		// Finally, use the standard window.onload with some hooking to preserve any previously assigned functionality
		} else {
			if (window.onload != null) {
				var onloadHook = window.onload;
				window.onload = function (e) {
					onloadHook(e);
					fnc();
				};
			} else {
				window.onload = fnc;
			}
		}
	}
};

var meetRuth = {
	rotateImagePath: '/wp-content/themes/lahmayer/graphic/content/meet-ruth/slideshow/',
	rotateImageFiles: [
		'0.jpg',
		'1.jpg'
	],
	rotateImages: [],
	pe: null,
	curIndex: 0,
	init: function () {	
		$A(meetRuth.rotateImageFiles).each(function (value) {
			var img = new Element('img');
			img.src = meetRuth.rotateImagePath + value;
			img.addClassName('rotating');
			img.opacity = 0.0;
			img.style.opacity = 0.0;
			img.style.MozOpacity = 0.0;
			img.style.filter = 'alpha(opacity=' + 0 + ')';
			meetRuth.rotateImages.push(img);
			$('meet-ruth-photo').insert(img);
		});

		meetRuth.rotateImages[0].style.display = 'block';
		meetRuth.rotateImages[0].opacity = 1.0;
		
		setTimeout(meetRuth.rotate, 2000);

		//meetRuth.pe = new PeriodicalExecuter(meetRuth.rotate, 3);
	},
	rotate: function () {
		var curIndex = meetRuth.curIndex;
		var nextIndex = (meetRuth.curIndex >= (meetRuth.rotateImages.length - 1)) ? 0 : meetRuth.curIndex + 1;

		meetRuth.curImage = meetRuth.rotateImages[curIndex];
		meetRuth.nextImage = meetRuth.rotateImages[nextIndex];

		meetRuth.fade();
	},
	fade: function () {
		meetRuth.curImage.opacity -= 0.05;
		meetRuth.nextImage.opacity += 0.05;

		meetRuth.nextImage.style.opacity = meetRuth.nextImage.opacity;
		meetRuth.nextImage.style.MozOpacity = meetRuth.nextImage.opacity;
		meetRuth.nextImage.style.filter = 'alpha(opacity=' + (meetRuth.nextImage.opacity * 100) + ')';
		meetRuth.nextImage.style.display = 'block';

		if (meetRuth.curImage.opacity <= 0) {
			meetRuth.curImage.style.display = 'none';
			meetRuth.curIndex = (meetRuth.curIndex >= (meetRuth.rotateImages.length - 1)) ? 0 : meetRuth.curIndex + 1;
			setTimeout(meetRuth.rotate, 2000);
		} else {
			meetRuth.curImage.style.opacity = meetRuth.curImage.opacity;
			meetRuth.curImage.style.MozOpacity = meetRuth.curImage.opacity;
			meetRuth.curImage.style.filter = 'alpha(opacity=' + (meetRuth.curImage.opacity * 100) + ')';
			setTimeout(meetRuth.fade, 50);
		}
	}
};

initMe(meetRuth.init);
