/*
 *  Object: whitehall
 *  Purpose: master behaviors for website
 */

var whitehall = new Whitehall(jQuery);
jQuery(document).ready(whitehall.init);

var log = whitehall.log; // general log function

function Whitehall($) {
	var templateDirectory;
	
	this.init = init;
	function init() {
		templateDirectory = $('#template_directory').val();
		$('#nav li:last-child').addClass('last');
		setupCalendar();
		setupSermons();
		$('a[href$=.jpg], a[href$=.gif], a[href$=.png]').prettyPhoto();
	}
	
	this.log = log;
	function log(msg) {
		if (console && console.log)
			console.log(msg);
	}
	
	function setupCalendar() {
		if ($('table.calendar-table').length) {
			// disable all event links (they're just for hover)
			$('table.calendar-table .calnk a').click(function() { return false; });
			// remove advert link
			$('.kjo-link').remove();
		}
	}
	
	function setupSermons() {
		if ($('#sermon-filters').length) {
			// setup filters
			$('#filters-series').click(function() {
				$('#panel-preachers').slideUp();
				$('#panel-series').slideToggle();
				$(this).blur();
				return false;
			});
			$('#filters-preacher').click(function () {
				$('#panel-series').slideUp();
				$('#panel-preachers').slideToggle();
				$(this).blur();
				return false;
			});
		}
		if ($('table#sermon-index').length) {
			var table = $('table#sermon-index');
			table.find('tbody tr:not(.not-found)')
				.addClass('clickable')
				.click(function() {
					document.location = $(this).find('td.title a').eq(0).attr('href');
				})
			;
			table.find('tbody a').click(function(e) {
				document.location = $(this).attr('href');
				e.stopPropagation();
			});
		}
	}
	
}


/*
 *  Object: whitehall.fonts
 *  Purpose: manage font information (cufon)
 */

whitehall.fonts = new Whitehall_Fonts();
whitehall.fonts.defaults();

function Whitehall_Fonts() {
	var fonts = {};
	
	this.add = add;
	function add(name, font) {
		if (fonts[name])
			fonts[name].selector += ', ' + font.selector;
		else
			fonts[name] = font;
	}
	
	this.config = config;
	function config() {
		for (i in fonts) {
			Cufon.replace(fonts[i].selector, {fontFamily:fonts[i].family});
		}
	}
	
	this.defaults = defaults;
	function defaults() {
		add('garamond', {
			selector: '#banner h1, #summary, .sidebar h2',
			family: 'Garamond Premier Pro Caption'
		});
	}
}
