BIG = {
	peopleClearActive: function(new_top) {
		var a = document.getElementById('people-container').getElementsByTagName('*'),
			l = a.length, i = 0, actives = [],
			doClear = function() {
				l = actives.length;
				for(i = 0; i < l; i++) {
					actives[i].className = actives[i].className.replace(/ active/g, '');
					if(document.body.className.indexOf('css-transitions') == -1) {
						actives[i].style.filter = 'alpha(opacity=0)';
						actives[i].style.opacity = 0;
					}
				}
			};
		for(i = 0; i < l; i++) {
			if(a[i].nodeName.toLowerCase() == 'li') {
				if(a[i].className.indexOf('active') !== -1) {
					actives[actives.length] = a[i];
				}
				a[i].style.zIndex = 1;
			} else {
				a[i].className = a[i].className.replace(/ active/g, '');
			}
		}
		setTimeout(doClear, 500);
	},
	peopleSwap: function(e) {
		e = e || event;
		var t = e.target || e.srcElement, new_top,
			fadeIn = function() {
				if(new_top.style.opacity < 1) {
					new_top.style.opacity = ((new_top.style.opacity*100+20)/100);
					new_top.style.filter = 'alpha(opacity=' + (new_top.style.opacity*100) + ')';
					setTimeout(fadeIn, 10);
				} else {
					new_top.style.opacity = 1;
					new_top.style.filter = 'alpha(opacity=100)';
				}
			};
		e.returnValue = false;
		if(e.preventDefault) {
			e.preventDefault();
		}
		if(t.nodeName.toLowerCase() != 'a') {
			while(t.parentNode && t.nodeName.toLowerCase() != 'a') {
				t = t.parentNode;
			}
			if(t.nodeName.toLowerCase() != 'a') {
				return;
			}
		}
		if(t.className.indexOf(' active') !== -1) {
			return;
		}
		new_top = document.getElementById(t.getAttribute('data-for'));
		if(new_top) {
			BIG.peopleClearActive(new_top);
			new_top.style.zIndex = document.getElementById('the-people').getElementsByTagName('li').length;
			new_top.className += ' active';
			t.className += ' active';
			if(document.body.className.indexOf('css-transitions') == -1) {
				new_top.style.filter = 'alpha(opacity=0)';
				new_top.style.opacity = 0;
				fadeIn(new_top);
			}
		}
	},
	peopleInit: function() {
		var peeps = document.getElementById('the-people').getElementsByTagName('li'),
			link_cont = document.createElement('div'),
			all_cont = document.createElement('div'),
			l = peeps.length, i = 0, tmp;
		all_cont.id = 'people-container';
		link_cont.id = 'link-container';
		if(window.addEventListener) {
			link_cont.addEventListener('click', BIG.peopleSwap, null);
		} else {
			link_cont.attachEvent('onclick', BIG.peopleSwap);
		}
		var tmp2 = '';
		peeps[0].className = ' active';
		peeps[0].style.zIndex = l;
		for(i = 0; i < l; i++) {
			tmp = peeps[i].getElementsByTagName('img');
			if(tmp.length > 0) {
				tmp.href = "#";
				tmp = document.createElement('a');
				tmp.setAttribute('data-for', peeps[i].id);
				tmp.innerHTML = '<img src="' + peeps[i].getElementsByTagName('img')[0].src.replace(/(.*)\/(.*?)$/, '$1/th_$2') + '">';
				if(i == 0) {
					tmp.className = ' active';
				}
				link_cont.appendChild(tmp);
			}
		}
		for(i = i; i%5 != 0; i++) {
			link_cont.appendChild(document.createElement('span'));
		}
		all_cont.appendChild(link_cont);
		document.getElementById('the-people').parentNode.insertBefore(all_cont, document.getElementById('the-people'));
		all_cont.appendChild(document.getElementById('the-people').parentNode.removeChild(document.getElementById('the-people')));
	},
	slideShowGetCurrent: function() {
		var f = document.getElementById('featured'),
			a = f.getElementsByTagName('a'),
			l = a.length, i = 0;
		for(i = 0; i < l; i++) {
			if(a[i].className.indexOf('current') !== -1) {
				return i;
			}
		}
		return false;
	},
	slideShowDo: function() {
		var f = document.getElementById('featured'),
			a = f.getElementsByTagName('a'),
			cur = BIG.slideShowGetCurrent(),
			n = false, cNumber = 0, nNumber = 0;
		if(cur !== false && document.getElementById('featured').slideShow != '0') {
			if(cur < a.length-1) {
				cNumber = cur;
				nNumber = cur+1;
				n = a[cur+1];
				cur = a[cur];
			} else {
				cNumber = cur;
				nNumber = 0;
				n = a[0];
				cur = a[cur];
			}
			BIG.slideShowSwap(cur, n, cNumber, nNumber);
			setTimeout(BIG.slideShowDo, 5000);
		}
	},
	slideShowSwap: function(cur, n, cNumber, nNumber) {
		var o = 100, 
			fade = function() {
				if(o > 0) {
					o = o - 10;
					cur.style.opacity = o/100;
					n.style.opacity = (100-o)/100;
					cur.style.filter = "alpha(opacity=" + o + ")";	/* IE7 & 8-only */
					n.style.filter = "alpha(opacity=" + (100-o) + ")";	/* IE7 & 8-only */
					setTimeout(fade, 10);
				} else {
					document.getElementById('slide-' + cNumber).className = document.getElementById('slide-' + cNumber).className.replace(/current/ig, '');
					document.getElementById('slide-' + nNumber).className += ' current';
					cur.className = cur.className.replace(/current/ig, '');
					n.className += ' current';
					cur.style.opacity = 0;
					n.style.opacity = 1;
					cur.style.filter = "alpha(opacity=0)";	/* IE7 & 8-only */
					n.style.filter = "alpha(opacity=100)";	/* IE7 & 8-only */
				}
			};
		if(cur && n) {
			if(document.body.className.indexOf('css-transitions') !== -1) {
				document.getElementById('slide-' + cNumber).className = document.getElementById('slide-' + cNumber).className.replace(/current/ig, '');
				document.getElementById('slide-' + nNumber).className += ' current';
				cur.className = cur.className.replace(/current/ig, '');
				n.className += ' current';
				setTimeout(function() {cur.style.webkitTransform = '';}, 1000);
			} else {
				n.style.zIndex = 20;
				cur.style.zIndex = 1;
				fade();
			}
		}
	},
	slideShowAction: function(e) {
		e = e || event;
		var t = e.target || e.srcElement,
			a = document.getElementById('featured').getElementsByTagName('a'),
			l = a.length,
			cur = BIG.slideShowGetCurrent(), cNumber = 0,
			n = false;
		e.returnValue = false;
		if(e.preventDefault) {
			e.preventDefault();
		}
		if(t.id !== '' && cur !== false) {
			switch(t.id) {
				default:
					if(t.id.indexOf('slide-') !== -1) {
						nNumber = t.id.split('-')[1];
						cNumber = cur;
						cur = a[cur];
						n = a[nNumber];
						if(cNumber == nNumber) {
							cur = false;
							n = false;
						}
					} else {
						cur = false;
						n = false;
					}
				break;
				case 'slide-next':
					cNumber = cur;
					if(cur < l-1) {
						nNumber = cur+1;
						n = a[nNumber];
					} else {
						nNumber = 0;
						n = a[0];
					}
					cur = a[cur];
				break;
				case 'slide-previous':
					cNumber = cur;
					if(cur > 0) {
						nNumber = cur-1;
						n = a[nNumber];
					} else {
						nNumber = l-1;
						n = a[nNumber];
					}
					cur = a[cur];
				break;
			}
			if(cur && n) {
				document.getElementById('featured').slideShow = 0;
				BIG.slideShowSwap(cur, n, cNumber, nNumber);
			}
		}
	},
	/* slideShowGeneratedContentSupport is IE7 & 8-only */
	slideShowGeneratedContentSupport: function() {
		var el = document.getElementById('featured'),
			a = el.getElementsByTagName('a'),
			l = a.length, tmp = '', i = 0;
		for(i = 0; i < l; i++) {
			tmp = document.createElement('div');
			tmp.innerHTML = a[i].title;
			tmp.className = 'feature-desc';
			a[i].appendChild(tmp);
		}
	},
	slideShowInit: function() {
		var el = document.getElementById('featured'),
			d = document.createElement('div'),
			a = el.getElementsByTagName('a'),
			l = a.length, tmp = '', i = 0,
			touches = [],
			trackTouchStart = function(e) {
				e.preventDefault();
				e.stopPropagation();
				touches = [];
			},
			trackTouchEnd = function(e) {
				var evnt = document.createEvent('MouseEvents'),
					evnt_target = false, tmp, mcnt = 0;
				e.preventDefault();
				e.stopPropagation();
				evnt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
				if(touches[0] > touches[touches.length-1]) {
					evnt_target = document.getElementById('slide-next');
					evnt_target.dispatchEvent(evnt);
				} else if(touches[0] < touches[touches.length-1]) {
					evnt_target = document.getElementById('slide-previous');
					evnt_target.dispatchEvent(evnt);
				} else {
					tmp = e.target;
					if(tmp.nodeName.toLowerCase() != 'a') {
						while(tmp.nodeName.toLowerCase() != 'a' && tmp.parentNode) {
							tmp = tmp.parentNode;
						}
					}
					if(tmp.nodeName.toLowerCase() == 'a') {
						location.href = tmp.href;
					}
				}
				touches = [];
			},
			trackTouch = function(e) {
				var f = document.getElementById('featured'),
					all = f.getElementsByTagName('a'),
					l = all.length,
					cur = BIG.slideShowGetCurrent(), nex, prev;
				if(e.target.nodeName.toLowerCase() != 'a' && e.target.parentNode.nodeName.toLowerCase() != 'a') {
					return;
				}
				if(cur+1 < l) {
					nex = cur+1;
				} else {
					nex = 0;					
				}
				if(cur-1 >= 0) {
					prev = cur-1;
				} else {
					prev = l-1;
				}
				
				nex = all[nex].getElementsByTagName('img')[0].src;
				prev = all[prev].getElementsByTagName('img')[0].src;
					
				document.getElementById('featured').slideShow = 0;
				e.preventDefault();
				e.stopPropagation();
				touches[touches.length] = e.targetTouches[0].clientX;
				e.target.parentNode.style.webkitTransform = 'translate(' + (e.targetTouches[0].clientX-touches[0]) + 'px, 0px)';
				if(e.targetTouches[0].clientX-touches[0] < 0) {
					document.getElementById('featured').style.background = '#5A5A5B url(' + nex + ')';
				} else {
					document.getElementById('featured').style.background = '#5A5A5B url(' + prev + ')';
				}
				
			};
		if(el.parentNode != document.body) {
			document.body.insertBefore(el.parentNode.removeChild(el), document.getElementById('header-background'));
		}
		el.slideShow = '0';
		if(l > 1) {
			
			if(window.addEventListener) {
				el.addEventListener('touchstart', trackTouchStart, null);
				el.addEventListener('touchend', trackTouchEnd, null);
				el.addEventListener('touchmove', trackTouch, null);
			}
			
			a[0].className = 'current';
			
			el.slideShow = '1';
			
			tmp += '<a href="#" title="Previous" id="slide-previous">&lt;</a>';
			for(i=0; i < l; i++) {
				tmp += '<a href="#" id="slide-' + i + '" class="' + (i == 0 ? 'current' : '') + ' hidden">&bull;</a>';
			}
			tmp += '<a href="#" title="Next" id="slide-next">&gt;</a>';
			d.innerHTML = tmp;
			d.id = 'featured-controls';
			if(window.addEventListener) {
				d.addEventListener('click', BIG.slideShowAction, null);
			} else {
				d.attachEvent('onclick', BIG.slideShowAction);	/* IE7 & 8-only */
			}
			document.getElementById('content').insertBefore(d, document.getElementById('content').firstChild);
			setTimeout(BIG.slideShowDo, 5000);
		} else if(a[0]) {
			a[0].className = 'current';
		}
		// Prep the :after content for IE7/8
		if((/MSIE [78]/ig).test(navigator.userAgent)) {
			BIG.slideShowGeneratedContentSupport();
		}
	},
	testCapabilities: function() {
		var addClass = ' js', prefix = ['webkit', 'o', 'msie', 'moz'],
			i = 0, l = prefix.length,
			el = document.createElement('div');
		try {
			el.innerHTML = '<div style="-webkit-transition:color 1s linear;-moz-transition:color 1s linear;-o-transition:color 1s linear;-msie-transition:color 1s linear;transition:color 1s linear;"></div>';
			if(el.style.hasOwnProperty('transition')) {
				addClass += ' css-transitions';
			} else if(el.firstChild.style.webkitTransition !== undefined || el.firstChild.style.MozTransition !== undefined) {
				addClass += ' css-transitions';
			} else {
				for(i=0; i < l; i++) {
					if(el.style.hasOwnProperty(prefix[i] + 'Transition')) {
						addClass += ' css-transitions';
					}
				}
			}
		} catch(err) {
			// IE Support?
		}
		document.body.className += addClass;
	},
	commentsFocus: function(e) {
		e = e || event;
		var t = e.target || e.srcElement;
		t.className += ' focus';
	},
	commentsBlur: function(e) {
		e = e || event;
		var t = e.target || e.srcElement;
		if(t.value == '') {
			t.className = t.className.replace(/ focus/g, '');
		} else {
			t.className = t.className.replace(/ focus/g, ' focus');
		}
	},
	commentsInit: function() {
		var inps = document.getElementById('commentform').getElementsByTagName('input'),
			l = inps.length, c;
		for(c = 0; c < l; c++) {
			if(inps[c].getAttribute('type') == 'text') {
				if(inps[c].value != '') {
					inps[c].className += ' focus';
				}
				if(window.addEventListener) {
					inps[c].addEventListener('focus', BIG.commentsFocus, null);
					inps[c].addEventListener('blur', BIG.commentsBlur, null);
				} else {
					inps[c].attachEvent('onfocus', BIG.commentsFocus);	/* IE7 & 8-only */
					inps[c].attachEvent('onblur', BIG.commentsBlur);	/* IE7 & 8-only */
				}
			}
		}
		inps = document.getElementById('commentform').getElementsByTagName('textarea');
		l = inps.length;
		for(c = 0; c < l; c++) {
			if(inps[c].value != '') {
				inps[c].className += ' focus';
			}
			if(window.addEventListener) {
				inps[c].addEventListener('focus', BIG.commentsFocus, null);
				inps[c].addEventListener('blur', BIG.commentsBlur, null);
			} else {
				inps[c].attachEvent('onfocus', BIG.commentsFocus);	/* IE7 & 8-only */
					inps[c].attachEvent('onblur', BIG.commentsBlur);	/* IE7 & 8-only */
			}
		}
	},
	init: function() {
		BIG.testCapabilities();
		if(document.getElementById('featured')) {
			if(window.addEventListener) {
				window.addEventListener('load', BIG.slideShowInit, null);
			} else {
				window.attachEvent('onload', BIG.slideShowInit);	/* IE7 & 8-only */
			}
		}
		if(document.getElementById('the-people')) {
			BIG.peopleInit();
		}
		if(document.getElementById('commentform')) {
			BIG.commentsInit();
		}
	}
};

BIG.init();
