// target image
var img;
var img2;
//current slide numerically
var current;
var next;
var timer;

//if images are being cycled, set clickable to false
var clickable = true;

//different slides
var s0 = {}; 
s0.image = new Image();  
s0.image.src = '/images/slide-landscaping.jpg';	
s0.image.alt='landscaping';
var s1 = {};
s1.image = new Image();  
s1.image.src = '/images/slide-ground.jpg';
s1.image.alt='ground-maintenance';
var s2 = {};
s2.image = new Image();  
s2.image.src = '/images/slide-fencing.jpg';
s2.image.alt='fencing';
var s3 = {};
s3.image = new Image();  
s3.image.src = '/images/slide-design.jpg';		
s3.image.alt='design';

// Read More Links
s0.href='/services/landscape/';
s1.href='/services/ground-maintenance/';
s2.href='/services/fencing/';
s3.href='/services/design/';

//slides array
var slides = [s0, s1, s2, s3];

//the function to cycle through slides
function cycle() {
	clickable = false;
	$('#tabs .read-more').hide();
	
	$(img).animate({left: '-974px'}, 500, function() {
		
		img2 = $(this);
		img2.css('left', '974px');
		$(img2).children('img').attr('src', slides[next].image.src);
		$(img2).children('img').attr('alt', slides[next].image.alt);
		$(img2).children('a').attr('href', slides[next].href);
	});
	$(img2).animate({left: '0px'}, 500, function() {
		img = $(this);
		clickable = true;
		$('#tabs .read-more').show();
	});
	
	if (current < 3) { current++; } else { current = 0; }
	if (next < 3) { next++; } else { next = 0; }
	
	$('#tabs .tab-nav li a').removeClass('active');
	$('#tabs .tab-nav .' + slides[current].image.alt + ' a').addClass('active');
}

//code to run when page loads
$(document).ready(function() {
	//holder of images
	holder = $('#img-slide');
	
	//the divs containing the img elements (because of IE)
	img = $('#img');
	img2 = $('#img2');	
	img3 = $('#img3');
	img4 = $('#img4');
	
	//finding the current slide
	switch($('#tabs .tab-nav .active').parent().attr('class')) {
		case 'landscaping': next = 1; break;
		case 'ground-maintenance': next = 2; break;
		case 'fencing': next = 3; break;
		case 'design': next = 0; break;
	}
	
	//alter current
	if (next > 0) { current = next - 1;} else { current = 3; }
	
	timer = setInterval("cycle()", 6000);
	
	$('.tab-nav a').click(function() {
		if (clickable == true) {
			clearInterval(timer);
			switch($(this).parent().attr('class')) {
				case 'landscaping': next = 0; break;
				case 'ground-maintenance': next = 1; break;
				case 'fencing': next = 2; break;
				case 'design': next = 3; break;
			}
			if (next != current) {
				if (next > 0) { current = next - 1; } else { current = 3; }
				$(img2).children('img').attr('src', slides[next].image.src);
				$(img2).children('a').attr('href', slides[next].href);
				cycle();
			} else {
				if (current < 3) { next = current + 1; } else { next = 0; }
			}
			timer = setInterval("cycle()", 6000);
		}
	});
	
});