//a simple function to click next link
//a timer will call this function, and the rotation will begin :)  
var contactClicked = false;
var speed = 8000; //5000
var run = setInterval('rotate('+contactClicked+')', speed);	

function rotate(contactClicked) {
	if(contactClicked == false){
		$('#next').click();
	} else {
		clearInterval(run);	
	}
	return false;
}
$(document).ready(function() {
	
	//grab the width and calculate left value
	var item_width = $('#slides li').outerWidth(); 
	var left_value = item_width * (-1);        
	//stop everything if form clicked
	$('#contactForm1 input').click(function(){
		//alert('clicked');
		contactClicked = true;
		clearInterval(run);
		return false;
	});
    //move the last item before first item, just in case user click prev button
	$('#slides li:first').before($('#slides li:last'));
	//set the default item to the correct position 
	$('#slides ul').css({'left' : left_value});

	$('#prev').click(function() {
		//get the right position            
		var left_indent = parseInt($('#slides ul').css('left')) + item_width;
		//slide the item            
		$('#slides ul:not(:animated)').animate({'left' : left_indent}, 200,function(){   
            //move the last item and put it as first item            	
			$('#slides li:first').before($('#slides li:last')); 
			//set the default item to correct position
			$('#slides ul').css({'left' : left_value});
		
		}); 
		clearInterval(run);
		//run = setInterval('rotate('+contactClicked+')', speed);
		return false;            
	});

	$('#next').click(function() {		
		//get the right position
		var left_indent = parseInt($('#slides ul').css('left')) - item_width;
		//slide the item
		$('#slides ul:not(:animated)').animate({'left' : left_indent}, 200, function () {
            //move the first item and put it as last item
			$('#slides li:last').after($('#slides li:first'));                 
			//set the default item to correct position
			$('#slides ul').css({'left' : left_value});
		});
		return false;
		
	});        
	
	//if mouse hover or form clicked, pause the auto rotation, otherwise rotate it
	if(!contactClicked){
		$('#slides ul li').hover(
			function() {
				clearInterval(run);
			}, 
			function() {
				run = setInterval('rotate('+contactClicked+')', speed);
			}
		); 	
	}
        
});   
