if (!katani) var katani = {};

katani.home = function() {

	var dom = global.dom,
		event = global.event,
		imagedetaillist = [],
		imagedetailcount,
		imageholderel,
		nextimageseq = 0;

	function initnextimagedelay() { window.setTimeout(nextimage,6000); }

	function buildimagetween(el,initial) {

		return new dom.animation.tween(
			{
				el: el,
				css: 'opacity'
			},
			(initial) ? 1 : 0,
			(initial) ? 0 : 1,
			2
		);
	}

	function nextimagestarttween() {

		var imagedetail = imagedetaillist[nextimageseq];

		if (!imagedetail.tween) {
			// generate tween animation object for image
			imagedetail.tween = buildimagetween(imagedetail.el);
		}

		// start tween for next image (fade in)
		imagedetail.tween.start();

		// start tween for previous image (fade out)
		nextimageseq--;
		if (nextimageseq < 0) nextimageseq = imagedetailcount - 1;
		imagedetaillist[nextimageseq].tween.start();

		// set nextimageseq forward to the next image
		nextimageseq += 2;
		if (nextimageseq >= imagedetailcount) nextimageseq -= imagedetailcount;

		// setup delay until the next image transition
		initnextimagedelay();
	}

	function nextimage() {

		var imagedetail = imagedetaillist[nextimageseq];

		if (imagedetail.loaded) {
			// image already loaded, so just start the tween
			nextimagestarttween();
			return;
		}

		// insert new image into DOM, save against imagedetaillist, set loaded flag and wait for the image to load
		var imageel = dom.node('img',{ opacity: 0 });
		imageholderel.appendChild(imageel);
		imagedetail.el = imageel;
		imagedetail.loaded = true;

		// attach onload event to image element and set image source
		// once image has loaded into browser cache nextimagestarttween() will be called and animation will start
		// ensure to attach handler before setting source for Safari to fire event correctly
		event.add(imageel,'load',nextimagestarttween);
		imageel.src = imagedetail.src;
	}

	function init() {

		var homeimage = $('homeimage');
		if (!homeimage || !dom.isnodecomplete(homeimage)) {
			// reset timeout to try again later
			window.setTimeout(init,20);
			return;
		}

		var initialimg = homeimage.getElementsByTagName('img')[0],
			imagepathregexp = /img\-([^ ]+)( |$)/g,
			imagedef = homeimage.className,
			match;

		// save image holder node (will be an <a> anchor node) where we will appendChild() new <img /> elements as they are required
		imageholderel = initialimg.parentNode;

		while (true) {
			match = imagepathregexp.exec(imagedef);
			if (!match) break;

			imagedetaillist[imagedetaillist.length] = { src: '/img/home/' + match[1] + '.jpg' };
		}

		// if no 'turn to' images then bail out
		if (!imagedetaillist.length) return;

		// save initial DOM image details into imagedetaillist and create a tween for it
		imagedetaillist[imagedetaillist.length] = {
			loaded: true,
			tween: buildimagetween(initialimg,true)
		};

		// save final image rotation count
		imagedetailcount = imagedetaillist.length;

		// delay for the first image transition
		initnextimagedelay();
	}

	function ie7orlower() {

		var match;
		if (match = /MSIE (\d+)\./.exec(navigator.appVersion)) {
			if (match[1] <= 7) return true;
		}

		return false;
	}

	// don't enable rotator for IE7 or lower, since the opacity on images overlap the drop down menus
	if (!ie7orlower()) init();
}();
