jQuery(function($){
	// slideshow stuff for the index.html page
	if($('test').slideShow !== undefined){
		$('.slideshow').slideShow({
			randomize: true,
			fadeSpeed: 3000,
			interval: 7000
		});
	}
	
	// news/patriot.html specific code
	if($('#freestyle_content .share_popup').length > 0){
		var sharePopup = $('#freestyle_content .share_popup'), shareLink = $('#freestyle_content .share a');
		// clicks on children of .share_popup do not bubble up the dom past .share_popup
		$('#freestyle_content').delegate('.share_popup', 'click', function(event){event.stopPropagation();});
		// share icons, and close button, fade out the popup
		$('.share_icons a', sharePopup).click(function(){sharePopup.fadeOut('fast');});
		$('a.close_button', sharePopup).click(function(){sharePopup.fadeOut('fast'); return false;});
		// watch the document for clicks, and hide the popup
		$(document).click(function(){sharePopup.fadeOut('fast');});
		// watch the share link for clicks, position the popup, and fade it in
		shareLink.click(function(){
			sharePopup.css({
				position: 'absolute',
				top: shareLink.position().top - sharePopup.outerHeight(),
				left: shareLink.position().left - sharePopup.outerWidth() + shareLink.outerWidth()
			}).fadeIn('fast');
			return false;
		});
	}
	
	// community/events.html specific popup code
	$('.an_event').each(function(){ // loops through all of the events
		var eventLink = $('.event_detail_link a', this),
				eventSummary = $('.event_summary_detail_container', this),
				eventTitle = $('.event_summary p a', this);
		if(eventSummary.length > 0){ // If no event summary, don't bind popup behavior.
			$(eventLink).hover(function(){
				showDetailsPopup(eventLink, eventSummary);
			}, function(){
				eventSummary.fadeOut('fast');
			});
			
			// This part assigns the hover events to the event title's
			$(eventTitle).hover(function(){
				showDetailsPopup(eventLink, eventSummary);
			}, function(){
				eventSummary.fadeOut('fast');
			});
		}
	});
	
	// housing pulldown code for houses/index.html and houses/locations.html
	$('.house_select').change(function(event){
		if($(this).val() !== '') window.location = $(this).val();
	});
		
	// search form stuff
	$('#searchform .search_text input').focus(function(event){
		if($(this).val() == 'SEARCH OUR SITE') $(this).val('');
	}).blur(function(event){
		if($(this).val() == '') $(this).val('SEARCH OUR SITE');
	});
	
	$('#searchform .search_submit a').click(function(event){
		$('#searchform').submit();
		return false;
	});
});

function showDetailsPopup(link, popup){
	// Bind variables before fadeIn so that css manipulation happens as fast as possible.
	var linkPos 		= link.position(),
			popupHeight = popup.outerHeight(),
			popupWidth 	= popup.outerWidth(),
			linkHeight 	= link.outerHeight(),
			linkWidth 	= link.outerWidth(),
			windowTop 	= $(window).scrollTop(),
			windowRight = $(window).width() + $(window).scrollLeft();
	// Position the popup and start fading in, oh, and 17 is the width of the little [+] icon.
	popup.css({left: linkPos.left + linkWidth - 17, top: linkPos.top - popupHeight}).fadeIn('fast');
	// All of this calculation code requires non-hidden elements, that's why fadeIn is called before
	// any of this. This code executes so fast though, that it's done before the fade effect completes.
	if(windowTop >= popup.offset().top) popup.css('top', linkPos.top + linkHeight);
	if(windowRight <= popup.offset().left + popupWidth) popup.css('left', linkPos.left - popupWidth + linkWidth);
}
