/**
 * A class that makes a contentblocks collection slidable
 * (Inspired on implementation in Topsport)
 *
 * @since Mon Nov 07 2011
 * @author Jaap Romijn
 **/
var WJCarouselfsv = Class.create(WJCarousel, {
	
	/**
	 * initialize
	 *
	 * Creates a new WJCarousel
	 *
	 * @since Mon Nov 07 2011
	 * @access public
	 * @param integer id
	 * @param Object options
	 * @return WJCarousel
	 **/
	initialize: function($super, id, options, slideselectors) {
		$super(id, options, slideselectors);
		this.startTimer();
		this.carousel.controls.invoke('observe', 'click', this.clearTimer.bind(this));
	},
	
	/**
	 * _beforeMove
	 *
	 * Performs actions when moving is complete
	 * If the last (additional added) slide is called which is the same as the first slide, select the first jumper
	 *
	 * @since Thu Apr 14 2011
	 * @access protected
	 * @return void
	 * @fires brandbox:end
	 **/
	_beforeMove: function($super) {
		$super();
	},
	
	/**
	 * _afterMove
	 *
	 * Performs actions when moving is complete
	 * If the last (additional added) slide is called which is the same as the first slide, select the first jumper
	 *
	 * @since Thu Apr 14 2011
	 * @access protected
	 * @return void
	 * @fires brandbox:end
	 **/
	_afterMove: function($super) {
		$super();
		this.startTimer();
	},
 
 	/**
	 * startTimer
	 *
	 * Starts the timer effect to show how long till the next slide
	 *
	 * @since Mon Nov 07 2011
	 * @access public
	 * @param 
	 * @return 
	 **/
	startTimer: function() {

		if (typeof(Effect) == "object") {
			this.currentjumper = this.container.down('.carousel-jumper-active');
			this.polarjumper = this.polarjumper;
			this.polartween = new Effect.Tween(null, 0, 32, 
				{
					queue: 'bxb',
					duration: this.options.frequency - this.options.duration,
					transition: Effect.Transitions.linear,
					afterFinish:
						function() {
							if (this.currentjumper) {
								this.currentjumper.setStyle({backgroundPosition: "left top"} );
							}
						}.bind(this),
	  				beforeStart:
						function() {
							this.carousel.stop();
							this.carousel.timer = null;
							this.carousel.start();
							if (typeof(this.polarjumper) !== "undefined") {
								this.polarjumper.setStyle({'backgroundPosition': "left top"} );
							}

						}.bind(this)
				}, 
				function(p) {
					offset =  (1+Math.ceil(p)) * 25;
					if (this.currentjumper) {
						this.currentjumper.setStyle({backgroundPosition: -offset +"px top"} );
					}
				}.bind(this)
			);
		}
	},
	
	/**
	 * clearTimer
	 *
	 * Restarts the polar timer for the clicked control.
	 * Has to do prev and then start in order to go to the right slide/position because "start" embeds "next"
	 * @see Carousel.start
	 *
	 * @since Mon Nov 07 2011
	 * @access 
	 **/
	clearTimer: function() {
		if (typeof(this.polarjumper) !== "undefined") {
			this.polarjumper.setStyle({'backgroundPosition': "left top"} );
		}
		
		this.polartween.cancel();
		//Cleaning up jumpers (for IE7)
		this.container.select('.carousel-jumper').each(
			function(jumper) {
				jumper.setStyle({'backgroundPosition': "left top"} );
			}.bind(this)
		)
	}
	
});

