PreviewCase.implement({

	/**
	 * Initialize the flash elements
	 * @param teaserIds Array
	 */
	initFlashTeaser: function(teaserIds) {

		for(var i = 0, l = this.teaserElements.length; i < l; i++) {
			if(!teaserIds.contains(this.teaserElements[i].get('id'))) continue;

			this.setFlashTeaser(this.teaserElements[i]);
			// setting the flashmovie with a delay of one second. 
			this.setSwfFile.delay(1500, this, this.teaserElements[i]);

			this.teaserElements[i].retrieve('tweenIn')
				.addEvent('start', this.flashTweenInStart.bind(this, [this.teaserElements[i]]))
				.addEvent('complete', this.flashTweenInComplete.bind(this, [this.teaserElements[i]]));

			this.teaserElements[i].retrieve('tweenOut')
				.addEvent('start', this.flashTweenOutStart.bind(this, [this.teaserElements[i]]));

			// If a flash teaser starts at pole position.
			if(i == 0) {
				if (this.options.autoplay) $clear(this.timer);
				this.flashTweenInComplete.delay(1600, this, this.teaserElements[i]);
			}
		}
	},

	/**
	 * Setting the Flash Element into the teaser
	 * @param teaserEl
	 */
	setFlashTeaser: function(teaserEl) {
		teaserEl.empty();
		return new Swiff('/fileadmin/flash/galleries/TeaserContainer.swf', {
			id: 'flash_' + teaserEl.get('id'),
			width: this.options.dimension[0],
			height: this.options.dimension[1],
			container: teaserEl
		});
	},

	/**
	 * Setting the SWF into the container
	 * @param teaserEl
	 */
	setSwfFile: function(teaserEl) {
		Swiff.remote($('flash_' + teaserEl.get('id')), 'setSWF', '/fileadmin/flash/' + teaserEl.get('id'), 0, -48);
	},

	/**
	 * Stop the interval for the stagetween
	 * @param teaserEl
	 */
	flashTweenInStart: function(teaserEl) {
		//console.log('startSWF TweenIn');
		Swiff.remote($('flash_' + teaserEl.get('id')), 'playSWF');
	},

	/**
	 * After the tween finished starts the flashmovie
	 * @param teaserEl
	 */
	flashTweenInComplete: function(teaserEl) {
		if (this.options.autoplay) $clear(this.timer);
		//console.log('finishSWF TweenIn');
	},

	/**
	 * If the flashmovie tween out, set a new tween interval
	 * @param teaserEl
	 */
	flashTweenOutStart: function(teaserEl) {
		if (this.options.autoplay) this.timer = this.tweenRight.periodical(this.options.delay, this);
	}
});

/**
 * If the flashmovie has finished, fire the tween Event.
 */
PreviewCase.Flash = function() {
	document.getElement('div.PreviewCase').fireEvent('tweenNext');
};

function container_play_complete(t) {
	PreviewCase.Flash();
}

function container_play_ready() {
	var el = document.getElement('div.PreviewCase div.teaser.active object');
	if(!el) return false;
	else Swiff.remote(el, 'playSWF');
}
