$(window).ready(function() {
/*----------------------------------------------------------------------------
	Fix IE hover:
----------------------------------------------------------------------------*/

	if ($.browser.msie && $.browser.version <= 7) {
		$('li, input.submit').hover(
			function() {
				$(this).addClass('hover');
			},
			function() {
				$(this).removeClass('hover');
			}
		);
	}
/*----------------------------------------------------------------------------
	Open external links in a new tab/window
----------------------------------------------------------------------------*/

	$('a[rel=external], .external, #searchForm a').live("click", function() {
		window.open($(this).attr('href'));
		return false;
	})
/*----------------------------------------------------------------------------
	Insert presentational | on items:
----------------------------------------------------------------------------*/

	$('div.contact ul.inline li:not(:last-child)').after("<li class='seperator'>|</li>");
	$('div.breadcrumbs ul li:not(:last-child)').after("<li class='seperator'>&gt;</li>");

/*----------------------------------------------------------------------------
	Use scrollTo for Top Links:
----------------------------------------------------------------------------*/

	$('a[href=#top]').live("click", function() {
		$(document).scrollTo($('#header'), 700);

		return false;
	});

/*----------------------------------------------------------------------------
	Share This
----------------------------------------------------------------------------*/

	if($('ul.share-list').length = 1) {
		var shareList = $('ul.share-list');
		shareList.hide();

		$('span.share-this').click(function() {
			shareList.show();
		});

		shareList.mouseleave(function() {
			shareList.hide();
		});
	}

/*----------------------------------------------------------------------------
	DL Widelist Height
----------------------------------------------------------------------------*/
	$('dl.widelist dt').each(function() {
		var ddNeighbour = $(this).next('dd');
		var newHeight = (ddNeighbour.height() > 100) ? ddNeighbour.height() : 100;

		if(newHeight == 100) {
			ddNeighbour.css('height', 100);
		}

		$(this).css('height', newHeight);
	});

/*----------------------------------------------------------------------------
	Cull Important Dates Listing
----------------------------------------------------------------------------*/
	
	if($('#aside .eventCalendar').length >0 ) {
	
	var contentW 	= $('#limelight'),
		eventsList	= $('#aside .eventCalendar'),
		lowerBox	= $('div.lower').height(), //Home
		offset		= (eventsList.offset().top - contentW.offset().top);

	if((eventsList.height() + offset) > contentW.height() + lowerBox) {
		var newHeight = contentW.height() - offset;
		var roundingHeight = $('dt',eventsList).height();

		/* Accounts for when contentW finishes before eventsList (y-axis) */
		if(newHeight > 0) {
			newHeight = (Math.round(newHeight / roundingHeight)) * roundingHeight;
		} else {
			newHeight = roundingHeight
		}

		eventsList.height(newHeight - 1);
	}}
});

$(document).ready(function() {
	$('#contact-form input:first-of-type:eq(0)').focus();
	/*----------------------------------------------------------------------------
		Equal Heights
	----------------------------------------------------------------------------*/
		$('#container.contact ul.grid li.item:nth-child(2n+2)').each(function() {
			var self = $(this),
				prev = self.prev(),
				heights = [
						self.height(),
						prev.height()
					];

			var gridHeight = Math.max.apply({}, heights);

			self.find('div.listFooter').height(gridHeight);
			prev.find('div.listFooter').height(gridHeight);
		});

	/*----------------------------------------------------------------------------
		Analytics Tracking
	----------------------------------------------------------------------------*/

		$('.donationLink').live("click", function() {
			pageTracker._trackPageview('/donation/click');
		});

	/*----------------------------------------------------------------------------
		News Ticker
	----------------------------------------------------------------------------*/
		if($("#news-ticker ul li").length = 1) {
			$('#news-ticker ul li:first').addClass('active');

			function tick() {
				var container = $('#news-ticker ul');
				var current = container.find('.active');

				/* Restart */
				var next = current.next().length ? current.next()
					: next = container.find('li:first');

				var position = next.prevAll().length;

				next.addClass('active').fadeIn("normal");
				current.removeClass('active').fadeOut("normal");

				return false;
			}

			if($('#news-ticker ul li').length > 1) {
				setInterval(tick, 5000);
			}
		}

})

