jQuery(document).ready(function(){		
				//jQuery('#slider-stage').carousel('#previous', '#next', '#nextslide' duration);
				jQuery('#viewport').carousel('#prev', '#next', '#nextslide', 1000);  
			
//The auto-scrolling function

	//harvesting the link paths
	var arrayVariable = [ ];	
    $("#viewport li > a").each(function (i){
       arrayVariable[i] = $(this).attr("href");	 
    });
	//Setting the initial overlink value
	jQuery('.overlink').attr('href',arrayVariable[1]);
	
	var thelength = arrayVariable.length;
	var mylength = 1;			
			
function slide(){
  $('#nextslide').click();
  //Changing the overlink value on rotate
  	mylength = mylength + 1;
	if(mylength < thelength){
	jQuery('.overlink').attr('href',arrayVariable[mylength]);
	}else if(mylength == thelength){
		mylength = 3;
	jQuery('.overlink').attr('href',arrayVariable[mylength]);
	}	
}


$('#prev').click(function() {
  	mylength = mylength - 1;
	if(mylength < 0){
		mylength = 4;
		}
	jQuery('.overlink').attr('href',arrayVariable[mylength]);  
});

$('#next').click(function(){
	mylength = mylength + 1;
	changelink();
	});

function changelink(){
	if(mylength < thelength){
	jQuery('.overlink').attr('href',arrayVariable[mylength]);
	}else if(mylength == thelength){
		mylength = 3;
	jQuery('.overlink').attr('href',arrayVariable[mylength]);
	}
}

//Launch the scroll every 2 seconds
var intervalId = window.setInterval(slide, 4000);

//On user hover deactivate auto-scrolling reactivate off hover
$('#prev, #next,.overlay').hover(
  function () {
   		window.clearInterval(intervalId);
  },
  function(){
	  window.clearInterval(intervalId);
	  intervalId = window.setInterval(slide, 4000);	  
	  });
			
});

