$(function() {
	// disable some primary navigation links
	$('a[href="/our-congregation/"], a[href="/shabbat-worship/"], a[href="/about-us/"], a[href="/contact/"]').click(function(e) {
		e.preventDefault();
	});

	$('a[href="#Contact"]').click(function() {
		$('#recipient').val($(this).attr('data-to'));
		$(this).fancybox({
			title: 'Contact ' + $(this).text(),
			titlePosition: 'inside',
			width: 700,
			height: 650,
			autoDimensions: false,
			autoScale: false,
			transitionIn: 'none',
			transitionOut: 'none',
			onClosed: function () {
				$('input:not(:submit), textarea').val('');
			}
		});
	});
	$('.overlay').fancybox();

	$('#calendarMenu select').change(function() {
		window.location = '/calendar/' + $('#year').val() + '/' + $('#month').val() + '/';
	});

	// hide rounded images. will be replaced with divs once all images are loaded
	$('img.rounded').css('visibility:hidden;');

	// move calendar item in footer to a different column
	$('footer .sf-menu > li:nth-child(3)').hide();
	$('footer .sf-menu > li:nth-child(1)').prepend('<li><a href="/">Home</a></li>');
	$('footer .sf-menu > li:nth-child(2)').prepend('<li><a href="/calendar/">Calendar</a></li>');

	// add asterisk to required form fields
	$('form *[required]').parent().prev().addClass('required').children().append(' *');

	// move checkbox labels to the dd element
	$('form input[type="checkbox"]').each(function() {
		$(this).after(' ' + $(this).parent().prev().children().text());
		$(this).parent().prev().children().remove();
	});
	$('form.ajax').append('<div class="whiteOut hide">&nbsp;</div>');

	// default AJAX form submission
	$('form.ajax').live('submit', function(e) {
		$form = $(this);
		e.preventDefault();
		$.ajax({
			type: 'POST',
			url: $(this).attr('action'),
			dataType: 'json',
			data: $(this).serialize(),

			beforeSend: function() {
				$form.children('.whiteOut').show().addClass('clearfix');
			},

			success: function(data) {
				$form.replaceWith(data.data);
			},

			error: function() {}
		});

	});

	// History page: make hashchange show/hide repective article
	if ($('#history').length) {
		if (location.hash.length) {
			loadTimeline();
		} else {
			window.location.hash = $('#timelineTabs li:first a').attr('href');
		}

		$(window).hashchange(function(e) {
			e.preventDefault();
			loadTimeline();
		});
		
		$('a.arrow').click(function(e) {
			e.preventDefault();
			if ($('#timelineTabs .current').length) {
				if ($(this).hasClass('right') && $('#timelineTabs .current').children('a').attr('href') != '#2000s') {
					window.location.hash = $('#timelineTabs .current').next().children('a').attr('href');
				} else if ($(this).hasClass('left') && $('#timelineTabs .current').children('a').attr('href') != '#1906') {
					window.location.hash = $('#timelineTabs .current').prev().children('a').attr('href');
				}
			} else {
				window.location.hash = $('#timelineTabs li:first a').attr('href');
			}
		});
	}
});

function loadTimeline() {
	$('#timelineArticles article:visible').hide();
	$('#timelineTabs .current').removeClass('current');
	$(window.location.hash).show();
	$('a[href="' + window.location.hash + '"]').parent().addClass('current');
	
	$('a.arrow').css('visibility','visible');
	
	if ($('#timelineTabs .current').is('#timelineTabs li:first-child')) {
		$('a.arrow.left').css('visibility','hidden');
	} else if ($('#timelineTabs .current').is('#timelineTabs li:last-child')) {
		$('a.arrow.right').css('visibility','hidden');
	}
}

window.onload = function() {
	// replace images with divs, set the image to the background, and round the corners
	$('img.rounded').each(function() {
		$(this).after('<div class="rounded" style="' + 'background-image:url(' + $(this).attr('src') + '); ' + 'height:' + $(this).height() + 'px; ' + 'width:' + $(this).width() + 'px"></div>');
		$(this).remove();
	});
};

