$(document).ready(function(){
	var photocount = $('#photofader img').size();
	if(photocount > 1){
		$('#photofader br').remove();
		$('#photofader img:first-child').addClass('active').next('img').addClass('next');
		if($('#maincontent').hasClass('home')){
			var fadeInt = setInterval(function(){ fadeIt(); }, 5000);
		}
	}
	
	//	Subpage slider
		if($('#sub_slider img').size() > 1){
			$('#sub_slider br').remove();
			$('#sub_slider img:first-child').addClass('active').next('img').addClass('next');
			if(!$('#maincontent').hasClass('home')){
				var fadeInt = setInterval(function(){ subfadeIt(); }, 5000);
			}
		}
})

function fadeIt(){
	$('#photofader img.active').fadeOut('slow', function(){
		$(this).removeClass('active');
		if($('#photofader img.next').is(':last-child')){
			$('#photofader img.next').removeClass('next').addClass('active');
			$('#photofader img:first-child').fadeIn('slow').addClass('next');
		} else {
			$('#photofader img.next').removeClass('next').addClass('active').next('img').fadeIn('slow').addClass('next');
		}
	})
}
function subfadeIt(){
	$('#sub_slider img.active').fadeOut('slow', function(){
		$(this).removeClass('active');
		if($('#sub_slider img.next').is(':last-child')){
			$('#sub_slider img.next').removeClass('next').addClass('active');
			$('#sub_slider img:first-child').fadeIn('slow').addClass('next');
		} else {
			$('#sub_slider img.next').removeClass('next').addClass('active').next('img').fadeIn('slow').addClass('next');
		}
	})
}
function slide(distance){
	var newLocation = parseInt($('#sub_slider #slides').css('left').replace('px','')) + distance;
	if(newLocation == 0){
		$('#sub_slider #slides').css('left', ($('#sub_slider img').size()-1)*-Math.abs(distance))
	} else if(newLocation == ($('#sub_slider img').size()-1)*-Math.abs(distance)){
		$('#sub_slider #slides').css('left', 0);
	}
	$('#sub_slider .prev, #sub_slider .next').unbind('click');
	$('#sub_slider #slides').animate({
		left: '+='+distance
	}, function(){
		$('#sub_slider .prev').click(function(e){
			e.preventDefault;
			slide(Math.abs(distance));
		})
		$('#sub_slider .next').click(function(e){
			e.preventDefault();
			slide(-Math.abs(distance));
		})
	});
}
