$(document).ready(function(){	

	$('#slider').cycle({ 
		fx:      'fade', 
		speed:    800, 
		timeout:  4000,
		random:   1,
		pager:   '#slider-nav',
  	pagerAnchorBuilder: pagerFactory
	});
	
	function pagerFactory(idx, slide) {
  	return '<li class="ir"><a href="#">'+(idx+1)+'</a></li>';
  };
  
  $('#article-slider').cycle({
  	fx:				'fade',
  	speed:		 2000,
  	timeout:	 1000
  });
	
	
	// Adds ability to link to specifics slides - must come first to work correctly   
	var index = 0, hash = window.location.hash;
	if (hash) {
		index = /\d+/.exec(hash)[0];
		index = (parseInt(index) || 1) - 1; // slides are zero-based
	}

	// Setup for Cycle Plugin
 	$('#timeline-slider').cycle({ 
    fx:							'scrollHorz', 
    prev:   				'#prev-btn', 
    next:   				'#next-btn',
    speed:					 600, 
    timeout:				 0, 
    fit:						 true,
    before:	 				 onBefore,
    startingSlide: 	 0,
    pager:  				'#timeline-nav', 
		pagerAnchorBuilder: function(idx, slide) { 
			return '<li><div class="overlay"><a href="#"><img src="' + jQuery(slide).find('img').attr('src') + '" width="100" /></div><h4>' + jQuery(slide).find('h2').eq(0).text() + '</h4></a></li>'; 
		} 
	});
	
	var bc = $('#controls');
	
	$('#timeline-slider').children().each(function(i, slide) { 
		// create input 
		$('<li><a href="#">' + jQuery(slide).find('h2').eq(0).text() + '</a></li>') 
			// append it to button container 
			.appendTo(bc) 
			// bind click handler 
			.click(function(idx) { 
	    	// cycle to the corresponding slide 
	    	$('#timeline-slider').cycle(i); 
	    	return false; 
		}); 
	}); 
	
	function onBefore(curr,next,opts) {
					
		// Shows current slide number
		current = $('#timeline-slider, #controls').children().index(this);
		//$('#current-slide').html(current + 1); 
		
		// Adds ability to link to certain slides
		{window.location.hash = current + 1;}
				
		//Centers the active thumbnail when slideshow is playing or next/previous buttons are clicked
		var carousel = $('#timeline-nav, #controls').data('jcarousel');
		var activeIdx = $('#timeline-nav img[src="'+next.src+'"], #controls li').closest('a').data('index') -2;
 		if (carousel)
   		{
      	carousel.scroll(activeIdx);
   		}
		
	};
	
	
	// Adds index to thumbnail links
	$('#timeline-nav li a, #controls li a').each(function(idx) {
	      $(this).data('index', (++idx));
	});
	
	//jCarousel
	$('#timeline-nav').jcarousel({
		scroll: 	6,
		visible: 	6,
		initCallback: initCallbackFunction
	});
	
	
	//Centers thumbnail when clicked as long as its not the first 2 or last 2.
	function initCallbackFunction(carousel) {
	  $('#controls li a').bind('click', function() {
	    var idx =  $(this).data('index') - 2;
	    carousel.scroll(idx);
	  }); 		
	};
  
  // Superfish
	$('#main-nav').superfish({
		autoArrows:   false, 
		dropShadows:	false,
		disableHI:		false
	}); 




});


$(window).load(function() {
	$('.client-content img, #testimonials figure img').vAlign();
});
