(function() {
	var views = [], prevIndex = 0, curIndex = 0, opacity = 0, pause = false, images;
	images = ['masthead1.jpg', 'masthead2.jpg', 'masthead3.jpg', 'masthead4.jpg', 'masthead5.jpg', 'masthead6.jpg'];
	function addView(div, img, name) {
		views[views.length] = {
			'div': div,
			'img': img,
			'name': name
		};
	}
	function updateAlpha() {
		if (opacity < 100) {
			opacity += 10;
			views[curIndex].div.style.filter = "alpha(opacity=" + opacity + ")";
			setTimeout(updateAlpha, 100);
		}
		else {
			views[curIndex].div.style.filter = "alpha(opacity=100)";
			finishTransition();
		}
	}
	function updateOpacity() {
		if (opacity < 1) {
			opacity += 0.1;
			views[curIndex].div.style.opacity = opacity;
			setTimeout(updateOpacity, 100);
		}
		else {
			views[curIndex].div.style.opacity = 1;
			finishTransition();
		}
	}
	function transitionImage() {
		opacity = 0;
		views[prevIndex].div.style.zIndex = '1';
		if (views[curIndex].div.filters) {
			updateAlpha();
		}
		else {
			updateOpacity();
		}
		views[curIndex].div.style.display = 'block';
		views[curIndex].div.style.zIndex = '2';
	}
	function displayImage() {
		var image, image0 = views[0].img;
		image = document.createElement('img');
		views[curIndex].img = image;
		image.onload = function() {
			image.onload = null;
			views[curIndex].div.appendChild(image);
			transitionImage();
		};
		image.width = image0.width;
		image.height = image0.height;
		image.alt = image0.alt;
		image.src = image0.src.replace(views[0].name, views[curIndex].name);
	}
	function startTransition() {
		if (pause) {
			setTimeout(startTransition, 100);
		}
		else {
			prevIndex = curIndex;
			curIndex = (curIndex + 1) % views.length;
			if (views[curIndex].img) {
				transitionImage();
			}
			else {
				displayImage();
			}
		}
	}
	function finishTransition() {
		views[prevIndex].div.style.display = 'none';
		views[prevIndex].div.style.zIndex = '0';
		setTimeout(startTransition, 3000);
	}
	function load() {
		var vp, div, img, fileName, i, l;
		vp = document.getElementById("homeMastheadViews");
		if (vp && vp.getElementsByTagName) {
			div = vp.getElementsByTagName('div');
			img = vp.getElementsByTagName('img');
			if (div && div.length === 1 && img && img.length === 1) {
				div = div[0];
				img = img[0];
				fileName = MakinHey.path.getFileName(img.src);
				if (fileName) {
					addView(div, img, fileName);
					if (MakinHey.array.remove(images, fileName)) {
						MakinHey.array.shuffle(images);
						for (i = 0, l = images.length; i < l; i++) {
							div = document.createElement('div');
							div.className = 'view';
							addView(div, false, images[i]);
							vp.appendChild(div);
						}
						vp.onmouseout = function() { pause = false; };
						vp.onmouseover = function() { pause = true; };
						setTimeout(startTransition, 3000);
					}
				}
			}
		}
	}
	if (document.getElementById) {
		MakinHey.event.addListener(window, 'load', load);
	}
} ());
