/*
 *
 * PCR-specific functions
 *
 */
jQuery(document).ready(function(){

	// Homepage carousel
	if( jQuery('#pcr-carousel').length > 0 ){
		var scroll = 3;
		var time = 15;
		jQuery.easing.easeInOutQuad = function (x, t, b, c, d) {
				if ((t/=d/2) < 1) return c/2*t*t + b;
				return -c/2 * ((--t)*(t-2) - 1) + b;
		};
		jQuery('#pcr-carousel').jcarousel({
			auto: time,
			wrap: 'both',
			easing: 'easeInOutQuad',
			scroll: scroll,
	        buttonNextHTML: null,
	        buttonPrevHTML: null,
			itemLoadCallback: {
				onAfterAnimation: function(){
					var current = Math.ceil((parseInt(jQuery('#pcr-carousel').css('left'))/-408)+1);
					jQuery('#pcr-carousel-controls li').removeClass('on');
					jQuery('#control-'+current).addClass('on');
				}
			},
			initCallback: function(carousel){
				var num = Math.ceil(jQuery('#pcr-carousel li').length/scroll);
				jQuery('#pcr-body').prepend('<ul id="pcr-carousel-controls"></ul>');
				for(i=1;i<=num;i++){
					jQuery('#pcr-carousel-controls').append('<li id="control-'+i+'"><a href="#">'+i+'</a>');
				}
				jQuery('#pcr-carousel-controls').css('width',18*num)
				jQuery('.jcarousel-clip').css('width',(126*scroll+10*(scroll-1)));
				jQuery('#pcr-carousel-controls li:first').addClass('on');
				jQuery('#pcr-carousel-controls a').click(function(event){
					event.preventDefault();
					carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()*scroll-2));
					carousel.stopAuto();
					carousel.startAuto();
				});
				carousel.clip.hover(function() {
					carousel.stopAuto();
				}, function() {
					carousel.startAuto();
				});
			}
		});
	}
	
});
