
var DACBeachcroft = {
	
	title: 'DAC Beachcroft',
	
	/**
	 * When DOM is ready...
	 */
	ready: function() {
		this.initAsides();
		this.setPanelHeights();
		this.initSortableTables();
		this.initQLinks();
		this.initPublications();
		this.initEvents();
		this.initSearchResults();
		this.initSearchBox();
		this.initPeopleSearchBox();
		this.initPublicationsSearchBox();
	},
	
	/**
	 * When page is loaded...
	 */
	loaded: function() {
		this.setPanelHeights();
	},
	
	
	/**
	 * Show asides if there are any.
	 */
	initAsides: function() {
		if (jQuery('#main #main-layout .asides .aside h3').length > 0) {
			jQuery('#main').removeClass('no-asides');
			jQuery('#main').addClass('has-asides');
		} else {
			jQuery('#main').addClass('no-asides');
		}
	},
	
	/**
	 * Make sure that the content and main panels are always longer
	 * than the nav and sidebar panels so that the rounded corners
	 * don't look silly.
	 */
	setPanelHeights: function() {
		var nav_height     = jQuery('#nav').height();
		var content_height = jQuery('#content').height();
		if ( content_height < nav_height ) {
			jQuery('#content').css('minHeight', nav_height + 20);
		}
		
		var sidebar_height = jQuery('#sidebar').height();
		var main_height    = jQuery('#main-layout').height();
		if ( main_height < sidebar_height ) {
			if (jQuery('#main').hasClass('no-asides')) {
				//jQuery('#content').height(sidebar_height + 20);
				jQuery('#content').css('minHeight', sidebar_height + 20);
			} else {
				//jQuery('#main-layout').height(sidebar_height + 20);
				jQuery('#main-layout').css('minHeight', sidebar_height + 20);
			}
		}
	},
	
	/**
	 * Check for sortable tables and initialise.
	 */
	initSortableTables: function() {
		if (jQuery("table.sortable-table").length > 0) {
			jQuery("table.sortable-table").tablesorter();
		}
	},
	
	/**
	 * Check for ? links and activate.
	 */
	initQLinks: function() {
		if (jQuery("a.q-link").length > 0) {
			jQuery("a.q-link").click(function(e){
				var id = jQuery(this).attr('href');
				jQuery(id).toggle();
				e.preventDefault();
			});
		}
	},
	
	/**
	 * Init Publications.
	 */
	initPublications: function() {
		if (jQuery(".publications .post a.more.expand").length > 0) {
			jQuery(".publications .post a.more.expand").click(function(e){
				if ( jQuery(this).text() == 'Show summary' ) {
					jQuery(this).text('Hide summary');
				} else {
					jQuery(this).text('Show summary');
				}
				jQuery(this).parent().parent().parent().find(".post-content").slideToggle();
				e.preventDefault();
			});
		}
	},
	
	/**
	 * Init Events.
	 */
	initEvents: function() {
		if (jQuery(".events .post a.more.expand").length > 0) {
			jQuery(".events .post a.more.expand").parent().parent().parent().find(".post-content").toggle();
			jQuery(".events .post a.more.expand").click(function(e){
				if ( jQuery(this).text() == 'Show summary' ) {
					jQuery(this).text('Hide summary');
				} else {
					jQuery(this).text('Show summary');
				}
				jQuery(this).parent().parent().parent().find(".post-content").slideToggle();
				e.preventDefault();
			});
		}
	},
	
	/**
	 * Init Search Results
	 */
	initSearchResults: function() {
		if (jQuery(".searchresults-groups").length > 0) {
			jQuery(".searchresults-groups .post.toggle-hide").hide();
			jQuery(".searchresults-groups .view-all a").each(function(){
				jQuery(this).data('text', jQuery(this).text() );
			});
			jQuery(".searchresults-groups .view-all a").click(function(e){
				jQuery(this).parent().siblings(".post.toggle-hide").toggle();
				if (jQuery(this).text() != jQuery(this).data('text')) {
					jQuery(this).text(jQuery(this).data('text'));
				} else {
					jQuery(this).text('Collapse search results');
				}
				e.preventDefault();
			});
		}
	},
	
	/**
	 * Init Search Box
	 */
	initSearchBox: function() {
		var search_field = jQuery(".search input.s");
		if (search_field.length > 0) {
			search_field.focus(function() {
				if ( search_field.val() == 'Search...' ) {
					search_field.val('');
				}
				search_field.removeClass('default-val');
			}).blur(function() {
				if ( search_field.val() == '' ) {
					search_field.val('Search...').addClass('default-val');
				}
			}).trigger('blur');
		}
	},
	
	/**
	 * Init People Search Box
	 */
	initPeopleSearchBox: function() {
		var psearch_field = jQuery(".people-search input.searchperson");
		if (psearch_field.length > 0) {
			psearch_field.focus(function() {
				if ( psearch_field.val() == 'Search for a person...' ) {
					psearch_field.val('');
				}
				psearch_field.removeClass('default-val');
			}).blur(function() {
				if ( psearch_field.val() == '' ) {
					psearch_field.val('Search for a person...').addClass('default-val');
				}
			}).trigger('blur');
		}
	},
	
	/**
	 * Init Publications Search Box
	 */
	initPublicationsSearchBox: function() {
		var psearch_field = jQuery(".publicationsearch input.searchpublications");
		if (psearch_field.length > 0) {
			psearch_field.focus(function() {
				if ( psearch_field.val() == 'Search for a publication...' ) {
					psearch_field.val('');
				}
				psearch_field.removeClass('default-val');
			}).blur(function() {
				if ( psearch_field.val() == '' ) {
					psearch_field.val('Search for a publication...').addClass('default-val');
				}
			}).trigger('blur');
		}
	}
	
}

// IE6. Grrr....
if (jQuery.browser.msie && jQuery.browser.version.substr(0, 1) < 7) {
	jQuery('html').addClass('ie6');
}

// When page ready...
jQuery(document).ready(function($) {
	DACBeachcroft.ready();
});

// When page loaded...
jQuery(window).load(function($) {
	DACBeachcroft.loaded();
});

