
var mycarousel_initCallback = function(carousel) {
	var startInteractiveMode = function(carousel) {
		carousel.stopAuto();
		carousel.options.easing = 'swing',
		carousel.options.animation = 500;
	};
	jQuery('.slides .buttons a').bind('click', function() {
		startInteractiveMode(carousel);
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
		return false;
	});
	jQuery('.slides .prevnext .prev').bind('click', function() {
		startInteractiveMode(carousel);
		carousel.prev();
		return false;
	});
	jQuery('.slides .prevnext .next').bind('click', function() {
		startInteractiveMode(carousel);
		carousel.next();
		return false;
	});

	jQuery('.slides .slide').show();

	var callback = function(carousel) {
		carousel.options.auto = 5;
		carousel.next();
		carousel.startAuto();
	};
	setTimeout(function() { callback(carousel); }, 5000);
};

var mycarousel_itemVisibleInCallback = function(carousel, li, idx) {
	jQuery('.slides .buttons a').removeClass('active');
	jQuery('.slides .buttons a').eq(idx - 1).addClass('active');
};

$.extend($.easing, {
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	}
});

$(function() {
	$('img.caption[alt]')
	.filter( function(){ return this.alt.length; })
	.each(function() {
		var oldImg = $(this);
		var img = oldImg.clone();

		var div = $('<div>');
		div.addClass('image-caption');
		if (img.attr('align')) {
			div.attr('align', img.attr('align'));
			img.attr('align', '');
		}
		if (img.hasClass('alignright')) {
			div.addClass('alignright');
			img.removeClass('alignright');
		}
		if (img.hasClass('alignleft')) {
			div.addClass('alignleft');
			img.removeClass('alignleft');
		}
		div.css('width', img.width());

		var p = $('<p>');
		p.text(img.attr('alt'));
		div.append(img);
		div.append(p);
		oldImg.replaceWith(div);
	});
});

$(function() {
	$('.slides ul').jcarousel({
		scroll: 1,
		animation: 3000,
		easing: 'easeInOutQuint',
		initCallback: mycarousel_initCallback,
		itemVisibleInCallback: {
			onBeforeAnimation: mycarousel_itemVisibleInCallback },
		buttonNextHTML: null,
		buttonPrevHTML: null,
		wrap: 'both'
	});

	$('.slide').each(function() {
		var slide = $(this);
		var text = slide.find('.text');
		var slideHeight = slide.height();
		var textHeight = text.height();
		if (slideHeight > textHeight) {
			var offset = $('html').hasClass('home') ? -20 : -12;
				// The text is a little higher than center
			var top = (slideHeight - textHeight)/2;
			if (top + offset > 5)
				top += offset;
			text.css({ top: top });
		}
	});
});

$(function() {
	var selectors = ['#header .navigation li', '#header .quick li',
			'#sidebar .section-tree li', '#map li li'];
	jQuery.each(selectors, function(i, string) {
		var navLis = jQuery(string);
		if (navLis.length > 0) {
			navLis.each(function() {
				var navLi = $(this);
				navLi.children('a').click(function() {
					var navA = $(this);
					var navLi = navA.closest('li');
					navLis.removeClass('active');
					navLi.addClass('active');
				});
			});
		}
	});
});

$(function() {
	$('input[placeholder], textarea[placeholder]').placeholder();
});

$(function() {
	$('select.style1').customStyle();
});

$(function() {
	$('.school-form form').each(function() {
		var form = $(this);
		var message = form.find('.message');
		var submit = form.find('input[type=submit]');

		submit.removeAttr('disabled'); // Firefox was keeping it disabled between reloads

		form.submit(function() {

			submit.attr('disabled', true);
			$.ajax({
				type: 'POST',
				url: '/school_submissions/add',
				data: form.serialize(),
				success: function(data) {
					if (data.exit !== 0) {
						var messageString = 'Something went wrong, please try again.';
						if (data.message)
							messageString = data.message;
						message.removeClass('good').addClass('bad').html(messageString);
						submit.removeAttr('disabled');
					} else {
						var messageString = 'Thank you! We will get back to you soon.';
						message.removeClass('bad').addClass('good').html(messageString);
						form.find('input[type=submit]').hide();
					}
				}
			});
			return false;
		});
	});
});

