/* 
 * Used on the homepage for the content slider
 * 
 */

jQuery(document).ready(function(){
	// Activate first link
	var activeLink = jQuery('#slider-nav a:first');
	activeLink.addClass('splash-nav-active');
	
	// Width of a single content area
	var contentwidth = jQuery('#home-slider').width();
	
	// Hide content sections and apply css
	jQuery('div.slider-content:not(:first)').css({
		'position': 'absolute',
		'left': contentwidth,
		'display': 'none'
	});
	
	// Get first content section
	// Had to put this down here because IE doesn't recognize :not(:first)
	var contentSections = jQuery('div.slider-content').length;
	var prevContentSection = contentSection;
	var contentSection = jQuery('div.slider-content:first').css({
		'position': 'absolute',
		'left': 0,
		'display': 'block'
	});
	
	// Checks to see if mouse is over element so rotation can be halted
	var mouseOver = false;
	jQuery(".slider-content").hover(
		function() {
			mouseOver = true;
		},
		function() {
			mouseOver = false;
		}
	);
	
	// Function for slider and slider navigation
	rotate = function(){
		// Remove active class
		jQuery('#slider-nav a').removeClass('splash-nav-active');

		// Add active class
		activeLink.addClass('splash-nav-active');

		// Slider animation
		contentSection.show().animate({
			left: 0
		}, 1500);
		prevContentSection.animate({
			left: -contentwidth
		}, 1500, function() {
			jQuery(this).hide().css('left', contentwidth);
		});
	};
	
	// Set time for rotation of slider
	rotation = function(){
		play = setInterval(function(){
			if(!mouseOver && contentSections > 1) {
				// Get next content section
				prevContentSection = contentSection;
				contentSection = contentSection.next('div.slider-content');

				// Next slider nav
				activeLink = jQuery('#slider-nav a.splash-nav-active').next();
				if (activeLink.length === 0) {
					// Get first nav element
					activeLink = jQuery('#slider-nav a:first');

					// Get first content section
					contentSection = jQuery('div.slider-content:first');
				}
				rotate();
			}
		}, 5000);
	};
	
	getContentSection = function() {
		var linkNumber = activeLink.attr('rel');
		var counter = 1;
		
		jQuery('div.slider-content').each(function(){
			if (counter == linkNumber) {
				prevContentSection = contentSection;
				contentSection = jQuery(this);
				return false;
			}
			counter++;
		});
	};
	
	// Run rotation function
	rotation();
	
	// Onclick of a nav item
	jQuery('#slider-nav a').click(function(e){
		if (activeLink.attr('rel') != jQuery(this).attr('rel')) {
			activeLink = jQuery(this);
			getContentSection();
			clearInterval(play);
			rotate();
			rotation();
		}
		e.preventDefault();
	});
});
