﻿if (typeof WTC === 'undefined' || !WTC) {
	var WTC = {};
}
if (!WTC.homeMasthead) {
	WTC.homeMasthead = (function() {
		var views = [], prevIndex = 0, curIndex = 0, opacity = 0, pause = false;
		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.useMap = image0.useMap;
			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, bgDiv, i, l;
			vp = document.getElementById("mastheadViews");
			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) {
						if (vp.insertBefore) {
							bgDiv = document.createElement('div');
							bgDiv.className = 'view';
							bgDiv.style.backgroundColor = '#000';
							bgDiv.style.display = 'block';
							bgDiv.style.zIndex = '1';
							vp.insertBefore(bgDiv, div);
						}
						addView(div, img, fileName);
						if (MakinHey.array.remove(homeMasthead, fileName)) {
							MakinHey.array.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) {
			MakinHey.event.addListener(window, 'load', load);
		}
		return true;
	} ());
}
if (!WTC.homeSponsor) {
	WTC.homeSponsor = (function() {
		var views = [], pause = false;
		function addView(div, img) {
			views[views.length] = {
				'div': div,
				'img': img
			};
		}
		function scroll() {
			var index = 0, view0 = views[0], view1, vpHt = 150, vTop = 0, next = 0;
			function s() {
				if (!pause) {
					if (vTop === next) {
						vTop = vpHt;
						if (view1) {
							view1.div.style.top = vpHt + "px";
						}
						view1 = view0;
						index = (index + 1) % views.length;
						view0 = views[index];
					}
					vTop -= 1;
					view0.div.style.top = vTop + "px";
					if (view1) {
						view1.div.style.top = (vTop + next - vpHt) + "px";
					}
				}
				setTimeout(s, 40);
			}
			s();
		}
		function start() {
			var i, l;
			for (i = 0, l = views.length; i < l; i++) {
				if (!views[i].img.complete) {
					setTimeout(start, 100);
					return;
				}
			}
			scroll();
		}
		function load() {
			var vp, div, a, img, href, alt, src, fileName, i, l;
			vp = document.getElementById('contentMainSponsorsVP');
			if (vp) {
				div = vp.getElementsByTagName('div');
				a = vp.getElementsByTagName('a');
				img = vp.getElementsByTagName('img');
				if (div && div.length === 1 && a && a.length === 1 && img && img.length === 1) {
					div = div[0];
					a = a[0];
					img = img[0];
					href = a.href;
					alt = img.alt;
					src = img.src;
					fileName = MakinHey.path.getFileName(src);
					if (fileName) {
						addView(div, img);
						if (MakinHey.array.remove(homeSponsor, fileName)) {
							MakinHey.array.shuffle(homeSponsor);
							for (i = 0, l = homeSponsor.length; i < l; i++) {
								div = document.createElement('div');
								div.className = 'view';
								a = document.createElement('a');
								a.href = href;
								img = document.createElement('img');
								img.alt = alt;
								img.src = src.replace(fileName, homeSponsor[i]);
								addView(div, img);
								vp.appendChild(div);
								div.appendChild(a);
								a.appendChild(img);
							}
							vp.onmouseout = function() { pause = false; };
							vp.onmouseover = function() { pause = true; };
							start();
						}
					}
				}
			}
		}
		if (window.homeSponsor && homeSponsor.length) {
			MakinHey.event.addListener(window, 'load', load);
		}
		return true;
	} ());
}
