/* StageCarousel for jQuery 1.3.2
 * Author: NOSE AG Design Intelligence, Sebastian Wohlrab <sebastian.wohlrab@nose.ch>
 * Date: 2009-11-20
 * Requirements: [jQuery 1.3.2]
 */
var Carousel = function() {
    this.options = {
        'backwardID'        : 'prev',
        'playID'       		: 'play',
        'pauseImg'       	: 'img/btn_pause.png',		
        'playImg'       	: 'img/btn_play.png',
        'controlsID'        : 'controls',
        'forwardID'         : 'next',
        'teaserStageID'     : 'presenter',
        'teaserInnerStageID': 'slides',
        'teaserClass'       : 'slide',
        'easingLeft'        : 'easeOutExpo',
        'easingRight'       : 'easeOutExpo',
        'nextTag'           : 'next',
        'autoPlay'          : true,
        'showControls'      : false,
        'scrollDuration'    : 1500
    };
    this.teaserChain        = $( '.' + this.options.teaserClass );
    this.actual             = 0;
    this.teaserStage        = $( '#' + this.options.teaserStageID );
    this.teaserInnerStage   = $( '#' + this.options.teaserInnerStageID );
    this.ctrlPlay	        = $( '#' + this.options.playID );
    this.ctrlPause	        = $( '#' + this.options.pauseID );
    this.ctrlForward        = $( '#' + this.options.forwardID );
    this.ctrlBackward       = $( '#' + this.options.backwardID );
    this.ctrlBackward.css({'opacity' : '0'});
    
    $( '.' + this.options.nextTag ).bind('mouseover', this, function(e) {
        $( e.currentTarget ).css('cursor', 'pointer');
    });
    // $( '.' + this.options.nextTag ).bind('click', this, this.Handle_ctrlForward_Click);
    $( '.' + this.options.nextTag ).bind('click', this, this.Handle_ctrlPlay_Click);

	if (!this.options.showControls) {
		$( '#' + this.options.controlsID ).hide().bind(
			'mouseenter', this, function(e) { $( '#' + e.data.options.controlsID ).show(); });
		$( '.' + this.options.nextTag )
			.bind(
				'mouseenter', this, function(e) { $( '#' + e.data.options.controlsID ).show(); })
			.bind(
				'mouseleave', this, function(e) { $( '#' + e.data.options.controlsID ).hide(); }
		);
	}
    if ( this.teaserChain.length > 1 ) {
        this.setEvents();
    }
    else {
        this.ctrlForward.css({'opacity' : '0'});
    }
    if ( this.teaserChain.length > 1 ) {
        this.Timer();
    }
}
Carousel.prototype.Timer = function() {
    var that = this;
	if (this.options.autoPlay) {
	    function Next() {
	        if ( that.actual == that.teaserChain.length-1 ) {
	            that.actual = -1;
	        }
	        that.moveNext();
	    }
	    this.timeOut = window.setTimeout( function() { Next(this) }, 5000 );
	}
}

Carousel.prototype.playToggle = function(e) {	
	if (this.options.autoPlay) {
		this.options.autoPlay = false;
		this.timeOut = clearTimeout( this.timeOut );
		$('img', this.ctrlPlay).attr({'src' : this.options.playImg});
	} else {
		this.options.autoPlay = true;
		$('img', this.ctrlPlay).attr({'src' : this.options.pauseImg});
		this.moveNext();
	}
};
Carousel.prototype.setEvents = function() {
    $( this.ctrlForward ).bind(
        'click', this, this.Handle_ctrlForward_Click
    );
    $( this.ctrlPlay ).bind(
        'click', this, this.Handle_ctrlPlay_Click
	);
    $( this.ctrlBackward ).bind(
        'click', this, this.Handle_ctrlBackward_Click
    );
};
Carousel.prototype.Handle_ctrlForward_Click = function( e ) {
    e.preventDefault();
    e.data.moveNext();
};
Carousel.prototype.Handle_ctrlBackward_Click = function( e ) {
    e.preventDefault();
    e.data.movePrev();
};
Carousel.prototype.Handle_ctrlPlay_Click = function( e ) {
	e.preventDefault();
	e.data.playToggle(e);
};
Carousel.prototype.moveNext = function() {
    this.timeOut = clearTimeout( this.timeOut );
    if ( this.actual + 1 < this.teaserChain.length  ) {
        this.actual++;
        if ( this.actual + 1 == this.teaserChain.length ) {
            this.ctrlForward.animate({'opacity' : '0'});
        }
        if ( (this.actual >= 0) && (this.actual + 1 < this.teaserChain.length) ) {
            this.ctrlForward.animate({'opacity' : '1'});
        }
        if ( this.actual > 0 ) {
            this.ctrlBackward.animate({'opacity' : '1'});
        }
        if ( this.actual <= 0 ) {
            this.ctrlBackward.animate({'opacity' : '0'});
        }
    }
    var pos = $( this.teaserChain[this.actual] ).position();

    $( this.teaserInnerStage ).animate({
        left: pos.left * -1 + "px"
    }, this.options.scrollDuration, this.options.easingRight );
    this.Timer();
};
Carousel.prototype.movePrev = function() {
    this.timeOut = clearTimeout( this.timeOut );
    if ( this.actual - 1 >= 0 ) {
        this.actual--;
        if ( this.actual + 1 < this.teaserChain.length ) {
            this.ctrlForward.animate({'opacity' : '1'});
        }
        if ( this.actual == 0 ) {
            this.ctrlBackward.animate({'opacity' : '0'});
        }
    } else {
    }
    var pos = $( this.teaserChain[this.actual] ).position();

    $( this.teaserInnerStage ).animate({
        left: pos.left * -1 + "px"
    }, this.options.scrollDuration, this.options.easingLeft);
    this.Timer();
};