var Carousel = function() {
    this.options = {
        'backwardID'        : 'prev',
        'playID'       		  : 'play',
        'pauseImg'       	  : 'typo3conf/ext/slider/img/btn_pause.png',		
        'playImg'       	  : 'typo3conf/ext/slider/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        = jQuery( '#' + this.options.teaserStageID );
    this.teaserInnerStage   = jQuery( '#' + this.options.teaserInnerStageID );
    this.ctrlPlay	          = jQuery( '#' + this.options.playID );
    this.ctrlForward        = jQuery( '#' + this.options.forwardID );
    this.ctrlBackward       = jQuery( '#' + 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'});
		jQuery('.' + this.options.teaserClass + ':first', this.teaserInnerStageID)
			.clone(true)
			.insertAfter(jQuery('.' + this.options.teaserClass + ':last', this.teaserInnerStageID)
		);
	    var pos = jQuery( jQuery( '.' + this.options.teaserClass ).eq(2) ).position();
    }
	
	this.teaserChain = jQuery( '.' + this.options.teaserClass );
	for ( i = 0; i < this.teaserChain.length; i++) {
		this.teaserChainWidth += jQuery(this.teaserChain[i]).width();
	}
	jQuery(this.teaserInnerStage).css({ 'width' : this.teaserChainWidth + 'px' });
	
    
    jQuery( '.' + this.options.nextTag ).bind('mouseover', this, function(e) {
        //jQuery( e.currentTarget ).css('cursor', 'pointer');
    });
    
    jQuery('#link_stuttgart').click(function(e){
      window.location = "http://www.boerse-stuttgart.de/mobile";         
    });
    jQuery('#link_mobile').click(function(e){
        window.location = "http://www.ti8m.ch/mobile.html";         
    });
    jQuery('#link_agile').click(function(e){
        window.location = "http://www.ti8m.ch/agile_.html";         
    });
    jQuery('#link_tim').click(function(e){
        window.location = "http://www.ti8m.ch/forum.html";         
    });
    jQuery('#link_timspecial').click(function(e){
        window.location = "http://www.ti8m.ch/tim-special11.html";         
    });
    jQuery('#link_jobs').click(function(e){
        window.location = "http://www.ti8m.ch/job.html";         
    });
	 jQuery('#link_zukunftstag').click(function(e){
        window.location = "http://www.ti8m.ch/nzukunftstag.html";         
    });
	 jQuery('#link_filialeroeffnung').click(function(e){
        window.location = "http://www.ti8m.ch/filialeroeffnungb.html.html";         
    });
	 jQuery('#link_BCV').click(function(e){
        window.location = "http://www.ti8m.ch/ti8m_mobile_bank.html";         
    });
	 jQuery('#link_BCV2').click(function(e){
        window.location = "http://www.ti8m.ch/ti8m_mobile_bank.html";         
    });
	 jQuery('#link_BCV3').click(function(e){
        window.location = "http://www.bcv.ch/de/privatpersonen/online_banking/bcv_mobile";         
    });


    jQuery( '.' + this.options.nextTag ).bind('click', this, this.Handle_ctrlPlay_Click);

	if (!this.options.showControls) {
		jQuery( '#' + this.options.controlsID ).hide().bind(
			'mouseenter', this, function(e) { jQuery( '#' + e.data.options.controlsID ).show(); });
		jQuery( '.' + this.options.nextTag )
			.bind(
				'mouseenter', this, function(e) { jQuery( '#' + e.data.options.controlsID ).show(); })
			.bind(
				'mouseleave', this, function(e) { jQuery( '#' + 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 );
		jQuery('img', this.ctrlPlay).attr({'src' : this.options.playImg});
	} else {
		this.options.autoPlay = true;
		jQuery('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() {
    jQuery( this.ctrlForward ).bind(
        'click', this, this.Handle_ctrlForward_Click
    );
    jQuery( this.ctrlPlay ).bind(
        'click', this, this.Handle_ctrlPlay_Click
	);
    jQuery( 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 = jQuery( this.teaserChain[this.actual] ).position();
    jQuery( 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;
			jQuery( 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;
			jQuery( this.teaserInnerStage ).css({ 'left': (( this.teaserChainWidth - jQuery(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 = jQuery( this.teaserChain[this.actual] ).position();
    jQuery( this.teaserInnerStage ).animate({
        left: pos.left * -1 + "px"
    }, this.options.scrollDuration, this.options.easingLeft, function() { 	
		that.moving = false;
	});

    this.Timer();
};

