jQuery(document).ready(function() {

	jQuery("#home-slideshow-container").jcarousel({
		scroll:1,
		buttonNextHTML:null,
		buttonPrevHTML:null,
		initCallback:pCarousel.init,
		wrap: 'last',
		animation: pCarousel.animationSpeed,
		itemFirstInCallback: pCarousel.firstItem
	});
	
	jQuery(".home-promo").each(function () {
		var defaultImg = jQuery(this).find('a').children('img:first');
		jQuery(this).find('a').hover(function() {
			jQuery(defaultImg).hide();
		},function() { 
			jQuery(defaultImg).show();
		});
	});
	
});

var pCarousel = {
	animationId: null,
	animationDir: 'right',
	
	buttonHit: false,

	//time in miliseconds to wait before scrolling
	animationTimeout: 6000,

	//time in miliseconds for the scrolling transition
	animationSpeed: 800,

	init: function(carousel) {
	

		// Disable autoscrolling if the user clicks the prev or next button.
		carousel.clip.hover(function() { pCarousel.halt() }, function() { pCarousel.animate(carousel); });
		pCarousel.animate(carousel);

		 jQuery('#home-slideshow-controls a').bind('click', function() {
			carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
			pCarousel.halt();
			pCarousel.buttonHit = true;
			return false;
		});
	 
	},

	firstItem: function(carousel, item, idx, state) {
		jQuery('#home-slideshow-controls a').removeClass('on');
		var num = jQuery(item).attr('jcarouselindex');
		jQuery('#control-'+num).addClass('on');
	},
	
	animate: function(carousel) {
		pCarousel.animationId = setInterval( function() {
			if (pCarousel.buttonHit ==false) {
				carousel.next();
			}
		}, pCarousel.animationTimeout );
	},

	halt: function() {
		if( pCarousel.animationId ) {
			clearInterval(pCarousel.animationId);
		}
	}
 
};

