﻿(function($) {
	function showNews(newsItem) {
		newsItem.find('div.newsItemBody').show('fast', function() {
			var prettyPhotos = $(this).parents('div.newsItem').addClass('newsItemOpen').find('a[rel="readyForPrettyPhoto"]');
			var length = prettyPhotos.size();
			if (length == 0) {
				return;
			} else if (length == 1) {
				prettyPhotos.attr('rel', 'prettyPhoto');
			} else {
				prettyPhotos.attr('rel', 'prettyPhoto[gallery]');
			}
			$.createGallery(prettyPhotos);
		});
	};
	function hideNews(newsItem) {
		newsItem.find('div.newsItemBody').hide('normal', function() {
			var prettyPhotos = $(this).parents('div.newsItem').removeClass('newsItemOpen').find('a[rel^="prettyPhoto"]');
			prettyPhotos.attr('rel', 'readyForPrettyPhoto');
		});
	};
	$(document).ready(function() {
		$('div.newsItem').each(function() {
			$(this).find('a.newsTitle').click(function() {
				var parentNewsItem = $(this).parents('div.newsItem');
				if (!parentNewsItem.is('.newsItemOpen')) {
					hideNews($('div.newsItemOpen'));
					showNews(parentNewsItem);
				}
				return false;
			});
		});
                if ($('div.newsItemOpen').length>0) showNews($('div.newsItemOpen'));
                else showNews($('div.newsItem:first'));
		$.prepareStandalonePhotos();
	});
})(jQuery);
