$(document).ready(function() {
	var timerval;
	var countImages = $('.slide-image').size();
	var count = 0;
	var squareTab = 0;
	var clickTriggered = 0;
	
	$('#image-location > div').each(function(){
		$(this).attr('id','id-'+squareTab);
		squareTab++;
	});
	
	$('.slide-image').each(function(){		
		$(this).addClass('fade'+count);	
		if(count>0){
			$(this).hide();
		}
		count++;
	});
	count=0;
	
	if(countImages >1){
		timeron();
	}
	
	function timeron(){
		clearInterval(timerval);
		timerval = setInterval(nextImage,8000);
	} 
	
	
	function nextImage() {
		$('.fade'+count).fadeOut();
		$('#id-'+count).removeClass('green-sq').addClass('white-sq');
		if(count<countImages-1 && clickTriggered ==0){
			count++;
		} else if(clickTriggered ==1){
			count = $('.clicked').attr('id').replace('id-','');
			clickTriggered =0;
			$('.clicked').removeClass('clicked');
		} else {
			count=0;
		}
		$('#id-'+count).addClass('green-sq').removeClass('white-sq');
		$('.fade'+count).fadeIn();
	}
	
	$("#image-location div").click(function(){
		clickTriggered =1;
		$(this).addClass('clicked');
		nextImage();
		timeron();
	})

});