$(function(){
	
	time = 5000
	
	slide_count = 0
	
	$('#clients_wrapper li').hide();
	
	$('#clients_wrapper li:first').fadeIn(time).addClass("current_client")
	
	
	to = setTimeout(function(){
		fade_client()
	}, time)
	
	/* Testimonials
	***************************/
	$('.testimonial').hide()
	randomnumber = Math.floor(Math.random()* $('.testimonial').length )
	$('.testimonial:eq('+randomnumber+')').show()
	
	/* Home Slides
	***************************/
	//Set width
	$('#hero_slides_wrapper').css({
		width: $('#hero_slides_wrapper .slide').length * 928
	})
	$('#hero_slides_wrapper .slide').css({
		float: "left"
	})
	
	slide_positions = $('#hero_slides_wrapper .slide')
	
	to_slide = setTimeout(function(){
		slide()
	}, time)
	
	$('#hero li a').click(function(){
		i = $(this).parent().index()
		
		force_slide(i)
		
		return false
	})
	
})

function fade_client(){
	clearTimeout(to)
	to = setTimeout(function(){
		$('#clients_wrapper li.current_client').fadeOut(time, function(){
			if($('#clients_wrapper li.current_client').next().length){
				$('#clients_wrapper li.current_client').next().fadeIn(time, function(){
					$('#clients_wrapper li.current_client').removeClass("current_client")
					$(this).addClass("current_client")
					fade_client()
				})
			}
			else{
				$('#clients_wrapper li:first').fadeIn(time, function(){
					$('#clients_wrapper li.current_client').removeClass("current_client")
					$(this).addClass("current_client")
					fade_client()
				})
			}
		})
		
	}, time)
}

function slide(){
	clearTimeout(to_slide)
	to_slide = setTimeout(function(){
		
		slide_count ++	
		
		if(slide_count == $('#hero_slides_wrapper .slide').length){
			slide_count = 0
		}
		
		
		$('#hero_slides_wrapper').animate({
			marginLeft: 928 * -1
		}, 500, function(){
			
			last_slide = $('#hero_slides_wrapper .slide:first')
			
			$('#hero_slides_wrapper .slide:first').remove()
			
			$('#hero_slides_wrapper').append(last_slide)
			
			$(this).css({
				marginLeft: 0
			})
			
			$('.selected_marker').removeClass("selected_marker")
			

			$('#hero li a:eq('+ slide_count +')').addClass("selected_marker")

			
			to_slide = setTimeout(function(){
				slide()
			}, time)
		})
		
	}, time)
}

function force_slide(i){
	clearTimeout(to_slide)


	$('#hero_slides_wrapper .slide:not(:first)').remove()

	$('#hero_slides_wrapper').stop().fadeOut()

	$('#hero_slides_wrapper').html(slide_positions[i]).stop().fadeIn()
	
	$('.selected_marker').removeClass("selected_marker")
	

	$('#hero li a:eq('+ i +')').addClass("selected_marker")
	
}

