/* 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',
        'carousel'          : true,
        'autoPlay'          : true,
        'showControls'      : false,
        'scrollDuration'    : 1500
    };
	this.teaserChainWidth 	= 0;
    this.actual             = 0;
	this.moving				= false;
    this.teaserStage        = $( '#' + this.options.teaserStageID );
    this.teaserInnerStage   = $( '#' + this.options.teaserInnerStageID );
    this.ctrlPlay	        = $( '#' + this.options.playID );
    this.ctrlForward        = $( '#' + this.options.forwardID );
    this.ctrlBackward       = $( '#' + this.options.backwardID );

    // this.ctrlBackward.css({'opacity' : '0'});
    this.ctrlBackward.css({'visibility' : 'hidden'});


	if (this.options.carousel) {
		// this.ctrlBackward.animate({'opacity' : '1'});
	    this.ctrlBackward.css({'visibility' : 'visible'});
		// this.ctrlForward.animate({'opacity' : '1'});
	    this.ctrlForward.css({'visibility' : 'visible'});
		$('.' + this.options.teaserClass + ':first', this.teaserInnerStageID)
			.clone(true)
			.insertAfter($('.' + this.options.teaserClass + ':last', this.teaserInnerStageID)
		);
	    var pos = $( $( '.' + this.options.teaserClass ).eq(2) ).position();
    }
	
	this.teaserChain = $( '.' + this.options.teaserClass );
	for ( i = 0; i < this.teaserChain.length; i++) {
		this.teaserChainWidth += $(this.teaserChain[i]).width();
	}
	$(this.teaserInnerStage).css({ 'width' : this.teaserChainWidth + 'px' });
	
    
    $( '.' + this.options.nextTag ).bind('mouseover', this, function(e) {
        $( e.currentTarget ).css('cursor', 'pointer');
    });

    $( '.' + 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'});
        this.ctrlForward.css({'visibility' : 'hidden'});
    }
    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});
		if (!this.options.carousel && this.actual == this.teaserChain.length-1 ) {
            this.actual = -1;
        }
		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() {
	if (this.moving) {
		return;
	}
	var that = this;
    this.timeOut = clearTimeout( this.timeOut );

	
	if (this.options.carousel) {
		this.actual++;
	} else {
		if ( this.actual + 1 < this.teaserChain.length  ) {
	    	this.actual++;
	        if ( this.actual + 1 == this.teaserChain.length ) {
	            // this.ctrlForward.animate({'opacity' : '0'});
		        this.ctrlForward.css({'visibility' : 'hidden'});
	        }
	        if ( (this.actual >= 0) && (this.actual + 1 < this.teaserChain.length) ) {
	            // this.ctrlForward.animate({'opacity' : '1'});
		        this.ctrlForward.css({'visibility' : 'visible'});
	        }
	        if ( this.actual > 0 ) {
	            // this.ctrlBackward.animate({'opacity' : '1'});
	            this.ctrlBackward.css({'visibility' : 'visible'});
	        }
	        if ( this.actual <= 0 ) {
	            // this.ctrlBackward.animate({'opacity' : '0'});
	            this.ctrlBackward.css({'visibility' : 'hidden'});
	        }
	    }
	}
	
	this.moving	= true;
    var pos = $( this.teaserChain[this.actual] ).position();
    $( this.teaserInnerStage ).animate({
        left: pos.left * -1 + "px"
    }, this.options.scrollDuration, this.options.easingRight, function() {
		if ( that.actual + 1 == that.teaserChain.length && that.options.carousel ) {
			that.actual = 0;
			$( that.teaserInnerStage).css({ 'left': "0px" });
		}
		that.moving = false;
	});
    this.Timer();
};
Carousel.prototype.movePrev = function() {
	if (this.moving) {
		return;
	}
	var that = this;
    this.timeOut = clearTimeout( this.timeOut );
	
	if (this.options.carousel) {
		if ( this.actual == 0 ) {
			this.actual = this.teaserChain.length - 1;
			$( this.teaserInnerStage ).css({ 'left': (( this.teaserChainWidth - $(this.teaserChain[this.teaserChain.length-1]).width()) * -1) + 'px' });
		}
		this.actual--;
	} else {
	
	    if ( this.actual - 1 >= 0 ) {
	        this.actual--;
	        if ( this.actual + 1 < this.teaserChain.length ) {
	            // this.ctrlForward.animate({'opacity' : '1'});
	            this.ctrlForward.css({'visibility' : 'visible'});
	        }
	        if ( this.actual == 0 ) {
	            // this.ctrlBackward.animate({'opacity' : '0'});
	            this.ctrlBackward.css({'visibility' : 'hidden'});
	        }
	    }
    }

	this.moving	= true;
    var pos = $( this.teaserChain[this.actual] ).position();
    $( this.teaserInnerStage ).animate({
        left: pos.left * -1 + "px"
    }, this.options.scrollDuration, this.options.easingLeft, function() { 	
		that.moving = false;
	});

    this.Timer();
};