if (!fusionsets) var fusionsets = {};

fusionsets.home = function() {

	var dom = global.dom,
		anim = dom.animation;

	function animatorhandler(containerel) {

		var obj = {},

			// animatorenabled will be set true if more than one item to animate
			animatorenabled,

			// tween objects
			tweentexton,
			tweentextoff,
			tweenimagemask,

			// text/image items
			textellist,
			imageellist,

			// current/next image/text counters and image mask node (from white to transparent)
			textnumcurrent = 0,
			textnumnext,
			imagenumcurrent = 0,
			imagenumnext,
			imagemaskel,

			// callback functions
			handlerpartcomplete,
			handlerpartcompletecalled,
			handlercomplete;

		obj.nextitem = function() {

			if (!animatorenabled) return;

			// setup text transition
			textnumnext = textnumcurrent + 1;
			textnumnext = (textnumnext == textellist.length) ? 0 : textnumnext;

			// show next text and set zero opacity
			var nexttextel = textellist[textnumnext];
			nexttextel.className = 'show';
			setopacity(nexttextel,0);

			// setup image transition
			imagenumnext = imagenumcurrent + 1;
			imagenumnext = (imagenumnext == imageellist.length) ? 0 : imagenumnext;

			// show next image and hide current one
			imageellist[imagenumnext].className = 'show';
			imageellist[imagenumcurrent].className = '';

			// start text tweens
			tweentexton.reset();
			tweentexton.el = nexttextel;
			tweentexton.start();

			tweentextoff.reset();
			tweentextoff.el = textellist[textnumcurrent];
			tweentextoff.start();

			// start image mask tween
			imagemaskel.className = 'mask';
			setopacity(imagemaskel,1);
			tweenimagemask.reset();
			tweenimagemask.start();

			handlerpartcompletecalled = false;
		};

		obj.setcallback = function(partcomplete,complete) {

			handlerpartcomplete = partcomplete;
			handlercomplete = complete;
		};

		function nextitemstep(tween) {

			// if 'part complete' handler already called for animation, or opacity still not half way - exit
			if (handlerpartcompletecalled || (tween.value < 0.5)) return;

			// flip 'part complete' called flag
			handlerpartcompletecalled = true;
			if (handlerpartcomplete) handlerpartcomplete();
		}

		function nextitemcomplete() {

			// hide previous text and image mask
			textellist[textnumcurrent].className = '';
			imagemaskel.className = 'maskoff';

			// update current text/image numbers
			textnumcurrent = textnumnext;
			imagenumcurrent = imagenumnext;

			// call animator complete handler
			if (handlercomplete) handlercomplete();
		}

		function init() {

			// find all list text segments and images
			textellist = getelementsbytagname(containerel,'li');
			imageellist = getelementsbytagname(containerel,'img');

			// if only one (or less items found) then dont animate and exit now
			if ((textellist.length <= 1) || (imageellist.length <= 1)) return;

			// create image mask node and place in with images
			imagemaskel = dom.node('div',{ 'class': 'maskoff' });
			getelementsbytagname(containerel,'a')[1].appendChild(imagemaskel);

			// setup tweens
			tweentexton = new anim.tween({ css: 'opacity',handler: nextitemstep,finish: nextitemcomplete },0,1,1);
			tweentextoff = new anim.tween({ css: 'opacity' },1,0,1);
			tweenimagemask = new anim.tween({ el: imagemaskel,css: 'opacity' },1,0,1);

			// enable animator
			animatorenabled = true;
		}

		init();

		return obj;
	}

	function setopacity(el,opacity) { dom.setopacity(el,opacity); }
	function getelementsbytagname(el,tag) { return el.getElementsByTagName(tag); }

	function init() {

		if (!$('footer')) {
			// reset timeout to try again later
			window.setTimeout(init,20);
			return;
		}

		// fetch all top level <div> nodes under '#homecasestudy'
		var casestudyel = $('homecasestudy');
			divellist = getelementsbytagname(casestudyel,'div'),
			animatorhandlerlist = [];

		function startanimationsequence() { window.setTimeout(function() { animatorhandlerlist[0].nextitem(); },4000); }

		for (var i = 0,j = divellist.length;i < j;i++) {
			var divel = divellist[i];
			if (divel.parentNode != casestudyel) continue;

			// create animatorhandler() for panel
			animatorhandlerlist[animatorhandlerlist.length] = animatorhandler(divel);
		}

		// with animatorhandlers created, setup the chaining of animations and kick off first animation
		animatorhandlerlist[0].setcallback(function() { animatorhandlerlist[1].nextitem(); },startanimationsequence);
		animatorhandlerlist[1].setcallback(function() { animatorhandlerlist[2].nextitem(); });

		startanimationsequence();
	}

	init();
}();
