﻿if (typeof CCF === 'undefined' || !CCF) {
	var CCF = {};
}
if (!CCF.home) {
	CCF.home = (function() {
		var views = [], prevIndex = 0, curIndex = 0, opacity = 0, pause = false,
		add = (function() {
			if (window.addEventListener) { return function(o, t, f) { o.addEventListener(t, f, false); }; }
			else if (window.attachEvent) { return function(o, t, f) { o.attachEvent('on' + t, f); }; }
			else { return function() { }; }
		} ()),
		remove = (function() {
			if (window.removeEventListener) { return function(o, t, f) { o.removeEventListener(t, f, false); }; }
			else if (window.detachEvent) { return function(o, t, f) { o.detachEvent('on' + t, f); }; }
			else { return function() { }; }
		} ());
		function addView(div, img, name) {
			views[views.length] = {
				'div': div,
				'img': img,
				'name': name
			};
		}
		function getFileName(path) {
			if (path) {
				var i = path.lastIndexOf('/');
				if (i >= 0) {
					path = path.substring(i + 1);
				}
			}
			return path;
		}
		function removeValue(a, v) {
			var i, l;
			for (i = 0, l = a.length; i < l; i++) {
				if (v === a[i]) {
					a.splice(i, 1);
					return true;
				}
			}
			return false;
		}
		function shuffle(a) {
			var i, n = a.length, t;
			while (n > 1) {
				i = Math.floor(Math.random() * n--);
				t = a[i];
				a[i] = a[n];
				a[n] = t;
			}
		}
		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;
			remove(window, 'load', load);
			vp = document.getElementById("mastheadViews");
			div = document.getElementById("mastheadView0Div");
			img = document.getElementById("mastheadView0Img");
			if (vp && div && img) {
				fileName = getFileName(img.src);
				if (fileName) {
					addView(div, img, fileName);
					if (removeValue(homeMasthead, fileName)) {
						shuffle(homeMasthead);
						for (i = 0, l = homeMasthead.length; i < l; i++) {
							div = document.createElement('div');
							div.className = 'view';
							addView(div, false, homeMasthead[i]);
							vp.appendChild(div);
						}
						vp.onmouseout = function() {
							pause = false;
						};
						vp.onmouseover = function() {
							pause = true;
						};
						setTimeout(startTransition, 3000);
					}
				}
			}
		}
		if (window.homeMasthead && homeMasthead.length) {
			add(window, 'load', load);
		}
		return true;
	} ());
}