var slideTimer = 0;
var testimonialFunc = null;
var testimonialObj = null;

$(document).ready(function() {
	
	//	SLIDESHOW
	
	$('#slideshow ul.buttons li.prev a').click(
		function () {
			slide_prev();
			return false;
		}
	);
	
	$('#slideshow ul.buttons li.next a').click(
		function () {
			slide_next();
			return false;
		}
	);
	
	$('#slideshow ul.dots-nav li a').click(
		function () {
			slide_index( $('#slideshow ul.dots-nav li a').index( this ) );
			return false;
		}
	);
	
	slideTimer = slide_speed; // 4 sec * 1000
	if (slide_enabled == 1) {
		slideTimeout = setTimeout('slide_next()', slideTimer);
	}
	
	// TESTIMONIALS
	
	$('#testimonials a').click(
		function () {
			return false;
		}
	);
	
	$('#testimonials a').hover(
		function () {
			
			if (testimonialFunc != null) return false;
			
			testimonialObj = this;
			
			testimonialFunc = setTimeout(function () {

				var index = $('#testimonials a').index(testimonialObj);
				$('#testimonials a').not($(testimonialObj)).find('img').animate({
					'opacity':1
				},
				'fast');
				
				$(testimonialObj).find('img').animate({
					'opacity':1
				},
				'fast',
				function () {
					var quote = $('#testimonials blockquote:eq(' + index + ')');
					$('#testimonials blockquote').not(quote).fadeOut();
					quote.fadeIn();
					testimonialFunc = null;
				});

			}, 200);
			
		},
		function () {
			
			try {
				clearTimeout(testimonialFunc);
				testimonialFunc = null;
			} catch (ex) {
				
			}
			
		}
	)
	
});

function slide_mark_dot(index) {
	
	 $('#slideshow .dots-nav a').removeClass('active');
	 $('#slideshow .dots-nav a:eq(' + index + ')').addClass('active');
	
}

function slide_prev() {
	
	var slide = $('#slideshow .photos img:visible');
	slide.fadeOut();
	var prev = slide.prev();
	if (prev.length == 0) {
		prev = $('#slideshow .photos img:last');
	}
	prev.fadeIn();
	
	slide_mark_dot($('#slideshow .photos img').index( prev ) );
	
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout('slide_next()', slideTimer);
	
}

function slide_next() {

	var slide = $('#slideshow .photos img').filter(':visible');
	var next = slide.parent().next().find('img');
	slide.fadeOut();

	if (next.length == 0) {
		next = $('#slideshow .photos img:first');
	}
	next.fadeIn();
	
	slide_mark_dot( $('#slideshow .photos img').index(next) );
	
	clearTimeout(slideTimeout);
	if (slide_enabled == 1) {
		slideTimeout = setTimeout('slide_next()', slideTimer);
	}
	
}

function slide_index(index) {
	
	var slide = $('#slideshow .photos img:visible');
	slide.fadeOut();
	var next = $('#slideshow .photos img:eq(' + index + ')');
	if (next.length == 0) {
		next = $('#slideshow .photos img:first');
	}
	next.fadeIn();
	
	slide_mark_dot($('#slideshow .photos img').index( next ) );
	
	clearTimeout(slideTimeout);
	slideTimeout = setTimeout('slide_next()', slideTimer);
	
}
