jQuery(function ($) {
	var submitClick = function () {
			$(this).parents('form').submit();
			return false;	
		},
		contactStringToEmail = function (contact) {
			return contact.replace(/(.*)\.(.*\..*)/, '$1@$2');
		},
		series = $('.series').size(),
		carousel = $('.carousel').size(),
		beginSeries = function () {
			$('.series').cycle({
				before: function (current, next) {
					if (!$(this).data('loaded') && $('span.img', this).size()) {
						var imageUrls = $('span', this).attr('rel'),
							initialUrl;
						
						if (imageUrls) {
							imageUrls = imageUrls.split(';');
						}
						
						if (imageUrls.length > 1) {
							initialUrl = imageUrls[1];
						} else {
							initialUrl = imageUrls[0];
						}
						
						$('span', this).append('<img src = "%s" alt="" />'.replace('%s', initialUrl));
						$(this).data('imageUrls', imageUrls);
						$(this).data('loaded', true);
					}
				}
			});
		},
		beginCarousel = function () {
			$('.carousel').jcarousel({
				scroll: 4,
				auto: 16,
				itemVisibleInCallback: function (carousel, item) {
					if (!$(item).data('loaded') && $('span.img', item).size()) {
						var imageUrls = $('span', item).attr('rel'),
							initialUrl;
						
						if (imageUrls) {
							imageUrls = imageUrls.split(';');
						}
						
						if (imageUrls.length > 1) {
							initialUrl = imageUrls[1];
						} else {
							initialUrl = imageUrls[0];
						}
						
						$('span', item).append('<img src = "%s" alt="" />'.replace('%s', initialUrl));
						$(item).data('imageUrls', imageUrls);
						$(item).data('loaded', true);
					}
				}
			});
		};
	
	$('#home_slideshow ul').cycle({
		before: function (current, next) {
			if (!$(this).data('loaded')) {
				if ($('.img', this).size()) {
					$('.img', this).replaceWith('<img src="%s" alt="" />'.replace('%s', $('.img', this).attr('rel')));
				}
				$(this).data('loaded', true);
			}
		}
	});
	
	$('.slides').cycle({
		timeout: 7500,
		pause: true,
		before: function (current, next) {
			if (!$(this).data('loaded')) {
				if ($('.img', this).size()) {
					$('.img', this).replaceWith('<img src="%s" alt="" />'.replace('%s', $('.img', this).attr('rel')));
				}
				$(this).data('loaded', true);
			}
		}
	});
	$('form:not(.post_search_form):not(#paypalForm) [type=submit]').each(function() {
		$(this)
			.hide()
			.after('<a href="#" class="button submit"><span>' + $(this).val() + '</span></a>');
	});
	$('a.submit').click(submitClick);
	$('form input[type=file]').addClass('file');
	$('a.contact').each(function () {
		var email = contactStringToEmail($(this).text());
		$(this).attr('href', ['mail', 'to', ':', email].join('')).text(email);
	});
	$('.question_list li > a').live('click', function () {
		$(this).parent().find('> div').stop();
		if ($(this).parent('li').is('.active')) {
			$(this).parent().find('> div').css({height: 'auto'}).slideUp(function () {
				$(this).parent('li').removeClass('active');
			});
		} else {
			$(this).parent().find('> div').css({display: 'none', height: 'auto'}).slideDown(function () {
				$(this).parent('li').addClass('active');
			});
		}
		return false;
	});
	
	if (series && $.fn.cycle) {
		beginSeries();
	}
	
	if (carousel && $.fn.jcarousel) {
		beginCarousel();
	}
	
	$('ul.sponsors li').bind('mouseenter', function () {
		var imageUrls = $(this).data('imageUrls');
		
		if (imageUrls && imageUrls.length > 1) {
			$('img', this).attr('src', $(this).data('imageUrls')[0]);
		}
	}).bind('mouseleave', function () {
		var imageUrls = $(this).data('imageUrls');
		
		if (imageUrls && imageUrls.length > 1) {
			$('img', this).attr('src', $(this).data('imageUrls')[1]);
		}
	});
});

