$(function() { // Made with Joy by What Cheer
	
	// Enter your HTML here
	var slide4 = '<div id="slide4"><a href="/about/joel-larmore-pt/"><img src="/wp-content/themes/rehabvisions/images/content/home/slider/family.jpg" alt="Family Owned and Opertated " width="730" height="330" /></a></div>';
	var slide1 = '<div id="slide1"><a href="/stories/"><img src="/wp-content/themes/rehabvisions/images/content/home/slider/story-smalltown.jpg" alt="Small Town, Big Growth" width="730" height="330" /></a></div>';
	var slide2 = '<div id="slide2"><a href="/about/"><img src="/wp-content/themes/rehabvisions/images/content/home/slider/wearetherapists.jpg" alt="We are Therapists Ourselves" width="730" height="330" /></a></div>';
	var slide3 = '<div id="slide3"><a href="/stories/"><img src="/wp-content/themes/rehabvisions/images/content/home/slider/story-requestalocation.jpg" alt="Request a Location" width="730" height="330" /></a></div>';	

	var timer = 8000;
	scrollInterval = setInterval(function() { advance(); }, timer);
	$('.hoverstop').hover(function() {
	    clearInterval(scrollInterval);
	  }, function() {
	    scrollInterval = setInterval(function() { advance(); }, timer);
	  });
	
	$('#faderright a').click(function(){

		var currentSelected = $('#fadercontent div').attr('id');
		if(currentSelected == 'slide1'){
			sitem = 'slide2';
		}
		if(currentSelected == 'slide2'){
			sitem = 'slide3';
		}
		if(currentSelected == 'slide3'){
			sitem = 'slide4';
		}
		if(currentSelected == 'slide4'){
			sitem = 'slide1';
		}
		switchSlide(sitem);
		clearInterval(scrollInterval);
		return false;
	});

	$('#faderleft a').click(function(){
		var currentSelected = $('#fadercontent div').attr('id');
		if(currentSelected == 'slide1'){
			sitem = 'slide4';
		}
		if(currentSelected == 'slide2'){
			sitem = 'slide1';
		}
		if(currentSelected == 'slide3'){
			sitem = 'slide2';
		}
		if(currentSelected == 'slide4'){
			sitem = 'slide3';
		}
		switchSlide(sitem);
		clearInterval(scrollInterval);
		return false;
	});
	
	// preload images
		$('<img />')
				.attr('src', '/wp-content/themes/rehabvisions/images/content/home/slider/story-smalltown.jpg')
				.load(function(){
				$('.profile').append( $(this) );
				});
		$('<img />')
				.attr('src', '/wp-content/themes/rehabvisions/images/content/home/slider/family.jpg')
				.load(function(){
				$('.profile').append( $(this) );
				});
		$('<img />')
				.attr('src', '/wp-content/themes/rehabvisions/images/content/home/slider/story-requestalocation.jpg')
				.load(function(){
				$('.profile').append( $(this) );
				});

	// end preload images

function switchSlide(item){
	var currentSelected = $('#fadercontent div').attr('id');
	if(currentSelected != item){
		if(item == 'slide1'){
			$('#fadercontent').fadeOut('fast', function(){
				$(this).html(slide1);
			}).fadeIn();
		}
		if(item == 'slide2'){
			$('#fadercontent').fadeOut('fast', function(){
				$(this).html(slide2);
			}).fadeIn();
		}
		if(item == 'slide3'){
			$('#fadercontent').fadeOut('fast', function(){
				$(this).html(slide3);
			}).fadeIn();
		}
		if(item == 'slide4'){
			$('#fadercontent').fadeOut('fast', function(){
				$(this).html(slide4);
			}).fadeIn();
		}
	}
}

function advance(){
	var currentSelected = $('#fadercontent div').attr('id');
	// the problem here is I can't keep a running total of the item...
	if(currentSelected == 'slide1'){
		switchSlide('slide2');
	}
	if(currentSelected == 'slide2'){
		switchSlide('slide3');
	}
	if(currentSelected == 'slide3'){
		switchSlide('slide4');
	}
	if(currentSelected == 'slide4'){
		switchSlide('slide1');
	}
}
});
