$(function() {
	
	// Archive page
	if ($('body.page-media-news-archive').length > 0) {
		var years = new Array();
		$('h3').each(function(){
			thisText = $(this).text();
			thisMonth = thisText.substr(0, 3);
			thisYear = thisText.substr(thisText.length - 4, thisText.length - 1);
			var anchorText = (thisMonth + '-' + thisYear).toLowerCase();
			var anchorLink = '<a name="' + anchorText + '"></a>';
			var anchorText = '<a href="#' + anchorText + '">' + thisMonth + '</a>';
			$(this).before(anchorLink);
			
			if (typeof years[thisYear] === 'undefined') {
				years[thisYear] = anchorText + ' | ';
			}
			else {
				years[thisYear] = years[thisYear] + anchorText + ' | ';
			}
		});		
		list = '<div id="news_archive_links">';	
		for (var year in years) {
		  list = list + '<span class="news-archive-year">' + year + '</span>: ' + years[year].substr(0,years[year].length - 4) + '<br />';			  
		}
		list = list + '</div>';
		$('div.view-header').append(list);
	}
		
	
	// Footer things
	if ($('div#footer').length > 0) {
		var footer = $('div#footer div.content');
		
		// Hide the lists
		$('div.footer-links', footer).hide();
		
		// Show/Hide clicks
		$('a.footer-show-hide').click(function() {
			var this_id = $(this).attr('href').replace('#footer_', '');
			var links = $('div#footer_links_container_' + this_id + ' div#footer_links_' + this_id, footer);
			var state = links.css('display');
			
			// Show?
			if (state == 'none')
			{
				links.slideDown('fast', function() {
					$('div#footer_links_container_' + this_id + ' a.footer-show-hide', footer).html('hide list');
				});
			}
			else // Or hide?
			{
				links.slideUp('fast', function() {
					$('div#footer_links_container_' + this_id + ' a.footer-show-hide', footer).html('show list');
				});
			}
			
			return false;
		});
	}
	
	if ($('#carousel').length > 0) {
		/*
		 * Switch the carousel content
		 */
		$('#carousel').tdClickSwitch({
			display: 'carousel_display',
			store: 'carousel_store',
			thumbs: 'carousel_tabs',
			fadespeed: 1,
			trigger: 'mouseover'
		});
		/*
		 * Alter the carousel buttons
		 */
		$('#carousel #carousel_tabs a').click(function() {
			return false;
		});
		$('#carousel #carousel_tabs a').hover(function() {
			$('#carousel #carousel_tabs a').removeClass('active');
			$(this).addClass('active');
			return false;
		});
	}
	
	// Contact us box height matching
	if ($('body.page-contact-us').length > 0)
	{
		var max_height = 0, cur_height = 0;
		
		$('div#content div.contact-location-height').each(function() {
			cur_height = $(this).height();
			if (cur_height > max_height) {
				max_height = cur_height;
			}
		});
		
		$('div#content div.contact-location-height').height(max_height);
	}
	
	// Contact feedback forms
	if ($('body.page-contact-us-feedback').length > 0)
	{
		var scheme_block = $('div#content-bottom div#block-webformblock-104');
		var stars_block = $('div#content-bottom div#block-webformblock-105');
		var chooser = $('div#content-area div#feedback_choose');
		$('h2', scheme_block).hide();
		$('h2', stars_block).hide();
		
		// Hide the stars form initially
		stars_block.hide();
		
		// Click events
		$('a#feedback_stars', chooser).click(function() {
			scheme_block.fadeOut('fast', function() {
				stars_block.fadeIn('fast');
			});
			
			$('a', chooser).removeClass('feedback-active');
			$(this).addClass('feedback-active');
			
			return false;
		});
		$('a#feedback_scheme', chooser).click(function() {
			stars_block.fadeOut('fast', function() {
				scheme_block.fadeIn('fast');
			});

			$('a', chooser).removeClass('feedback-active');
			$(this).addClass('feedback-active');

			return false;
		});
	}
});
