Gallery = $.klass({
	initialize: function(links, featuredImage) {
		if (!links || !featuredImage) return false;

		var $this = this;

		$this.featuredImage = featuredImage;

		links.each(function(i, link) {
			var link = $(link);

			if (i == 0) link.addClass('loaded selected');

			link.click(function(event) {
				event.preventDefault();

				$this.unselectCurrent(link);
				$this.swapPic(link);
			});
			
			link.mouseover(function() {
				link.addClass('hover');
			});
			
			link.mouseout(function() {
				link.removeClass('hover');
			});
		});
	},

	unselectCurrent: function(link) {
		var ul = link.parent('li').parent('ul').get();
		var selected = $(ul).find('a.selected');
		selected.removeClass('selected');
	},
	
	swapPic: function(link) {
		var $this = this;

		link.addClass('loaded selected');

		$($this.featuredImage).fadeOut('slow', function() {
			// get src of new image
			var newSrc = link.attr('href');

			// load new image
			var newImg = new Image();
			$(newImg).attr('src', newSrc).hide();
			$($this.featuredImage).attr('src', newSrc);

			// after it's loaded...
			$(newImg).load(function() {
				$($this.featuredImage).fadeIn('slow');
			});
			if (link.hasClass('loaded') && $.browser.msie) {
				$($this.featuredImage).fadeIn('slow');
			}
		});
	}
}); // Gallery



Communities = $.klass({
	links: [],

	initialize: function(div, initial) {
		if (!div) return false;

		var $this = this;
		$this.div = div;

		if (initial) {
			$this.div.find('a:first').addClass('first selected');
			$this.div.find('a:last').addClass('last');
		}
		
		// set event handlers for arrows
		$('#previous').click(function(event) {
			$this.swap(event, 'previous');
		});
		$('#next').click(function(event) {
			$this.swap(event, 'next');
		});
	},
	
	swap: function(event, type) {
		event.preventDefault();
		var $this = this;

		var currentCommunity = $this.div.find('a.selected');

		var newCommunity;
		if (type == 'previous') {
			if (currentCommunity.hasClass('first')) {
				newCommunity = currentCommunity.parent('li').parent('ul').find('li a.last');
			} else {
				newCommunity = currentCommunity.parent('li')
				.prev('li')
				.find('a')				
			}
		} else if (type == 'next') {
			if (currentCommunity.hasClass('last')) {
				newCommunity = currentCommunity.parent('li').parent('ul').find('li a.first');
			} else {
				newCommunity = currentCommunity.parent('li')
				.next('li')
				.find('a')				
			}
		}

		newCommunity.addClass('selected');
		currentCommunity.removeClass('selected');

		// hide current community content
		featuredDiv = $('#featured');
		featuredDiv.wrapInner('<div></div>');
		var innerDiv = featuredDiv.children('div');
		innerDiv.fadeOut('slow', function() {
			innerDiv.remove();

			// load new community with ajax
			var href = newCommunity.attr('href');
			featuredDiv.load(href, function() {
				var gallery = new Gallery($('#homegallery a'), $('#featuredimage'));
				var communities = new Communities($('#communities'));
			});
		});

	}
}); // Communities



$(document).ready(function() {

	// global nav hovers
	$('#nav ul').superfish({
		delay: 400
	});
	
	if ($('body').hasClass('home')) {
		var gallery = new Gallery($('#homegallery a'), $('#featuredimage'));
		var communities = new Communities($('#communities'), 'yes');
	}
	
	if ($('body').hasClass('mhfaqs')) {
		$('#faq').makefaq();
	}
	
	if ($('body').hasClass('communities')) {
		$('#communities-subnav').accordion({
			active: false,
			header: 'span.toplevel',
			selectedClass: 'open',
			event: 'mouseover',
			autoHeight: false
		})
		if ($('body').hasClass('oregon')) $('#communities-subnav').accordion('activate', 0);
		if ($('body').hasClass('california')) $('#communities-subnav').accordion('activate', 1);
		if ($('body').hasClass('oklahoma')) $('#communities-subnav').accordion('activate', 2);

		// if it's a community details page, also create accordion for info sidebar
		if ($('body').hasClass('details')) {
			$('#info').accordion({
				header: 'h3',
				selectedClass: 'open',
				event: 'mouseover',
				autoHeight: false
			});
			var gallery = new Gallery($('#gallery a'), $('#featuredimage'));			
		}
	}
	
	if ($('body').hasClass('housingneeds') && $('body').hasClass('details')) {
		var gallery = new Gallery($('#gallery a'), $('#featuredimage'));
	}
});