	/* --- Fun Stuff Functionality --- */
	
	var fun_width = 100;
	var fun_things = 0;
	var polls_answered = new Array();
	Event.observe(document, 'dom:loaded', function() {
		$$('#fun-stuff .thing').each(function(x) {
			fun_width += x.getWidth();
			fun_things++;
		});
	
		$('fun-stuff').setStyle({
			width: '0px',
			left: '878px'
		});
		$('fun-stuff-inner').setStyle({ width: fun_width + 'px' });
		
		if ($$('#fun-stuff .flickr').length) {
			$$('#fun-stuff .flickr .thumbs img').each(function(x) {
				var images = new Array();
				if ($(x).up('a').previousSiblings().length > 0) {
					images[$(x).previousSiblings().length - 1] = new Image();
					images[$(x).previousSiblings().length - 1].src = $(x).readAttribute('src').replace('_s', '');
				}
			});
		}
		
		// display sub navigation if they exist
		$$('ul#nav li, #top ul li').each(function(x) {
			if ($(x).down('ul')) {
				x.onmouseover = function() { $(this).addClassName('selected'); }
				x.onmouseout = function() { $(this).removeClassName('selected'); }
			}
		});
		
		// update sub navigation left position
		$$('ul#nav li ul, #top ul li ul').each(function(x) {
			$(x).setStyle({ left: (($(x).getWidth() - $(x).previous('a').getWidth()) / 2 * -1) + 'px' });
		});
				
		// search field
		$('search-text').onfocus = function() { if (this.value == 'search') this.value = ''; }
		$('search-text').onblur = function() { if (this.value == '') this.value = 'search'; }
		
		// newsletter field
		$('newsletter-email').onfocus = function() { if (this.value == 'your email address') this.value = ''; }
		$('newsletter-email').onblur = function() { if (this.value == '') this.value = 'your email address'; }
		
		// poll voting
		$$('#lower-survey input[type=radio], #polls .poll input[type=radio]').each(function(x) {
			x.onclick = function() {
				var split = $(this).identify().split('-');
				var poll = split[1];
				var answer = split[2];
				
				if (polls_answered.indexOf(poll) == -1) {
					new Ajax.Request('/index.php', {
						method: 'post',
						parameters: {
							'action': 'poll-submission',
							'poll_id': poll,
							'answer': answer
						},
						onComplete: function(transport) {
							var width = 190;
							var most = 1;
							var responses = transport.responseText.evalJSON();
							for (var i = 1; i <= 5; i++) {
								responses['answer' + i] = parseInt(responses['answer' + i]);
								if (responses['answer' + i] > responses['answer' + most]) most = i;
								if ($('answer-' + poll + '-' + i)) {
									$('answer-' + poll + '-' + i).down('.fill').setStyle({ width: '0px' });
									$('answer-' + poll + '-' + i).down('.fill').morph('width: ' + (width * responses['answer' + i] / 100) + 'px;', { duration: .5 });
								}
							}
							$('answer-' + poll + '-' + most).down('span').addClassName('selected');
						}
					});
					polls_answered.push(poll);
				} else {
					alert("You have already responded to this poll.");
				}
			}
		});
		
		$$('.missing').each(function(x) {
			$(x).setStyle({ color: '#FF00FF' });
		});	
				
		if ($('emergency')) {
			setInterval(function() {
				$('emergency').setStyle({ width: get_window_width() + 'px' });
				$('emergency').down('.wrapper').setStyle({ marginLeft: $('top').down('.wrapper').positionedOffset()[0] + 'px' }).show();
			}, 1);
		}
	});
	
	function toggle_fun(duration) {
		var dur = (duration != undefined) ? duration : fun_things * .15;
		var button = ($('fun-stuff-button')) ? $('fun-stuff-button') : $('fun-stuff-button-home');
		button.toggleClassName('open');		
		if (button.hasClassName('open')) {
			$('fun-stuff').morph('width:' + fun_width + 'px;', { duration: dur });
			new Ajax.Request('/inc/fun-stuff-status.php?status=1');
		} else {
			$('fun-stuff').morph('width: 0px;', { duration: dur });
			new Ajax.Request('/inc/fun-stuff-status.php?status=0');
		}
	}
	
	function get_window_width() {
		var document_width, window_width;
		
		if (window.innerWidth && window.scrollMaxX) document_width = window.innerWidth + window.scrollMaxX;
		else if (document.body.scrollWidth > document.body.offsetWidth) document_width = document.body.scrollWidth;
		else document_width = document.body.offsetWidth;
		
		if (self.innerWidth) window_width = self.innerWidth;
		else if (document.documentElement && document.documentElement.clientWidth) window_width = document.documentElement.clientWidth;
		else if (document.body) window_width = document.body.clientWidth;
		
		return (document_width < window_width) ? window_width : document_width;
	}
		
	/* --- YouTube Video (fun) --- */
	
	Event.observe(document, 'dom:loaded', function() {
		if ($('youtube-play-button')) {
			$('youtube-play-button').onclick = function() {
				var state = ytplayer.getPlayerState();
				if (state == 2) { // paused
					ytplayer.playVideo();
					$(this).addClassName('pause');
				} else if (state == 1) { // playing
					ytplayer.pauseVideo();
					$(this).removeClassName('pause');
				} else if (state == 0) {
					ytplayer.seekTo(0);
					ytplayer.playVideo();
					$('youtube-play-button').addClassName('pause').show();
				}
				return false;
			}
		}
	});
		
	function youtubeStateChange(newState) {
		if (newState == 0) { // video ended
			$('youtube-play-button').removeClassName('pause');
		} else if (newState == 1) {
			$('youtube-play-button').show();
		}
	}
