var currentCarouselImage = 0;
var intervalID;

$(document).ready(function(){
	
	$("#skillsCarousel").jCarouselLite({
        btnNext: "#skillsCarouselWrapper #next",
        btnPrev: "#skillsCarouselWrapper #prev",
        speed: 400
    });
    
    $("#skillsCarousel .button").hover(function() {
    	var skillsPageID = $(this).attr('id').substr(6);
    	var skillsSynopsis = $("#description"+skillsPageID).html();
    	$("#skillsDescriptionBox #description").html(skillsSynopsis);
  
    });
    
    $("#skillsCarousel .button").click(function() {
    	var thisID = $(this).attr('id');
    	var link = $("#"+thisID+"_href").attr('value');
    	document.location.href = link;
  
    });
    
    $("#skillsCarousel .button:first-child").hover();
	intervalID = setTimeout( 'scrollGallery()', 9000 );

    //scrollGallery(0);
    $('#imglink0').css('backgroundColor','#993');
    
    $(".gallery #navigation .imglink").click(function() {
    	var imgID=$(this).attr('id').substr(7);
    	scrollGallery(imgID);
    	clearTimeout( intervalID );
    });

});


function scrollGallery(nextImgID) { 


	var imgCount = $(".gallery_wrapper img").length;
	
	thisImgID = currentCarouselImage;
	
	if (! nextImgID ) nextImgID = ++currentCarouselImage;
	
	
	if(nextImgID > imgCount-1) nextImgID=0; 
		
	var imgWidth = $('.gallery #img'+nextImgID).attr('width');
	
	//var position = $('.gallery #img'+imgID).position();
	//console.log('.gallery #img'+imgID+' pos: '+position.left);
	$(".gallery_wrapper").animate( {
		marginLeft: "-"+(imgWidth*nextImgID)+"px"
	} , 1000, function() {
		$('.imglink').css('backgroundColor','#fff');
		$('#imglink'+nextImgID).css('backgroundColor','#993');
		currentCarouselImage = nextImgID;
		intervalID = setTimeout( 'scrollGallery()', 9000 );
	}); 

}
