jQuery(document).ready(function($) {

// Adding classes for the features 
	$('#features article:last-child').addClass('last');


// Adding classes for the footer
	$('ul#sitemap > li').addClass('section');
	$('footer').find('li').find('li');
	$('footer').find("li:contains('Where We Work')").find('ul.children').addClass('where_we_work');


// Removing bottom border on subnav
	$('nav#secondary li:last-child').css('border-bottom', 'none');


// Add class to first paragraph of articles
//	$('#content_column article p').first().addClass('first');


// Spacing the features on landing pages
	$('#landing_features article:first-child').css('margin-right', '30px');


// Footer Profiles {
	$('#sitemap .page-item-12 .children').append('<li><a href="our-people/serving-profiles">Serving Profiles</a></li>');


// Serving Profiles

	$('#serving_profiles .text').hide();
	$('#serving_profiles .excerpt .more_link').hide();
	$('#serving_profiles .excerpt p').append('...')

	$('#serving_profiles .more_link').click(function(e) {
		e.preventDefault();
		$(this).prev("section").slideToggle('fast');
		$(this).prev("section").prev("section").slideToggle('fast');
		var clicked = $(this);
//		$('.more_link').not(clicked).prev('section').slideUp('fast');
	});
	
	$('#serving_profiles .more_link').toggle(function() {
		$(this).text('Read Less');
	}, function() {
		$(this).text('Read More');
	});
	
	
});

var offset= 0;
var currentImage=0;
var containerOffset = 0;
var containerPosition = 0;
var images = new Array();


jQuery(document).ready(function($) {
	// Stuff to do as soon as the DOM is ready;
	init();	


function init()
{
	//get the width of the gallery object we are working with.
	offset = $('#slides').width() + containerOffset ;
	//get reference to all the images that we have to work with this function returns an array of all the images so we'll set our
	//variable to the return of the "find function"
	images = $('.slides').find('article');
	
	$.each(images, function(index, val) {
	  	var css = {'left': (index * offset)};
		$(val).css(css);
		var dot = $('<span/>');
		
		if (index == 0)
		{
			dot.addClass('active');
		} else {
			dot.addClass('inactive');
		}
		
		dot.addClass('inactive');
		$('#thumbs').append(dot);
		
		$(dot).click(function(event) {
			var index = $(this).closest('#thumbs span').index();
			showImage(index);
		});

	});
	
	thumbs = $.find('#thumbs span');
	
}

function nextImage()
{
	currentImage++;
	if(currentImage >= images.length)
		currentImage = 0;		

	showImage(currentImage);

	
}



function showImage(indexOfImageToShow)
{
	currentImage = indexOfImageToShow;
	containerPosition = -(currentImage * offset - containerOffset);
	$('.slides').animate({"left": containerPosition, 'position':'absolute'}, '600');
	
	$.each(thumbs, function(index, val) {
	  if(index == currentImage)
		{
			$(val).addClass('active').removeClass('inactive');
		} else {
			$(val).removeClass('active').addClass('inactive');
		}
	});
}

	var locked = false;
	var autopager = window.setInterval(function(){
	    if (!locked) {
	        locked = true;
	        window.setTimeout(function(){
	            locked=false;
	        }, 100);
	        nextImage();
	    }
	}, 10000);
	

	$('.main_view article').click(function(event) {
		window.location=$(this).find("a").attr("href");return false;
	});
	
	$('.main_view article').css('cursor', 'pointer');
	
	$('.main_view article').hover(function() {
		$('.text .more a').css('text-decoration', 'underline')
	}, function() {
		$('.text .more a').css('text-decoration', 'none')
	});

});

//////////////////////////////////////////////////////////////////////////////////////////

jQuery(document).ready(function($) {
	//Show the paging and activate its first link
	$(".paging").show();
	$(".paging a:first").addClass("active");

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $(".window").width();
	var imageSum = $(".image_reel img").size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$(".image_reel").css({'width' : imageReelWidth});
	
	//Paging  and Slider Function
	rotate = function(){
	    var triggerID = $active.attr("rel") - 1; //Get number of times to slide
	    var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

	    $(".paging a").removeClass('active'); //Remove all active class
	    $active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

	    //Slider Animation
	    $(".image_reel").animate({
	        left: -image_reelPosition
	    }, 500 );

	}; 

	//Rotation  and Timing Event
	rotateSwitch = function(){
	    play = setInterval(function(){ //Set timer - this will repeat itself every 7 seconds
	        $active = $('.paging a.active').next(); //Move to the next paging
	        if ( $active.length === 0) { //If paging reaches the end...
	            $active = $('.paging a:first'); //go back to first
	        }
	        rotate(); //Trigger the paging and slider function
	    }, 10000); //Timer speed in milliseconds (7 seconds)
	};

	rotateSwitch(); //Run function on launch

	//On Hover
	$(".image_reel a").hover(function() {
	    clearInterval(play); //Stop the rotation
	}, function() {
	    rotateSwitch(); //Resume rotation timer
	});	

	//On Click
	$(".paging a").click(function() {
	    $active = $(this); //Activate the clicked paging
	    //Reset Timer
	    clearInterval(play); //Stop the rotation
	    rotate(); //Trigger rotation immediately
	    rotateSwitch(); // Resume rotation timer
	    return false; //Prevent browser jump to link anchor
	});
});


