jQuery(document).ready(function() {
	
	$ = jQuery;
	
	var menuItems = $('#top-nav ul.main-menu > li');
	$('#top-nav ul.main-menu li.root:first').addClass('first');
	$('#top-nav ul.main-menu li.root:last').addClass('last');
	menuItems.not(':has(ul.below)').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	var separator = $('<li>').addClass('separator');
	menuItems.not(':last').after(separator);
	//$('#top-nav ul.main-menu > li:first').before(separator);
	var totalOuterWidth = 0;
	$('#top-nav ul.main-menu > li').each(function() {
		totalOuterWidth += $(this).outerWidth();
	});
	var freeArea = $('#top-nav .wrapper').width() - totalOuterWidth;
	var availPadding = freeArea / menuItems.not('.separator').length;
	var remainingPadding = 0;
	menuItems.not('.separator').each(function() {
		var actualPadding = availPadding/2;
		var realPadding = Math.floor(actualPadding);
		$(this).css('padding', '0 ' + realPadding + 'px');
		remainingPadding += (actualPadding - realPadding);
	});
	
	$('.cs-button').each(function() {
		$(this).click(function() {
			window.location.href = $(this).find('a:first').attr('href');
		});
	});
	
	// Dropdowns
	$('#top-nav ul.main-menu > li:has(ul.below)').each(function() {
		var hPos = 'left';
		if ($(this).hasClass('last')) {
			hPos = 'right';
		}
		$(this).qtip({
			content: $(this).find('ul.below'),
			position: {
				my: 'top ' + hPos,
				at: 'bottom ' + hPos
			},
			style: {
				classes: 'top-nav-dropdown',
				tip: false
			},
			hide: {
				fixed: true
			},
			show: {
				solo: true
			},
			events: {
				show: function(event, api) {
					api.elements.target.addClass('hover');
					if ($('#top-nav').hasClass('pri')) {
						api.elements.tooltip.addClass('pri');
					}
				},
				hide: function(event, api) {
					api.elements.target.removeClass('hover');
				}
			}
		});
	});
	
	$('#aof-buttons > div.button').each(function() {
		$(this).qtip({
			content: $(this).find('ul:first'),
			position: {
				my: 'top left',
				at: 'top right',
				adjust: {
					x: -5,
					y: -4
				}
			},
			style: {
				classes: 'aof-dropdown',
				tip: false
			},
			hide: {
				fixed: true
			},
			show: {
				solo: true
			},
			events: {
				render: function(event, api) {
					api.elements.tooltip.append($('<div>').addClass('bottom'));
				},
				show: function(event, api) {
					api.elements.target.addClass('hover');
				},
				hide: function(event, api) {
					api.elements.target.removeClass('hover');
				}
			}
		});
	});
	
	// Rotate images
	window.setTimeout(function() {
		if ($('body').hasClass('front')) {
			$('#top-features').jshowoff({ speed: 8000, controls: false, links: false, hoverPause: false });
		}
	}, 500);
	
	// Class schedule
	$('#class-schedule tbody tr').hover(function() {
		$(this).addClass('hover');
	}, function() {
		$(this).removeClass('hover');
	});
	
	// Automatically color datatables
	$('table.datatable tr:even').addClass('alt');
	$('table.datatable tr').hover(function() {
		$(this).addClass('hover');		
	}, function() {
		$(this).removeClass('hover');
	});

});;

