function init() {
	$('a').focus(function() { $(this).blur(); });
}

news = {
	space: 10,
	initTimeout: 70,
	animTimeout: 70,
	enabled: true,
	init: function() {
		$('.reel p').css('position', 'absolute').eq(0).nextAll().each(function() {
			$(this).css('top',
				($(this).prev().position().top + $(this).prev().height() + news.space) + 'px');
		})
		if ($('.reel p:last').position().top + $('.reel p:last').height() > $('.reel').height())
			setTimeout(news.scroll, news.initTimeout);
		$('.reel').hover(function() { news.enabled = false; }, function() { news.enabled = true; });
	},
	scroll: function() {
		setTimeout(news.scroll, news.animTimeout);
		if (news.enabled) {
			$('.reel p').each(function() {
				$(this).css('top', ($(this).position().top - 1) + 'px');
			});
			if ($('.reel p:eq(1)').position().top <= 0) {
				var node = $('.reel p:first').insertAfter($('.reel p:last')).hide().fadeIn('slow');
				node.css('top', (node.prev().position().top + node.prev().height() + news.space) + 'px');
			}
		}
	}
};

searchbar = {
	clsActive: 'active',
	text: 'search',
	empty: true,
	headerText: 'Search Results',
	init: function(mode) {
		$('#searchbar').val(searchbar.text)
			.focus(function() {
				if (searchbar.empty)
					$(this).val('').addClass(searchbar.clsActive);
			})
			.blur(function() {
				if ($(this).val() == '') {
					searchbar.empty = true;
					$(this).removeClass(searchbar.clsActive).val(searchbar.text);
				}
			})
			.change(function() {
				searchbar.empty = false;
			});
		$('#searchform').submit(function() { searchbar.search('0', mode); return false; });
	},
	search: function(start, mode) {
		$('#searchquery').val('site:oglethorpega.org ' + $('#searchbar').val());
		$('#searchstart').val(start);
		$.ajax({
			cache: false,
			data: $('#searchform').serialize(),
			url: 'http://ajax.googleapis.com/ajax/services/search/web',
			dataType: 'jsonp',
			error: function(xhr, textStatus) {
				alert('An error occurred: ' + textStatus);
			},
			success: function(data, textStatus) {
				if (mode == 'index')
					var header = $('<h1></h1>').addClass('contentLike');
				else
					var header = $('.content .text:visible h1');
				var hr = $('.content .text div.hr');
				var searchResults = $('<div></div>').addClass('searchResults');
				var pager = $('<div></div>').addClass('searchPager');
				var note = $('<div></div>').addClass('searchNote');
				$('.contentContainer>h1').text(searchbar.headerText);
				$('.nav a').removeClass('current');
				for (var i in data.responseData.results) {
					searchResults.append(
						$('<p></p>').html('<a href="' + data.responseData.results[i].url +	'">' +
								data.responseData.results[i].title + '</a>')
							.append('<p>' + data.responseData.results[i].content + '</p>')
					);
				}
				for (var i in data.responseData.cursor.pages) {
					if (i == data.responseData.cursor.currentPageIndex) {
						pager.append(
							$('<span></span>').text(data.responseData.cursor.pages[i].label)
						);
					} else {
						pager.append(
							$('<a href="javascript:void(0)"></a>').text(data.responseData.cursor.pages[i].label)
								.click(function() { searchbar.search($(this).data('start'), mode); })
								.data('start', data.responseData.cursor.pages[i].start)
						);
					}
				}
				if (data.responseData.results.length == 0)
					searchResults.html('<p>Sorry, your search returned no results</p>');
				header.text(searchbar.headerText);
				document.title = searchbar.headerText;
				note.html('Powered by <a href="http://www.google.com/">Google</a>');
				if (mode == 'index') {
					$('.searchInsert').html(header).append(searchResults).append(pager).append(note);
					$('.searchInsert:not(:visible)').slideDown('normal');
					$('.container .bottom').animate({marginTop: '12px'}, 'normal');
					$('html,body').animate({scrollTop: $('#searchform').offset().top}, 'normal');
				} else {
					$('.content .text:visible').empty().append(header).append(searchResults).append(pager).append(note)
						.append(hr);
				}
			}
		});
	}
};

slideshow = {
	pictures: null,
	path: 'img/index/slideshow',
	current: 0,
	interval: 6000, // msec
	speed: 2500, // msec, 'slow', 'normal', or 'fast'
	init: function() {
		$.ajax({
			cache: false,
			url: slideshow.path + '/list.php',
			dataType: 'json',
			error: function(xhr, textStatus) {
				alert('Error: Cannot start slideshow (' + textStatus + ')');
			},
			success: function(data) {
				slideshow.pictures = data;
				slideshow.start();
			}
		});
	},
	start: function() {
		$('.photo').css('background', this.currentPic());
		this.current = ++this.current % this.pictures.length;
		$('.photo-fade').hide().css('background', this.currentPic());
		setTimeout('slideshow.next()', this.interval);
	},
	next: function() {
		$('.photo-fade').fadeIn(this.speed, function() { slideshow.start(); });
	},
	currentPic: function() {
		return 'url(' + this.path + '/' + this.pictures[this.current] + ')';
	}
};
