// jQuery script to provide functionality of the carousel on the home page
// Creator: Lindsay Creek

//Function to turn all classes off when there are not clicked
function setActive(ref) {
	for(i = 0; i < 4; i++) {
		$("#" + (i+1)).attr("class", "off");
	}
	$("#"+ ref).attr("class", "on");
}

function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a').bind('click', function() {
		carousel.scroll(jQuery.jcarousel.intval(jQuery(this).attr('id')));
		return false;
	});

	jQuery('.jcarousel-scroll select').bind('change', function() {
		carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
		return false;
	});

	jQuery('#mycarousel-next').bind('click', function() {
		carousel.next();
		return false;
	});

	jQuery('#mycarousel-prev').bind('click', function() {
		carousel.prev();
		return false;
	});

	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(
		function() {carousel.stopAuto();},
		function() {carousel.startAuto();}
	);
};




function mycarousel_itemFirstInCallback(carousel, item, idx, state) {
	var currentImage = idx;
	if(currentImage == 1){setActive(1);}
	if(currentImage == 2){setActive(2);}
	if(currentImage == 3){setActive(3);}
	if(currentImage == 4){setActive(4);}
};

// Ride the carousel...
jQuery(document).ready(function() {
	jQuery("#mycarousel").jcarousel({
		scroll: 1,
		auto: 10,
		wrap: 'last',
		initCallback: mycarousel_initCallback,
		// This tells jCarousel NOT to autobuild prev/next buttons
		buttonNextHTML: null,
		buttonPrevHTML: null,
		itemFirstInCallback:  mycarousel_itemFirstInCallback
	});
					
	//Each call will turn the correct button to 'on' when clicked
	$('#1').click(function(event){setActive(1);});
	$('#2').click(function(event){setActive(2);});
	$('#3').click(function(event){setActive(3);});
	$('#4').click(function(event){setActive(3);});
});
